added separate histogram entry for V0A and C BG flag
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALClusterizerv1.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 **************************************************************************/
173558f2 15
483b0559 16/* $Id$ */
803d1ab0 17
3a8be91c 18//-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (SUBATECH & Kurchatov Institute)
05a92d59 19// August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
20// of new IO (à la PHOS)
f1487f22 21// Mar 2007, Aleksei Pavlinov - new algoritmh of pseudo clusters
483b0559 22//////////////////////////////////////////////////////////////////////////////
23// Clusterization class. Performs clusterization (collects neighbouring active cells) and
24// unfolds the clusters having several local maxima.
25// Results are stored in TreeR#, branches EMCALTowerRP (EMC recPoints),
26// EMCALPreShoRP (CPV RecPoints) and AliEMCALClusterizer (Clusterizer with all
27// parameters including input digits branch title, thresholds etc.)
28// This TTask is normally called from Reconstructioner, but can as well be used in
29// standalone mode.
30// Use Case:
31// root [0] AliEMCALClusterizerv1 * cl = new AliEMCALClusterizerv1("galice.root")
32// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
33// //reads gAlice from header file "..."
34// root [1] cl->ExecuteTask()
35// //finds RecPoints in all events stored in galice.root
36// root [2] cl->SetDigitsBranch("digits2")
37// //sets another title for Digitis (input) branch
38// root [3] cl->SetRecPointsBranch("recp2")
39// //sets another title four output branches
40// root [4] cl->SetTowerLocalMaxCut(0.03)
41// //set clusterization parameters
42// root [5] cl->ExecuteTask("deb all time")
43// //once more finds RecPoints options are
44// // deb - print number of found rec points
45// // deb all - print number of found RecPoints and some their characteristics
46// // time - print benchmarking results
47
48// --- ROOT system ---
7dd95fbf 49#include <cassert>
483b0559 50
1d59832c 51class TROOT;
e52475ed 52#include <TH1.h>
53#include <TFile.h>
1d59832c 54class TFolder;
e52475ed 55#include <TMath.h>
56#include <TMinuit.h>
57#include <TTree.h>
1d59832c 58class TSystem;
e52475ed 59#include <TBenchmark.h>
60#include <TBrowser.h>
a1e17193 61#include <TROOT.h>
1d59832c 62
483b0559 63// --- Standard library ---
64
173558f2 65
483b0559 66// --- AliRoot header files ---
5dee926e 67#include "AliRunLoader.h"
68#include "AliRun.h"
1d59832c 69#include "AliESD.h"
483b0559 70#include "AliEMCALClusterizerv1.h"
70a93198 71#include "AliEMCALRecPoint.h"
483b0559 72#include "AliEMCALDigit.h"
73#include "AliEMCALDigitizer.h"
483b0559 74#include "AliEMCAL.h"
05a92d59 75#include "AliEMCALGeometry.h"
c47157cd 76#include "AliEMCALRecParam.h"
77#include "AliEMCALReconstructor.h"
1bd98442 78#include "AliCDBManager.h"
40164976 79#include "AliCaloCalibPedestal.h"
1d59832c 80
81class AliCDBStorage;
1bd98442 82#include "AliCDBEntry.h"
483b0559 83
84ClassImp(AliEMCALClusterizerv1)
1963b290 85
483b0559 86//____________________________________________________________________________
c47157cd 87AliEMCALClusterizerv1::AliEMCALClusterizerv1()
18a21c7c 88 : AliEMCALClusterizer(),
7ea6391b 89 fGeom(0),
18a21c7c 90 fDefaultInit(kFALSE),
91 fToUnfold(kFALSE),
40164976 92 fNumberOfECAClusters(0),fCalibData(0),fCaloPed(0),
18a21c7c 93 fADCchannelECA(0.),fADCpedestalECA(0.),fECAClusteringThreshold(0.),fECALocMaxCut(0.),
c47157cd 94 fECAW0(0.),fTimeCut(0.),fMinECut(0.)
483b0559 95{
96 // ctor with the indication of the file where header Tree and digits Tree are stored
97
483b0559 98 Init() ;
483b0559 99}
05a92d59 100
483b0559 101//____________________________________________________________________________
5544799a 102AliEMCALClusterizerv1::AliEMCALClusterizerv1(AliEMCALGeometry* geometry)
103 : AliEMCALClusterizer(),
104 fGeom(geometry),
105 fDefaultInit(kFALSE),
106 fToUnfold(kFALSE),
40164976 107 fNumberOfECAClusters(0),fCalibData(0), fCaloPed(0),
5544799a 108 fADCchannelECA(0.),fADCpedestalECA(0.),fECAClusteringThreshold(0.),fECALocMaxCut(0.),
109 fECAW0(0.),fTimeCut(0.),fMinECut(0.)
110{
111 // ctor with the indication of the file where header Tree and digits Tree are stored
112 // use this contructor to avoid usage of Init() which uses runloader
113 // change needed by HLT - MP
114
5544799a 115 // Note for the future: the use on runloader should be avoided or optional at least
116 // another way is to make Init virtual and protected at least such that the deriving classes can overload
117 // Init() ;
118 //
119
120 if (!fGeom)
121 {
122 AliFatal("Geometry not initialized.");
123 }
124
125 if(!gMinuit)
126 gMinuit = new TMinuit(100) ;
127
128}
129
130//____________________________________________________________________________
40164976 131AliEMCALClusterizerv1::AliEMCALClusterizerv1(AliEMCALGeometry* geometry, AliEMCALCalibData * calib, AliCaloCalibPedestal * caloped)
0c5b726e 132: AliEMCALClusterizer(),
133fGeom(geometry),
134fDefaultInit(kFALSE),
135fToUnfold(kFALSE),
40164976 136fNumberOfECAClusters(0),fCalibData(calib), fCaloPed(caloped),
0c5b726e 137fADCchannelECA(0.),fADCpedestalECA(0.),fECAClusteringThreshold(0.),fECALocMaxCut(0.),
138fECAW0(0.),fTimeCut(0.),fMinECut(0.)
139{
140 // ctor, geometry and calibration are initialized elsewhere.
141
142 if (!fGeom)
143 AliFatal("Geometry not initialized.");
144
145 if(!gMinuit)
146 gMinuit = new TMinuit(100) ;
147
148}
149
150
151//____________________________________________________________________________
483b0559 152 AliEMCALClusterizerv1::~AliEMCALClusterizerv1()
153{
ef305168 154 // dtor
ef305168 155}
156
157//____________________________________________________________________________
1bd98442 158Float_t AliEMCALClusterizerv1::Calibrate(Int_t amp, Int_t AbsId)
9859bfc0 159{
1bd98442 160
161 // Convert digitized amplitude into energy.
162 // Calibration parameters are taken from calibration data base for raw data,
163 // or from digitizer parameters for simulated data.
164
165 if(fCalibData){
1bd98442 166
a5c60732 167 if (fGeom==0)
1bd98442 168 AliFatal("Did not get geometry from EMCALLoader") ;
169
170 Int_t iSupMod = -1;
2bb3725c 171 Int_t nModule = -1;
1bd98442 172 Int_t nIphi = -1;
173 Int_t nIeta = -1;
174 Int_t iphi = -1;
175 Int_t ieta = -1;
176
2bb3725c 177 Bool_t bCell = fGeom->GetCellIndex(AbsId, iSupMod, nModule, nIphi, nIeta) ;
1d46d1f6 178 if(!bCell) {
179 fGeom->PrintGeometry();
180 Error("Calibrate()"," Wrong cell id number : %i", AbsId);
181 assert(0);
182 }
54b24bce 183
2bb3725c 184 fGeom->GetCellPhiEtaIndexInSModule(iSupMod,nModule,nIphi, nIeta,iphi,ieta);
40164976 185
186 // Check if channel is bad (dead, hot ...), in this case return 0.
187 if(fCaloPed->IsBadChannel(iSupMod,ieta,iphi)) {
188 AliDebug(2,Form("Tower from SM %d, ieta %d, iphi %d is BAD!!!\n",iSupMod,ieta,iphi));
189 return 0;
190 }
191
1bd98442 192 fADCchannelECA = fCalibData->GetADCchannel (iSupMod,ieta,iphi);
193 fADCpedestalECA = fCalibData->GetADCpedestal(iSupMod,ieta,iphi);
54b24bce 194
195 return -fADCpedestalECA + amp * fADCchannelECA ;
1bd98442 196
197 }
198 else //Return energy with default parameters if calibration is not available
199 return -fADCpedestalECA + amp * fADCchannelECA ;
200
483b0559 201}
05a92d59 202
483b0559 203//____________________________________________________________________________
c47157cd 204void AliEMCALClusterizerv1::Digits2Clusters(Option_t * option)
483b0559 205{
98e9578e 206 // Steering method to perform clusterization for the current event
207 // in AliEMCALLoader
483b0559 208
483b0559 209 if(strstr(option,"tim"))
210 gBenchmark->Start("EMCALClusterizer");
211
212 if(strstr(option,"print"))
213 Print("") ;
98e9578e 214
1bd98442 215 //Get calibration parameters from file or digitizer default values.
216 GetCalibrationParameters() ;
217
40164976 218 //Get dead channel map from file or digitizer default values.
219 GetCaloCalibPedestal() ;
220
98e9578e 221 fNumberOfECAClusters = 0;
483b0559 222
0e7c6655 223 MakeClusters() ; //only the real clusters
a5c60732 224
98e9578e 225 if(fToUnfold)
226 MakeUnfolding() ;
ed611565 227
bbac7e6f 228 Int_t index ;
229
230 //Evaluate position, dispersion and other RecPoint properties for EC section
231 for(index = 0; index < fRecPoints->GetEntries(); index++) {
bbac7e6f 232 dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index))->EvalAll(fECAW0,fDigitsArr) ;
40164976 233 //For each rec.point set the distance to the nearest bad crystal
234 dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index))->EvalDistanceToBadChannels(fCaloPed);
bbac7e6f 235 }
236
237 fRecPoints->Sort() ;
238
239 for(index = 0; index < fRecPoints->GetEntries(); index++) {
240 (dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index)))->SetIndexInList(index) ;
241 (dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index)))->Print();
242 }
243
c47157cd 244 fTreeR->Fill();
245
98e9578e 246 if(strstr(option,"deb") || strstr(option,"all"))
247 PrintRecPoints(option) ;
483b0559 248
c47157cd 249 AliDebug(1,Form("EMCAL Clusterizer found %d Rec Points",fRecPoints->GetEntriesFast()));
88cb7938 250
3f9ad99f 251 fRecPoints->Delete();
252
483b0559 253 if(strstr(option,"tim")){
254 gBenchmark->Stop("EMCALClusterizer");
98e9578e 255 printf("Exec took %f seconds for Clusterizing",
256 gBenchmark->GetCpuTime("EMCALClusterizer"));
257 }
483b0559 258}
259
260//____________________________________________________________________________
d804d556 261Bool_t AliEMCALClusterizerv1::FindFit(AliEMCALRecPoint * recPoint, AliEMCALDigit ** maxAt,
262 const Float_t* maxAtEnergy,
225cd96d 263 Int_t nPar, Float_t * fitparameters) const
264{
265 // Calls TMinuit to fit the energy distribution of a cluster with several maxima
266 // The initial values for fitting procedure are set equal to the
267 // positions of local maxima.
268 // Cluster will be fitted as a superposition of nPar/3
269 // electromagnetic showers
270
271 if (fGeom==0) AliFatal("Did not get geometry from EMCALLoader");
483b0559 272
483b0559 273 gMinuit->mncler(); // Reset Minuit's list of paramters
274 gMinuit->SetPrintLevel(-1) ; // No Printout
225cd96d 275 gMinuit->SetFCN(AliEMCALClusterizerv1::UnfoldingChiSquare) ;
276 // To set the address of the minimization function
483b0559 277 TList * toMinuit = new TList();
d804d556 278 toMinuit->AddAt(recPoint,0) ;
c47157cd 279 toMinuit->AddAt(fDigitsArr,1) ;
225cd96d 280 toMinuit->AddAt(fGeom,2) ;
281
483b0559 282 gMinuit->SetObjectFit(toMinuit) ; // To tranfer pointer to UnfoldingChiSquare
283
284 // filling initial values for fit parameters
285 AliEMCALDigit * digit ;
286
225cd96d 287 Int_t ierflg = 0;
483b0559 288 Int_t index = 0 ;
289 Int_t nDigits = (Int_t) nPar / 3 ;
290
291 Int_t iDigit ;
292
483b0559 293 for(iDigit = 0; iDigit < nDigits; iDigit++){
225cd96d 294 digit = maxAt[iDigit];
295 Double_t x = 0.;
296 Double_t y = 0.;
297 Double_t z = 0.;
483b0559 298
225cd96d 299 fGeom->RelPosCellInSModule(digit->GetId(), y, x, z);
483b0559 300
301 Float_t energy = maxAtEnergy[iDigit] ;
302
303 gMinuit->mnparm(index, "x", x, 0.1, 0, 0, ierflg) ;
225cd96d 304 index++ ;
305 if(ierflg != 0){
306 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : x = %f", x ) ;
483b0559 307 return kFALSE;
308 }
309 gMinuit->mnparm(index, "z", z, 0.1, 0, 0, ierflg) ;
225cd96d 310 index++ ;
483b0559 311 if(ierflg != 0){
225cd96d 312 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : z = %f", z) ;
483b0559 313 return kFALSE;
314 }
315 gMinuit->mnparm(index, "Energy", energy , 0.05*energy, 0., 4.*energy, ierflg) ;
225cd96d 316 index++ ;
483b0559 317 if(ierflg != 0){
225cd96d 318 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : energy = %f", energy) ;
483b0559 319 return kFALSE;
320 }
321 }
322
225cd96d 323 Double_t p0 = 0.1 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ;
324 // The number of function call slightly depends on it.
cb1efdae 325 //Double_t p1 = 1.0 ;
483b0559 326 Double_t p2 = 0.0 ;
327
225cd96d 328 gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ; // force TMinuit to reduce function calls
0561e246 329 // gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ; // force TMinuit to use my gradient
483b0559 330 gMinuit->SetMaxIterations(5);
331 gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ; // No Warnings
225cd96d 332 gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ; // minimize
483b0559 333
225cd96d 334 if(ierflg == 4){ // Minimum not found
335 Error("FindFit", "EMCAL Unfolding Fit not converged, cluster abandoned " ) ;
483b0559 336 return kFALSE ;
225cd96d 337 }
483b0559 338 for(index = 0; index < nPar; index++){
339 Double_t err ;
340 Double_t val ;
341 gMinuit->GetParameter(index, val, err) ; // Returns value and error of parameter index
342 fitparameters[index] = val ;
225cd96d 343 }
483b0559 344
345 delete toMinuit ;
346 return kTRUE;
347
348}
349
350//____________________________________________________________________________
351void AliEMCALClusterizerv1::GetCalibrationParameters()
352{
1bd98442 353 // Set calibration parameters:
354 // if calibration database exists, they are read from database,
355 // otherwise, they are taken from digitizer.
356 //
357 // It is a user responsilibity to open CDB before reconstruction,
358 // for example:
359 // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
360
361 //Check if calibration is stored in data base
54b24bce 362
325a1f94 363 if(!fCalibData)
c47157cd 364 {
365 AliCDBEntry *entry = (AliCDBEntry*)
366 AliCDBManager::Instance()->Get("EMCAL/Calib/Data");
367 if (entry) fCalibData = (AliEMCALCalibData*) entry->GetObject();
368 }
369
370 if(!fCalibData)
371 AliFatal("Calibration parameters not found in CDB!");
372
483b0559 373}
05a92d59 374
483b0559 375//____________________________________________________________________________
40164976 376void AliEMCALClusterizerv1::GetCaloCalibPedestal()
377{
378 // Set calibration parameters:
379 // if calibration database exists, they are read from database,
380 // otherwise, they are taken from digitizer.
381 //
382 // It is a user responsilibity to open CDB before reconstruction,
383 // for example:
384 // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
385
386 //Check if calibration is stored in data base
387
388 if(!fCaloPed)
389 {
390 AliCDBEntry *entry = (AliCDBEntry*)
391 AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals");
392 if (entry) fCaloPed = (AliCaloCalibPedestal*) entry->GetObject();
393 }
394
395 if(!fCaloPed)
396 AliFatal("Pedestal info not found in CDB!");
397
398}
399
400
401//____________________________________________________________________________
483b0559 402void AliEMCALClusterizerv1::Init()
403{
404 // Make all memory allocations which can not be done in default constructor.
405 // Attach the Clusterizer task to the list of EMCAL tasks
406
33c3c91a 407 AliRunLoader *rl = AliRunLoader::Instance();
98e9578e 408 if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL"))
409 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
410 else
937d0661 411 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
98e9578e 412
6d0b6861 413 AliDebug(1,Form("geom 0x%x",fGeom));
05a92d59 414
483b0559 415 if(!gMinuit)
416 gMinuit = new TMinuit(100) ;
a5c60732 417
483b0559 418}
419
420//____________________________________________________________________________
839828a6 421void AliEMCALClusterizerv1::InitParameters()
fdebddeb 422{
423 // Initializes the parameters for the Clusterizer
424 fNumberOfECAClusters = 0;
98e9578e 425 fTimeCut = 300e-9 ; // 300 ns time cut (to be tuned)
1bd98442 426
427 fCalibData = 0 ;
40164976 428 fCaloPed = 0 ;
429
c47157cd 430 const AliEMCALRecParam* recParam = AliEMCALReconstructor::GetRecParam();
431 if(!recParam) {
432 AliFatal("Reconstruction parameters for EMCAL not set!");
0832a2bf 433 } else {
c47157cd 434 fECAClusteringThreshold = recParam->GetClusteringThreshold();
435 fECAW0 = recParam->GetW0();
225cd96d 436 fMinECut = recParam->GetMinECut();
437 fToUnfold = recParam->GetUnfold();
0561e246 438 if(fToUnfold) AliWarning("Cluster Unfolding ON. Implementing only for eta=0 case!!!");
225cd96d 439 fECALocMaxCut = recParam->GetLocMaxCut();
0832a2bf 440
225cd96d 441 AliDebug(1,Form("Reconstruction parameters: fECAClusteringThreshold=%.3f, fECAW=%.3f, fMinECut=%.3f, fToUnfold=%d, fECALocMaxCut=%.3f",
0832a2bf 442 fECAClusteringThreshold,fECAW0,fMinECut,fToUnfold,fECALocMaxCut));
3a8be91c 443 }
444
839828a6 445}
446
447//____________________________________________________________________________
e52475ed 448Int_t AliEMCALClusterizerv1::AreNeighbours(AliEMCALDigit * d1, AliEMCALDigit * d2) const
483b0559 449{
1d46d1f6 450 // Gives the neighbourness of two digits = 0 are not neighbour ; continue searching
483b0559 451 // = 1 are neighbour
1d46d1f6 452 // = 2 is in different SM; continue searching
483b0559 453 // neighbours are defined as digits having at least a common vertex
454 // The order of d1 and d2 is important: first (d1) should be a digit already in a cluster
455 // which is compared to a digit (d2) not yet in a cluster
456
e52475ed 457 static Int_t rv;
2bb3725c 458 static Int_t nSupMod1=0, nModule1=0, nIphi1=0, nIeta1=0, iphi1=0, ieta1=0;
459 static Int_t nSupMod2=0, nModule2=0, nIphi2=0, nIeta2=0, iphi2=0, ieta2=0;
e52475ed 460 static Int_t rowdiff, coldiff;
461 rv = 0 ;
483b0559 462
2bb3725c 463 fGeom->GetCellIndex(d1->GetId(), nSupMod1,nModule1,nIphi1,nIeta1);
464 fGeom->GetCellIndex(d2->GetId(), nSupMod2,nModule2,nIphi2,nIeta2);
e52475ed 465 if(nSupMod1 != nSupMod2) return 2; // different SM
466
2bb3725c 467 fGeom->GetCellPhiEtaIndexInSModule(nSupMod1,nModule1,nIphi1,nIeta1, iphi1,ieta1);
468 fGeom->GetCellPhiEtaIndexInSModule(nSupMod2,nModule2,nIphi2,nIeta2, iphi2,ieta2);
e52475ed 469
470 rowdiff = TMath::Abs(iphi1 - iphi2);
471 coldiff = TMath::Abs(ieta1 - ieta2) ;
70a93198 472
3d99d6d5 473 // neighbours with at least commom side; May 11, 2007
474 if ((coldiff==0 && abs(rowdiff)==1) || (rowdiff==0 && abs(coldiff)==1)) rv = 1;
70a93198 475
e52475ed 476 if (gDebug == 2 && rv==1)
477 printf("AreNeighbours: neighbours=%d, id1=%d, relid1=%d,%d \n id2=%d, relid2=%d,%d \n",
478 rv, d1->GetId(), iphi1,ieta1, d2->GetId(), iphi2,ieta2);
ed611565 479
483b0559 480 return rv ;
481}
482
88cb7938 483//____________________________________________________________________________
0e7c6655 484void AliEMCALClusterizerv1::MakeClusters()
483b0559 485{
486 // Steering method to construct the clusters stored in a list of Reconstructed Points
487 // A cluster is defined as a list of neighbour digits
f1487f22 488 // Mar 03, 2007 by PAI
ed611565 489
f1487f22 490 if (fGeom==0) AliFatal("Did not get geometry from EMCALLoader");
ed611565 491
c47157cd 492 fRecPoints->Clear();
98e9578e 493
494 // Set up TObjArray with pointers to digits to work on
495 TObjArray *digitsC = new TObjArray();
c47157cd 496 TIter nextdigit(fDigitsArr);
98e9578e 497 AliEMCALDigit *digit;
498 while ( (digit = dynamic_cast<AliEMCALDigit*>(nextdigit())) ) {
499 digitsC->AddLast(digit);
500 }
a5c60732 501
1d46d1f6 502 double e = 0.0, ehs = 0.0;
98e9578e 503 TIter nextdigitC(digitsC);
504
1d46d1f6 505 while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigitC())) ) { // clean up digits
1bd98442 506 e = Calibrate(digit->GetAmp(), digit->GetId());
98e9578e 507 if ( e < fMinECut || digit->GetTimeR() > fTimeCut )
508 digitsC->Remove(digit);
509 else
510 ehs += e;
1d59832c 511 }
512 AliDebug(1,Form("MakeClusters: Number of digits %d -> (e %f), ehs %d\n",
c47157cd 513 fDigitsArr->GetEntries(),fMinECut,ehs));
98e9578e 514
1d46d1f6 515 nextdigitC.Reset();
98e9578e 516
1d46d1f6 517 while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigitC())) ) { // scan over the list of digitsC
c47157cd 518 TArrayI clusterECAdigitslist(fDigitsArr->GetEntries());
e52475ed 519
a5c60732 520 if(fGeom->CheckAbsCellId(digit->GetId()) && (Calibrate(digit->GetAmp(), digit->GetId()) > fECAClusteringThreshold ) ){
98e9578e 521 // start a new Tower RecPoint
c47157cd 522 if(fNumberOfECAClusters >= fRecPoints->GetSize()) fRecPoints->Expand(2*fNumberOfECAClusters+1) ;
ab6a174f 523
98e9578e 524 AliEMCALRecPoint *recPoint = new AliEMCALRecPoint("") ;
c47157cd 525 fRecPoints->AddAt(recPoint, fNumberOfECAClusters) ;
526 recPoint = dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(fNumberOfECAClusters)) ;
4aa81449 527 fNumberOfECAClusters++ ;
a5c60732 528
8ada0ffe 529 recPoint->SetClusterType(AliESDCaloCluster::kEMCALClusterv1);
a5c60732 530
1bd98442 531 recPoint->AddDigit(*digit, Calibrate(digit->GetAmp(), digit->GetId())) ;
98e9578e 532 TObjArray clusterDigits;
533 clusterDigits.AddLast(digit);
4aa81449 534 digitsC->Remove(digit) ;
f1487f22 535
536 AliDebug(1,Form("MakeClusters: OK id = %d, ene = %f , cell.th. = %f \n", digit->GetId(),
537 Calibrate(digit->GetAmp(),digit->GetId()), fECAClusteringThreshold));
483b0559 538
98e9578e 539 // Grow cluster by finding neighbours
540 TIter nextClusterDigit(&clusterDigits);
541 while ( (digit = dynamic_cast<AliEMCALDigit*>(nextClusterDigit())) ) { // scan over digits in cluster
542 TIter nextdigitN(digitsC);
543 AliEMCALDigit *digitN = 0; // digi neighbor
544 while ( (digitN = (AliEMCALDigit *)nextdigitN()) ) { // scan over all digits to look for neighbours
545 if (AreNeighbours(digit, digitN)==1) { // call (digit,digitN) in THAT oder !!!!!
1d46d1f6 546 recPoint->AddDigit(*digitN, Calibrate(digitN->GetAmp(),digitN->GetId()) ) ;
98e9578e 547 clusterDigits.AddLast(digitN) ;
1d46d1f6 548 digitsC->Remove(digitN) ;
1d46d1f6 549 } // if(ineb==1)
98e9578e 550 } // scan over digits
e52475ed 551 } // scan over digits already in cluster
98e9578e 552 if(recPoint)
553 AliDebug(2,Form("MakeClusters: %d digitd, energy %f \n", clusterDigits.GetEntries(), recPoint->GetEnergy()));
554 } // If seed found
1d59832c 555 } // while digit
98e9578e 556
483b0559 557 delete digitsC ;
ab6a174f 558
c47157cd 559 AliDebug(1,Form("total no of clusters %d from %d digits",fNumberOfECAClusters,fDigitsArr->GetEntriesFast()));
483b0559 560}
561
7ea6391b 562//____________________________________________________________________________
225cd96d 563void AliEMCALClusterizerv1::MakeUnfolding()
483b0559 564{
225cd96d 565 // Unfolds clusters using the shape of an ElectroMagnetic shower
566 // Performs unfolding of all clusters
567
568 if(fNumberOfECAClusters > 0){
569 if (fGeom==0)
570 AliFatal("Did not get geometry from EMCALLoader") ;
571 Int_t nModulesToUnfold = fGeom->GetNCells();
572
573 Int_t numberofNotUnfolded = fNumberOfECAClusters ;
574 Int_t index ;
575 for(index = 0 ; index < numberofNotUnfolded ; index++){
576
d804d556 577 AliEMCALRecPoint * recPoint = dynamic_cast<AliEMCALRecPoint *>( fRecPoints->At(index) ) ;
225cd96d 578
579 TVector3 gpos;
580 Int_t absId;
d804d556 581 recPoint->GetGlobalPosition(gpos);
225cd96d 582 fGeom->GetAbsCellIdFromEtaPhi(gpos.Eta(),gpos.Phi(),absId);
583 if(absId > nModulesToUnfold)
584 break ;
585
d804d556 586 Int_t nMultipl = recPoint->GetMultiplicity() ;
225cd96d 587 AliEMCALDigit ** maxAt = new AliEMCALDigit*[nMultipl] ;
588 Float_t * maxAtEnergy = new Float_t[nMultipl] ;
d804d556 589 Int_t nMax = recPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy,fECALocMaxCut,fDigitsArr) ;
225cd96d 590
591 if( nMax > 1 ) { // if cluster is very flat (no pronounced maximum) then nMax = 0
d804d556 592 UnfoldCluster(recPoint, nMax, maxAt, maxAtEnergy) ;
593 fRecPoints->Remove(recPoint);
225cd96d 594 fRecPoints->Compress() ;
595 index-- ;
596 fNumberOfECAClusters-- ;
597 numberofNotUnfolded-- ;
598 }
599 else{
d804d556 600 recPoint->SetNExMax(1) ; //Only one local maximum
225cd96d 601 }
602
603 delete[] maxAt ;
604 delete[] maxAtEnergy ;
605 }
606 }
607 // End of Unfolding of clusters
483b0559 608}
609
610//____________________________________________________________________________
225cd96d 611Double_t AliEMCALClusterizerv1::ShowerShape(Double_t x, Double_t y)
483b0559 612{
225cd96d 613 // Shape of the shower
483b0559 614 // If you change this function, change also the gradient evaluation in ChiSquare()
615
225cd96d 616 Double_t r = sqrt(x*x+y*y);
617 Double_t r133 = TMath::Power(r, 1.33) ;
618 Double_t r669 = TMath::Power(r, 6.69) ;
619 Double_t shape = TMath::Exp( -r133 * (1. / (1.57 + 0.0860 * r133) - 0.55 / (1 + 0.000563 * r669) ) ) ;
483b0559 620 return shape ;
621}
622
623//____________________________________________________________________________
225cd96d 624void AliEMCALClusterizerv1::UnfoldCluster(AliEMCALRecPoint * iniTower,
625 Int_t nMax,
626 AliEMCALDigit ** maxAt,
627 Float_t * maxAtEnergy)
483b0559 628{
629 // Performs the unfolding of a cluster with nMax overlapping showers
225cd96d 630 Int_t nPar = 3 * nMax ;
631 Float_t * fitparameters = new Float_t[nPar] ;
632
633 if (fGeom==0)
634 AliFatal("Did not get geometry from EMCALLoader") ;
635
636 Bool_t rv = FindFit(iniTower, maxAt, maxAtEnergy, nPar, fitparameters) ;
637 if( !rv ) {
638 // Fit failed, return and remove cluster
639 iniTower->SetNExMax(-1) ;
640 delete[] fitparameters ;
641 return ;
642 }
643
644 // create unfolded rec points and fill them with new energy lists
645 // First calculate energy deposited in each sell in accordance with
646 // fit (without fluctuations): efit[]
647 // and later correct this number in acordance with actual energy
648 // deposition
649
650 Int_t nDigits = iniTower->GetMultiplicity() ;
651 Float_t * efit = new Float_t[nDigits] ;
652 Double_t xDigit=0.,yDigit=0.,zDigit=0. ;
653 Float_t xpar=0.,zpar=0.,epar=0. ;
654
655 AliEMCALDigit * digit = 0 ;
d804d556 656 Int_t * digitsList = iniTower->GetDigitsList() ;
225cd96d 657
658 Int_t iparam ;
659 Int_t iDigit ;
660 for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
d804d556 661 digit = dynamic_cast<AliEMCALDigit*>( fDigitsArr->At(digitsList[iDigit] ) ) ;
225cd96d 662 fGeom->RelPosCellInSModule(digit->GetId(), yDigit, xDigit, zDigit);
663 efit[iDigit] = 0;
664
665 iparam = 0 ;
666 while(iparam < nPar ){
667 xpar = fitparameters[iparam] ;
668 zpar = fitparameters[iparam+1] ;
669 epar = fitparameters[iparam+2] ;
670 iparam += 3 ;
671 efit[iDigit] += epar * ShowerShape(xDigit - xpar,zDigit - zpar) ;
672 }
673 }
674
675
676 // Now create new RecPoints and fill energy lists with efit corrected to fluctuations
677 // so that energy deposited in each cell is distributed between new clusters proportionally
678 // to its contribution to efit
679
d804d556 680 Float_t * energiesList = iniTower->GetEnergiesList() ;
225cd96d 681 Float_t ratio ;
682
683 iparam = 0 ;
684 while(iparam < nPar ){
685 xpar = fitparameters[iparam] ;
686 zpar = fitparameters[iparam+1] ;
687 epar = fitparameters[iparam+2] ;
688 iparam += 3 ;
689
d804d556 690 AliEMCALRecPoint * recPoint = 0 ;
225cd96d 691
692 if(fNumberOfECAClusters >= fRecPoints->GetSize())
693 fRecPoints->Expand(2*fNumberOfECAClusters) ;
694
695 (*fRecPoints)[fNumberOfECAClusters] = new AliEMCALRecPoint("") ;
d804d556 696 recPoint = dynamic_cast<AliEMCALRecPoint *>( fRecPoints->At(fNumberOfECAClusters) ) ;
225cd96d 697 fNumberOfECAClusters++ ;
d804d556 698 recPoint->SetNExMax((Int_t)nPar/3) ;
225cd96d 699
700 Float_t eDigit ;
701 for(iDigit = 0 ; iDigit < nDigits ; iDigit ++){
d804d556 702 digit = dynamic_cast<AliEMCALDigit*>( fDigitsArr->At( digitsList[iDigit] ) ) ;
225cd96d 703 fGeom->RelPosCellInSModule(digit->GetId(), yDigit, xDigit, zDigit);
704
705 ratio = epar * ShowerShape(xDigit - xpar,zDigit - zpar) / efit[iDigit] ;
d804d556 706 eDigit = energiesList[iDigit] * ratio ;
707 recPoint->AddDigit( *digit, eDigit ) ;
225cd96d 708 }
709 }
710
711 delete[] fitparameters ;
712 delete[] efit ;
173558f2 713
483b0559 714}
715
716//_____________________________________________________________________________
225cd96d 717void AliEMCALClusterizerv1::UnfoldingChiSquare(Int_t & nPar, Double_t * Grad,
718 Double_t & fret,
719 Double_t * x, Int_t iflag)
483b0559 720{
721 // Calculates the Chi square for the cluster unfolding minimization
722 // Number of parameters, Gradient, Chi squared, parameters, what to do
225cd96d 723
724 TList * toMinuit = dynamic_cast<TList*>( gMinuit->GetObjectFit() ) ;
725
d804d556 726 AliEMCALRecPoint * recPoint = dynamic_cast<AliEMCALRecPoint*>( toMinuit->At(0) ) ;
225cd96d 727 TClonesArray * digits = dynamic_cast<TClonesArray*>( toMinuit->At(1) ) ;
728 // A bit buggy way to get an access to the geometry
729 // To be revised!
730 AliEMCALGeometry *geom = dynamic_cast<AliEMCALGeometry *>(toMinuit->At(2));
731
d804d556 732 Int_t * digitsList = recPoint->GetDigitsList() ;
225cd96d 733
d804d556 734 Int_t nOdigits = recPoint->GetDigitsMultiplicity() ;
225cd96d 735
d804d556 736 Float_t * energiesList = recPoint->GetEnergiesList() ;
225cd96d 737
738 fret = 0. ;
739 Int_t iparam ;
740
741 if(iflag == 2)
742 for(iparam = 0 ; iparam < nPar ; iparam++)
743 Grad[iparam] = 0 ; // Will evaluate gradient
744
745 Double_t efit ;
746
747 AliEMCALDigit * digit ;
748 Int_t iDigit ;
749
750 for( iDigit = 0 ; iDigit < nOdigits ; iDigit++) {
751
d804d556 752 digit = dynamic_cast<AliEMCALDigit*>( digits->At( digitsList[iDigit] ) );
225cd96d 753
754 Double_t xDigit=0 ;
755 Double_t zDigit=0 ;
756 Double_t yDigit=0 ;//not used yet, assumed to be 0
757
758 geom->RelPosCellInSModule(digit->GetId(), yDigit, xDigit, zDigit);
759
760 if(iflag == 2){ // calculate gradient
761 Int_t iParam = 0 ;
762 efit = 0 ;
763 while(iParam < nPar ){
764 Double_t dx = (xDigit - x[iParam]) ;
765 iParam++ ;
766 Double_t dz = (zDigit - x[iParam]) ;
767 iParam++ ;
768 efit += x[iParam] * ShowerShape(dx,dz) ;
769 iParam++ ;
770 }
d804d556 771 Double_t sum = 2. * (efit - energiesList[iDigit]) / energiesList[iDigit] ; // Here we assume, that sigma = sqrt(E)
225cd96d 772 iParam = 0 ;
773 while(iParam < nPar ){
774 Double_t xpar = x[iParam] ;
775 Double_t zpar = x[iParam+1] ;
776 Double_t epar = x[iParam+2] ;
777 Double_t dr = TMath::Sqrt( (xDigit - xpar) * (xDigit - xpar) + (zDigit - zpar) * (zDigit - zpar) );
778 Double_t shape = sum * ShowerShape(xDigit - xpar,zDigit - zpar) ;
779 Double_t r133 = TMath::Power(dr, 1.33);
780 Double_t r669 = TMath::Power(dr,6.69);
781 Double_t deriv =-1.33 * TMath::Power(dr,0.33)*dr * ( 1.57 / ( (1.57 + 0.0860 * r133) * (1.57 + 0.0860 * r133) )
782 - 0.55 / (1 + 0.000563 * r669) / ( (1 + 0.000563 * r669) * (1 + 0.000563 * r669) ) ) ;
783
784 Grad[iParam] += epar * shape * deriv * (xpar - xDigit) ; // Derivative over x
785 iParam++ ;
786 Grad[iParam] += epar * shape * deriv * (zpar - zDigit) ; // Derivative over z
787 iParam++ ;
788 Grad[iParam] += shape ; // Derivative over energy
789 iParam++ ;
790 }
791 }
792 efit = 0;
793 iparam = 0 ;
794
795
796 while(iparam < nPar ){
797 Double_t xpar = x[iparam] ;
798 Double_t zpar = x[iparam+1] ;
799 Double_t epar = x[iparam+2] ;
800 iparam += 3 ;
801 efit += epar * ShowerShape(xDigit - xpar,zDigit - zpar) ;
802 }
803
d804d556 804 fret += (efit-energiesList[iDigit])*(efit-energiesList[iDigit])/energiesList[iDigit] ;
225cd96d 805 // Here we assume, that sigma = sqrt(E)
806 }
483b0559 807}
483b0559 808//____________________________________________________________________________
9e5d2067 809void AliEMCALClusterizerv1::Print(Option_t * /*option*/)const
483b0559 810{
811 // Print clusterizer parameters
812
9859bfc0 813 TString message("\n") ;
814
483b0559 815 if( strcmp(GetName(), "") !=0 ){
816
817 // Print parameters
818
c47157cd 819 TString taskName(Version()) ;
9859bfc0 820
fdebddeb 821 printf("--------------- ");
d804d556 822 printf("%s",taskName.Data()) ;
fdebddeb 823 printf(" ");
c47157cd 824 printf("Clusterizing digits: ");
fdebddeb 825 printf("\n ECA Local Maximum cut = %f", fECALocMaxCut);
b481a360 826 printf("\n ECA Logarithmic weight = %f", fECAW0);
483b0559 827 if(fToUnfold)
fdebddeb 828 printf("\nUnfolding on\n");
483b0559 829 else
fdebddeb 830 printf("\nUnfolding off\n");
483b0559 831
fdebddeb 832 printf("------------------------------------------------------------------");
483b0559 833 }
834 else
fdebddeb 835 printf("AliEMCALClusterizerv1 not initialized ") ;
483b0559 836}
173558f2 837
483b0559 838//____________________________________________________________________________
839void AliEMCALClusterizerv1::PrintRecPoints(Option_t * option)
840{
b481a360 841 // Prints list of RecPoints produced at the current pass of AliEMCALClusterizer
1d46d1f6 842 if(strstr(option,"deb")) {
843 printf("PrintRecPoints: Clusterization result:") ;
ed611565 844
1d46d1f6 845 printf(" Found %d ECA Rec Points\n ",
c47157cd 846 fRecPoints->GetEntriesFast()) ;
1d46d1f6 847 }
483b0559 848
849 if(strstr(option,"all")) {
1d46d1f6 850 if(strstr(option,"deb")) {
851 printf("\n-----------------------------------------------------------------------\n") ;
852 printf("Clusters in ECAL section\n") ;
853 printf("Index Ene(GeV) Multi Module GX GY GZ lX lY lZ Dispersion Lambda 1 Lambda 2 # of prim Primaries list\n") ;
854 }
855 Int_t index =0;
1d46d1f6 856
c47157cd 857 for (index = 0 ; index < fRecPoints->GetEntries() ; index++) {
858 AliEMCALRecPoint * rp = dynamic_cast<AliEMCALRecPoint * >(fRecPoints->At(index)) ;
483b0559 859 TVector3 globalpos;
1963b290 860 //rp->GetGlobalPosition(globalpos);
ed611565 861 TVector3 localpos;
862 rp->GetLocalPosition(localpos);
483b0559 863 Float_t lambda[2];
864 rp->GetElipsAxis(lambda);
865 Int_t * primaries;
866 Int_t nprimaries;
867 primaries = rp->GetPrimaries(nprimaries);
1d46d1f6 868 if(strstr(option,"deb"))
70a93198 869 printf("\n%6d %8.4f %3d %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4f %4f %2d : ",
870 rp->GetIndexInList(), rp->GetEnergy(), rp->GetMultiplicity(),
ed611565 871 globalpos.X(), globalpos.Y(), globalpos.Z(), localpos.X(), localpos.Y(), localpos.Z(),
872 rp->GetDispersion(), lambda[0], lambda[1], nprimaries) ;
1d46d1f6 873 if(strstr(option,"deb")){
874 for (Int_t iprimary=0; iprimary<nprimaries; iprimary++) {
875 printf("%d ", primaries[iprimary] ) ;
876 }
877 }
483b0559 878 }
1963b290 879
1d46d1f6 880 if(strstr(option,"deb"))
ed611565 881 printf("\n-----------------------------------------------------------------------\n");
483b0559 882 }
883}
7ea6391b 884
7ea6391b 885//___________________________________________________________________
1d46d1f6 886void AliEMCALClusterizerv1::PrintRecoInfo()
887{
888 printf(" AliEMCALClusterizerv1::PrintRecoInfo() : version %s \n", Version() );
1d46d1f6 889
1d46d1f6 890}