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