Correction of previous commit.
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALReconstructor.cxx
CommitLineData
f6019cda 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18//_________________________________________________________________________
fa42b1f3 19//--
20//-- Yves Schutz (SUBATECH)
f6019cda 21// Reconstruction class. Redesigned from the old AliReconstructionner class and
22// derived from STEER/AliReconstructor.
23//
29c9d15f 24
85c25c2e 25
f6019cda 26// --- ROOT system ---
85c25c2e 27#include <TClonesArray.h>
0c5b726e 28#include "TGeoManager.h"
29#include "TGeoMatrix.h"
f6019cda 30
31// --- Standard library ---
32
33// --- AliRoot header files ---
f6019cda 34#include "AliEMCALReconstructor.h"
5dee926e 35
aaa3cb7c 36#include "AliCodeTimer.h"
23ca956b 37#include "AliCaloCalibPedestal.h"
38#include "AliEMCALCalibData.h"
af885e0f 39#include "AliESDEvent.h"
89ffc0b0 40#include "AliESDCaloCluster.h"
0e7c6655 41#include "AliESDCaloCells.h"
6a0cf740 42#include "AliESDtrack.h"
5dee926e 43#include "AliEMCALLoader.h"
98e9578e 44#include "AliEMCALRawUtils.h"
0e7c6655 45#include "AliEMCALDigit.h"
f6019cda 46#include "AliEMCALClusterizerv1.h"
ee08edde 47#include "AliEMCALClusterizerNxN.h"
5dee926e 48#include "AliEMCALRecPoint.h"
dc293ae9 49#include "AliEMCALPID.h"
0964c2e9 50#include "AliEMCALTrigger.h"
1d59832c 51#include "AliRawReader.h"
fa42b1f3 52#include "AliCDBEntry.h"
53#include "AliCDBManager.h"
65bdc82f 54#include "AliEMCALGeometry.h"
ac8ae9fe 55#include "AliEMCAL.h"
85c25c2e 56#include "AliESDVZERO.h"
0c5b726e 57#include "AliCDBManager.h"
72c58de0 58#include "AliRunLoader.h"
59#include "AliRun.h"
916f1e76 60#include "AliEMCALTriggerData.h"
61#include "AliEMCALTriggerElectronics.h"
fff39dd1 62#include "AliEMCALTriggerDCSConfigDB.h"
63#include "AliEMCALTriggerDCSConfig.h"
de39a0ff 64#include "AliEMCALTriggerData.h"
65#include "AliEMCALTriggerRawDigit.h"
66#include "AliEMCALTriggerPatch.h"
67#include "AliEMCALTriggerTypes.h"
1d59832c 68
85c25c2e 69ClassImp(AliEMCALReconstructor)
95ffa314 70
5970dfe2 71const AliEMCALRecParam* AliEMCALReconstructor::fgkRecParam = 0; // EMCAL rec. parameters
b57cc9b9 72AliEMCALRawUtils* AliEMCALReconstructor::fgRawUtils = 0; // EMCAL raw utilities class
73AliEMCALClusterizer* AliEMCALReconstructor::fgClusterizer = 0; // EMCAL clusterizer class
74TClonesArray* AliEMCALReconstructor::fgDigitsArr = 0; // list of digits, to be used multiple times
75TObjArray* AliEMCALReconstructor::fgClustersArr = 0; // list of clusters, to be used multiple times
4bd37bd4 76TClonesArray* AliEMCALReconstructor::fgTriggerDigits = 0; // list of trigger digits, to be used multiple times
916f1e76 77AliEMCALTriggerElectronics* AliEMCALReconstructor::fgTriggerProcessor = 0x0;
f6019cda 78//____________________________________________________________________________
18a21c7c 79AliEMCALReconstructor::AliEMCALReconstructor()
5970dfe2 80 : fGeom(0),fCalibData(0),fPedestalData(0),fTriggerData(0x0), fMatches(0x0)
f6019cda 81{
82 // ctor
95ffa314 83
65bdc82f 84 fgRawUtils = new AliEMCALRawUtils;
29c9d15f 85
72c58de0 86 //To make sure we match with the geometry in a simulation file,
87 //let's try to get it first. If not, take the default geometry
33c3c91a 88 AliRunLoader *rl = AliRunLoader::Instance();
7e1d9a9b 89 if (rl->GetAliRun()){
90 AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
91 if(emcal) fGeom = emcal->GetGeometry();
92 }
93
94 if(!fGeom) {
72c58de0 95 AliInfo(Form("Using default geometry in reconstruction"));
937d0661 96 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
65bdc82f 97 }
29c9d15f 98
0c5b726e 99 //Get calibration parameters
100 if(!fCalibData)
101 {
29c9d15f 102 AliCDBEntry *entry = (AliCDBEntry*)
103 AliCDBManager::Instance()->Get("EMCAL/Calib/Data");
104 if (entry) fCalibData = (AliEMCALCalibData*) entry->GetObject();
0c5b726e 105 }
29c9d15f 106
0c5b726e 107 if(!fCalibData)
29c9d15f 108 AliFatal("Calibration parameters not found in CDB!");
109
40164976 110 //Get calibration parameters
111 if(!fPedestalData)
112 {
de39a0ff 113 AliCDBEntry *entry = (AliCDBEntry*)
29c9d15f 114 AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals");
de39a0ff 115 if (entry) fPedestalData = (AliCaloCalibPedestal*) entry->GetObject();
40164976 116 }
de39a0ff 117
ee08edde 118 if(!fPedestalData)
119 AliFatal("Dead map not found in CDB!");
29c9d15f 120
72c58de0 121 if(!fGeom) AliFatal(Form("Could not get geometry!"));
29c9d15f 122
f62c044a 123 AliEMCALTriggerDCSConfigDB* dcsConfigDB = AliEMCALTriggerDCSConfigDB::Instance();
29c9d15f 124
f62c044a 125 const AliEMCALTriggerDCSConfig* dcsConfig = dcsConfigDB->GetTriggerDCSConfig();
29c9d15f 126
f62c044a 127 if (!dcsConfig) AliFatal("No Trigger DCS Configuration from OCDB!");
128 fgTriggerProcessor = new AliEMCALTriggerElectronics( dcsConfig );
29c9d15f 129
f62c044a 130 fTriggerData = new AliEMCALTriggerData();
29c9d15f 131
f62c044a 132 //Init temporary list of digits
4bd37bd4 133 fgDigitsArr = new TClonesArray("AliEMCALDigit",1000);
134 fgClustersArr = new TObjArray(1000);
135 fgTriggerDigits = new TClonesArray("AliEMCALTriggerRawDigit",1000);
5970dfe2 136
137 //Track matching
138 fMatches = new TList();
139 fMatches->SetOwner(kTRUE);
f6019cda 140}
141
0a4cb131 142//____________________________________________________________________________
f6019cda 143AliEMCALReconstructor::~AliEMCALReconstructor()
144{
145 // dtor
29c9d15f 146
23ca956b 147 if(fGeom) delete fGeom;
80453a9f 148
149 //No need to delete, recovered from OCDB
150 //if(fCalibData) delete fCalibData;
151 //if(fPedestalData) delete fPedestalData;
de39a0ff 152
b57cc9b9 153 if(fgDigitsArr){
154 fgDigitsArr->Clear("C");
155 delete fgDigitsArr;
156 }
157
158 if(fgClustersArr){
159 fgClustersArr->Clear();
160 delete fgClustersArr;
161 }
162
4bd37bd4 163 if(fgTriggerDigits){
7dbb454b 164 fgTriggerDigits->Clear();
4bd37bd4 165 delete fgTriggerDigits;
166 }
167
23ca956b 168 if(fgRawUtils) delete fgRawUtils;
169 if(fgClusterizer) delete fgClusterizer;
f62c044a 170 if(fgTriggerProcessor) delete fgTriggerProcessor;
b57cc9b9 171
5970dfe2 172 if(fMatches) { fMatches->Delete(); delete fMatches; fMatches = 0;}
173
aaa3cb7c 174 AliCodeTimer::Instance()->Print();
f6019cda 175}
176
9519e3e3 177//____________________________________________________________________________
178void AliEMCALReconstructor::InitClusterizer() const
ee08edde 179{
9519e3e3 180 //Init the clusterizer with geometry and calibration pointers, avoid doing it twice.
181 Int_t clusterizerType = -1;
182 Int_t eventType = -1;
183 if(GetRecParam()) {
184 clusterizerType = GetRecParam()->GetClusterizerFlag();
185 eventType = GetRecParam()->GetEventSpecie();
7e1d9a9b 186 }
187 else{
9519e3e3 188 AliCDBEntry *entry = (AliCDBEntry*)
29c9d15f 189 AliCDBManager::Instance()->Get("EMCAL/Calib/RecoParam");
9519e3e3 190 //Get The reco param for the default event specie
191 if (entry) {
192 AliEMCALRecParam *recParam = (AliEMCALRecParam*)((TObjArray *) entry->GetObject())->At(0);
193 if(recParam) clusterizerType = recParam->GetClusterizerFlag();
de39a0ff 194 }
7e1d9a9b 195 }
ee08edde 196
9519e3e3 197 //Check if clusterizer previously set corresponds to what is needed for this event type
198 if(fgClusterizer){
199 if(eventType!=AliRecoParam::kCalib){
200 //printf("ReCreate clusterizer? Clusterizer set <%d>, Clusterizer in use <%s>\n",
29c9d15f 201 // clusterizerType, fgClusterizer->Version());
9519e3e3 202
203 if (clusterizerType == AliEMCALRecParam::kClusterizerv1 && !strcmp(fgClusterizer->Version(),"clu-v1")) return;
204
205 else if(clusterizerType == AliEMCALRecParam::kClusterizerNxN && !strcmp(fgClusterizer->Version(),"clu-NxN")) return;
206
207 //Need to create new clusterizer, the one set previously is not the correct one
208 delete fgClusterizer;
209 }
210 else return;
211 }
29c9d15f 212
9519e3e3 213 if (clusterizerType == AliEMCALRecParam::kClusterizerv1)
29c9d15f 214 {
215 fgClusterizer = new AliEMCALClusterizerv1(fGeom, fCalibData,fPedestalData);
216 }
9519e3e3 217 else
29c9d15f 218 {
219 fgClusterizer = new AliEMCALClusterizerNxN(fGeom, fCalibData,fPedestalData);
220 }
ee08edde 221}
222
223//____________________________________________________________________________
c47157cd 224void AliEMCALReconstructor::Reconstruct(TTree* digitsTree, TTree* clustersTree) const
f6019cda 225{
226 // method called by AliReconstruction;
227 // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
228 // segment maker needs access to the AliESD object to retrieve the tracks reconstructed by
229 // the global tracking.
c47157cd 230 // Works on the current event.
29c9d15f 231
1a429a6b 232 AliCodeTimerAuto("",0)
29c9d15f 233
234 //Get input digits and put them in fgDigitsArr, clear the list before
85c25c2e 235 ReadDigitsArrayFromTree(digitsTree);
29c9d15f 236
9519e3e3 237 InitClusterizer();
238
0832a2bf 239 fgClusterizer->InitParameters();
4601e3a7 240 fgClusterizer->SetOutput(clustersTree);
9519e3e3 241
e853f058 242 //Skip clusterization of LED events
243 if (GetRecParam()->GetEventSpecie()!=AliRecoParam::kCalib){
9517d886 244
29c9d15f 245 if(fgDigitsArr && fgDigitsArr->GetEntries()) {
246
247 fgClusterizer->SetInput(digitsTree);
248
249 //fgClusterizer->Digits2Clusters("deb all") ; //For debugging
250 fgClusterizer->Digits2Clusters("");
251
252 fgClusterizer->Clear();
253
254 }//digits array exists and has somethind
e853f058 255 }//not a LED event
29c9d15f 256
916f1e76 257 clustersTree->Fill();
f6019cda 258}
259
260//____________________________________________________________________________
c47157cd 261void AliEMCALReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
29c9d15f 262
a68156e6 263{
c47157cd 264 // Conversion from raw data to
265 // EMCAL digits.
266 // Works on a single-event basis
95ffa314 267
98e9578e 268 rawReader->Reset() ;
29c9d15f 269
de39a0ff 270 fTriggerData->SetMode(1);
29c9d15f 271
b57cc9b9 272 if(fgDigitsArr) fgDigitsArr->Clear("C");
29c9d15f 273
de39a0ff 274 TClonesArray *digitsTrg = new TClonesArray("AliEMCALTriggerRawDigit", 32 * 96);
29c9d15f 275
c47157cd 276 Int_t bufsize = 32000;
b57cc9b9 277 digitsTree->Branch("EMCAL", &fgDigitsArr, bufsize);
916f1e76 278 digitsTree->Branch("EMTRG", &digitsTrg, bufsize);
29c9d15f 279
e853f058 280 //Skip calibration events do the rest
281 Bool_t doFit = kTRUE;
95ffa314 282 if ( !(GetRecParam()->FitLEDEvents()) && GetRecParam()->GetEventSpecie()==AliRecoParam::kCalib) doFit = kFALSE;
e853f058 283 if (doFit){
29c9d15f 284 //must be done here because, in constructor, option is not yet known
285 fgRawUtils->SetOption(GetOption());
286
92d9f317 287 // fgRawUtils->SetRawFormatHighLowGainFactor(GetRecParam()->GetHighLowGainFactor());
95ffa314 288
92d9f317 289 // fgRawUtils->SetRawFormatOrder(GetRecParam()->GetOrderParameter());
290 // fgRawUtils->SetRawFormatTau(GetRecParam()->GetTau());
29c9d15f 291 fgRawUtils->SetNoiseThreshold(GetRecParam()->GetNoiseThreshold());
292 fgRawUtils->SetNPedSamples(GetRecParam()->GetNPedSamples());
293 fgRawUtils->SetRemoveBadChannels(GetRecParam()->GetRemoveBadChannels());
294 fgRawUtils->SetFittingAlgorithm(GetRecParam()->GetFittingAlgorithm());
295 fgRawUtils->SetFALTROUsage(GetRecParam()->UseFALTRO());
92d9f317 296
297 //fgRawUtils->SetTimeMin(GetRecParam()->GetTimeMin());
298 //fgRawUtils->SetTimeMax(GetRecParam()->GetTimeMax());
299
300 // fgRawUtils->SetTimeMin(-99999 );
301 // fgRawUtils->SetTimeMax( 99999 );
29c9d15f 302
303 fgRawUtils->Raw2Digits(rawReader,fgDigitsArr,fPedestalData,digitsTrg,fTriggerData);
92d9f317 304
793176c1 305 }//skip calibration event
e853f058 306 else{
29c9d15f 307 AliDebug(1," Calibration Event, skip!");
e853f058 308 }
29c9d15f 309
c615db53 310 digitsTree->Fill();
916f1e76 311 digitsTrg->Delete();
916f1e76 312 delete digitsTrg;
29c9d15f 313
a68156e6 314}
315
85c25c2e 316
a68156e6 317//____________________________________________________________________________
0e7c6655 318void AliEMCALReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree,
c47157cd 319 AliESDEvent* esd) const
f6019cda 320{
98e9578e 321 // Called by AliReconstruct after Reconstruct() and global tracking and vertexing
85c25c2e 322 // and V0
c47157cd 323 // Works on the current event
de39a0ff 324 // printf(" ## AliEMCALReconstructor::FillESD() is started ### \n ");
85c25c2e 325 //return;
29c9d15f 326
7ba18c0e 327 //########################################
328 // Trigger
329 //########################################
de39a0ff 330
da6062af 331 static int saveOnce = 0;
95ffa314 332
29c9d15f 333 Int_t v0M[2] = {0, 0};
334
335 AliESDVZERO* esdV0 = esd->GetVZEROData();
336
337 if (esdV0)
338 {
339 for (Int_t i = 0; i < 32; i++)
340 {
341 v0M[0] += (Int_t)esdV0->GetAdcV0C(i);
342 v0M[1] += (Int_t)esdV0->GetAdcV0A(i);
343 }
344 }
345 else
346 {
347 AliWarning("Cannot retrieve V0 ESD! Run w/ null V0 charges");
348 }
349
350 if (fgTriggerDigits) fgTriggerDigits->Clear();
351
352 TBranch *branchtrg = digitsTree->GetBranch("EMTRG");
353
354 if (!branchtrg)
355 {
356 AliError("Can't get the branch with the EMCAL trigger digits!");
357 return;
358 }
359
360 branchtrg->SetAddress(&fgTriggerDigits);
361 branchtrg->GetEntry(0);
362
363 // Note: fgTriggerProcessor reset done at the end of this method
364 fgTriggerProcessor->Digits2Trigger(fgTriggerDigits, v0M, fTriggerData);
365
366 // Fill ESD
367 AliESDCaloTrigger* trgESD = esd->GetCaloTrigger("EMCAL");
368
369 if (trgESD)
370 {
371 trgESD->Allocate(fgTriggerDigits->GetEntriesFast());
372
373 for (Int_t i = 0; i < fgTriggerDigits->GetEntriesFast(); i++)
374 {
375 AliEMCALTriggerRawDigit* rdig = (AliEMCALTriggerRawDigit*)fgTriggerDigits->At(i);
de39a0ff 376
29c9d15f 377 Int_t px, py;
378 if (fGeom->GetPositionInEMCALFromAbsFastORIndex(rdig->GetId(), px, py))
379 {
380 Int_t a = -1, t = -1, times[10];
381
382 rdig->GetMaximum(a, t);
383 rdig->GetL0Times(times);
384
385 trgESD->Add(px, py, a, t, times, rdig->GetNL0Times(), rdig->GetL1TimeSum(), rdig->GetTriggerBits());
386 }
387 }
388
389 trgESD->SetL1Threshold(0, fTriggerData->GetL1GammaThreshold());
390
391 trgESD->SetL1Threshold(1, fTriggerData->GetL1JetThreshold() );
95ffa314 392
393 Int_t v0[2];
394 fTriggerData->GetL1V0(v0);
395
396 trgESD->SetL1V0(v0);
397 trgESD->SetL1FrameMask(fTriggerData->GetL1FrameMask());
398
399 if (!saveOnce && fTriggerData->GetL1DataDecoded())
400 {
401 int type[8] = {0};
402 fTriggerData->GetL1TriggerType(type);
403
404 esd->SetCaloTriggerType(type);
405
406 saveOnce = 1;
407 }
29c9d15f 408 }
409
410 // Resetting
411 fTriggerData->Reset();
de39a0ff 412
0e7c6655 413 //########################################
414 //##############Fill CaloCells###############
415 //########################################
29c9d15f 416
417 //Get input digits and put them in fgDigitsArr, clear the list before
b57cc9b9 418 ReadDigitsArrayFromTree(digitsTree);
29c9d15f 419
b57cc9b9 420 Int_t nDigits = fgDigitsArr->GetEntries(), idignew = 0 ;
0e7c6655 421 AliDebug(1,Form("%d digits",nDigits));
29c9d15f 422
0e7c6655 423 AliESDCaloCells &emcCells = *(esd->GetEMCALCells());
424 emcCells.CreateContainer(nDigits);
c8fe2783 425 emcCells.SetType(AliVCaloCells::kEMCALCell);
0c5b726e 426 Float_t energy = 0;
0e7c6655 427 for (Int_t idig = 0 ; idig < nDigits ; idig++) {
b57cc9b9 428 const AliEMCALDigit * dig = (const AliEMCALDigit*)fgDigitsArr->At(idig);
829ba234 429 if(dig->GetAmplitude() > 0 ){
29c9d15f 430 energy = fgClusterizer->Calibrate(dig->GetAmplitude(),dig->GetTime(),dig->GetId()); //TimeR or Time?
431 if(energy > 0){ //Digits tagged as bad (dead, hot, not alive) are set to 0 in calibrate, remove them
95ffa314 432 emcCells.SetCell(idignew,dig->GetId(),energy, dig->GetTime());
433 idignew++;
29c9d15f 434 }
0e7c6655 435 }
436 }
437 emcCells.SetNumberOfCells(idignew);
438 emcCells.Sort();
29c9d15f 439
0e7c6655 440 //------------------------------------------------------------
441 //-----------------CLUSTERS-----------------------------
442 //------------------------------------------------------------
9dce5a21 443 clustersTree->SetBranchStatus("*",0); //disable all branches
444 clustersTree->SetBranchStatus("EMCALECARP",1); //Enable only the branch we need
b57cc9b9 445 if(fgClustersArr) fgClustersArr->Clear();
0e7c6655 446 TBranch *branch = clustersTree->GetBranch("EMCALECARP");
b57cc9b9 447 branch->SetAddress(&fgClustersArr);
9dce5a21 448 branch->GetEntry(0);
449 //clustersTree->GetEvent(0);
29c9d15f 450
b57cc9b9 451 Int_t nClusters = fgClustersArr->GetEntries(), nClustersNew=0;
0e7c6655 452 AliDebug(1,Form("%d clusters",nClusters));
5970dfe2 453
85c25c2e 454
6a0cf740 455 //########################################
85c25c2e 456 //##############Fill CaloClusters#############
6a0cf740 457 //########################################
5dee926e 458 for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
b57cc9b9 459 const AliEMCALRecPoint * clust = (const AliEMCALRecPoint*)fgClustersArr->At(iClust);
5970dfe2 460 if(!clust) continue;
c8fe2783 461 //if(clust->GetClusterType()== AliVCluster::kEMCALClusterv1) nRP++; else nPC++;
29c9d15f 462 // clust->Print(); //For debugging
a7a5421e 463 // Get information from EMCAL reconstruction points
85c60a8e 464 Float_t xyz[3];
5dee926e 465 TVector3 gpos;
466 clust->GetGlobalPosition(gpos);
35397e76 467 for (Int_t ixyz=0; ixyz<3; ixyz++)
5dee926e 468 xyz[ixyz] = gpos[ixyz];
85c25c2e 469 Float_t elipAxis[2];
470 clust->GetElipsAxis(elipAxis);
29c9d15f 471 //Create digits lists
35397e76 472 Int_t cellMult = clust->GetMultiplicity();
473 //TArrayS digiList(digitMult);
474 Float_t *amplFloat = clust->GetEnergiesList();
475 Int_t *digitInts = clust->GetAbsId();
476 TArrayS absIdList(cellMult);
eb972628 477 TArrayD fracList(cellMult);
29c9d15f 478
35397e76 479 Int_t newCellMult = 0;
480 for (Int_t iCell=0; iCell<cellMult; iCell++) {
481 if (amplFloat[iCell] > 0) {
95ffa314 482 absIdList[newCellMult] = (UShort_t)(digitInts[iCell]);
483 //Calculate Fraction
484 if(emcCells.GetCellAmplitude(digitInts[iCell])>0 && GetRecParam()->GetUnfold()){
485 fracList[newCellMult] = amplFloat[iCell]/(emcCells.GetCellAmplitude(digitInts[iCell]));//get cell calibration value
486
487 }
488 else{
489 fracList[newCellMult] = 0;
490 }
491 newCellMult++;
92da3372 492 }
92da3372 493 }
29c9d15f 494
eb972628 495 absIdList.Set(newCellMult);
496 fracList.Set(newCellMult);
497
35397e76 498 if(newCellMult > 0) { // accept cluster if it has some digit
499 nClustersNew++;
65721814 500 //Primaries
7592dfc4 501 Int_t parentMult = 0;
fa42b1f3 502 Int_t *parentList = clust->GetParents(parentMult);
a7a5421e 503 // fills the ESDCaloCluster
35397e76 504 AliESDCaloCluster * ec = new AliESDCaloCluster() ;
c8fe2783 505 ec->SetType(AliVCluster::kEMCALClusterv1);
7592dfc4 506 ec->SetPosition(xyz);
507 ec->SetE(clust->GetEnergy());
29c9d15f 508
ee08edde 509 //Distance to the nearest bad crystal
510 ec->SetDistanceToBadChannel(clust->GetDistanceToBadTower());
29c9d15f 511
35397e76 512 ec->SetNCells(newCellMult);
513 //Change type of list from short to ushort
514 UShort_t *newAbsIdList = new UShort_t[newCellMult];
ee08edde 515 Double_t *newFracList = new Double_t[newCellMult];
35397e76 516 for(Int_t i = 0; i < newCellMult ; i++) {
517 newAbsIdList[i]=absIdList[i];
de39a0ff 518 newFracList[i] =fracList[i];
35397e76 519 }
520 ec->SetCellsAbsId(newAbsIdList);
eb972628 521 ec->SetCellsAmplitudeFraction(newFracList);
c8fe2783 522 ec->SetDispersion(clust->GetDispersion());
523 ec->SetChi2(-1); //not yet implemented
35397e76 524 ec->SetM02(elipAxis[0]*elipAxis[0]) ;
525 ec->SetM20(elipAxis[1]*elipAxis[1]) ;
78902954 526 ec->SetTOF(clust->GetTime()) ; //time-of-fligh
225cd96d 527 ec->SetNExMax(clust->GetNExMax()); //number of local maxima
5970dfe2 528
29c9d15f 529
35397e76 530 TArrayI arrayParents(parentMult,parentList);
531 ec->AddLabels(arrayParents);
5970dfe2 532 //
533 //Track matching
534 //
535 fMatches->Clear();
536 Int_t nTracks = esd->GetNumberOfTracks();
537 for (Int_t itrack = 0; itrack < nTracks; itrack++)
538 {
539 AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
540 if(track->GetEMCALcluster()==iClust)
541 {
542 Double_t dEta=-999, dPhi=-999;
543 Bool_t isMatch = CalculateResidual(track, ec, dEta, dPhi);
544 if(!isMatch)
545 {
a8fbdc61 546 // AliDebug(10, "Not good");
5970dfe2 547 continue;
5970dfe2 548 }
549 AliEMCALMatch *match = new AliEMCALMatch();
550 match->SetIndexT(itrack);
551 match->SetDistance(TMath::Sqrt(dEta*dEta+dPhi*dPhi));
552 match->SetdEta(dEta);
553 match->SetdPhi(dPhi);
554 fMatches->Add(match);
555 }
556 }
557 fMatches->Sort(kSortAscending); //Sort matched tracks from closest to furthest
558 Int_t nMatch = fMatches->GetEntries();
559 TArrayI arrayTrackMatched(nMatch);
560 for(Int_t imatch=0; imatch<nMatch; imatch++)
561 {
562 AliEMCALMatch *match = (AliEMCALMatch*)fMatches->At(imatch);
563 arrayTrackMatched[imatch] = match->GetIndexT();
564 if(imatch==0)
565 {
566 ec->SetTrackDistance(match->GetdPhi(), match->GetdEta());
567 }
568 }
569 ec->AddTracksMatched(arrayTrackMatched);
570
571 //add the cluster to the esd object
eb972628 572 esd->AddCaloCluster(ec);
5970dfe2 573
a7a5421e 574 delete ec;
85d4cbde 575 delete [] newAbsIdList ;
eb972628 576 delete [] newFracList ;
29c9d15f 577 }
578 } // cycle on clusters
5970dfe2 579
580 //
581 //Reset the index of matched cluster for tracks
582 //to the one in CaloCluster array
583 Int_t ncls = esd->GetNumberOfCaloClusters();
584 for(Int_t icl=0; icl<ncls; icl++)
585 {
586 AliESDCaloCluster *cluster = esd->GetCaloCluster(icl);
587 if(!cluster || !cluster->IsEMCAL()) continue;
588 TArrayI *trackIndex = cluster->GetTracksMatched();
589 for(Int_t itr=0; itr<trackIndex->GetSize(); itr++)
590 {
591 AliESDtrack *track = esd->GetTrack(trackIndex->At(itr));
592 track->SetEMCALcluster(cluster->GetID());
593 }
594 }
29c9d15f 595
29c9d15f 596
597 //Fill ESDCaloCluster with PID weights
598 AliEMCALPID *pid = new AliEMCALPID;
599 //pid->SetPrintInfo(kTRUE);
600 pid->SetReconstructor(kTRUE);
601 pid->RunPID(esd);
602 delete pid;
603
604 //Store EMCAL misalignment matrixes
605 FillMisalMatrixes(esd) ;
606
0c5b726e 607}
608
609//==================================================================================
610void AliEMCALReconstructor::FillMisalMatrixes(AliESDEvent* esd)const{
29c9d15f 611 //Store EMCAL matrixes in ESD Header
612
613 //Check, if matrixes was already stored
614 for(Int_t sm = 0 ; sm < fGeom->GetNumberOfSuperModules(); sm++){
615 if(esd->GetEMCALMatrix(sm)!=0)
616 return ;
617 }
618
619 //Create and store matrixes
620 if(!gGeoManager){
621 AliError("Can not store misal. matrixes: no gGeoManager! \n") ;
622 return ;
623 }
624 //Note, that owner of copied marixes will be header
7e1d9a9b 625 const Int_t bufsize = 255;
29c9d15f 626 char path[bufsize] ;
627 TGeoHMatrix * m = 0x0;
628 for(Int_t sm = 0; sm < fGeom->GetNumberOfSuperModules(); sm++){
629 snprintf(path,bufsize,"/ALIC_1/XEN1_1/SMOD_%d",sm+1) ; //In Geometry modules numbered 1,2,.,5
630 if(sm >= 10) snprintf(path,bufsize,"/ALIC_1/XEN1_1/SM10_%d",sm-10+1) ;
631
632 if (gGeoManager->CheckPath(path)){
633 gGeoManager->cd(path);
634 m = gGeoManager->GetCurrentMatrix() ;
635 // printf("================================================= \n");
636 // printf("AliEMCALReconstructor::FixMisalMatrixes(), sm %d, \n",sm);
637 // m->Print("");
638 esd->SetEMCALMatrix(new TGeoHMatrix(*m),sm) ;
639 // printf("================================================= \n");
640 }
641 else{
642 esd->SetEMCALMatrix(NULL,sm) ;
643 }
644 }
f6019cda 645}
dc293ae9 646
9517d886 647//__________________________________________________________________________
85c25c2e 648void AliEMCALReconstructor::ReadDigitsArrayFromTree(TTree *digitsTree) const
649{
29c9d15f 650 // Read the digits from the input tree
651 // See AliEMCALClusterizer::SetInput(TTree *digitsTree);
652
653 // Clear previous digits in the list
654 if(fgDigitsArr){
b57cc9b9 655 fgDigitsArr->Clear("C");
85c25c2e 656 }
29c9d15f 657 else{
658 // It should not happen, but just in case ...
659 fgDigitsArr = new TClonesArray("AliEMCALDigit",100);
660 }
661
85c25c2e 662 // Read the digits from the input tree
663 TBranch *branch = digitsTree->GetBranch("EMCAL");
664 if (!branch) {
665 AliError("can't get the branch with the EMCAL digits !");
666 return;
29c9d15f 667 }
668
85c25c2e 669 branch->SetAddress(&fgDigitsArr);
670 branch->GetEntry(0);
671}
98e9578e 672
5970dfe2 673//==================================================================================
674Bool_t AliEMCALReconstructor::CalculateResidual(AliESDtrack *track, AliESDCaloCluster *cluster, Double_t &dEta, Double_t &dPhi)const
675{
676 //
677 // calculate the residual between track and cluster
678 //
679
680 // If the esdFriend is available, use the TPCOuter point as the starting point of extrapolation
681 // Otherwise use the TPCInner point
456126ad 682 AliExternalTrackParam *trkParam = 0;
5970dfe2 683 const AliESDfriendTrack* friendTrack = track->GetFriendTrack();
684 if(friendTrack && friendTrack->GetTPCOut())
685 trkParam = const_cast<AliExternalTrackParam*>(friendTrack->GetTPCOut());
686 else
687 trkParam = const_cast<AliExternalTrackParam*>(track->GetInnerParam());
688 if(!trkParam) return kFALSE;
689
690 //Perform extrapolation
691 Double_t trkPos[3];
692 Float_t clsPos[3];
693
456126ad 694 AliExternalTrackParam trkParamTmp (*trkParam);
5970dfe2 695 cluster->GetPosition(clsPos);
696 TVector3 vec(clsPos[0],clsPos[1],clsPos[2]);
697 Double_t alpha = ((int)(vec.Phi()*TMath::RadToDeg()/20)+0.5)*20*TMath::DegToRad();
698 //Rotate to proper local coordinate
699 vec.RotateZ(-alpha);
456126ad 700 trkParamTmp.Rotate(alpha);
5970dfe2 701 //extrapolation is done here
456126ad 702 if(!AliTrackerBase::PropagateTrackToBxByBz(&trkParamTmp, vec.X(), track->GetMass(), GetRecParam()->GetExtrapolateStep(), kFALSE, 0.8, -1))
5970dfe2 703 return kFALSE;
704
705 //Calculate the residuals
456126ad 706 trkParamTmp.GetXYZ(trkPos);
5970dfe2 707
708 TVector3 clsPosVec(clsPos[0],clsPos[1],clsPos[2]);
709 TVector3 trkPosVec(trkPos[0],trkPos[1],trkPos[2]);
710
711 Double_t clsPhi = clsPosVec.Phi();
712 if(clsPhi<0) clsPhi+=2*TMath::Pi();
713 Double_t trkPhi = trkPosVec.Phi();
714 if(trkPhi<0) trkPhi+=2*TMath::Pi();
715
716 dPhi = clsPhi-trkPhi;
717 dEta = clsPosVec.Eta()-trkPosVec.Eta();
718
719 return kTRUE;
720}
721
722//
723//==================================================================================
724//
725AliEMCALReconstructor::AliEMCALMatch::AliEMCALMatch()
726 : TObject(),
727 fIndexT(-1),
728 fDistance(-999.),
729 fdEta(-999.),
730 fdPhi(-999.)
731{
732 //default constructor
916f1e76 733
5970dfe2 734}
735
736//
737//==================================================================================
738//
739AliEMCALReconstructor::AliEMCALMatch::AliEMCALMatch(const AliEMCALMatch& copy)
740 : TObject(),
741 fIndexT(copy.fIndexT),
742 fDistance(copy.fDistance),
743 fdEta(copy.fdEta),
744 fdPhi(copy.fdPhi)
745{
746 //copy ctor
747}
748
749//
750//==================================================================================
751//
752Int_t AliEMCALReconstructor::AliEMCALMatch::Compare(const TObject *obj) const
753{
754 //
755 // Compare wrt the residual
756 //
757
758 AliEMCALReconstructor::AliEMCALMatch *that = (AliEMCALReconstructor::AliEMCALMatch*)obj;
759
760 Double_t thisDist = fDistance;//fDistance;
761 Double_t thatDist = that->fDistance;//that->GetDistance();
762
763 if (thisDist > thatDist) return 1;
764 else if (thisDist < thatDist) return -1;
765 return 0;
766}