Taking into account the masked local boards in regional input (Christian)
[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
483b0559 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)
483b0559 21//////////////////////////////////////////////////////////////////////////////
22// Clusterization class. Performs clusterization (collects neighbouring active cells) and
23// unfolds the clusters having several local maxima.
24// Results are stored in TreeR#, branches EMCALTowerRP (EMC recPoints),
25// EMCALPreShoRP (CPV RecPoints) and AliEMCALClusterizer (Clusterizer with all
26// parameters including input digits branch title, thresholds etc.)
27// This TTask is normally called from Reconstructioner, but can as well be used in
28// standalone mode.
29// Use Case:
30// root [0] AliEMCALClusterizerv1 * cl = new AliEMCALClusterizerv1("galice.root")
31// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32// //reads gAlice from header file "..."
33// root [1] cl->ExecuteTask()
34// //finds RecPoints in all events stored in galice.root
35// root [2] cl->SetDigitsBranch("digits2")
36// //sets another title for Digitis (input) branch
37// root [3] cl->SetRecPointsBranch("recp2")
38// //sets another title four output branches
39// root [4] cl->SetTowerLocalMaxCut(0.03)
40// //set clusterization parameters
41// root [5] cl->ExecuteTask("deb all time")
42// //once more finds RecPoints options are
43// // deb - print number of found rec points
44// // deb all - print number of found RecPoints and some their characteristics
45// // time - print benchmarking results
46
47// --- ROOT system ---
48
1d59832c 49class TROOT;
e52475ed 50#include <TH1.h>
51#include <TFile.h>
1d59832c 52class TFolder;
e52475ed 53#include <TMath.h>
54#include <TMinuit.h>
55#include <TTree.h>
1d59832c 56class TSystem;
e52475ed 57#include <TBenchmark.h>
58#include <TBrowser.h>
1d59832c 59
483b0559 60// --- Standard library ---
61
173558f2 62
483b0559 63// --- AliRoot header files ---
5dee926e 64#include "AliRunLoader.h"
65#include "AliRun.h"
1d59832c 66#include "AliESD.h"
5dee926e 67#include "AliEMCALLoader.h"
483b0559 68#include "AliEMCALClusterizerv1.h"
70a93198 69#include "AliEMCALRecPoint.h"
483b0559 70#include "AliEMCALDigit.h"
71#include "AliEMCALDigitizer.h"
483b0559 72#include "AliEMCAL.h"
05a92d59 73#include "AliEMCALGeometry.h"
e52475ed 74#include "AliEMCALHistoUtilities.h"
1bd98442 75#include "AliCDBManager.h"
1d59832c 76
77class AliCDBStorage;
1bd98442 78#include "AliCDBEntry.h"
483b0559 79
80ClassImp(AliEMCALClusterizerv1)
1963b290 81
483b0559 82//____________________________________________________________________________
83 AliEMCALClusterizerv1::AliEMCALClusterizerv1() : AliEMCALClusterizer()
84{
85 // default ctor (to be used mainly by Streamer)
86
839828a6 87 InitParameters() ;
1963b290 88 fDefaultInit = kTRUE ;
a5c60732 89 fGeom = AliEMCALGeometry::GetInstance();
90 fGeom->GetTransformationForSM(); // Global <-> Local
483b0559 91}
92
93//____________________________________________________________________________
88cb7938 94AliEMCALClusterizerv1::AliEMCALClusterizerv1(const TString alirunFileName, const TString eventFolderName)
95:AliEMCALClusterizer(alirunFileName, eventFolderName)
483b0559 96{
97 // ctor with the indication of the file where header Tree and digits Tree are stored
98
839828a6 99 InitParameters() ;
483b0559 100 Init() ;
05a92d59 101 fDefaultInit = kFALSE ;
483b0559 102}
05a92d59 103
483b0559 104//____________________________________________________________________________
105 AliEMCALClusterizerv1::~AliEMCALClusterizerv1()
106{
ef305168 107 // dtor
ef305168 108}
109
110//____________________________________________________________________________
111const TString AliEMCALClusterizerv1::BranchName() const
9859bfc0 112{
88cb7938 113 return GetName();
483b0559 114}
839828a6 115
483b0559 116//____________________________________________________________________________
1bd98442 117Float_t AliEMCALClusterizerv1::Calibrate(Int_t amp, Int_t AbsId)
9859bfc0 118{
1bd98442 119
120 // Convert digitized amplitude into energy.
121 // Calibration parameters are taken from calibration data base for raw data,
122 // or from digitizer parameters for simulated data.
123
124 if(fCalibData){
a5c60732 125
126 //JLK 13-Mar-2006
127 //We now get geometry at a higher level
128 //
1bd98442 129 // Loader
a5c60732 130 // AliRunLoader *rl = AliRunLoader::GetRunLoader();
1bd98442 131
a5c60732 132 // Load EMCAL Geomtry
133 // rl->LoadgAlice();
134 //AliRun * gAlice = rl->GetAliRun();
135 //AliEMCAL * emcal = (AliEMCAL*)gAlice->GetDetector("EMCAL");
136 //AliEMCALGeometry * geom = emcal->GetGeometry();
1bd98442 137
a5c60732 138 if (fGeom==0)
1bd98442 139 AliFatal("Did not get geometry from EMCALLoader") ;
140
141 Int_t iSupMod = -1;
142 Int_t nTower = -1;
143 Int_t nIphi = -1;
144 Int_t nIeta = -1;
145 Int_t iphi = -1;
146 Int_t ieta = -1;
147
a5c60732 148 Bool_t bCell = fGeom->GetCellIndex(AbsId, iSupMod, nTower, nIphi, nIeta) ;
1bd98442 149 if(!bCell)
150 Error("DigitizeEnergy","Wrong cell id number") ;
a5c60732 151 fGeom->GetCellPhiEtaIndexInSModule(iSupMod,nTower,nIphi, nIeta,iphi,ieta);
1bd98442 152
153 fADCchannelECA = fCalibData->GetADCchannel (iSupMod,ieta,iphi);
154 fADCpedestalECA = fCalibData->GetADCpedestal(iSupMod,ieta,iphi);
155 return -fADCpedestalECA + amp * fADCchannelECA ;
156
157 }
158 else //Return energy with default parameters if calibration is not available
159 return -fADCpedestalECA + amp * fADCchannelECA ;
160
483b0559 161}
05a92d59 162
483b0559 163//____________________________________________________________________________
164void AliEMCALClusterizerv1::Exec(Option_t * option)
165{
12c4dd95 166 // Steering method to perform clusterization for events
167 // in the range from fFirstEvent to fLastEvent.
168 // This range is optionally set by SetEventRange().
169 // if fLastEvent=-1 (by default), then process events until the end.
483b0559 170
483b0559 171 if(strstr(option,"tim"))
172 gBenchmark->Start("EMCALClusterizer");
173
174 if(strstr(option,"print"))
175 Print("") ;
176
5dee926e 177 AliRunLoader *rl = AliRunLoader::GetRunLoader();
178 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
88cb7938 179
1bd98442 180 //Get calibration parameters from file or digitizer default values.
181 GetCalibrationParameters() ;
182
12c4dd95 183 if (fLastEvent == -1)
5dee926e 184 fLastEvent = rl->GetNumberOfEvents() - 1;
12c4dd95 185 Int_t nEvents = fLastEvent - fFirstEvent + 1;
483b0559 186
12c4dd95 187 Int_t ievent ;
5dee926e 188 rl->LoadDigits("EMCAL");
12c4dd95 189 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 190 rl->GetEvent(ievent);
483b0559 191
fdebddeb 192 fNumberOfECAClusters = 0;
a5c60732 193
194 if(strstr(option,"pseudo"))
195 MakeClusters("pseudo") ; //both types
196 else
197 MakeClusters("") ; //only the real clusters
ed611565 198
483b0559 199 if(fToUnfold)
200 MakeUnfolding() ;
201
9e5d2067 202 WriteRecPoints() ;
483b0559 203
204 if(strstr(option,"deb"))
205 PrintRecPoints(option) ;
206
fdebddeb 207 //increment the total number of recpoints per run
5dee926e 208 fRecPointsInRun += emcalLoader->RecPoints()->GetEntriesFast() ;
88cb7938 209 }
483b0559 210
88cb7938 211 Unload();
212
483b0559 213 if(strstr(option,"tim")){
214 gBenchmark->Stop("EMCALClusterizer");
fdebddeb 215 printf("Exec took %f seconds for Clusterizing %f seconds per event",
e52475ed 216 gBenchmark->GetCpuTime("EMCALClusterizer"), gBenchmark->GetCpuTime("EMCALClusterizer")/nEvents );
483b0559 217 }
1963b290 218
483b0559 219}
220
221//____________________________________________________________________________
70a93198 222Bool_t AliEMCALClusterizerv1::FindFit(AliEMCALRecPoint * emcRP, AliEMCALDigit ** maxAt, Float_t * maxAtEnergy,
483b0559 223 Int_t nPar, Float_t * fitparameters) const
224{
225 // Calls TMinuit to fit the energy distribution of a cluster with several maxima
226 // The initial values for fitting procedure are set equal to the positions of local maxima.
227 // Cluster will be fitted as a superposition of nPar/3 electromagnetic showers
228
5dee926e 229 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
230 TClonesArray *digits = emcalLoader->Digits();
5dee926e 231
483b0559 232 gMinuit->mncler(); // Reset Minuit's list of paramters
233 gMinuit->SetPrintLevel(-1) ; // No Printout
234 gMinuit->SetFCN(AliEMCALClusterizerv1::UnfoldingChiSquare) ;
235 // To set the address of the minimization function
483b0559 236 TList * toMinuit = new TList();
237 toMinuit->AddAt(emcRP,0) ;
238 toMinuit->AddAt(digits,1) ;
239
240 gMinuit->SetObjectFit(toMinuit) ; // To tranfer pointer to UnfoldingChiSquare
241
242 // filling initial values for fit parameters
243 AliEMCALDigit * digit ;
244
245 Int_t ierflg = 0;
246 Int_t index = 0 ;
247 Int_t nDigits = (Int_t) nPar / 3 ;
248
249 Int_t iDigit ;
250
483b0559 251 for(iDigit = 0; iDigit < nDigits; iDigit++){
a0636361 252 digit = maxAt[iDigit];
483b0559 253
483b0559 254 Float_t x = 0.;
255 Float_t z = 0.;
dc7da436 256 // have to be tune for TRD1; May 31,06
257 // Int_t relid[2] ;
258 // fGeom->AbsToRelNumbering(digit->GetId(), relid) ; // obsolete method
259 // fGeom->PosInAlice(relid, x, z) ; // obsolete method
483b0559 260
261 Float_t energy = maxAtEnergy[iDigit] ;
262
263 gMinuit->mnparm(index, "x", x, 0.1, 0, 0, ierflg) ;
264 index++ ;
265 if(ierflg != 0){
9859bfc0 266 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : x = %f", x ) ;
483b0559 267 return kFALSE;
268 }
269 gMinuit->mnparm(index, "z", z, 0.1, 0, 0, ierflg) ;
270 index++ ;
271 if(ierflg != 0){
9859bfc0 272 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : z = %f", z) ;
483b0559 273 return kFALSE;
274 }
275 gMinuit->mnparm(index, "Energy", energy , 0.05*energy, 0., 4.*energy, ierflg) ;
276 index++ ;
277 if(ierflg != 0){
9859bfc0 278 Error("FindFit", "EMCAL Unfolding Unable to set initial value for fit procedure : energy = %f", energy) ;
483b0559 279 return kFALSE;
280 }
281 }
282
283 Double_t p0 = 0.1 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
284 // depends on it.
285 Double_t p1 = 1.0 ;
286 Double_t p2 = 0.0 ;
287
288 gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ; // force TMinuit to reduce function calls
289 gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ; // force TMinuit to use my gradient
290 gMinuit->SetMaxIterations(5);
291 gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ; // No Warnings
292
293 gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ; // minimize
294
295 if(ierflg == 4){ // Minimum not found
9859bfc0 296 Error("FindFit", "EMCAL Unfolding Fit not converged, cluster abandoned " ) ;
483b0559 297 return kFALSE ;
298 }
299 for(index = 0; index < nPar; index++){
300 Double_t err ;
301 Double_t val ;
302 gMinuit->GetParameter(index, val, err) ; // Returns value and error of parameter index
303 fitparameters[index] = val ;
304 }
305
306 delete toMinuit ;
307 return kTRUE;
308
309}
310
311//____________________________________________________________________________
312void AliEMCALClusterizerv1::GetCalibrationParameters()
313{
1bd98442 314 // Set calibration parameters:
315 // if calibration database exists, they are read from database,
316 // otherwise, they are taken from digitizer.
317 //
318 // It is a user responsilibity to open CDB before reconstruction,
319 // for example:
320 // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
321
322 //Check if calibration is stored in data base
323 if(AliCDBManager::Instance()->IsDefaultStorageSet()){
fdfc080e 324 AliCDBEntry *entry = (AliCDBEntry*)
325 AliCDBManager::Instance()->Get("EMCAL/Calib/Data");
1bd98442 326 if (entry) fCalibData = (AliEMCALCalibData*) entry->GetObject();
327 }
328 if(!fCalibData)
329 {
330 //If calibration is not available use default parameters
331 //Loader
332 AliEMCALLoader *emcalLoader =
333 dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
334 if ( !emcalLoader->Digitizer() )
335 emcalLoader->LoadDigitizer();
336 AliEMCALDigitizer * dig = dynamic_cast<AliEMCALDigitizer*>(emcalLoader->Digitizer());
337
338 fADCchannelECA = dig->GetECAchannel() ;
339 fADCpedestalECA = dig->GetECApedestal();
340 }
483b0559 341}
05a92d59 342
483b0559 343//____________________________________________________________________________
344void AliEMCALClusterizerv1::Init()
345{
346 // Make all memory allocations which can not be done in default constructor.
347 // Attach the Clusterizer task to the list of EMCAL tasks
348
5dee926e 349 AliRunLoader *rl = AliRunLoader::GetRunLoader();
a5c60732 350 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
351 fGeom->GetTransformationForSM(); // Global <-> Local
352 AliInfo(Form("geom 0x%x",fGeom));
05a92d59 353
483b0559 354 if(!gMinuit)
355 gMinuit = new TMinuit(100) ;
a5c60732 356
357 fHists = BookHists();
483b0559 358}
359
360//____________________________________________________________________________
839828a6 361void AliEMCALClusterizerv1::InitParameters()
fdebddeb 362{
363 // Initializes the parameters for the Clusterizer
364 fNumberOfECAClusters = 0;
1963b290 365
a5c60732 366 fNTowerInGroup = 36; //Produces maximum of 80 pseudoclusters per event
367
1963b290 368 fECAClusteringThreshold = 0.5; // value obtained from Alexei
e52475ed 369 fECALocMaxCut = 0.03; // ??
1963b290 370
88cb7938 371 fECAW0 = 4.5 ;
839828a6 372 fTimeGate = 1.e-8 ;
839828a6 373 fToUnfold = kFALSE ;
fdebddeb 374 fRecPointsInRun = 0 ;
e52475ed 375 fMinECut = 0.01; // have to be tune
1bd98442 376
377 fCalibData = 0 ;
839828a6 378}
379
380//____________________________________________________________________________
e52475ed 381Int_t AliEMCALClusterizerv1::AreNeighbours(AliEMCALDigit * d1, AliEMCALDigit * d2) const
483b0559 382{
383 // Gives the neighbourness of two digits = 0 are not neighbour but continue searching
384 // = 1 are neighbour
e52475ed 385 // = 2 is in different SM, quit from searching
483b0559 386 // neighbours are defined as digits having at least a common vertex
387 // The order of d1 and d2 is important: first (d1) should be a digit already in a cluster
388 // which is compared to a digit (d2) not yet in a cluster
389
e52475ed 390 static Int_t rv;
391 static Int_t nSupMod1=0, nTower1=0, nIphi1=0, nIeta1=0, iphi1=0, ieta1=0;
392 static Int_t nSupMod2=0, nTower2=0, nIphi2=0, nIeta2=0, iphi2=0, ieta2=0;
393 static Int_t rowdiff, coldiff;
394 rv = 0 ;
483b0559 395
a5c60732 396 fGeom->GetCellIndex(d1->GetId(), nSupMod1,nTower1,nIphi1,nIeta1);
397 fGeom->GetCellIndex(d2->GetId(), nSupMod2,nTower2,nIphi2,nIeta2);
e52475ed 398 if(nSupMod1 != nSupMod2) return 2; // different SM
399
a5c60732 400 fGeom->GetCellPhiEtaIndexInSModule(nSupMod1,nTower1,nIphi1,nIeta1, iphi1,ieta1);
401 fGeom->GetCellPhiEtaIndexInSModule(nSupMod2,nTower2,nIphi2,nIeta2, iphi2,ieta2);
e52475ed 402
403 rowdiff = TMath::Abs(iphi1 - iphi2);
404 coldiff = TMath::Abs(ieta1 - ieta2) ;
70a93198 405
e52475ed 406 if (( coldiff <= 1 ) && ( rowdiff <= 1 )) rv = 1; // neighbours with at least commom vertex
70a93198 407
e52475ed 408 if (gDebug == 2 && rv==1)
409 printf("AreNeighbours: neighbours=%d, id1=%d, relid1=%d,%d \n id2=%d, relid2=%d,%d \n",
410 rv, d1->GetId(), iphi1,ieta1, d2->GetId(), iphi2,ieta2);
ed611565 411
483b0559 412 return rv ;
413}
414
88cb7938 415//____________________________________________________________________________
a5c60732 416Int_t AliEMCALClusterizerv1::AreInGroup(AliEMCALDigit * d1, AliEMCALDigit * d2) const
417{
418 // Tells whether two digits fall within the same supermodule and
419 // tower grouping. The number of towers in a group is controlled by
420 // the parameter nTowersInGroup
421 // = 0 are not in same group but continue searching
422 // = 1 same group
423 // = 2 is in different SM, quit from searching
424 // = 3 different tower group, quit from searching
425 //
426 // The order of d1 and d2 is important: first (d1) should be a digit
427 // already in a cluster which is compared to a digit (d2) not yet in a cluster
428
429 //JLK Question: does the quit from searching assume that the digits
430 //are ordered, so that once you are in a different SM, you'll not
431 //find another in the list that will match? How about my TowerGroup search?
432
433 static Int_t rv;
434 static Int_t nSupMod1=0, nTower1=0, nIphi1=0, nIeta1=0, iphi1=0, ieta1=0;
435 static Int_t nSupMod2=0, nTower2=0, nIphi2=0, nIeta2=0, iphi2=0, ieta2=0;
436 static Int_t towerGroup1 = -1, towerGroup2 = -1;
437 rv = 0 ;
438
439 fGeom->GetCellIndex(d1->GetId(), nSupMod1,nTower1,nIphi1,nIeta1);
440 fGeom->GetCellIndex(d2->GetId(), nSupMod2,nTower2,nIphi2,nIeta2);
441 if(nSupMod1 != nSupMod2) return 2; // different SM
442
443 static Int_t nTowerInSM = fGeom->GetNCellsInSupMod()/fGeom->GetNCellsInTower();
444
445 //figure out which tower grouping each digit belongs to
446 for(int it = 0; it < nTowerInSM/fNTowerInGroup; it++) {
447 if(nTower1 <= nTowerInSM - it*fNTowerInGroup) towerGroup1 = it;
448 if(nTower2 <= nTowerInSM - it*fNTowerInGroup) towerGroup2 = it;
449 }
450 if(towerGroup1 != towerGroup2) return 3; //different Towergroup
451
452 //same SM, same towergroup, we're happy
453 if(towerGroup1 == towerGroup2 && towerGroup2 >= 0)
454 rv = 1;
455
456 if (gDebug == 2 && rv==1)
457 printf("AreInGroup: neighbours=%d, id1=%d, relid1=%d,%d \n id2=%d, relid2=%d,%d \n",
458 rv, d1->GetId(), iphi1,ieta1, d2->GetId(), iphi2,ieta2);
459
460 return rv ;
461}
462
463//____________________________________________________________________________
88cb7938 464void AliEMCALClusterizerv1::Unload()
465{
fdebddeb 466 // Unloads the Digits and RecPoints
5dee926e 467 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
468
469 emcalLoader->UnloadDigits() ;
470 emcalLoader->UnloadRecPoints() ;
88cb7938 471}
483b0559 472
483b0559 473//____________________________________________________________________________
9e5d2067 474void AliEMCALClusterizerv1::WriteRecPoints()
483b0559 475{
476
477 // Creates new branches with given title
478 // fills and writes into TreeR.
479
5dee926e 480 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
ed611565 481
5dee926e 482 TObjArray * aECARecPoints = emcalLoader->RecPoints() ;
ed611565 483
5dee926e 484 TClonesArray * digits = emcalLoader->Digits() ;
485 TTree * treeR = emcalLoader->TreeR();
486 if ( treeR==0 ) {
487 emcalLoader->MakeRecPointsContainer();
488 treeR = emcalLoader->TreeR();
489 }
05a92d59 490 Int_t index ;
ed611565 491
ed611565 492 //Evaluate position, dispersion and other RecPoint properties for EC section
88cb7938 493 for(index = 0; index < aECARecPoints->GetEntries(); index++)
70a93198 494 (dynamic_cast<AliEMCALRecPoint *>(aECARecPoints->At(index)))->EvalAll(fECAW0,digits) ;
ed611565 495
88cb7938 496 aECARecPoints->Sort() ;
483b0559 497
e52475ed 498 for(index = 0; index < aECARecPoints->GetEntries(); index++) {
70a93198 499 (dynamic_cast<AliEMCALRecPoint *>(aECARecPoints->At(index)))->SetIndexInList(index) ;
e52475ed 500 (dynamic_cast<AliEMCALRecPoint *>(aECARecPoints->At(index)))->Print();
501 }
483b0559 502
483b0559 503 Int_t bufferSize = 32000 ;
9859bfc0 504 Int_t splitlevel = 0 ;
ed611565 505
506 //EC section branch
5dee926e 507
508 TBranch * branchECA = 0;
509 if ((branchECA = treeR->GetBranch("EMCALECARP")))
510 branchECA->SetAddress(&aECARecPoints);
511 else
512 treeR->Branch("EMCALECARP","TObjArray",&aECARecPoints,bufferSize,splitlevel);
513 treeR->Fill() ;
483b0559 514
5dee926e 515 emcalLoader->WriteRecPoints("OVERWRITE");
a5c60732 516
483b0559 517}
518
519//____________________________________________________________________________
a5c60732 520void AliEMCALClusterizerv1::MakeClusters(char* option)
483b0559 521{
522 // Steering method to construct the clusters stored in a list of Reconstructed Points
523 // A cluster is defined as a list of neighbour digits
ed611565 524
5dee926e 525 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
ed611565 526
5dee926e 527 TObjArray * aECARecPoints = emcalLoader->RecPoints() ;
528
a5c60732 529 if (fGeom==0)
5dee926e 530 AliFatal("Did not get geometry from EMCALLoader");
ed611565 531
5dee926e 532 aECARecPoints->Clear();
ed611565 533
a5c60732 534 //Start with pseudoclusters, if option
535 if(strstr(option,"pseudo")) {
536 TClonesArray * digits = emcalLoader->Digits() ;
537 TClonesArray * digitsC = dynamic_cast<TClonesArray*>(digits->Clone());
538
539 TIter nextdigit(digitsC) ;
540 AliEMCALDigit * digit;
541
542 AliEMCALRecPoint * recPoint = 0 ;
543 int ineb=0;
544 nextdigit.Reset();
545
546 // PseudoClusterization starts
547 while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigit())) ) { // scan over the list of digitsC
548 recPoint = 0 ;
549 TArrayI clusterECAdigitslist(1000); // what is this
550
551 if(fGeom->CheckAbsCellId(digit->GetId()) ) { //Is this an EMCAL digit? Just maing sure...
552 Int_t iDigitInECACluster = 0; // start a new Tower RecPoint
553 if(fNumberOfECAClusters >= aECARecPoints->GetSize()) aECARecPoints->Expand(2*fNumberOfECAClusters+1) ;
554 AliEMCALRecPoint * rp = new AliEMCALRecPoint("") ;
555 aECARecPoints->AddAt(rp, fNumberOfECAClusters) ;
556 recPoint = dynamic_cast<AliEMCALRecPoint *>(aECARecPoints->At(fNumberOfECAClusters)) ;
557 fNumberOfECAClusters++ ;
558
1d59832c 559 recPoint->SetClusterType(AliESDCaloCluster::kPseudoCluster);
a5c60732 560
561 recPoint->AddDigit(*digit, digit->GetAmp()) ;
562 clusterECAdigitslist[iDigitInECACluster] = digit->GetIndexInList() ;
563 iDigitInECACluster++ ;
564 digitsC->Remove(digit) ;
565 AliDebug(1,Form("MakePseudoClusters: OK id = %d, adc = %f \n", digit->GetId(), digit->GetAmp()));
566 nextdigit.Reset(); // will start from beggining
567
568 AliEMCALDigit * digitN = 0; // digi neighbor
569 Int_t index = 0 ;
570
571 // Find the neighbours
572 while (index < iDigitInECACluster){ // scan over digits already in cluster
573 digit = (AliEMCALDigit*)digits->At(clusterECAdigitslist[index]);
574 index++ ;
575 while ( (digitN = (AliEMCALDigit *)nextdigit())) { // scan over the reduced list of digits
576 ineb = AreInGroup(digit, digitN); // call (digit,digitN) in THAT oder !!!!!
577 switch (ineb ) {
578 case 0 : // not a neighbour
579 break ;
580 case 1 : // are neighbours
581 recPoint->AddDigit(*digitN, digitN->GetAmp() ) ;
582 clusterECAdigitslist[iDigitInECACluster] = digitN->GetIndexInList() ;
583 iDigitInECACluster++ ;
584 digitsC->Remove(digitN) ;
585 break ;
586 case 2 : // different SM
587 break ;
588 case 3 : // different Tower group
589 break ;
590 } // switch
591 } // scan over the reduced list of digits
592 } // scan over digits already in cluster
593 nextdigit.Reset() ; // will start from beggining
594 }
595 }
1d59832c 596 if(recPoint)
597 AliDebug(1,Form("MakeClusters: cl.e %f \n", recPoint->GetEnergy()));
598 //if(recPoint) cout << "cl.e " << recPoint->GetEnergy() << endl;
a5c60732 599 delete digitsC ;
600 }
601
602 //Now do real clusters
e52475ed 603 TClonesArray * digits = emcalLoader->Digits() ;
604 TClonesArray * digitsC = dynamic_cast<TClonesArray*>(digits->Clone());
4aa81449 605
4aa81449 606 TIter nextdigit(digitsC) ;
607 AliEMCALDigit * digit;
a5c60732 608
609 AliEMCALRecPoint * recPoint = 0 ;
610 int ineb=0;
611 nextdigit.Reset();
612
e52475ed 613 double e=0.0, ehs = 0.0;
614 while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigit())) ) { // clean up digits
1bd98442 615 e = Calibrate(digit->GetAmp(), digit->GetId());
e52475ed 616 AliEMCALHistoUtilities::FillH1(fHists, 10, digit->GetAmp());
617 AliEMCALHistoUtilities::FillH1(fHists, 11, e);
618 if(e < fMinECut ) digitsC->Remove(digit);
619 else ehs += e;
1d59832c 620 }
621 AliDebug(1,Form("MakeClusters: Number of digits %d -> (e %f), ehs %d\n",
622 digits->GetEntries(),fMinECut,ehs));
623 //cout << " Number of digits " << digits->GetEntries() << " -> (e>" <<fMinECut <<")";
624 //cout << digitsC->GetEntries()<< " ehs "<<ehs<<endl;
1963b290 625
e52475ed 626 // Clusterization starts
627 // cout << "Outer Loop" << endl;
a5c60732 628 ineb=0;
e52475ed 629 nextdigit.Reset();
a5c60732 630 recPoint = 0 ;
483b0559 631 while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigit())) ) { // scan over the list of digitsC
e52475ed 632 recPoint = 0 ;
633 TArrayI clusterECAdigitslist(1000); // what is this
634
a5c60732 635 if(fGeom->CheckAbsCellId(digit->GetId()) && (Calibrate(digit->GetAmp(), digit->GetId()) > fECAClusteringThreshold ) ){
e52475ed 636 Int_t iDigitInECACluster = 0; // start a new Tower RecPoint
637 if(fNumberOfECAClusters >= aECARecPoints->GetSize()) aECARecPoints->Expand(2*fNumberOfECAClusters+1) ;
4aa81449 638 AliEMCALRecPoint * rp = new AliEMCALRecPoint("") ;
639 aECARecPoints->AddAt(rp, fNumberOfECAClusters) ;
e52475ed 640 recPoint = dynamic_cast<AliEMCALRecPoint *>(aECARecPoints->At(fNumberOfECAClusters)) ;
4aa81449 641 fNumberOfECAClusters++ ;
a5c60732 642
1d59832c 643 recPoint->SetClusterType(AliESDCaloCluster::kClusterv1);
a5c60732 644
1bd98442 645 recPoint->AddDigit(*digit, Calibrate(digit->GetAmp(), digit->GetId())) ;
4aa81449 646 clusterECAdigitslist[iDigitInECACluster] = digit->GetIndexInList() ;
647 iDigitInECACluster++ ;
648 digitsC->Remove(digit) ;
e52475ed 649 AliDebug(1,Form("MakeClusters: OK id = %d, ene = %f , thre = %f \n", digit->GetId(),
1bd98442 650 Calibrate(digit->GetAmp(),digit->GetId()), fECAClusteringThreshold));
e52475ed 651 nextdigit.Reset(); // will start from beggining
483b0559 652
e52475ed 653 AliEMCALDigit * digitN = 0; // digi neighbor
ed611565 654 Int_t index = 0 ;
655
4aa81449 656 // Find the neighbours
88cb7938 657 while (index < iDigitInECACluster){ // scan over digits already in cluster
5dee926e 658 digit = (AliEMCALDigit*)digits->At(clusterECAdigitslist[index]);
483b0559 659 index++ ;
c8be63ae 660 while ( (digitN = (AliEMCALDigit *)nextdigit())) { // scan over the reduced list of digits
e52475ed 661 ineb = AreNeighbours(digit, digitN); // call (digit,digitN) in THAT oder !!!!!
662 switch (ineb ) {
663 case 0 : // not a neighbour
664 break ;
665 case 1 : // are neighbours
1bd98442 666 recPoint->AddDigit(*digitN, Calibrate(digitN->GetAmp(),digitN->GetId()) ) ;
e52475ed 667 clusterECAdigitslist[iDigitInECACluster] = digitN->GetIndexInList() ;
668 iDigitInECACluster++ ;
669 digitsC->Remove(digitN) ;
670 break ;
671 case 2 : // different SM
672 break ;
483b0559 673 } // switch
e52475ed 674 } // scan over the reduced list of digits
675 } // scan over digits already in cluster
676 nextdigit.Reset() ; // will start from beggining
c8be63ae 677 }
1d59832c 678 } // while digit
679 if(recPoint)
680 AliDebug(1,Form("MakeClusters: cl.e %f \n", recPoint->GetEnergy()));
681 //if(recPoint) cout << "cl.e " << recPoint->GetEnergy() << endl;
483b0559 682 delete digitsC ;
a5c60732 683
5dee926e 684 AliDebug(1,Form("total no of clusters %d from %d digits",fNumberOfECAClusters,digits->GetEntriesFast()));
483b0559 685}
686
687//____________________________________________________________________________
fdebddeb 688void AliEMCALClusterizerv1::MakeUnfolding() const
483b0559 689{
690 Fatal("AliEMCALClusterizerv1::MakeUnfolding", "--> Unfolding not implemented") ;
9859bfc0 691
483b0559 692}
693
694//____________________________________________________________________________
695Double_t AliEMCALClusterizerv1::ShowerShape(Double_t r)
696{
697 // Shape of the shower (see EMCAL TDR)
698 // If you change this function, change also the gradient evaluation in ChiSquare()
699
700 Double_t r4 = r*r*r*r ;
701 Double_t r295 = TMath::Power(r, 2.95) ;
702 Double_t shape = TMath::Exp( -r4 * (1. / (2.32 + 0.26 * r4) + 0.0316 / (1 + 0.0652 * r295) ) ) ;
703 return shape ;
704}
705
706//____________________________________________________________________________
70a93198 707void AliEMCALClusterizerv1::UnfoldCluster(AliEMCALRecPoint * /*iniTower*/,
9e5d2067 708 Int_t /*nMax*/,
709 AliEMCALDigit ** /*maxAt*/,
fdebddeb 710 Float_t * /*maxAtEnergy*/) const
483b0559 711{
712 // Performs the unfolding of a cluster with nMax overlapping showers
483b0559 713
9859bfc0 714 Fatal("UnfoldCluster", "--> Unfolding not implemented") ;
173558f2 715
483b0559 716}
717
718//_____________________________________________________________________________
9e5d2067 719void AliEMCALClusterizerv1::UnfoldingChiSquare(Int_t & /*nPar*/, Double_t * /*Grad*/,
720 Double_t & /*fret*/,
721 Double_t * /*x*/, Int_t /*iflag*/)
483b0559 722{
723 // Calculates the Chi square for the cluster unfolding minimization
724 // Number of parameters, Gradient, Chi squared, parameters, what to do
483b0559 725
9859bfc0 726 ::Fatal("UnfoldingChiSquare","Unfolding not implemented") ;
483b0559 727}
483b0559 728//____________________________________________________________________________
9e5d2067 729void AliEMCALClusterizerv1::Print(Option_t * /*option*/)const
483b0559 730{
731 // Print clusterizer parameters
732
9859bfc0 733 TString message("\n") ;
734
483b0559 735 if( strcmp(GetName(), "") !=0 ){
736
737 // Print parameters
738
fdebddeb 739 TString taskName(GetName()) ;
483b0559 740 taskName.ReplaceAll(Version(), "") ;
9859bfc0 741
fdebddeb 742 printf("--------------- ");
743 printf(taskName.Data()) ;
744 printf(" ");
745 printf(GetTitle()) ;
746 printf("-----------\n");
747 printf("Clusterizing digits from the file: ");
748 printf(taskName.Data());
749 printf("\n Branch: ");
750 printf(GetName());
751 printf("\n ECA Local Maximum cut = %f", fECALocMaxCut);
b481a360 752 printf("\n ECA Logarithmic weight = %f", fECAW0);
483b0559 753 if(fToUnfold)
fdebddeb 754 printf("\nUnfolding on\n");
483b0559 755 else
fdebddeb 756 printf("\nUnfolding off\n");
483b0559 757
fdebddeb 758 printf("------------------------------------------------------------------");
483b0559 759 }
760 else
fdebddeb 761 printf("AliEMCALClusterizerv1 not initialized ") ;
483b0559 762}
173558f2 763
483b0559 764//____________________________________________________________________________
765void AliEMCALClusterizerv1::PrintRecPoints(Option_t * option)
766{
b481a360 767 // Prints list of RecPoints produced at the current pass of AliEMCALClusterizer
5dee926e 768 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
769 TObjArray * aECARecPoints = emcalLoader->RecPoints() ;
770
fdebddeb 771 printf("PrintRecPoints: Clusterization result:") ;
ed611565 772
5dee926e 773 printf("event # %d\n", emcalLoader->GetRunLoader()->GetEventNumber() ) ;
fdebddeb 774 printf(" Found %d ECA Rec Points\n ",
775 aECARecPoints->GetEntriesFast()) ;
483b0559 776
88cb7938 777 fRecPointsInRun += aECARecPoints->GetEntriesFast() ;
11f9c5ff 778
483b0559 779 if(strstr(option,"all")) {
fdebddeb 780 Int_t index =0;
ed611565 781 printf("\n-----------------------------------------------------------------------\n") ;
782 printf("Clusters in ECAL section\n") ;
783 printf("Index Ene(GeV) Multi Module phi r theta X Y Z Dispersion Lambda 1 Lambda 2 # of prim Primaries list\n") ;
1963b290 784 Float_t maxE=0;
785 Float_t maxL1=0;
786 Float_t maxL2=0;
787 Float_t maxDis=0;
788
88cb7938 789 for (index = 0 ; index < aECARecPoints->GetEntries() ; index++) {
70a93198 790 AliEMCALRecPoint * rp = dynamic_cast<AliEMCALRecPoint * >(aECARecPoints->At(index)) ;
483b0559 791 TVector3 globalpos;
1963b290 792 //rp->GetGlobalPosition(globalpos);
ed611565 793 TVector3 localpos;
794 rp->GetLocalPosition(localpos);
483b0559 795 Float_t lambda[2];
796 rp->GetElipsAxis(lambda);
797 Int_t * primaries;
798 Int_t nprimaries;
799 primaries = rp->GetPrimaries(nprimaries);
70a93198 800 printf("\n%6d %8.4f %3d %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4f %4f %2d : ",
801 rp->GetIndexInList(), rp->GetEnergy(), rp->GetMultiplicity(),
ed611565 802 globalpos.X(), globalpos.Y(), globalpos.Z(), localpos.X(), localpos.Y(), localpos.Z(),
803 rp->GetDispersion(), lambda[0], lambda[1], nprimaries) ;
1963b290 804 /////////////
805 if(rp->GetEnergy()>maxE){
806 maxE=rp->GetEnergy();
807 maxL1=lambda[0];
808 maxL2=lambda[1];
809 maxDis=rp->GetDispersion();
810 }
a5c60732 811 fPointE->Fill(rp->GetEnergy());
812 fPointL1->Fill(lambda[0]);
813 fPointL2->Fill(lambda[1]);
814 fPointDis->Fill(rp->GetDispersion());
815 fPointMult->Fill(rp->GetMultiplicity());
1963b290 816 /////////////
9859bfc0 817 for (Int_t iprimary=0; iprimary<nprimaries; iprimary++) {
ed611565 818 printf("%d ", primaries[iprimary] ) ;
9859bfc0 819 }
483b0559 820 }
1963b290 821
a5c60732 822 fMaxE->Fill(maxE);
823 fMaxL1->Fill(maxL1);
824 fMaxL2->Fill(maxL2);
825 fMaxDis->Fill(maxDis);
1963b290 826
827
ed611565 828 printf("\n-----------------------------------------------------------------------\n");
483b0559 829 }
830}
e52475ed 831TList* AliEMCALClusterizerv1::BookHists()
1963b290 832{
14ce0a6e 833 //set up histograms for monitoring clusterizer performance
834
e52475ed 835 gROOT->cd();
836
a5c60732 837 fPointE = new TH1F("pointE","point energy", 2000, 0.0, 150.);
838 fPointL1 = new TH1F("pointL1","point L1", 1000, 0.0, 3.);
839 fPointL2 = new TH1F("pointL2","point L2", 1000, 0.0, 3.);
840 fPointDis = new TH1F("pointDis","point Dis", 1000, 0.0, 3.);
841 fPointMult = new TH1F("pointMult","point Mult", 100, 0.0, 100.);
842 fDigitAmp = new TH1F("digitAmp","Digit Amplitude", 2000, 0.0, 5000.);
843 fMaxE = new TH1F("maxE","Max point energy", 2000, 0.0, 150.);
844 fMaxL1 = new TH1F("maxL1","Max point L1", 1000, 0.0, 3.);
845 fMaxL2 = new TH1F("maxL2","Max point L2", 1000, 0.0, 3.);
846 fMaxDis = new TH1F("maxDis","Max point Dis", 1000, 0.0, 3.); // 9
e52475ed 847 //
848 new TH1F("adcOfDigits","adc of digits(threshold control)", 1001, -0.5, 1000.5); // 10
849 new TH1F("energyOfDigits","energy of digits(threshold control)", 1000, 0.0, 1.); // 11
850
851 return AliEMCALHistoUtilities::MoveHistsToList("EmcalClusterizerv1ControlHists", kFALSE);
1963b290 852}
e52475ed 853
854void AliEMCALClusterizerv1::SaveHists(const char *fn)
1963b290 855{
e52475ed 856 AliEMCALHistoUtilities::SaveListOfHists(fHists, fn, kTRUE);
1963b290 857}
858
e52475ed 859void AliEMCALClusterizerv1::Browse(TBrowser* b)
1963b290 860{
e52475ed 861 if(fHists) b->Add(fHists);
862 TTask::Browse(b);
1963b290 863}