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