Coverity (Jens)
[u/mrichter/AliRoot.git] / PWG4 / CaloCalib / AliAnalysisTaskEMCALClusterizeFast.cxx
CommitLineData
2f7259cf 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// --- Root ---
19#include <TClonesArray.h>
20#include <TGeoManager.h>
21#include <TObjArray.h>
22#include <TString.h>
23#include <TTree.h>
24#include "AliAODCaloCluster.h"
25#include "AliAODEvent.h"
26#include "AliAnalysisManager.h"
27#include "AliCDBEntry.h"
28#include "AliCDBManager.h"
29#include "AliCaloCalibPedestal.h"
30#include "AliEMCALAfterBurnerUF.h"
31#include "AliEMCALCalibData.h"
32#include "AliEMCALClusterizerNxN.h"
33#include "AliEMCALClusterizerv1.h"
3786256f 34#include "AliEMCALClusterizerv2.h"
2f7259cf 35#include "AliEMCALDigit.h"
36#include "AliEMCALGeometry.h"
37#include "AliEMCALRecParam.h"
38#include "AliEMCALRecPoint.h"
39#include "AliEMCALRecoUtils.h"
40#include "AliESDEvent.h"
6b7ed7bd 41#include "AliInputEventHandler.h"
2f7259cf 42#include "AliLog.h"
43
44#include "AliAnalysisTaskEMCALClusterizeFast.h"
45
46ClassImp(AliAnalysisTaskEMCALClusterizeFast)
47
48//________________________________________________________________________
2431b20f 49AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast()
50 : AliAnalysisTaskSE(),
51 fRun(0),
52 fDigitsArr(0),
53 fClusterArr(0),
54 fRecParam(0),
55 fClusterizer(0),
56 fUnfolder(0),
57 fJustUnfold(kFALSE),
58 fGeomName(),
59 fGeomMatrixSet(kFALSE),
60 fLoadGeomMatrices(kFALSE),
61 fOCDBpath(),
62 fCalibData(0),
63 fPedestalData(0),
64 fOutputAODBranch(0),
65 fOutputAODBrName(),
66 fRecoUtils(0),
67 fLoadCalib(0),
4cf9204b 68 fLoadPed(0),
b0e354fe 69 fAttachClusters(0),
95f69c66 70 fRecalibOnly(0),
71 fSubBackground(0)
2431b20f 72{
73 // Constructor
74}
75
76//________________________________________________________________________
2f7259cf 77AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast(const char *name)
78 : AliAnalysisTaskSE(name),
79 fRun(0),
80 fDigitsArr(0),
81 fClusterArr(0),
82 fRecParam(new AliEMCALRecParam),
83 fClusterizer(0),
84 fUnfolder(0),
85 fJustUnfold(kFALSE),
86 fGeomName("EMCAL_FIRSTYEARV1"),
87 fGeomMatrixSet(kFALSE),
88 fLoadGeomMatrices(kFALSE),
89 fOCDBpath(),
90 fCalibData(0),
91 fPedestalData(0),
92 fOutputAODBranch(0),
93 fOutputAODBrName(),
10d33986 94 fRecoUtils(0),
95 fLoadCalib(0),
4cf9204b 96 fLoadPed(0),
b0e354fe 97 fAttachClusters(0),
95f69c66 98 fRecalibOnly(0),
99 fSubBackground(0)
2f7259cf 100{
101 // Constructor
102
01bc1ce8 103 fBranchNames = "ESD:AliESDHeader.,AliESDRun.,EMCALCells. AOD:header,emcalCells";
95f69c66 104 for(Int_t i = 0; i < 12; ++i)
2f7259cf 105 fGeomMatrix[i] = 0;
106}
107
108//________________________________________________________________________
109AliAnalysisTaskEMCALClusterizeFast::~AliAnalysisTaskEMCALClusterizeFast()
110{
111 // Destructor.
112
113 delete fDigitsArr;
114 delete fClusterizer;
115 delete fUnfolder;
116 delete fRecoUtils;
117}
118
119//-------------------------------------------------------------------
120void AliAnalysisTaskEMCALClusterizeFast::UserCreateOutputObjects()
121{
122 // Create output objects.
123
124 if (!fOutputAODBrName.IsNull()) {
125 fOutputAODBranch = new TClonesArray("AliAODCaloCluster", 0);
126 fOutputAODBranch->SetName(fOutputAODBrName);
127 AddAODBranch("TClonesArray", &fOutputAODBranch);
128 AliInfo(Form("Created Branch: %s",fOutputAODBrName.Data()));
129 }
130}
131
132//________________________________________________________________________
133void AliAnalysisTaskEMCALClusterizeFast::UserExec(Option_t *)
134{
135 // Main loop, called for each event
136
137 // remove the contents of output list set in the previous event
138 if (fOutputAODBranch)
139 fOutputAODBranch->Clear("C");
140
141 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(InputEvent());
142 AliAODEvent *aodevent = dynamic_cast<AliAODEvent*>(InputEvent());
143
144 if (!esdevent&&!aodevent) {
145 Error("UserExec","Event not available");
146 return;
147 }
148
149 LoadBranches();
6b7ed7bd 150
151 UInt_t offtrigger = 0;
152 if (esdevent) {
153 UInt_t mask = esdevent->GetESDRun()->GetDetectorsInReco();
154 if ((mask >> 18) & 0x1 == 0) { //AliDAQ::OfflineModuleName(180=="EMCAL"
155 AliError(Form("EMCAL not reconstructed: %u (%u)", mask, esdevent->GetESDRun()->GetDetectorsInDAQ()));
156 return;
157 }
158 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
159 offtrigger = ((AliInputEventHandler*)(am->GetInputEventHandler()))->IsEventSelected();
160 } else if (aodevent) {
161 offtrigger = aodevent->GetHeader()->GetOfflineTrigger();
162 }
163 if (offtrigger & AliVEvent::kFastOnly) {
164 AliWarning(Form("EMCAL not in fast only partition"));
165 return;
166 }
2f7259cf 167
168 Init();
169
170 if (fJustUnfold) {
171 AliWarning("Unfolding not implemented");
84ccad86 172 return;
173 }
174
95f69c66 175 FillDigitsArray();
84ccad86 176
95f69c66 177 if (fRecalibOnly) {
178 UpdateCells();
179 return; // not requested to run clusterizer
84ccad86 180 }
7099ec08 181
95f69c66 182 Clusterize();
183 UpdateCells();
184 UpdateClusters();
2431b20f 185
95f69c66 186 if (fOutputAODBranch)
187 RecPoints2Clusters(fOutputAODBranch);
2431b20f 188}
189
190//________________________________________________________________________
95f69c66 191void AliAnalysisTaskEMCALClusterizeFast::Clusterize()
2431b20f 192{
95f69c66 193 // Clusterize
7099ec08 194
95f69c66 195 if (fSubBackground) {
196 fClusterizer->SetInputCalibrated(kTRUE);
197 fClusterizer->SetCalibrationParameters(0);
2f7259cf 198 }
95f69c66 199 fClusterizer->Digits2Clusters("");
200 if (fSubBackground) {
4889ed8b 201 if (fCalibData) {
202 fClusterizer->SetInputCalibrated(kFALSE);
203 fClusterizer->SetCalibrationParameters(fCalibData);
204 }
4cf9204b 205 }
4cf9204b 206}
207
208//________________________________________________________________________
95f69c66 209void AliAnalysisTaskEMCALClusterizeFast::FillDigitsArray()
2f7259cf 210{
211 // Fill digits from cells.
212
213 fDigitsArr->Clear("C");
95f69c66 214 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
215 Double_t avgE = 0; // for background subtraction
2f7259cf 216 Int_t ncells = cells->GetNumberOfCells();
2f7259cf 217 for (Int_t icell = 0, idigit = 0; icell < ncells; ++icell) {
218 Double_t cellAmplitude=0, cellTime=0;
219 Short_t cellNumber=0;
220 if (cells->GetCell(icell, cellNumber, cellAmplitude, cellTime) != kTRUE)
221 break;
95f69c66 222 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
2f7259cf 223 digit->SetId(cellNumber);
2f7259cf 224 digit->SetTime(cellTime);
225 digit->SetTimeR(cellTime);
226 digit->SetIndexInList(idigit);
227 digit->SetType(AliEMCALDigit::kHG);
95f69c66 228 if (fRecalibOnly||fSubBackground) {
b0e354fe 229 Double_t energy = fClusterizer->Calibrate(cellAmplitude,cellTime,cellNumber);
95f69c66 230 digit->SetAmplitude(energy);
231 avgE += energy;
232 } else {
233 digit->SetAmplitude(cellAmplitude);
b0e354fe 234 }
2f7259cf 235 idigit++;
236 }
2f7259cf 237
95f69c66 238 if (fSubBackground) {
239 avgE /= AliEMCALGeometry::GetInstance(fGeomName)->GetNumberOfSuperModules()*48*24;
240 Int_t ndigis = fDigitsArr->GetEntries();
241 for (Int_t i = 0; i < ndigis; ++i) {
242 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(i));
243 Double_t energy = digit->GetAmplitude() - avgE;
244 if (energy<=0.001) {
245 //fDigitsArr->RemoveAt(i);
246 digit->SetAmplitude(0);
247 } else {
248 digit->SetAmplitude(energy);
249 }
b0e354fe 250 }
2f7259cf 251 }
95f69c66 252 fDigitsArr->Compress();
84ccad86 253 fDigitsArr->Sort();
2f7259cf 254}
255
256//________________________________________________________________________________________
95f69c66 257void AliAnalysisTaskEMCALClusterizeFast::RecPoints2Clusters(TClonesArray *clus)
4cf9204b 258{
259 // Cluster energy, global position, cells and their amplitude fractions are restored.
260
95f69c66 261 Bool_t esdobjects = 0;
262 if (strcmp(clus->GetClass()->GetName(),"AliESDCaloCluster")==0)
263 esdobjects = 1;
264
b98487bc 265 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
266 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(fGeomName);
267
4cf9204b 268 Int_t Ncls = fClusterArr->GetEntriesFast();
269 for(Int_t i=0, nout=clus->GetEntries(); i < Ncls; ++i) {
270 AliEMCALRecPoint *recpoint = static_cast<AliEMCALRecPoint*>(fClusterArr->At(i));
271 Int_t ncells_true = 0;
272 const Int_t ncells = recpoint->GetMultiplicity();
273 UShort_t absIds[ncells];
274 Double32_t ratios[ncells];
275 Int_t *dlist = recpoint->GetDigitsList();
276 Float_t *elist = recpoint->GetEnergiesList();
277 for (Int_t c = 0; c < ncells; ++c) {
278 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(dlist[c]));
279 absIds[ncells_true] = digit->GetId();
280 ratios[ncells_true] = elist[c]/digit->GetAmplitude();
84ccad86 281 if (ratios[ncells_true] < 0.001)
282 continue;
283 ++ncells_true;
4cf9204b 284 }
285
286 if (ncells_true < 1) {
287 AliWarning("Skipping cluster with no cells");
288 continue;
289 }
290
291 // calculate new cluster position
292 TVector3 gpos;
4cf9204b 293 recpoint->GetGlobalPosition(gpos);
84ccad86 294 Float_t g[3];
4cf9204b 295 gpos.GetXYZ(g);
296
95f69c66 297 AliVCluster *c = static_cast<AliVCluster*>(clus->New(nout++));
4cf9204b 298 c->SetType(AliVCluster::kEMCALClusterv1);
299 c->SetE(recpoint->GetEnergy());
300 c->SetPosition(g);
301 c->SetNCells(ncells_true);
4cf9204b 302 c->SetDispersion(recpoint->GetDispersion());
84ccad86 303 c->SetEmcCpvDistance(-1); //not yet implemented
4cf9204b 304 c->SetChi2(-1); //not yet implemented
305 c->SetTOF(recpoint->GetTime()) ; //time-of-flight
306 c->SetNExMax(recpoint->GetNExMax()); //number of local maxima
307 Float_t elipAxis[2];
308 recpoint->GetElipsAxis(elipAxis);
309 c->SetM02(elipAxis[0]*elipAxis[0]) ;
310 c->SetM20(elipAxis[1]*elipAxis[1]) ;
b98487bc 311 if (fRecoUtils && fRecoUtils->IsBadChannelsRemovalSwitchedOn()) {
312 fRecoUtils->RecalculateClusterDistanceToBadChannel(geom, cells, c);
313 } else {
314 if (fPedestalData)
315 recpoint->EvalDistanceToBadChannels(fPedestalData);
316 c->SetDistanceToBadChannel(recpoint->GetDistanceToBadTower());
317 }
318
95f69c66 319 if (esdobjects) {
320 AliESDCaloCluster *cesd = static_cast<AliESDCaloCluster*>(c);
321 cesd->SetCellsAbsId(absIds);
322 cesd->SetCellsAmplitudeFraction(ratios);
323 } else {
324 AliAODCaloCluster *caod = static_cast<AliAODCaloCluster*>(c);
325 caod->SetCellsAbsId(absIds);
326 caod->SetCellsAmplitudeFraction(ratios);
327 }
84ccad86 328 }
329
330 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(InputEvent());
331 if (!esdevent)
332 return;
333 if (!fRecoUtils)
334 return;
335
95f69c66 336 AliAnalysisManager::GetAnalysisManager()->LoadBranch("Tracks");
84ccad86 337 fRecoUtils->FindMatches(esdevent,clus);
338
95f69c66 339 if (!esdobjects) {
340 Int_t Nclus = clus->GetEntries();
341 for(Int_t i=0; i < Nclus; ++i) {
342 AliAODCaloCluster *c = static_cast<AliAODCaloCluster*>(clus->At(i));
343 Int_t trackIndex = fRecoUtils->GetMatchedTrackIndex(i);
344 if(trackIndex >= 0) {
345 Float_t dR, dZ;
346 fRecoUtils->GetMatchedResiduals(i,dR, dZ);
347 c->AddTrackMatched(0x0); //esdevent->GetTrack(trackIndex));
348 c->SetTrackDistance(dR,dZ); // not implemented
349 c->SetEmcCpvDistance(dR);
350 c->SetChi2(dZ);
351 if(DebugLevel() > 1)
352 AliInfo(Form("Matched Track index %d to new cluster %d\n",trackIndex,i));
353 }
2f7259cf 354 }
95f69c66 355 } else {
84ccad86 356 Int_t Nclus = clus->GetEntries();
357 for(Int_t i=0; i < Nclus; ++i) {
358 AliESDCaloCluster *c = static_cast<AliESDCaloCluster*>(clus->At(i));
2f7259cf 359 Int_t trackIndex = fRecoUtils->GetMatchedTrackIndex(i);
360 if(trackIndex >= 0) {
84ccad86 361 Float_t dR, dZ;
362 fRecoUtils->GetMatchedResiduals(i,dR, dZ);
363 c->SetTrackDistance(dR,dZ);
364 c->SetEmcCpvDistance(dR); //to be consistent with AODs
365 c->SetChi2(dZ); //to be consistent with AODs
4cf9204b 366 TArrayI tm(1,&trackIndex);
367 c->AddTracksMatched(tm);
2f7259cf 368 if(DebugLevel() > 1)
369 AliInfo(Form("Matched Track index %d to new cluster %d\n",trackIndex,i));
370 }
371 }
372 }
373}
374
95f69c66 375//________________________________________________________________________
376void AliAnalysisTaskEMCALClusterizeFast::UpdateCells()
377{
378 // Update cells in case re-calibration was done.
379
380 if (!fCalibData&&!fSubBackground)
381 return;
382
383 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
384 Int_t ncells = cells->GetNumberOfCells();
385 Int_t ndigis = fDigitsArr->GetEntries();
386
387 if (ncells!=ndigis) {
388 cells->DeleteContainer();
389 cells->CreateContainer(ndigis);
390 }
391 for (Int_t idigit = 0; idigit < ndigis; ++idigit) {
392 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(idigit));
393 Double_t cellAmplitude = digit->GetCalibAmp();
394 Short_t cellNumber = digit->GetId();
395 Double_t cellTime = digit->GetTime();
396 cells->SetCell(idigit, cellNumber, cellAmplitude, cellTime);
397 }
398}
399
400//________________________________________________________________________
401void AliAnalysisTaskEMCALClusterizeFast::UpdateClusters()
402{
403 // Update cells in case re-calibration was done.
404
405 if (!fAttachClusters)
406 return;
407
408 TClonesArray *clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("caloClusters"));
409 if (!clus)
410 clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("CaloClusters"));
411 if(!clus)
412 return;
413
414 Int_t nents = clus->GetEntries();
415 for (Int_t i=0;i<nents;++i) {
416 AliVCluster *c = static_cast<AliVCluster*>(clus->At(i));
417 if (!c)
418 continue;
419 if (c->IsEMCAL())
420 clus->RemoveAt(i);
421 }
422 clus->Compress();
423 RecPoints2Clusters(clus);
424}
425
2f7259cf 426//________________________________________________________________________________________
427void AliAnalysisTaskEMCALClusterizeFast::Init()
428{
429 //Select clusterization/unfolding algorithm and set all the needed parameters
01bc1ce8 430
2f7259cf 431 AliVEvent * event = InputEvent();
432 if (!event) {
433 AliWarning("Event not available!!!");
434 return;
435 }
436
437 if (event->GetRunNumber()==fRun)
438 return;
439 fRun = event->GetRunNumber();
440
441 if (fJustUnfold){
442 // init the unfolding afterburner
443 delete fUnfolder;
444 fUnfolder = new AliEMCALAfterBurnerUF(fRecParam->GetW0(),fRecParam->GetLocMaxCut());
445 return;
446 }
447
2f7259cf 448 AliEMCALGeometry *geometry = AliEMCALGeometry::GetInstance(fGeomName);
449 if (!geometry) {
450 AliFatal("Geometry not available!!!");
451 return;
452 }
453
454 if (!fGeomMatrixSet) {
455 if (fLoadGeomMatrices) {
e51bc3f5 456 for(Int_t mod=0; mod < geometry->GetNumberOfSuperModules(); ++mod) {
2f7259cf 457 if(fGeomMatrix[mod]){
458 if(DebugLevel() > 2)
459 fGeomMatrix[mod]->Print();
460 geometry->SetMisalMatrix(fGeomMatrix[mod],mod);
461 }
462 }
29b496d5 463 } else { // get matrix from file (work around bug in aliroot)
2f7259cf 464 for(Int_t mod=0; mod < geometry->GetEMCGeometry()->GetNumberOfSuperModules(); ++mod) {
29b496d5 465 const TGeoHMatrix *gm = 0;
441c4d3c 466 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(event);
29b496d5 467 if (esdevent) {
468 gm = esdevent->GetEMCALMatrix(mod);
469 } else {
470 AliAODHeader *aodheader = dynamic_cast<AliAODHeader*>(event->GetHeader());
471 if (aodheader) {
472 gm = aodheader->GetEMCALMatrix(mod);
473 }
474 }
475 if (gm) {
2f7259cf 476 if(DebugLevel() > 2)
29b496d5 477 gm->Print();
478 geometry->SetMisalMatrix(gm,mod);
2f7259cf 479 }
480 }
481 }
482 fGeomMatrixSet=kTRUE;
483 }
484
485 // setup digit array if needed
486 if (!fDigitsArr) {
487 fDigitsArr = new TClonesArray("AliEMCALDigit", 1000);
488 fDigitsArr->SetOwner(1);
489 }
490
491 // then setup clusterizer
492 delete fClusterizer;
493 if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv1)
494 fClusterizer = new AliEMCALClusterizerv1(geometry);
495 else if(fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerNxN)
496 fClusterizer = new AliEMCALClusterizerNxN(geometry);
3786256f 497 else if(fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv2)
498 fClusterizer = new AliEMCALClusterizerv2(geometry);
2f7259cf 499 else if(fRecParam->GetClusterizerFlag() > AliEMCALRecParam::kClusterizerNxN) {
500 AliEMCALClusterizerNxN *clusterizer = new AliEMCALClusterizerNxN(geometry);
501 clusterizer->SetNRowDiff(2);
502 clusterizer->SetNColDiff(2);
503 fClusterizer = clusterizer;
504 } else{
505 AliFatal(Form("Clusterizer < %d > not available", fRecParam->GetClusterizerFlag()));
506 }
507 fClusterizer->InitParameters(fRecParam);
01bc1ce8 508
509 if ((!fCalibData&&fLoadCalib) || (!fPedestalData&&fLoadPed)) {
510 AliCDBManager *cdb = AliCDBManager::Instance();
511 if (!cdb->IsDefaultStorageSet() && !fOCDBpath.IsNull())
512 cdb->SetDefaultStorage(fOCDBpath);
513 if (fRun!=cdb->GetRun())
514 cdb->SetRun(fRun);
515 }
10d33986 516 if (!fCalibData&&fLoadCalib) {
2f7259cf 517 AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Data"));
518 if (entry)
519 fCalibData = static_cast<AliEMCALCalibData*>(entry->GetObject());
520 if (!fCalibData)
521 AliFatal("Calibration parameters not found in CDB!");
522 }
10d33986 523 if (!fPedestalData&&fLoadPed) {
2f7259cf 524 AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals"));
525 if (entry)
526 fPedestalData = static_cast<AliCaloCalibPedestal*>(entry->GetObject());
527 }
10d33986 528 if (fCalibData) {
529 fClusterizer->SetInputCalibrated(kFALSE);
530 fClusterizer->SetCalibrationParameters(fCalibData);
10d33986 531 } else {
532 fClusterizer->SetInputCalibrated(kTRUE);
533 }
441c4d3c 534 fClusterizer->SetCaloCalibPedestal(fPedestalData);
10d33986 535 fClusterizer->SetJustClusters(kTRUE);
2f7259cf 536 fClusterizer->SetDigitsArr(fDigitsArr);
537 fClusterizer->SetOutput(0);
538 fClusterArr = const_cast<TObjArray *>(fClusterizer->GetRecPoints());
539}