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