1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
19 #include <TClonesArray.h>
20 #include <TGeoManager.h>
21 #include <TObjArray.h>
26 #include "AliAODCaloCluster.h"
27 #include "AliAODEvent.h"
28 #include "AliAnalysisManager.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBManager.h"
31 #include "AliCaloCalibPedestal.h"
32 #include "AliEMCALAfterBurnerUF.h"
33 #include "AliEMCALCalibData.h"
34 #include "AliEMCALClusterizerNxN.h"
35 #include "AliEMCALClusterizerv1.h"
36 #include "AliEMCALClusterizerv2.h"
37 #include "AliEMCALClusterizerFixedWindow.h"
38 #include "AliEMCALFixedWindowClusterInfo.h"
39 #include "AliEMCALDigit.h"
40 #include "AliEMCALGeometry.h"
41 #include "AliEMCALRecParam.h"
42 #include "AliEMCALRecPoint.h"
43 #include "AliEMCALRecoUtils.h"
44 #include "AliESDEvent.h"
45 #include "AliInputEventHandler.h"
48 #include "AliAnalysisTaskEMCALClusterizeFast.h"
50 ClassImp(AliAnalysisTaskEMCALClusterizeFast)
52 //________________________________________________________________________
53 AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast()
54 : AliAnalysisTaskSE(),
63 fGeomMatrixSet(kFALSE),
64 fLoadGeomMatrices(kFALSE),
78 fNewClusterArrayName("newCaloClusters"),
84 fStoreAdditionalInformation(0)
89 //________________________________________________________________________
90 AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast(const char *name)
91 : AliAnalysisTaskSE(name),
95 fRecParam(new AliEMCALRecParam),
99 fGeomName("EMCAL_FIRSTYEARV1"),
100 fGeomMatrixSet(kFALSE),
101 fLoadGeomMatrices(kFALSE),
115 fNewClusterArrayName("newCaloClusters"),
121 fStoreAdditionalInformation(0)
125 fBranchNames = "ESD:AliESDHeader.,AliESDRun.,EMCALCells. AOD:header,emcalCells";
126 for(Int_t i = 0; i < 12; ++i)
130 //________________________________________________________________________
131 AliAnalysisTaskEMCALClusterizeFast::~AliAnalysisTaskEMCALClusterizeFast()
141 //-------------------------------------------------------------------
142 void AliAnalysisTaskEMCALClusterizeFast::UserCreateOutputObjects()
144 // Create output objects.
146 if (!fOutputAODBrName.IsNull()) {
147 fOutputAODBranch = new TClonesArray("AliAODCaloCluster", 0);
148 fOutputAODBranch->SetName(fOutputAODBrName);
149 AddAODBranch("TClonesArray", &fOutputAODBranch);
150 AliInfo(Form("Created Branch: %s",fOutputAODBrName.Data()));
154 //________________________________________________________________________
155 void AliAnalysisTaskEMCALClusterizeFast::UserExec(Option_t *)
157 // Main loop, called for each event
159 // remove the contents of output list set in the previous event
160 if (fOutputAODBranch)
161 fOutputAODBranch->Clear("C");
163 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(InputEvent());
164 AliAODEvent *aodevent = dynamic_cast<AliAODEvent*>(InputEvent());
166 if (!esdevent&&!aodevent) {
167 Error("UserExec","Event not available");
173 UInt_t offtrigger = 0;
175 UInt_t mask1 = esdevent->GetESDRun()->GetDetectorsInDAQ();
176 UInt_t mask2 = esdevent->GetESDRun()->GetDetectorsInReco();
177 Bool_t desc1 = (mask1 >> 18) & 0x1;
178 Bool_t desc2 = (mask2 >> 18) & 0x1;
179 if (desc1==0 || desc2==0) { //AliDAQ::OfflineModuleName(180=="EMCAL"
180 AliError(Form("EMCAL not in DAQ/RECO: %u (%u)/%u (%u)",
181 mask1, esdevent->GetESDRun()->GetDetectorsInReco(),
182 mask2, esdevent->GetESDRun()->GetDetectorsInDAQ()));
185 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
186 offtrigger = ((AliInputEventHandler*)(am->GetInputEventHandler()))->IsEventSelected();
187 } else if (aodevent) {
188 offtrigger = aodevent->GetHeader()->GetOfflineTrigger();
190 if (offtrigger & AliVEvent::kFastOnly) {
191 AliWarning(Form("EMCAL not in fast only partition"));
198 AliWarning("Unfolding not implemented");
206 return; // not requested to run clusterizer
213 if (fStoreAdditionalInformation)
214 StoreAdditionalInformation();
216 if (fOutputAODBranch)
217 RecPoints2Clusters(fOutputAODBranch);
220 //________________________________________________________________________
221 void AliAnalysisTaskEMCALClusterizeFast::StoreAdditionalInformation()
223 if (fClusterizer->ClassName() != TString("AliEMCALClusterizerFixedWindow"))
226 TString addInfoName(fNewClusterArrayName);
227 addInfoName.Append("_AbsIds");
229 AliEMCALFixedWindowClusterInfo *clusInfo = dynamic_cast<AliEMCALFixedWindowClusterInfo*>(InputEvent()->FindListObject(addInfoName));
233 AliEMCALClusterizerFixedWindow *clusterizer = dynamic_cast<AliEMCALClusterizerFixedWindow*> (fClusterizer);
236 clusInfo = clusterizer->GetClustersInfo();
239 clusInfo->SetName(addInfoName);
240 InputEvent()->AddObject(clusInfo);
244 //________________________________________________________________________
245 void AliAnalysisTaskEMCALClusterizeFast::Clusterize()
249 if (fSubBackground) {
250 fClusterizer->SetInputCalibrated(kTRUE);
251 fClusterizer->SetCalibrationParameters(0);
254 fClusterizer->Digits2Clusters("");
255 if (fSubBackground) {
257 fClusterizer->SetInputCalibrated(kFALSE);
258 fClusterizer->SetCalibrationParameters(fCalibData);
263 //________________________________________________________________________
264 void AliAnalysisTaskEMCALClusterizeFast::FillDigitsArray()
269 AliEMCALGeometry *fGeom = AliEMCALGeometry::GetInstance(fGeomName);
271 fDigitsArr->Clear("C");
272 Int_t maxd = fGeom->GetNCells() / 4;
274 for (Int_t idigit = 0; idigit < maxd; idigit++)
276 if (idigit % 24 == 12) idigit += 12;
277 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
278 digit->SetId(idigit * 4);
280 digit->SetTimeR(600);
281 digit->SetIndexInList(idigit);
282 digit->SetType(AliEMCALDigit::kHG);
283 digit->SetAmplitude(0.1);
291 // Fill digits from cells.
293 fDigitsArr->Clear("C");
294 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
295 Double_t avgE = 0; // for background subtraction
296 Int_t ncells = cells->GetNumberOfCells();
297 for (Int_t icell = 0, idigit = 0; icell < ncells; ++icell) {
298 Double_t cellAmplitude=0, cellTime=0;
299 Short_t cellNumber=0;
300 if (cells->GetCell(icell, cellNumber, cellAmplitude, cellTime) != kTRUE)
302 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
303 digit->SetId(cellNumber);
304 digit->SetTime(cellTime);
305 digit->SetTimeR(cellTime);
306 digit->SetIndexInList(idigit);
307 digit->SetType(AliEMCALDigit::kHG);
308 if (fRecalibOnly||fSubBackground) {
309 Float_t energy = cellAmplitude;
310 Float_t time = cellTime;
311 fClusterizer->Calibrate(energy,time,cellNumber);
312 digit->SetAmplitude(energy);
315 digit->SetAmplitude(cellAmplitude);
320 if (fSubBackground) {
321 avgE /= AliEMCALGeometry::GetInstance(fGeomName)->GetNumberOfSuperModules()*48*24;
322 Int_t ndigis = fDigitsArr->GetEntries();
323 for (Int_t i = 0; i < ndigis; ++i) {
324 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(i));
325 Double_t energy = digit->GetAmplitude() - avgE;
327 digit->SetAmplitude(0);
329 digit->SetAmplitude(energy);
336 //________________________________________________________________________________________
337 void AliAnalysisTaskEMCALClusterizeFast::RecPoints2Clusters(TClonesArray *clus)
339 // Cluster energy, global position, cells and their amplitude fractions are restored.
341 Bool_t esdobjects = 0;
342 if (strcmp(clus->GetClass()->GetName(),"AliESDCaloCluster")==0)
345 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
346 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(fGeomName);
348 Int_t Ncls = fClusterArr->GetEntriesFast();
349 for(Int_t i=0, nout=clus->GetEntries(); i < Ncls; ++i) {
350 AliEMCALRecPoint *recpoint = static_cast<AliEMCALRecPoint*>(fClusterArr->At(i));
351 Int_t ncells_true = 0;
352 const Int_t ncells = recpoint->GetMultiplicity();
353 UShort_t absIds[ncells];
354 Double32_t ratios[ncells];
355 Int_t *dlist = recpoint->GetDigitsList();
356 Float_t *elist = recpoint->GetEnergiesList();
357 for (Int_t c = 0; c < ncells; ++c) {
358 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(dlist[c]));
359 absIds[ncells_true] = digit->GetId();
360 ratios[ncells_true] = elist[c]/digit->GetAmplitude();
364 if (ncells_true < 1) {
365 AliWarning("Skipping cluster with no cells");
369 // calculate new cluster position
371 recpoint->GetGlobalPosition(gpos);
375 AliVCluster *c = static_cast<AliVCluster*>(clus->New(nout++));
376 c->SetType(AliVCluster::kEMCALClusterv1);
377 c->SetE(recpoint->GetEnergy());
379 c->SetNCells(ncells_true);
380 c->SetDispersion(recpoint->GetDispersion());
381 c->SetEmcCpvDistance(-1); //not yet implemented
382 c->SetChi2(-1); //not yet implemented
383 c->SetTOF(recpoint->GetTime()) ; //time-of-flight
384 c->SetNExMax(recpoint->GetNExMax()); //number of local maxima
386 recpoint->GetElipsAxis(elipAxis);
387 c->SetM02(elipAxis[0]*elipAxis[0]) ;
388 c->SetM20(elipAxis[1]*elipAxis[1]) ;
389 if (fRecoUtils && fRecoUtils->IsBadChannelsRemovalSwitchedOn()) {
390 fRecoUtils->RecalculateClusterDistanceToBadChannel(geom, cells, c);
393 recpoint->EvalDistanceToBadChannels(fPedestalData);
394 c->SetDistanceToBadChannel(recpoint->GetDistanceToBadTower());
398 AliESDCaloCluster *cesd = static_cast<AliESDCaloCluster*>(c);
399 cesd->SetCellsAbsId(absIds);
400 cesd->SetCellsAmplitudeFraction(ratios);
402 AliAODCaloCluster *caod = static_cast<AliAODCaloCluster*>(c);
403 caod->SetCellsAbsId(absIds);
404 caod->SetCellsAmplitudeFraction(ratios);
408 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(InputEvent());
414 AliAnalysisManager::GetAnalysisManager()->LoadBranch("Tracks");
415 fRecoUtils->FindMatches(esdevent,clus);
418 Int_t Nclus = clus->GetEntries();
419 for(Int_t i=0; i < Nclus; ++i) {
420 AliAODCaloCluster *c = static_cast<AliAODCaloCluster*>(clus->At(i));
421 Int_t trackIndex = fRecoUtils->GetMatchedTrackIndex(i);
422 if(trackIndex >= 0) {
424 fRecoUtils->GetMatchedResiduals(i,dR, dZ);
425 c->AddTrackMatched(0x0); //esdevent->GetTrack(trackIndex));
426 c->SetTrackDistance(dR,dZ); // not implemented
427 c->SetEmcCpvDistance(dR);
430 AliInfo(Form("Matched Track index %d to new cluster %d\n",trackIndex,i));
434 Int_t Nclus = clus->GetEntries();
435 for(Int_t i=0; i < Nclus; ++i) {
436 AliESDCaloCluster *c = static_cast<AliESDCaloCluster*>(clus->At(i));
437 Int_t trackIndex = fRecoUtils->GetMatchedTrackIndex(i);
438 if(trackIndex >= 0) {
440 fRecoUtils->GetMatchedResiduals(i,dR, dZ);
441 c->SetTrackDistance(dR,dZ);
442 c->SetEmcCpvDistance(dR); //to be consistent with AODs
443 c->SetChi2(dZ); //to be consistent with AODs
444 TArrayI tm(1,&trackIndex);
445 c->AddTracksMatched(tm);
447 AliInfo(Form("Matched Track index %d to new cluster %d\n",trackIndex,i));
453 //________________________________________________________________________
454 void AliAnalysisTaskEMCALClusterizeFast::UpdateCells()
456 // Update cells in case re-calibration was done.
458 if (!fCalibData&&!fSubBackground)
461 AliVCaloCells *cells = InputEvent()->GetEMCALCells();
462 Int_t ncells = cells->GetNumberOfCells();
463 Int_t ndigis = fDigitsArr->GetEntries();
465 if (ncells!=ndigis) {
466 cells->DeleteContainer();
467 cells->CreateContainer(ndigis);
469 for (Int_t idigit = 0; idigit < ndigis; ++idigit) {
470 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(idigit));
471 Double_t cellAmplitude = digit->GetCalibAmp();
472 Short_t cellNumber = digit->GetId();
473 Double_t cellTime = digit->GetTime();
474 cells->SetCell(idigit, cellNumber, cellAmplitude, cellTime);
478 //________________________________________________________________________
479 void AliAnalysisTaskEMCALClusterizeFast::UpdateClusters()
481 // Update cells in case re-calibration was done.
483 if (!fAttachClusters)
490 clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("caloClusters"));
492 clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("CaloClusters"));
496 Int_t nents = clus->GetEntries();
497 for (Int_t i=0;i<nents;++i) {
498 AliVCluster *c = static_cast<AliVCluster*>(clus->At(i));
502 delete clus->RemoveAt(i);
507 clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fNewClusterArrayName));
510 clus = new TClonesArray("AliESDCaloCluster");
511 clus->SetName(fNewClusterArrayName);
512 InputEvent()->AddObject(clus);
517 Int_t nents = clus->GetEntries();
518 for (Int_t i=0;i<nents;++i) {
519 AliVCluster *c = static_cast<AliVCluster*>(clus->At(i));
523 delete clus->RemoveAt(i);
529 RecPoints2Clusters(clus);
532 //________________________________________________________________________________________
533 void AliAnalysisTaskEMCALClusterizeFast::Init()
535 //Select clusterization/unfolding algorithm and set all the needed parameters
537 AliVEvent * event = InputEvent();
539 AliWarning("Event not available!!!");
543 if (event->GetRunNumber()==fRun)
545 fRun = event->GetRunNumber();
548 // init the unfolding afterburner
550 fUnfolder = new AliEMCALAfterBurnerUF(fRecParam->GetW0(),fRecParam->GetLocMaxCut());
554 AliEMCALGeometry *geometry = AliEMCALGeometry::GetInstance(fGeomName);
556 AliFatal("Geometry not available!!!");
560 if (!fGeomMatrixSet) {
561 if (fLoadGeomMatrices) {
562 for(Int_t mod=0; mod < geometry->GetNumberOfSuperModules(); ++mod) {
563 if(fGeomMatrix[mod]){
565 fGeomMatrix[mod]->Print();
566 geometry->SetMisalMatrix(fGeomMatrix[mod],mod);
569 } else { // get matrix from file (work around bug in aliroot)
570 for(Int_t mod=0; mod < geometry->GetEMCGeometry()->GetNumberOfSuperModules(); ++mod) {
571 const TGeoHMatrix *gm = 0;
572 AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(event);
574 gm = esdevent->GetEMCALMatrix(mod);
576 AliAODHeader *aodheader = dynamic_cast<AliAODHeader*>(event->GetHeader());
578 gm = aodheader->GetEMCALMatrix(mod);
584 geometry->SetMisalMatrix(gm,mod);
588 fGeomMatrixSet=kTRUE;
591 // setup digit array if needed
593 fDigitsArr = new TClonesArray("AliEMCALDigit", 1000);
594 fDigitsArr->SetOwner(1);
597 // then setup clusterizer
599 if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv1)
600 fClusterizer = new AliEMCALClusterizerv1(geometry);
601 else if(fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerNxN) {
602 AliEMCALClusterizerNxN *clusterizer = new AliEMCALClusterizerNxN(geometry);
603 clusterizer->SetNRowDiff(fRecParam->GetNRowDiff());
604 clusterizer->SetNColDiff(fRecParam->GetNColDiff());
605 fClusterizer = clusterizer;
606 } else if(fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv2)
607 fClusterizer = new AliEMCALClusterizerv2(geometry);
608 else if(fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerFW){
609 AliEMCALClusterizerFixedWindow *clusterizer = new AliEMCALClusterizerFixedWindow(geometry);
610 clusterizer->SetNphi(fNPhi);
611 clusterizer->SetNeta(fNEta);
612 clusterizer->SetShiftPhi(fShiftPhi);
613 clusterizer->SetShiftEta(fShiftEta);
614 clusterizer->SetTRUshift(fTRUShift);
615 fClusterizer = clusterizer;
618 AliFatal(Form("Clusterizer < %d > not available", fRecParam->GetClusterizerFlag()));
620 fClusterizer->InitParameters(fRecParam);
622 if ((!fCalibData&&fLoadCalib) || (!fPedestalData&&fLoadPed)) {
623 AliCDBManager *cdb = AliCDBManager::Instance();
624 if (!cdb->IsDefaultStorageSet() && !fOCDBpath.IsNull())
625 cdb->SetDefaultStorage(fOCDBpath);
626 if (fRun!=cdb->GetRun())
629 if (!fCalibData&&fLoadCalib&&fRun>0) {
630 AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Data"));
632 fCalibData = static_cast<AliEMCALCalibData*>(entry->GetObject());
634 AliFatal("Calibration parameters not found in CDB!");
636 if (!fPedestalData&&fLoadPed&&fRun>0) {
637 AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals"));
639 fPedestalData = static_cast<AliCaloCalibPedestal*>(entry->GetObject());
642 fClusterizer->SetInputCalibrated(kFALSE);
643 fClusterizer->SetCalibrationParameters(fCalibData);
645 fClusterizer->SetInputCalibrated(kTRUE);
647 fClusterizer->SetCaloCalibPedestal(fPedestalData);
648 fClusterizer->SetJustClusters(kTRUE);
649 fClusterizer->SetDigitsArr(fDigitsArr);
650 fClusterizer->SetOutput(0);
651 fClusterArr = const_cast<TObjArray *>(fClusterizer->GetRecPoints());