]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALClusterizer.cxx
attempt to fix the mem leak reported in https://savannah.cern.ch/bugs/?82308 ; additi...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALClusterizer.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 //  Base class for the clusterization algorithm (pure abstract)
20 //*--
21 //*-- Author: Yves Schutz  SUBATECH 
22 // 
23 //   Clusterization mother class. Contains common methods/data members of different 
24 //   clusterizers. GCB 2010
25 //////////////////////////////////////////////////////////////////////////////
26
27 // --- ROOT system ---
28 #include "TClonesArray.h"
29 #include "TTree.h"
30 #include <TFile.h> 
31 class TFolder;
32 #include <TMath.h> 
33 #include <TMinuit.h>
34 #include <TTree.h> 
35 class TSystem; 
36 #include <TBenchmark.h>
37 #include <TBrowser.h>
38 #include <TROOT.h>
39
40 // --- Standard library ---
41 #include <cassert>
42
43 // --- AliRoot header files ---
44 #include "AliEMCALClusterizer.h"
45 #include "AliEMCALReconstructor.h"
46 #include "AliRunLoader.h"
47 #include "AliRun.h"
48 #include "AliLog.h"
49 #include "AliEMCAL.h"
50 #include "AliEMCALRecPoint.h"
51 #include "AliEMCALRecParam.h"
52 #include "AliEMCALGeometry.h"
53 #include "AliEMCALRecParam.h"
54 #include "AliCDBManager.h"
55 #include "AliCaloCalibPedestal.h"
56 #include "AliEMCALCalibData.h"
57 class AliCDBStorage;
58 #include "AliCDBEntry.h"
59
60 ClassImp(AliEMCALClusterizer)
61
62 //____________________________________________________________________________
63 AliEMCALClusterizer::AliEMCALClusterizer():
64   fIsInputCalibrated(kFALSE),
65   fJustClusters(kFALSE),
66   fDigitsArr(NULL),
67   fTreeR(NULL),
68   fRecPoints(NULL),
69   fGeom(NULL),
70   fCalibData(NULL), 
71   fCaloPed(NULL),
72   fADCchannelECA(0.),fADCpedestalECA(0.),
73   fTimeMin(-1.),fTimeMax(1.),fTimeCut(1.),
74   fDefaultInit(kFALSE),fToUnfold(kFALSE),
75   fNumberOfECAClusters(0), fECAClusteringThreshold(0.),
76   fECALocMaxCut(0.),fECAW0(0.),fMinECut(0.),
77   fClusterUnfolding(NULL)
78 {
79   // ctor
80   
81   Init();
82 }
83
84 //____________________________________________________________________________
85 AliEMCALClusterizer::AliEMCALClusterizer(AliEMCALGeometry* geometry): 
86   fIsInputCalibrated(kFALSE),
87   fJustClusters(kFALSE),
88   fDigitsArr(NULL),
89   fTreeR(NULL),
90   fRecPoints(NULL),
91   fGeom(geometry),
92   fCalibData(NULL), 
93   fCaloPed(NULL),
94   fADCchannelECA(0.),fADCpedestalECA(0.),
95   fTimeMin(-1.),fTimeMax(1.),fTimeCut(1.),
96   fDefaultInit(kFALSE),fToUnfold(kFALSE),
97   fNumberOfECAClusters(0), fECAClusteringThreshold(0.),
98   fECALocMaxCut(0.),fECAW0(0.),fMinECut(0.),
99   fClusterUnfolding(NULL)
100 {
101   // Ctor with the indication of the file where header Tree and digits Tree are stored.
102   // Use this contructor to avoid usage of Init() which uses runloader.
103   // Change needed by HLT - MP.
104   // Note for the future: the use on runloader should be avoided or optional at least.
105   // Another way is to make Init virtual and protected at least 
106   // such that the deriving classes can overload Init();
107   
108   if (!fGeom)
109   {
110     AliFatal("Geometry not initialized.");
111   }
112   Int_t i=0;
113   for (i = 0; i < 8; i++)
114     fSSPars[i] = 0.;
115   for (i = 0; i < 3; i++) {
116     fPar5[i] = 0.;
117     fPar6[i] = 0.;
118   }
119 }
120
121 //____________________________________________________________________________
122 AliEMCALClusterizer::AliEMCALClusterizer(AliEMCALGeometry *geometry, 
123                                          AliEMCALCalibData *calib, 
124                                          AliCaloCalibPedestal *caloped): 
125   fIsInputCalibrated(kFALSE),
126   fJustClusters(kFALSE),
127   fDigitsArr(NULL),
128   fTreeR(NULL),
129   fRecPoints(NULL),
130   fGeom(geometry),
131   fCalibData(calib),
132   fCaloPed(caloped),
133   fADCchannelECA(0.),fADCpedestalECA(0.),
134   fTimeMin(-1.),fTimeMax(1.),fTimeCut(1.),
135   fDefaultInit(kFALSE),fToUnfold(kFALSE),
136   fNumberOfECAClusters(0), fECAClusteringThreshold(0.),
137   fECALocMaxCut(0.),fECAW0(0.),fMinECut(0.),
138   fClusterUnfolding(NULL)
139 {
140   // ctor, geometry and calibration are initialized elsewhere.
141   
142   if (!fGeom)
143     AliFatal("Geometry not initialized.");
144   
145   Int_t i=0;
146   for (i = 0; i < 8; i++)
147     fSSPars[i] = 0.;
148   for (i = 0; i < 3; i++) {
149     fPar5[i] = 0.;
150     fPar6[i] = 0.;
151   }
152 }
153
154 //____________________________________________________________________________
155 AliEMCALClusterizer::~AliEMCALClusterizer()
156 {
157   // dtor
158   //Already deleted in AliEMCALReconstructor.
159
160   if(fClusterUnfolding) delete fClusterUnfolding;
161
162   // make sure we delete the rec points array
163   DeleteRecPoints();
164 }
165
166 //____________________________________________________________________________
167 void AliEMCALClusterizer::DeleteRecPoints()
168 {
169   // free the cluster array
170   if (fRecPoints) 
171     {
172       AliDebug(2, "Deleting fRecPoints.");
173       fRecPoints->Delete();
174       delete fRecPoints;
175       fRecPoints = 0;
176     }
177 }
178
179 //____________________________________________________________________________
180 Float_t AliEMCALClusterizer::Calibrate(const Float_t amp, const Float_t time, const Int_t absId) 
181 {
182   // Convert digitized amplitude into energy.
183   // Calibration parameters are taken from calibration data base for raw data,
184   // or from digitizer parameters for simulated data.
185
186   //Return energy with default parameters if calibration is not available
187   if (!fCalibData&&!fCaloPed) {
188     if (fIsInputCalibrated == kTRUE)
189     {
190       AliDebug(10, Form("Input already calibrated!"));
191       return amp;
192     }    
193     
194     return -fADCpedestalECA + amp * fADCchannelECA ; 
195   }
196   
197   if (fGeom==0)
198     AliFatal("Did not get geometry from EMCALLoader") ;
199     
200   Int_t iSupMod = -1;
201   Int_t nModule = -1;
202   Int_t nIphi   = -1;
203   Int_t nIeta   = -1;
204   Int_t iphi    = -1;
205   Int_t ieta    = -1;
206     
207   Bool_t bCell = fGeom->GetCellIndex(absId, iSupMod, nModule, nIphi, nIeta) ;
208   if(!bCell) {
209     fGeom->PrintGeometry();
210     AliError(Form("Wrong cell id number : %i", absId));
211     //assert(0); // GCB: This aborts reconstruction of raw simulations 
212     //where simulation had more SM than default geometry, 
213     //change to return 0, to avoid aborting good generations.
214     return 0;
215   }
216     
217   fGeom->GetCellPhiEtaIndexInSModule(iSupMod,nModule,nIphi, nIeta,iphi,ieta);
218           
219   // Check if channel is bad (dead or hot), in this case return 0.      
220   // Gustavo: 15-12-09 In case of RAW data this selection is already done, but not in simulation.
221   // for the moment keep it here but remember to do the selection at the sdigitizer level 
222   // and remove it from here
223   if (fCaloPed) {
224     Int_t channelStatus = (Int_t)(fCaloPed->GetDeadMap(iSupMod))->GetBinContent(ieta,iphi);
225     if(channelStatus == AliCaloCalibPedestal::kHot || channelStatus == AliCaloCalibPedestal::kDead) {
226       AliDebug(2,Form("Tower from SM %d, ieta %d, iphi %d is BAD : status %d !!!",iSupMod,ieta,iphi, channelStatus));
227       return 0;
228     }
229   }
230   //Check if time is too large or too small, indication of a noisy channel, remove in this case
231   if(time > fTimeMax || time < fTimeMin) return 0;
232     
233   if (fIsInputCalibrated||!fCalibData)
234   {
235     AliDebug(10, Form("Input already calibrated!"));
236     return amp;
237   }
238           
239   fADCchannelECA  = fCalibData->GetADCchannel (iSupMod,ieta,iphi);
240   fADCpedestalECA = fCalibData->GetADCpedestal(iSupMod,ieta,iphi);
241       
242   return -fADCpedestalECA + amp * fADCchannelECA ;        
243 }
244
245 //____________________________________________________________________________
246 void AliEMCALClusterizer::GetCalibrationParameters() 
247 {
248   // Set calibration parameters:
249   // If calibration database exists, they are read from database,
250   // otherwise, they are taken from digitizer.
251   // It is a user responsilibity to open CDB before reconstruction, 
252   // for example: 
253   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
254
255   if (fIsInputCalibrated)
256     return;
257   
258   //Check if calibration is stored in data base
259   if(!fCalibData)
260   {
261     AliCDBEntry *entry = (AliCDBEntry*) 
262     AliCDBManager::Instance()->Get("EMCAL/Calib/Data");
263     if (entry) fCalibData =  (AliEMCALCalibData*) entry->GetObject();
264   }
265   
266   if(!fCalibData)
267     AliFatal("Calibration parameters not found in CDB!");
268 }
269
270 //____________________________________________________________________________
271 void AliEMCALClusterizer::GetCaloCalibPedestal() 
272 {
273   // Set calibration parameters:
274   // if calibration database exists, they are read from database,
275   // otherwise, they are taken from digitizer.
276   //
277   // It is a user responsilibity to open CDB before reconstruction, 
278   // for example: 
279   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
280
281   if (fIsInputCalibrated)
282     return;
283   
284   // Check if calibration is stored in data base
285   if(!fCaloPed)
286     {
287       AliCDBEntry *entry = (AliCDBEntry*) 
288         AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals");
289       if (entry) fCaloPed =  (AliCaloCalibPedestal*) entry->GetObject();
290     }
291   
292   if(!fCaloPed)
293     AliFatal("Pedestal info not found in CDB!");
294 }
295
296 //____________________________________________________________________________
297 void AliEMCALClusterizer::Init()
298 {
299   // Make all memory allocations which can not be done in default constructor.
300   // Attach the Clusterizer task to the list of EMCAL tasks
301   
302   AliRunLoader *rl = AliRunLoader::Instance();
303   if (rl->GetAliRun()){
304     AliEMCAL* emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
305     if(emcal)fGeom = emcal->GetGeometry();
306   }
307   
308   if(!fGeom){ 
309     fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
310   }
311   
312   AliDebug(1,Form("geom %p",fGeom));
313   
314   if(!gMinuit) 
315     gMinuit = new TMinuit(100) ;
316   
317   Int_t i=0;
318   for (i = 0; i < 8; i++)
319     fSSPars[i] = 0.;
320   for (i = 0; i < 3; i++) {
321     fPar5[i] = 0.;
322     fPar6[i] = 0.;
323   }
324 }
325
326 //____________________________________________________________________________
327 void AliEMCALClusterizer::InitParameters()
328
329   // Initializes the parameters for the Clusterizer from AliEMCALReconstructor::GetRecParam().
330
331   return InitParameters(AliEMCALReconstructor::GetRecParam());
332 }
333
334 //____________________________________________________________________________
335 void AliEMCALClusterizer::InitParameters(const AliEMCALRecParam* recParam)
336
337   // Initializes the parameters for the Clusterizer
338
339   fNumberOfECAClusters = 0 ;
340   fCalibData           = 0 ;
341   fCaloPed             = 0 ;
342         
343   if(!recParam) {
344     AliFatal("Reconstruction parameters for EMCAL not set!");
345   } 
346
347   fECAClusteringThreshold = recParam->GetClusteringThreshold();
348   fECAW0                  = recParam->GetW0();
349   fMinECut                = recParam->GetMinECut();    
350   fToUnfold               = recParam->GetUnfold();
351   fECALocMaxCut           = recParam->GetLocMaxCut();
352   fTimeCut                = recParam->GetTimeCut();
353   fTimeMin                = recParam->GetTimeMin();
354   fTimeMax                = recParam->GetTimeMax();
355     
356   AliDebug(1,Form("Reconstruction parameters: fECAClusteringThreshold=%.3f GeV, fECAW=%.3f, fMinECut=%.3f GeV, "
357                   "fToUnfold=%d, fECALocMaxCut=%.3f GeV, fTimeCut=%e s,fTimeMin=%e s,fTimeMax=%e s",
358                   fECAClusteringThreshold,fECAW0,fMinECut,fToUnfold,fECALocMaxCut,fTimeCut, fTimeMin, fTimeMax));
359
360   if (fToUnfold) {
361     Int_t i=0;
362     for (i = 0; i < 8; i++) {
363       fSSPars[i] = recParam->GetSSPars(i);
364     } //end of loop over parameters
365     for (i = 0; i < 3; i++) {
366       fPar5[i] = recParam->GetPar5(i);
367       fPar6[i] = recParam->GetPar6(i);
368     } //end of loop over parameters
369       
370     InitClusterUnfolding();
371       
372     for (i = 0; i < 8; i++) {
373       AliDebug(1,Form("unfolding shower shape parameters: fSSPars=%f \n",fSSPars[i]));
374     }
375     for (i = 0; i < 3; i++) {
376       AliDebug(1,Form("unfolding parameter 5: fPar5=%f \n",fPar5[i]));
377       AliDebug(1,Form("unfolding parameter 6: fPar6=%f \n",fPar6[i]));
378     }
379   } // to unfold
380 }
381
382 //____________________________________________________________________________
383 void AliEMCALClusterizer::Print(Option_t * /*option*/)const
384 {
385   // Print clusterizer parameters
386   
387   TString message("\n") ; 
388   
389   if (strcmp(GetName(),"") == 0) {
390     printf("AliEMCALClusterizer not initialized\n");
391     return;
392   }
393     
394   // Print parameters
395   TString taskName(Version()) ;
396     
397   printf("--------------- "); 
398   printf("%s",taskName.Data()) ; 
399   printf(" "); 
400   printf("Clusterizing digits: "); 
401   printf("\n                       ECA Local Maximum cut    = %f", fECALocMaxCut); 
402   printf("\n                       ECA Logarithmic weight   = %f", fECAW0); 
403   if (fToUnfold) {
404     printf("\nUnfolding on\n");
405     printf("Unfolding parameters: fSSpars: \n");
406     Int_t i=0;
407     for (i = 0; i < 8; i++) {
408       printf("fSSPars[%d] = %f \n", i, fSSPars[i]);
409     }
410     printf("Unfolding parameter 5 and 6: fPar5 and fPar6: \n");
411     for (i = 0; i < 3; i++) {
412       printf("fPar5[%d] = %f \n", i, fPar5[i]);
413       printf("fPar6[%d] = %f \n", i, fPar6[i]);
414     }
415   }
416   else
417     printf("\nUnfolding off\n");
418     
419   printf("------------------------------------------------------------------"); 
420 }
421
422 //____________________________________________________________________________
423 void AliEMCALClusterizer::PrintRecPoints(Option_t * option)
424 {
425   // Prints list of RecPoints produced at the current pass of AliEMCALClusterizer
426
427   if (strstr(option,"deb")) {
428     printf("PrintRecPoints: Clusterization result:") ; 
429     printf("           Found %d ECA Rec Points\n ", 
430            fRecPoints->GetEntriesFast()) ; 
431   }
432   
433   if (strstr(option,"all")) {
434     if (strstr(option,"deb")) {
435       printf("\n-----------------------------------------------------------------------\n") ;
436       printf("Clusters in ECAL section\n") ;
437       printf("Index    Ene(GeV) Multi Module     GX    GY   GZ  lX    lY   lZ   Dispersion Lambda 1   Lambda 2  # of prim  Primaries list\n") ;
438     }
439     Int_t index; 
440     for (index =  0 ; index < fRecPoints->GetEntries() ; index++) {
441       AliEMCALRecPoint * rp = dynamic_cast<AliEMCALRecPoint * >(fRecPoints->At(index)) ; 
442       if (!rp) 
443         continue;
444
445       TVector3  globalpos;  
446       //rp->GetGlobalPosition(globalpos);
447       TVector3  localpos;  
448       rp->GetLocalPosition(localpos);
449       Float_t lambda[2]; 
450       rp->GetElipsAxis(lambda);
451         
452       Int_t nprimaries=0;
453       Int_t * primaries = rp->GetPrimaries(nprimaries);
454       if(strstr(option,"deb")) 
455         printf("\n%6d  %8.4f  %3d     %4.1f    %4.1f %4.1f  %4.1f %4.1f %4.1f    %4.1f   %4f  %4f    %2d     : ", 
456                rp->GetIndexInList(), rp->GetEnergy(), rp->GetMultiplicity(),
457                globalpos.X(), globalpos.Y(), globalpos.Z(), localpos.X(), localpos.Y(), localpos.Z(), 
458                rp->GetDispersion(), lambda[0], lambda[1], nprimaries) ; 
459       if(strstr(option,"deb")){ 
460         for (Int_t iprimary=0; iprimary<nprimaries; iprimary++) {
461           printf("%d ", primaries[iprimary] ) ; 
462         }
463       }
464     }
465     
466     if(strstr(option,"deb"))
467       printf("\n-----------------------------------------------------------------------\n");
468   }
469 }
470
471 //___________________________________________________________________
472 void  AliEMCALClusterizer::PrintRecoInfo()
473 {
474   // Print reco version
475
476   printf(" AliEMCALClusterizer::PrintRecoInfo() : version %s \n", Version() );
477 }
478
479 //____________________________________________________________________________
480 void AliEMCALClusterizer::SetInput(TTree *digitsTree)
481 {
482   // Read the digits from the input tree
483
484   TBranch *branch = digitsTree->GetBranch("EMCAL");
485   if (!branch) { 
486     AliError("can't get the branch with the EMCAL digits !");
487     return;
488   }
489   if (!fDigitsArr)
490     fDigitsArr = new TClonesArray("AliEMCALDigit",100);
491   branch->SetAddress(&fDigitsArr);
492   branch->GetEntry(0);
493 }
494
495 //____________________________________________________________________________
496 void AliEMCALClusterizer::SetOutput(TTree *clustersTree)
497 {
498   // Read the digits from the input tree
499
500   AliDebug(9, "Making array for EMCAL clusters");
501   fRecPoints = new TObjArray(1000);
502   if (clustersTree) {
503     fTreeR = clustersTree;
504     Int_t split = 0;
505     Int_t bufsize = 32000;
506     fTreeR->Branch("EMCALECARP", "TObjArray", &fRecPoints, bufsize, split);
507   }
508 }
509
510 //___________________________________________________________________
511 void AliEMCALClusterizer::SetInputCalibrated(Bool_t val)
512 {
513   // Flag to indicate that input is calibrated - the case when we run already on ESD
514
515   fIsInputCalibrated = val;
516 }
517
518 //___________________________________________________________________
519 void AliEMCALClusterizer::SetJustClusters(Bool_t val)
520 {
521   // Flag to indicate that we are running on ESDs, when calling 
522   // rp->EvalAll(fECAW0,fDigitsArr,fJustClusters); in derived classes
523
524   fJustClusters = val;
525 }