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