removed possible division by zero
[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//
85c25c2e 24//-- Aleksei Pavlinov : added staf for EMCAL jet trigger 9Apr 25, 2008)
25// : fgDigitsArr should read just once at event
26
f6019cda 27// --- ROOT system ---
85c25c2e 28#include <TList.h>
29#include <TClonesArray.h>
f6019cda 30
31// --- Standard library ---
32
33// --- AliRoot header files ---
f6019cda 34#include "AliEMCALReconstructor.h"
5dee926e 35
aaa3cb7c 36#include "AliCodeTimer.h"
af885e0f 37#include "AliESDEvent.h"
89ffc0b0 38#include "AliESDCaloCluster.h"
0e7c6655 39#include "AliESDCaloCells.h"
6a0cf740 40#include "AliESDtrack.h"
5dee926e 41#include "AliEMCALLoader.h"
98e9578e 42#include "AliEMCALRawUtils.h"
0e7c6655 43#include "AliEMCALDigit.h"
f6019cda 44#include "AliEMCALClusterizerv1.h"
5dee926e 45#include "AliEMCALRecPoint.h"
dc293ae9 46#include "AliEMCALPID.h"
0964c2e9 47#include "AliEMCALTrigger.h"
1d59832c 48#include "AliRawReader.h"
fa42b1f3 49#include "AliCDBEntry.h"
50#include "AliCDBManager.h"
51#include "AliEMCALRecParam.h"
65bdc82f 52#include "AliEMCALGeometry.h"
ac8ae9fe 53#include "AliEMCAL.h"
85c25c2e 54#include "AliEMCALHistoUtilities.h"
55#include "AliESDVZERO.h"
65bdc82f 56
72c58de0 57#include "AliRunLoader.h"
58#include "AliRun.h"
1d59832c 59
85c25c2e 60ClassImp(AliEMCALReconstructor)
f6019cda 61
c47157cd 62AliEMCALRecParam* AliEMCALReconstructor::fgkRecParam = 0; // EMCAL rec. parameters
65bdc82f 63AliEMCALRawUtils* AliEMCALReconstructor::fgRawUtils = 0; // EMCAL raw utilities class
9517d886 64AliEMCALClusterizer* AliEMCALReconstructor::fgClusterizer = 0; // EMCAL clusterizer class
85c25c2e 65TClonesArray* AliEMCALReconstructor::fgDigitsArr = 0; // shoud read just once at event
f6019cda 66//____________________________________________________________________________
18a21c7c 67AliEMCALReconstructor::AliEMCALReconstructor()
85c25c2e 68 : fDebug(kFALSE), fList(0), fGeom(0)
f6019cda 69{
70 // ctor
65bdc82f 71 InitRecParam();
72
73 fgRawUtils = new AliEMCALRawUtils;
9517d886 74 fgClusterizer = new AliEMCALClusterizerv1;
72c58de0 75
76 //To make sure we match with the geometry in a simulation file,
77 //let's try to get it first. If not, take the default geometry
78 AliRunLoader *rl = AliRunLoader::GetRunLoader();
79 if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
80 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
81 } else {
82 AliInfo(Form("Using default geometry in reconstruction"));
937d0661 83 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
65bdc82f 84 }
0e7c6655 85
72c58de0 86 if(!fGeom) AliFatal(Form("Could not get geometry!"));
87
f6019cda 88}
89
0a4cb131 90//____________________________________________________________________________
18a21c7c 91AliEMCALReconstructor::AliEMCALReconstructor(const AliEMCALReconstructor & rec)
92 : AliReconstructor(rec),
65bdc82f 93 fDebug(rec.fDebug),
85c25c2e 94 fList(rec.fList),
65bdc82f 95 fGeom(rec.fGeom)
0a4cb131 96{
97 //copy ctor
0a4cb131 98}
f6019cda 99
100//____________________________________________________________________________
101AliEMCALReconstructor::~AliEMCALReconstructor()
102{
103 // dtor
65bdc82f 104 delete fGeom;
aaa3cb7c 105 AliCodeTimer::Instance()->Print();
f6019cda 106}
107
108//____________________________________________________________________________
85c25c2e 109void AliEMCALReconstructor::Init()
110{
111 // Trigger hists - Oct 24, 2007
112 fList = AliEMCALHistoUtilities::GetTriggersListOfHists(kTRUE);
113}
114
115//____________________________________________________________________________
fa42b1f3 116void AliEMCALReconstructor::InitRecParam() const
117{
118 // Check if the instance of AliEMCALRecParam exists,
119 // if not, get it from OCDB if available, otherwise create a default one
120
121 if (!fgkRecParam && (AliCDBManager::Instance()->IsDefaultStorageSet())) {
122 AliCDBEntry *entry = (AliCDBEntry*)
123 AliCDBManager::Instance()->Get("EMCAL/Config/RecParam");
124 if (entry) fgkRecParam = (AliEMCALRecParam*) entry->GetObject();
125 }
126
127 if(!fgkRecParam){
128 AliWarning("The Reconstruction parameters for EMCAL nonitialized - Used default one");
129 fgkRecParam = new AliEMCALRecParam;
130 }
131}
132
133//____________________________________________________________________________
c47157cd 134void AliEMCALReconstructor::Reconstruct(TTree* digitsTree, TTree* clustersTree) const
f6019cda 135{
136 // method called by AliReconstruction;
137 // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
138 // segment maker needs access to the AliESD object to retrieve the tracks reconstructed by
139 // the global tracking.
c47157cd 140 // Works on the current event.
fa42b1f3 141
aaa3cb7c 142 AliCodeTimerAuto("")
143
85c25c2e 144 ReadDigitsArrayFromTree(digitsTree);
145
146 if(fgDigitsArr && fgDigitsArr->GetEntries()) {
147
9517d886 148 fgClusterizer->SetInput(digitsTree);
149 fgClusterizer->SetOutput(clustersTree);
150
85c25c2e 151 if(Debug())
9517d886 152 fgClusterizer->Digits2Clusters("deb all") ;
85c25c2e 153 else
9517d886 154 fgClusterizer->Digits2Clusters("");
155
156 fgClusterizer->Clear();
85c25c2e 157
158 }
9517d886 159
f6019cda 160}
161
162//____________________________________________________________________________
c47157cd 163void AliEMCALReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
98e9578e 164
a68156e6 165{
c47157cd 166 // Conversion from raw data to
167 // EMCAL digits.
168 // Works on a single-event basis
85c60a8e 169
98e9578e 170 rawReader->Reset() ;
98e9578e 171
c47157cd 172 TClonesArray *digitsArr = new TClonesArray("AliEMCALDigit",100);
173 Int_t bufsize = 32000;
174 digitsTree->Branch("EMCAL", &digitsArr, bufsize);
98e9578e 175
65bdc82f 176 //must be done here because, in constructor, option is not yet known
177 fgRawUtils->SetOption(GetOption());
b4133f05 178
179 fgRawUtils->SetRawFormatHighLowGainFactor(fgkRecParam->GetHighLowGainFactor());
180 fgRawUtils->SetRawFormatOrder(fgkRecParam->GetOrderParameter());
181 fgRawUtils->SetRawFormatTau(fgkRecParam->GetTau());
182 fgRawUtils->SetNoiseThreshold(fgkRecParam->GetNoiseThreshold());
183 fgRawUtils->SetNPedSamples(fgkRecParam->GetNPedSamples());
184
65bdc82f 185 fgRawUtils->Raw2Digits(rawReader,digitsArr);
c615db53 186
187 digitsTree->Fill();
188 digitsArr->Delete();
189 delete digitsArr;
190
a68156e6 191}
192
85c25c2e 193
a68156e6 194//____________________________________________________________________________
0e7c6655 195void AliEMCALReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree,
c47157cd 196 AliESDEvent* esd) const
f6019cda 197{
98e9578e 198 // Called by AliReconstruct after Reconstruct() and global tracking and vertexing
85c25c2e 199 // and V0
c47157cd 200 // Works on the current event
85c25c2e 201 // printf(" ## AliEMCALReconstructor::FillESD() is started ### \n ");
202 //return;
92da3372 203
6a0cf740 204 //######################################################
0964c2e9 205 //#########Calculate trigger and set trigger info###########
6a0cf740 206 //######################################################
85c25c2e 207
208 AliEMCALTrigger tr;
209 // tr.SetPatchSize(1); // create 4x4 patches
210 tr.SetSimulation(kFALSE); // Reconstruction mode
211 tr.SetDigitsList(fgDigitsArr);
212 // Get VZERO total multiplicity for jet trigger simulation
213 // The simulation of jey trigger will be incorrect if no VZERO data
214 // at ESD
215 AliESDVZERO* vZero = esd->GetVZEROData();
216 if(vZero) {
217 tr.SetVZER0Multiplicity(vZero->GetMTotV0A() + vZero->GetMTotV0C());
218 }
219 //
0964c2e9 220 tr.Trigger();
85c25c2e 221
0964c2e9 222 Float_t maxAmp2x2 = tr.Get2x2MaxAmplitude();
223 Float_t maxAmpnxn = tr.GetnxnMaxAmplitude();
224 Float_t ampOutOfPatch2x2 = tr.Get2x2AmpOutOfPatch() ;
225 Float_t ampOutOfPatchnxn = tr.GetnxnAmpOutOfPatch() ;
226
0964c2e9 227 Int_t iSM2x2 = tr.Get2x2SuperModule();
228 Int_t iSMnxn = tr.GetnxnSuperModule();
85c25c2e 229 Int_t iModulePhi2x2 = tr.Get2x2ModulePhi();
230 Int_t iModulePhinxn = tr.GetnxnModulePhi();
231 Int_t iModuleEta2x2 = tr.Get2x2ModuleEta();
232 Int_t iModuleEtanxn = tr.GetnxnModuleEta();
0964c2e9 233
85c25c2e 234 AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d", maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iModulePhi2x2, iModuleEta2x2));
235 AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d", maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iModulePhinxn, iModuleEtanxn));
0964c2e9 236
237 TVector3 pos2x2(-1,-1,-1);
238 TVector3 posnxn(-1,-1,-1);
239
85c25c2e 240 Int_t iAbsId2x2 = fGeom->GetAbsCellIdFromCellIndexes( iSM2x2, iModulePhi2x2, iModuleEta2x2) ; // should be changed to Module
241 Int_t iAbsIdnxn = fGeom->GetAbsCellIdFromCellIndexes( iSMnxn, iModulePhinxn, iModuleEtanxn) ;
65bdc82f 242 fGeom->GetGlobal(iAbsId2x2, pos2x2);
243 fGeom->GetGlobal(iAbsIdnxn, posnxn);
85c25c2e 244 //printf(" iAbsId2x2 %i iAbsIdnxn %i \n", iAbsId2x2, iAbsIdnxn);
0964c2e9 245
246 TArrayF triggerPosition(6);
247 triggerPosition[0] = pos2x2(0) ;
248 triggerPosition[1] = pos2x2(1) ;
249 triggerPosition[2] = pos2x2(2) ;
250 triggerPosition[3] = posnxn(0) ;
251 triggerPosition[4] = posnxn(1) ;
85c25c2e 252 triggerPosition[5] = posnxn(2) ;
253 //printf(" triggerPosition ");
254 //for(int i=0; i<6; i++) printf(" %i %f : ", i, triggerPosition[i]);
0964c2e9 255
256 TArrayF triggerAmplitudes(4);
257 triggerAmplitudes[0] = maxAmp2x2 ;
258 triggerAmplitudes[1] = ampOutOfPatch2x2 ;
259 triggerAmplitudes[2] = maxAmpnxn ;
260 triggerAmplitudes[3] = ampOutOfPatchnxn ;
85c25c2e 261 //printf("\n triggerAmplitudes ");
262 //for(int i=0; i<4; i++) printf(" %i %f : ", i, triggerAmplitudes[i]);
263 //printf("\n");
264 tr.Print("");
0964c2e9 265
266 esd->AddEMCALTriggerPosition(triggerPosition);
267 esd->AddEMCALTriggerAmplitudes(triggerAmplitudes);
85c25c2e 268 // Fill trigger hists
269 AliEMCALHistoUtilities::FillTriggersListOfHists(fList,&triggerPosition,&triggerAmplitudes);
aaa3cb7c 270
0e7c6655 271 //########################################
272 //##############Fill CaloCells###############
273 //########################################
aaa3cb7c 274
0e7c6655 275 TClonesArray *digits = new TClonesArray("AliEMCALDigit",1000);
276 TBranch *branchdig = digitsTree->GetBranch("EMCAL");
277 if (!branchdig) {
278 AliError("can't get the branch with the PHOS digits !");
279 return;
280 }
281 branchdig->SetAddress(&digits);
282 digitsTree->GetEvent(0);
283 Int_t nDigits = digits->GetEntries(), idignew = 0 ;
284 AliDebug(1,Form("%d digits",nDigits));
285
286 AliESDCaloCells &emcCells = *(esd->GetEMCALCells());
287 emcCells.CreateContainer(nDigits);
288 emcCells.SetType(AliESDCaloCells::kEMCALCell);
289 for (Int_t idig = 0 ; idig < nDigits ; idig++) {
290 const AliEMCALDigit * dig = (const AliEMCALDigit*)digits->At(idig);
291 if(dig->GetAmp() > 0 ){
292 emcCells.SetCell(idignew,dig->GetId(),dig->GetAmp(), dig->GetTime());
293 idignew++;
294 }
295 }
296 emcCells.SetNumberOfCells(idignew);
297 emcCells.Sort();
298
299 //------------------------------------------------------------
300 //-----------------CLUSTERS-----------------------------
301 //------------------------------------------------------------
302 TObjArray *clusters = new TObjArray(100);
303 TBranch *branch = clustersTree->GetBranch("EMCALECARP");
304 branch->SetAddress(&clusters);
305 clustersTree->GetEvent(0);
306
307 Int_t nClusters = clusters->GetEntries(), nClustersNew=0;
308 AliDebug(1,Form("%d clusters",nClusters));
309 esd->SetFirstEMCALCluster(esd->GetNumberOfCaloClusters()); // Put after Phos clusters
310
85c25c2e 311
6a0cf740 312 //######################################################
313 //#######################TRACK MATCHING###############
314 //######################################################
315 //Fill list of integers, each one is index of track to which the cluster belongs.
316
317 // step 1 - initialize array of matched track indexes
318 Int_t *matchedTrack = new Int_t[nClusters];
319 for (Int_t iclus = 0; iclus < nClusters; iclus++)
320 matchedTrack[iclus] = -1; // neg. index --> no matched track
321
322 // step 2, change the flag for all matched clusters found in tracks
323 Int_t iemcalMatch = -1;
324 Int_t endtpc = esd->GetNumberOfTracks();
325 for (Int_t itrack = 0; itrack < endtpc; itrack++) {
326 AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
327 iemcalMatch = track->GetEMCALcluster();
65f4a419 328 if(iemcalMatch >= 0) matchedTrack[iemcalMatch] = itrack;
6a0cf740 329 }
85c25c2e 330
6a0cf740 331 //########################################
85c25c2e 332 //##############Fill CaloClusters#############
6a0cf740 333 //########################################
35397e76 334 esd->SetNumberOfEMCALClusters(nClusters);
5dee926e 335 for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
c47157cd 336 const AliEMCALRecPoint * clust = (const AliEMCALRecPoint*)clusters->At(iClust);
8ada0ffe 337 //if(clust->GetClusterType()== AliESDCaloCluster::kEMCALClusterv1) nRP++; else nPC++;
85c60a8e 338 if (Debug()) clust->Print();
a7a5421e 339 // Get information from EMCAL reconstruction points
85c60a8e 340 Float_t xyz[3];
5dee926e 341 TVector3 gpos;
342 clust->GetGlobalPosition(gpos);
35397e76 343 for (Int_t ixyz=0; ixyz<3; ixyz++)
5dee926e 344 xyz[ixyz] = gpos[ixyz];
85c25c2e 345 Float_t elipAxis[2];
346 clust->GetElipsAxis(elipAxis);
35397e76 347 //Create digits lists
348 Int_t cellMult = clust->GetMultiplicity();
349 //TArrayS digiList(digitMult);
350 Float_t *amplFloat = clust->GetEnergiesList();
351 Int_t *digitInts = clust->GetAbsId();
352 TArrayS absIdList(cellMult);
353 //Uncomment when unfolding is done
354 //TArrayD fracList(cellMult);
355
356 Int_t newCellMult = 0;
357 for (Int_t iCell=0; iCell<cellMult; iCell++) {
358 if (amplFloat[iCell] > 0) {
359 absIdList[newCellMult] = (UShort_t)(digitInts[iCell]);
360 //Uncomment when unfolding is done
361 //fracList[newCellMult] = amplFloat[iCell]/emcCells.GetCellAmplitude(digitInts[iCell]);
362 newCellMult++;
92da3372 363 }
92da3372 364 }
35397e76 365 absIdList.Set(newCellMult);
366 //Uncomment when unfolding is done
367 //fracList.Set(newCellMult);
85c25c2e 368
35397e76 369 if(newCellMult > 0) { // accept cluster if it has some digit
370 nClustersNew++;
65721814 371 //Primaries
7592dfc4 372 Int_t parentMult = 0;
fa42b1f3 373 Int_t *parentList = clust->GetParents(parentMult);
a7a5421e 374 // fills the ESDCaloCluster
35397e76 375 AliESDCaloCluster * ec = new AliESDCaloCluster() ;
376 ec->SetClusterType(AliESDCaloCluster::kEMCALClusterv1);
7592dfc4 377 ec->SetPosition(xyz);
378 ec->SetE(clust->GetEnergy());
35397e76 379 ec->SetNCells(newCellMult);
380 //Change type of list from short to ushort
381 UShort_t *newAbsIdList = new UShort_t[newCellMult];
382 //Uncomment when unfolding is done
383 //Double_t *newFracList = new Double_t[newCellMult];
384 for(Int_t i = 0; i < newCellMult ; i++) {
385 newAbsIdList[i]=absIdList[i];
386 //Uncomment when unfolding is done
387 //newFracList[i]=fracList[i];
388 }
389 ec->SetCellsAbsId(newAbsIdList);
390 //Uncomment when unfolding is done
391 //ec->SetCellsAmplitudeFraction(newFracList);
392 ec->SetClusterDisp(clust->GetDispersion());
393 ec->SetClusterChi2(-1); //not yet implemented
394 ec->SetM02(elipAxis[0]*elipAxis[0]) ;
395 ec->SetM20(elipAxis[1]*elipAxis[1]) ;
396 ec->SetM11(-1) ; //not yet implemented
397
398 TArrayI arrayTrackMatched(1);// Only one track, temporal solution.
399 arrayTrackMatched[0]= matchedTrack[iClust];
400 ec->AddTracksMatched(arrayTrackMatched);
401
402 TArrayI arrayParents(parentMult,parentList);
403 ec->AddLabels(arrayParents);
404
6a0cf740 405 // add the cluster to the esd object
35397e76 406 esd->AddCaloCluster(ec);
a7a5421e 407 delete ec;
35397e76 408 //delete [] newAbsIdList ;
409 //delete [] newFracList ;
410 }
411 } // cycle on clusters
85c25c2e 412
35397e76 413 delete [] matchedTrack;
85c25c2e 414
35397e76 415 esd->SetNumberOfEMCALClusters(nClustersNew);
416 //if(nClustersNew != nClusters)
417 //printf(" ##### nClusters %i -> new %i ##### \n", nClusters, nClustersNew );
85c25c2e 418
35397e76 419 //Fill ESDCaloCluster with PID weights
85c25c2e 420 AliEMCALPID *pid = new AliEMCALPID;
421 //pid->SetPrintInfo(kTRUE);
422 pid->SetReconstructor(kTRUE);
423 pid->RunPID(esd);
85c25c2e 424 delete pid;
35397e76 425
426 delete digits;
85c25c2e 427 delete clusters;
01b3aba7 428
35397e76 429 // printf(" ## AliEMCALReconstructor::FillESD() is ended : ncl %i -> %i ### \n ",nClusters, nClustersNew);
f6019cda 430}
dc293ae9 431
9517d886 432//__________________________________________________________________________
85c25c2e 433void AliEMCALReconstructor::ReadDigitsArrayFromTree(TTree *digitsTree) const
434{
435 // See AliEMCALClusterizer::SetInput(TTree *digitsTree);
436 if(fgDigitsArr) {
437 // Clear previous digits
438 fgDigitsArr->Delete();
439 delete fgDigitsArr;
440 }
441 // Read the digits from the input tree
442 TBranch *branch = digitsTree->GetBranch("EMCAL");
443 if (!branch) {
444 AliError("can't get the branch with the EMCAL digits !");
445 return;
446 }
447 fgDigitsArr = new TClonesArray("AliEMCALDigit",100);
448 branch->SetAddress(&fgDigitsArr);
449 branch->GetEntry(0);
450}
98e9578e 451