5 // Author: S.Aiola, C.Loizides
7 #include "AliJetModelBaseTask.h"
9 #include <TClonesArray.h>
11 #include <TLorentzVector.h>
15 #include "AliVEvent.h"
16 #include "AliAODCaloCluster.h"
17 #include "AliESDCaloCluster.h"
18 #include "AliVCluster.h"
19 #include "AliEMCALDigit.h"
20 #include "AliEMCALRecPoint.h"
21 #include "AliESDCaloCells.h"
22 #include "AliAODCaloCells.h"
23 #include "AliAODMCParticle.h"
24 #include "AliVCaloCells.h"
25 #include "AliPicoTrack.h"
26 #include "AliEMCALGeometry.h"
28 #include "AliNamedArrayI.h"
30 ClassImp(AliJetModelBaseTask)
32 //________________________________________________________________________
33 AliJetModelBaseTask::AliJetModelBaseTask() :
34 AliAnalysisTaskSE("AliJetModelBaseTask"),
43 fOutMCParticlesName(),
49 fPhiMax(TMath::Pi() * 2),
71 fOutMCParticlesMap(0),
76 // Default constructor.
79 //________________________________________________________________________
80 AliJetModelBaseTask::AliJetModelBaseTask(const char *name, Bool_t drawqa) :
81 AliAnalysisTaskSE(name),
83 fTracksName("PicoTracks"),
84 fOutTracksName("PicoTracksEmbedded"),
85 fCaloName("CaloClustersCorr"),
86 fOutCaloName("CaloClustersCorrEmbedded"),
90 fOutMCParticlesName(""),
96 fPhiMax(TMath::Pi() * 2),
118 fOutMCParticlesMap(0),
123 // Standard constructor.
126 DefineOutput(1, TList::Class());
130 //________________________________________________________________________
131 AliJetModelBaseTask::~AliJetModelBaseTask()
136 //________________________________________________________________________
137 void AliJetModelBaseTask::UserCreateOutputObjects()
139 // Create user output.
144 fOutput = new TList();
147 PostData(1, fOutput);
150 //________________________________________________________________________
151 void AliJetModelBaseTask::UserExec(Option_t *)
153 // Execute per event.
156 fIsInit = ExecOnce();
165 const AliVVertex *vert = InputEvent()->GetPrimaryVertex();
167 vert->GetXYZ(fVertex);
171 fOutTracks->Delete();
173 fOutClusters->Delete();
175 fOutMCParticles->Delete();
179 if (fOutMCParticlesMap)
180 fOutMCParticlesMap->Clear();
182 AliVCaloCells *tempCaloCells = 0;
187 tempCaloCells = fCaloCells;
188 fCaloCells = static_cast<AliVCaloCells*>(tempCaloCells->Clone(Form("%s_old",fCaloCells->GetName())));
194 if (fCaloCells && !fCopyArray) {
196 fCaloCells = tempCaloCells;
200 //________________________________________________________________________
201 Bool_t AliJetModelBaseTask::ExecOnce()
206 gRandom = new TRandom3(0);
208 fEsdMode = InputEvent()->InheritsFrom("AliESDEvent");
210 if (fPtMax < fPtMin) {
211 AliWarning (Form("PtMax (%f) < PtMin (%f), setting PtMax = PtMin = %f", fPtMax, fPtMin, fPtMin));
215 if (fEtaMax < fEtaMin) {
216 AliWarning (Form("EtaMax (%f) < EtaMin (%f), setting EtaMax = EtaMin = %f", fEtaMax, fEtaMin, fEtaMin));
220 if (fPhiMax < fPhiMin) {
221 AliWarning (Form("PhiMax (%f) < PhiMin (%f), setting PhiMax = PhiMin = %f", fPhiMax, fPhiMin, fPhiMin));
225 if (!fCellsName.IsNull()) {
226 fCaloCells = dynamic_cast<AliVCaloCells*>(InputEvent()->FindListObject(fCellsName));
228 AliWarning(Form("%s: Couldn't retrieve calo cells with name %s!", GetName(), fCellsName.Data()));
230 else if (!fCaloCells->InheritsFrom("AliVCaloCells")) {
231 AliError(Form("%s: Collection %s does not contain a AliVCaloCells object!", GetName(), fCellsName.Data()));
236 if (!fOutCaloCells) {
237 fOutCellsName = fCellsName;
239 fOutCellsName += fSuffix;
240 if (fCopyArray || !fCaloCells) {
242 fOutCaloCells = new AliESDCaloCells(fOutCellsName,fOutCellsName);
244 fOutCaloCells = new AliAODCaloCells(fOutCellsName,fOutCellsName);
246 if (InputEvent()->FindListObject(fOutCellsName)) {
247 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCellsName.Data()));
251 InputEvent()->AddObject(fOutCaloCells);
255 fOutCaloCells = fCaloCells;
260 if (fNTracks > 0 && !fTracksName.IsNull()) {
261 fTracks = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fTracksName));
263 AliWarning(Form("%s: Couldn't retrieve tracks with name %s!", GetName(), fTracksName.Data()));
265 else if (!fTracks->GetClass()->GetBaseClass("AliPicoTrack")) {
266 AliError(Form("%s: Collection %s does not contain AliPicoTrack objects!", GetName(), fTracksName.Data()));
272 fOutTracksName = fTracksName;
274 fOutTracksName += fSuffix;
275 if (fCopyArray || !fTracks) {
276 fOutTracks = new TClonesArray("AliPicoTrack");
277 fOutTracks->SetName(fOutTracksName);
278 if (InputEvent()->FindListObject(fOutTracksName)) {
279 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutTracksName.Data()));
283 InputEvent()->AddObject(fOutTracks);
287 fOutTracks = fTracks;
292 if (fNClusters > 0 && !fCaloName.IsNull()) {
293 fClusters = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fCaloName));
296 AliWarning(Form("%s: Couldn't retrieve clusters with name %s!", GetName(), fCaloName.Data()));
298 else if (!fClusters->GetClass()->GetBaseClass("AliVCluster")) {
299 AliError(Form("%s: Collection %s does not contain AliVCluster objects!", GetName(), fCaloName.Data()));
305 fOutCaloName = fCaloName;
307 fOutCaloName += fSuffix;
310 className = fClusters->GetClass()->GetName();
312 className = "AliESDCaloCluster";
314 className = "AliAODCaloCluster";
316 if (fCopyArray || !fClusters) {
317 fOutClusters = new TClonesArray(className.Data());
318 fOutClusters->SetName(fOutCaloName);
319 if (InputEvent()->FindListObject(fOutCaloName)) {
320 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCaloName.Data()));
324 InputEvent()->AddObject(fOutClusters);
328 fOutClusters = fClusters;
333 if (!fMCParticlesName.IsNull()) {
334 fMCParticles = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fMCParticlesName));
336 AliWarning(Form("%s: Couldn't retrieve MC particles with name %s!", GetName(), fMCParticlesName.Data()));
339 if (!fMCParticles->GetClass()->GetBaseClass("AliAODMCParticle")) {
340 AliError(Form("%s: Collection %s does not contain AliAODMCParticle objects!", GetName(), fMCParticlesName.Data()));
345 fMCParticlesMap = dynamic_cast<AliNamedArrayI*>(InputEvent()->FindListObject(fMCParticlesName + "_Map"));
347 if (!fMCParticlesMap) {
348 AliWarning(Form("%s: Could not retrieve map for MC particles %s! Will assume MC labels consistent with indexes...", GetName(), fMCParticlesName.Data()));
349 fMCParticlesMap = new AliNamedArrayI(fMCParticlesName + "_Map", 99999);
350 for (Int_t i = 0; i < 99999; i++) {
351 fMCParticlesMap->AddAt(i,i);
356 if (!fOutMCParticles) {
357 fOutMCParticlesName = fMCParticlesName;
359 fOutMCParticlesName += fSuffix;
360 if (fCopyArray || !fMCParticles) {
362 fOutMCParticles = new TClonesArray("AliAODMCParticle");
363 fOutMCParticles->SetName(fOutMCParticlesName);
364 if (InputEvent()->FindListObject(fOutMCParticlesName)) {
365 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutMCParticlesName.Data()));
369 InputEvent()->AddObject(fOutMCParticles);
372 fOutMCParticlesMap = new AliNamedArrayI(fOutMCParticlesName + "_Map",99999);
373 if (InputEvent()->FindListObject(fOutMCParticlesName + "_Map")) {
374 AliFatal(Form("%s: Map %s_Map is already present in the event!", GetName(), fOutMCParticlesName.Data()));
378 InputEvent()->AddObject(fOutMCParticlesMap);
382 fOutMCParticles = fMCParticles;
383 fOutMCParticlesMap = fMCParticlesMap;
389 if (fGeomName.Length() > 0) {
390 fGeom = AliEMCALGeometry::GetInstance(fGeomName);
392 AliFatal(Form("Could not get geometry with name %s!", fGeomName.Data()));
396 fGeom = AliEMCALGeometry::GetInstance();
398 AliFatal("Could not get default geometry!");
407 //________________________________________________________________________
408 Int_t AliJetModelBaseTask::SetNumberOfOutCells(Int_t n)
410 if (fOutCaloCells->GetNumberOfCells() < n) {
411 fOutCaloCells->DeleteContainer();
412 fOutCaloCells->CreateContainer(n);
415 fOutCaloCells->SetNumberOfCells(n);
423 //________________________________________________________________________
424 Int_t AliJetModelBaseTask::AddCell(Double_t e, Double_t eta, Double_t phi)
426 // Add a cell to the event.
429 if (eta < -100 || phi < 0) {
430 GetRandomCell(eta, phi, absId);
433 fGeom->EtaPhiFromIndex(absId, eta, phi);
437 AliWarning(Form("Unable to embed cell in eta = %f, phi = %f!"
438 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
439 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
444 Double_t pt = GetRandomPt();
445 TLorentzVector nPart;
446 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
450 return AddCell(e, absId);
453 //________________________________________________________________________
454 Int_t AliJetModelBaseTask::AddCell(Double_t e, Int_t absId, Double_t time, Int_t label)
456 // Add a cell to the event.
461 label += fMarkMC + fMCLabelShift;
465 pos = fCaloCells->GetCellPosition(absId);
468 Bool_t increaseOnSuccess = kFALSE;
471 increaseOnSuccess = kTRUE;
475 Short_t cellNumber = -1;
477 Double_t old_time = 0;
479 Double_t old_efrac = 0;
480 fOutCaloCells->GetCell(pos, cellNumber, old_e, old_time, old_label, old_efrac);
482 efrac = e / (old_e + e);
484 if (old_label > 0 && e < old_efrac * old_e) {
493 Bool_t r = fOutCaloCells->SetCell(pos, absId, e, time, label, efrac);
496 if (increaseOnSuccess)
504 //________________________________________________________________________
505 AliVCluster* AliJetModelBaseTask::AddCluster(AliVCluster *oc)
507 // Add a cluster to the event.
509 const Int_t nClusters = fOutClusters->GetEntriesFast();
510 AliVCluster *dc = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
511 dc->SetType(AliVCluster::kEMCALClusterv1);
513 Float_t pos[3] = {0};
514 oc->GetPosition(pos);
515 dc->SetPosition(pos);
516 dc->SetNCells(oc->GetNCells());
517 dc->SetCellsAbsId(oc->GetCellsAbsId());
518 dc->SetCellsAmplitudeFraction(oc->GetCellsAmplitudeFraction());
519 dc->SetID(oc->GetID());
520 dc->SetDispersion(oc->GetDispersion());
521 dc->SetEmcCpvDistance(-1);
523 dc->SetTOF(oc->GetTOF()); //time-of-flight
524 dc->SetNExMax(oc->GetNExMax()); //number of local maxima
525 dc->SetM02(oc->GetM02());
526 dc->SetM20(oc->GetM20());
527 dc->SetDistanceToBadChannel(oc->GetDistanceToBadChannel());
530 UInt_t nlabels = oc->GetNLabels();
531 Int_t *labels = oc->GetLabels();
533 if (nlabels != 0 && labels)
534 parents.Set(nlabels, labels);
541 if (fMarkMC+fMCLabelShift != 0) {
542 for (UInt_t i = 0; i < nlabels; i++) {
543 parents[i] += fMarkMC+fMCLabelShift;
547 AliESDCaloCluster *esdClus = dynamic_cast<AliESDCaloCluster*>(dc);
549 esdClus->AddLabels(parents);
552 AliAODCaloCluster *aodClus = dynamic_cast<AliAODCaloCluster*>(dc);
554 aodClus->SetLabel(parents.GetArray(), nlabels);
560 //________________________________________________________________________
561 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Double_t eta, Double_t phi, Int_t label)
563 // Add a cluster to the event.
566 if (eta < -100 || phi < 0) {
567 GetRandomCell(eta, phi, absId);
570 fGeom->EtaPhiFromIndex(absId, eta, phi);
574 AliWarning(Form("Unable to embed cluster in eta = %f, phi = %f!"
575 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
576 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
581 Double_t pt = GetRandomPt();
582 TLorentzVector nPart;
583 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
587 return AddCluster(e, absId, label);
590 //________________________________________________________________________
591 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Int_t absId, Int_t label)
593 // Add a cluster to the event.
595 const Int_t nClusters = fOutClusters->GetEntriesFast();
597 TClonesArray digits("AliEMCALDigit", 1);
599 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(digits.New(0));
601 digit->SetIndexInList(0);
602 digit->SetType(AliEMCALDigit::kHG);
603 digit->SetAmplitude(e);
605 AliEMCALRecPoint recPoint("");
606 recPoint.AddDigit(*digit, e, kFALSE);
607 recPoint.EvalGlobalPosition(0, &digits);
610 recPoint.GetGlobalPosition(gpos);
614 AliVCluster *cluster = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
615 cluster->SetType(AliVCluster::kEMCALClusterv1);
616 cluster->SetE(recPoint.GetEnergy());
617 cluster->SetPosition(g);
618 cluster->SetNCells(1);
619 UShort_t shortAbsId = absId;
620 cluster->SetCellsAbsId(&shortAbsId);
621 Double32_t fract = 1;
622 cluster->SetCellsAmplitudeFraction(&fract);
623 cluster->SetID(nClusters);
624 cluster->SetEmcCpvDistance(-1);
630 label += fMarkMC+fMCLabelShift;
633 AliESDCaloCluster *esdClus = static_cast<AliESDCaloCluster*>(cluster);
634 TArrayI parents(1, &label);
635 esdClus->AddLabels(parents);
638 AliAODCaloCluster *aodClus = static_cast<AliAODCaloCluster*>(cluster);
639 aodClus->SetLabel(&label, 1);
645 //________________________________________________________________________
646 AliPicoTrack* AliJetModelBaseTask::AddTrack(Double_t pt, Double_t eta, Double_t phi, Byte_t type, Double_t etaemc, Double_t phiemc, Double_t ptemc, Bool_t ise, Int_t label, Short_t charge)
648 // Add a track to the event.
650 const Int_t nTracks = fOutTracks->GetEntriesFast();
655 eta = GetRandomEta();
657 phi = GetRandomPhi();
660 label += fMarkMC+fMCLabelShift;
662 label -= fMarkMC+fMCLabelShift;
664 AliPicoTrack *track = new ((*fOutTracks)[nTracks]) AliPicoTrack(pt,
678 //________________________________________________________________________
679 AliAODMCParticle* AliJetModelBaseTask::AddMCParticle(AliAODMCParticle *part, Int_t origIndex)
681 const Int_t nPart = fOutMCParticles->GetEntriesFast();
683 AliAODMCParticle *aodpart = new ((*fOutMCParticles)[nPart]) AliAODMCParticle(*part);
685 if (origIndex + fMCLabelShift >= fOutMCParticlesMap->GetSize())
686 fOutMCParticlesMap->Set((origIndex + fMCLabelShift)*2);
688 fOutMCParticlesMap->AddAt(nPart, origIndex + fMCLabelShift);
689 AliDebug(2, Form("Setting bin %d to %d (fMCLabelShift=%d, origIndex=%d)",
690 origIndex + fMCLabelShift, fOutMCParticlesMap->At(origIndex + fMCLabelShift), fMCLabelShift, origIndex));
695 //________________________________________________________________________
696 void AliJetModelBaseTask::CopyCells()
703 for (Short_t i = 0; i < fCaloCells->GetNumberOfCells(); i++) {
707 Short_t cellNum = -1;
710 fCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
715 fOutCaloCells->SetCell(i, cellNum, amp, time, mclabel, efrac);
719 AliDebug(2, Form("%d cells from the current event", fAddedCells));
722 //________________________________________________________________________
723 void AliJetModelBaseTask::CopyClusters()
725 // Copy all the clusters in the new collection
729 const Int_t nClusters = fClusters->GetEntriesFast();
730 Int_t nCopiedClusters = 0;
733 for (Int_t i = 0; i < nClusters; ++i) {
734 AliESDCaloCluster *esdcluster = static_cast<AliESDCaloCluster*>(fClusters->At(i));
735 if (!esdcluster || !esdcluster->IsEMCAL())
737 AliESDCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliESDCaloCluster(*esdcluster);
739 TArrayI *labels = clus->GetLabelsArray();
747 for (Int_t i = 0; i < nClusters; ++i) {
748 AliAODCaloCluster *aodcluster = static_cast<AliAODCaloCluster*>(fClusters->At(i));
749 if (!aodcluster || !aodcluster->IsEMCAL())
751 AliAODCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliAODCaloCluster(*aodcluster);
759 //________________________________________________________________________
760 void AliJetModelBaseTask::CopyTracks()
762 // Copy all the tracks in the new collection
767 const Int_t nTracks = fTracks->GetEntriesFast();
768 Int_t nCopiedTracks = 0;
769 for (Int_t i = 0; i < nTracks; ++i) {
770 AliPicoTrack *picotrack = static_cast<AliPicoTrack*>(fTracks->At(i));
773 AliPicoTrack *track = new ((*fOutTracks)[nCopiedTracks]) AliPicoTrack(*picotrack);
774 if (!fIsMC && track->GetLabel() != 0)
780 //________________________________________________________________________
781 void AliJetModelBaseTask::CopyMCParticles()
783 // Copy all the MC particles in the new collection
788 const Int_t nPart = fMCParticles->GetEntriesFast();
789 Int_t nCopiedPart = 0;
790 for (Int_t i = 0; i < nPart; ++i) {
791 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fMCParticles->At(i));
794 new ((*fOutMCParticles)[nCopiedPart]) AliAODMCParticle(*part);
799 if (!fMCParticlesMap || !fOutMCParticlesMap)
802 if (fOutMCParticlesMap->GetSize() < fMCParticlesMap->GetSize())
803 fOutMCParticlesMap->Set(fMCParticlesMap->GetSize() * 2);
805 for (Int_t i = 0; i < fMCParticlesMap->GetSize(); i++) {
806 fOutMCParticlesMap->AddAt(fMCParticlesMap->At(i), i);
807 if (fMCParticlesMap->At(i) >= 0)
811 AliDebug(2,Form("MC particles copied. fMCLabelShift=%d",fMCLabelShift));
814 //________________________________________________________________________
815 void AliJetModelBaseTask::GetRandomCell(Double_t &eta, Double_t &phi, Int_t &absId)
820 Double_t rndEta = eta;
821 Double_t rndPhi = phi;
824 rndEta = GetRandomEta(kTRUE);
826 rndPhi = GetRandomPhi(kTRUE);
827 fGeom->GetAbsCellIdFromEtaPhi(rndEta, rndPhi, absId);
829 } while (absId == -1 && repeats < 100);
832 AliWarning(Form("Could not extract random cluster! Random eta-phi extracted more than 100 times!\n"
833 "eta [%f, %f], phi [%f, %f]\n", fEtaMin, fEtaMax, fPhiMin, fPhiMax));
841 //________________________________________________________________________
842 Double_t AliJetModelBaseTask::GetRandomEta(Bool_t emcal)
846 Double_t etamax = fEtaMax;
847 Double_t etamin = fEtaMin;
850 const Double_t EmcalMinEta = fGeom->GetArm1EtaMin();
851 const Double_t EmcalMaxEta = fGeom->GetArm1EtaMax();
853 if (etamax > EmcalMaxEta) etamax = EmcalMaxEta;
854 if (etamax < EmcalMinEta) etamax = EmcalMinEta;
855 if (etamin > EmcalMaxEta) etamin = EmcalMaxEta;
856 if (etamin < EmcalMinEta) etamin = EmcalMinEta;
859 return gRandom->Rndm() * (etamax - etamin) + etamin;
862 //________________________________________________________________________
863 Double_t AliJetModelBaseTask::GetRandomPhi(Bool_t emcal)
867 Double_t phimax = fPhiMax;
868 Double_t phimin = fPhiMin;
871 const Double_t EmcalMinPhi = fGeom->GetArm1PhiMin() * TMath::DegToRad();
872 const Double_t EmcalMaxPhi = fGeom->GetArm1PhiMax() * TMath::DegToRad();
874 if (phimax > EmcalMaxPhi) phimax = EmcalMaxPhi;
875 if (phimax < EmcalMinPhi) phimax = EmcalMinPhi;
876 if (phimin > EmcalMaxPhi) phimin = EmcalMaxPhi;
877 if (phimin < EmcalMinPhi) phimin = EmcalMinPhi;
880 return gRandom->Rndm() * (phimax - phimin) + phimin;
883 //________________________________________________________________________
884 Double_t AliJetModelBaseTask::GetRandomPt()
889 return fPtSpectrum->GetRandom();
891 return gRandom->Rndm() * (fPtMax - fPtMin) + fPtMin;
894 //________________________________________________________________________
895 void AliJetModelBaseTask::Run()