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