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();
163 fOutTracks->Delete();
165 fOutClusters->Delete();
167 fOutMCParticles->Delete();
171 if (fOutMCParticlesMap)
172 fOutMCParticlesMap->Clear();
174 AliVCaloCells *tempCaloCells = 0;
179 tempCaloCells = fCaloCells;
180 fCaloCells = static_cast<AliVCaloCells*>(tempCaloCells->Clone(Form("%s_old",fCaloCells->GetName())));
186 if (fCaloCells && !fCopyArray) {
188 fCaloCells = tempCaloCells;
192 //________________________________________________________________________
193 Bool_t AliJetModelBaseTask::ExecOnce()
198 gRandom = new TRandom3(0);
200 fEsdMode = InputEvent()->InheritsFrom("AliESDEvent");
202 if (fPtMax < fPtMin) {
203 AliWarning (Form("PtMax (%f) < PtMin (%f), setting PtMax = PtMin = %f", fPtMax, fPtMin, fPtMin));
207 if (fEtaMax < fEtaMin) {
208 AliWarning (Form("EtaMax (%f) < EtaMin (%f), setting EtaMax = EtaMin = %f", fEtaMax, fEtaMin, fEtaMin));
212 if (fPhiMax < fPhiMin) {
213 AliWarning (Form("PhiMax (%f) < PhiMin (%f), setting PhiMax = PhiMin = %f", fPhiMax, fPhiMin, fPhiMin));
217 if (!fCellsName.IsNull()) {
218 fCaloCells = dynamic_cast<AliVCaloCells*>(InputEvent()->FindListObject(fCellsName));
220 AliWarning(Form("%s: Couldn't retrieve calo cells with name %s!", GetName(), fCellsName.Data()));
222 else if (!fCaloCells->InheritsFrom("AliVCaloCells")) {
223 AliError(Form("%s: Collection %s does not contain a AliVCaloCells object!", GetName(), fCellsName.Data()));
228 if (!fOutCaloCells) {
229 fOutCellsName = fCellsName;
231 fOutCellsName += fSuffix;
232 if (fCopyArray || !fCaloCells) {
234 fOutCaloCells = new AliESDCaloCells(fOutCellsName,fOutCellsName);
236 fOutCaloCells = new AliAODCaloCells(fOutCellsName,fOutCellsName);
238 if (InputEvent()->FindListObject(fOutCellsName)) {
239 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCellsName.Data()));
243 InputEvent()->AddObject(fOutCaloCells);
247 fOutCaloCells = fCaloCells;
252 if (fNTracks > 0 && !fTracksName.IsNull()) {
253 fTracks = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fTracksName));
255 AliWarning(Form("%s: Couldn't retrieve tracks with name %s!", GetName(), fTracksName.Data()));
257 else if (!fTracks->GetClass()->GetBaseClass("AliPicoTrack")) {
258 AliError(Form("%s: Collection %s does not contain AliPicoTrack objects!", GetName(), fTracksName.Data()));
264 fOutTracksName = fTracksName;
266 fOutTracksName += fSuffix;
267 if (fCopyArray || !fTracks) {
268 fOutTracks = new TClonesArray("AliPicoTrack");
269 fOutTracks->SetName(fOutTracksName);
270 if (InputEvent()->FindListObject(fOutTracksName)) {
271 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutTracksName.Data()));
275 InputEvent()->AddObject(fOutTracks);
279 fOutTracks = fTracks;
284 if (fNClusters > 0 && !fCaloName.IsNull()) {
285 fClusters = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fCaloName));
288 AliWarning(Form("%s: Couldn't retrieve clusters with name %s!", GetName(), fCaloName.Data()));
290 else if (!fClusters->GetClass()->GetBaseClass("AliVCluster")) {
291 AliError(Form("%s: Collection %s does not contain AliVCluster objects!", GetName(), fCaloName.Data()));
297 fOutCaloName = fCaloName;
299 fOutCaloName += fSuffix;
302 className = fClusters->GetClass()->GetName();
304 className = "AliESDCaloCluster";
306 className = "AliAODCaloCluster";
308 if (fCopyArray || !fClusters) {
309 fOutClusters = new TClonesArray(className.Data());
310 fOutClusters->SetName(fOutCaloName);
311 if (InputEvent()->FindListObject(fOutCaloName)) {
312 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCaloName.Data()));
316 InputEvent()->AddObject(fOutClusters);
320 fOutClusters = fClusters;
325 if (!fMCParticlesName.IsNull()) {
326 fMCParticles = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fMCParticlesName));
328 AliWarning(Form("%s: Couldn't retrieve MC particles with name %s!", GetName(), fMCParticlesName.Data()));
331 if (!fMCParticles->GetClass()->GetBaseClass("AliAODMCParticle")) {
332 AliError(Form("%s: Collection %s does not contain AliAODMCParticle objects!", GetName(), fMCParticlesName.Data()));
337 fMCParticlesMap = dynamic_cast<AliNamedArrayI*>(InputEvent()->FindListObject(fMCParticlesName + "_Map"));
339 if (!fMCParticlesMap) {
340 AliWarning(Form("%s: Could not retrieve map for MC particles %s! Will assume MC labels consistent with indexes...", GetName(), fMCParticlesName.Data()));
341 fMCParticlesMap = new AliNamedArrayI(fMCParticlesName + "_Map", 99999);
342 for (Int_t i = 0; i < 99999; i++) {
343 fMCParticlesMap->AddAt(i,i);
348 if (!fOutMCParticles) {
349 fOutMCParticlesName = fMCParticlesName;
351 fOutMCParticlesName += fSuffix;
352 if (fCopyArray || !fMCParticles) {
354 fOutMCParticles = new TClonesArray("AliAODMCParticle");
355 fOutMCParticles->SetName(fOutMCParticlesName);
356 if (InputEvent()->FindListObject(fOutMCParticlesName)) {
357 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutMCParticlesName.Data()));
361 InputEvent()->AddObject(fOutMCParticles);
364 fOutMCParticlesMap = new AliNamedArrayI(fOutMCParticlesName + "_Map",99999);
365 if (InputEvent()->FindListObject(fOutMCParticlesName + "_Map")) {
366 AliFatal(Form("%s: Map %s_Map is already present in the event!", GetName(), fOutMCParticlesName.Data()));
370 InputEvent()->AddObject(fOutMCParticlesMap);
374 fOutMCParticles = fMCParticles;
375 fOutMCParticlesMap = fMCParticlesMap;
381 if (fGeomName.Length() > 0) {
382 fGeom = AliEMCALGeometry::GetInstance(fGeomName);
384 AliFatal(Form("Could not get geometry with name %s!", fGeomName.Data()));
388 fGeom = AliEMCALGeometry::GetInstance();
390 AliFatal("Could not get default geometry!");
399 //________________________________________________________________________
400 Int_t AliJetModelBaseTask::SetNumberOfOutCells(Int_t n)
402 if (fOutCaloCells->GetNumberOfCells() < n) {
403 fOutCaloCells->DeleteContainer();
404 fOutCaloCells->CreateContainer(n);
407 fOutCaloCells->SetNumberOfCells(n);
415 //________________________________________________________________________
416 Int_t AliJetModelBaseTask::AddCell(Double_t e, Double_t eta, Double_t phi)
418 // Add a cell to the event.
421 if (eta < -100 || phi < 0) {
422 GetRandomCell(eta, phi, absId);
425 fGeom->EtaPhiFromIndex(absId, eta, phi);
429 AliWarning(Form("Unable to embed cell in eta = %f, phi = %f!"
430 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
431 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
436 Double_t pt = GetRandomPt();
437 TLorentzVector nPart;
438 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
442 return AddCell(e, absId);
445 //________________________________________________________________________
446 Int_t AliJetModelBaseTask::AddCell(Double_t e, Int_t absId, Double_t time, Int_t label)
448 // Add a cell to the event.
453 label += fMarkMC + fMCLabelShift;
457 pos = fCaloCells->GetCellPosition(absId);
460 Bool_t increaseOnSuccess = kFALSE;
463 increaseOnSuccess = kTRUE;
467 Short_t cellNumber = -1;
469 Double_t old_time = 0;
471 Double_t old_efrac = 0;
472 fOutCaloCells->GetCell(pos, cellNumber, old_e, old_time, old_label, old_efrac);
474 efrac = e / (old_e + e);
476 if (old_label > 0 && e < old_efrac * old_e) {
485 Bool_t r = fOutCaloCells->SetCell(pos, absId, e, time, label, efrac);
488 if (increaseOnSuccess)
496 //________________________________________________________________________
497 AliVCluster* AliJetModelBaseTask::AddCluster(AliVCluster *oc)
499 // Add a cluster to the event.
501 const Int_t nClusters = fOutClusters->GetEntriesFast();
502 AliVCluster *dc = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
503 dc->SetType(AliVCluster::kEMCALClusterv1);
505 Float_t pos[3] = {0};
506 oc->GetPosition(pos);
507 dc->SetPosition(pos);
508 dc->SetNCells(oc->GetNCells());
509 dc->SetCellsAbsId(oc->GetCellsAbsId());
510 dc->SetCellsAmplitudeFraction(oc->GetCellsAmplitudeFraction());
511 dc->SetID(oc->GetID());
512 dc->SetDispersion(oc->GetDispersion());
513 dc->SetEmcCpvDistance(-1);
515 dc->SetTOF(oc->GetTOF()); //time-of-flight
516 dc->SetNExMax(oc->GetNExMax()); //number of local maxima
517 dc->SetM02(oc->GetM02());
518 dc->SetM20(oc->GetM20());
519 dc->SetDistanceToBadChannel(oc->GetDistanceToBadChannel());
522 UInt_t nlabels = oc->GetNLabels();
523 Int_t *labels = oc->GetLabels();
525 if (nlabels != 0 && labels)
526 parents.Set(nlabels, labels);
533 if (fMarkMC+fMCLabelShift != 0) {
534 for (UInt_t i = 0; i < nlabels; i++) {
535 parents[i] += fMarkMC+fMCLabelShift;
539 AliESDCaloCluster *esdClus = dynamic_cast<AliESDCaloCluster*>(dc);
541 esdClus->AddLabels(parents);
544 AliAODCaloCluster *aodClus = dynamic_cast<AliAODCaloCluster*>(dc);
546 aodClus->SetLabel(parents.GetArray(), nlabels);
552 //________________________________________________________________________
553 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Double_t eta, Double_t phi, Int_t label)
555 // Add a cluster to the event.
558 if (eta < -100 || phi < 0) {
559 GetRandomCell(eta, phi, absId);
562 fGeom->EtaPhiFromIndex(absId, eta, phi);
566 AliWarning(Form("Unable to embed cluster in eta = %f, phi = %f!"
567 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
568 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
573 Double_t pt = GetRandomPt();
574 TLorentzVector nPart;
575 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
579 return AddCluster(e, absId, label);
582 //________________________________________________________________________
583 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Int_t absId, Int_t label)
585 // Add a cluster to the event.
587 const Int_t nClusters = fOutClusters->GetEntriesFast();
589 TClonesArray digits("AliEMCALDigit", 1);
591 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(digits.New(0));
593 digit->SetIndexInList(0);
594 digit->SetType(AliEMCALDigit::kHG);
595 digit->SetAmplitude(e);
597 AliEMCALRecPoint recPoint("");
598 recPoint.AddDigit(*digit, e, kFALSE);
599 recPoint.EvalGlobalPosition(0, &digits);
602 recPoint.GetGlobalPosition(gpos);
606 AliVCluster *cluster = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
607 cluster->SetType(AliVCluster::kEMCALClusterv1);
608 cluster->SetE(recPoint.GetEnergy());
609 cluster->SetPosition(g);
610 cluster->SetNCells(1);
611 UShort_t shortAbsId = absId;
612 cluster->SetCellsAbsId(&shortAbsId);
613 Double32_t fract = 1;
614 cluster->SetCellsAmplitudeFraction(&fract);
615 cluster->SetID(nClusters);
616 cluster->SetEmcCpvDistance(-1);
622 label += fMarkMC+fMCLabelShift;
625 AliESDCaloCluster *esdClus = static_cast<AliESDCaloCluster*>(cluster);
626 TArrayI parents(1, &label);
627 esdClus->AddLabels(parents);
630 AliAODCaloCluster *aodClus = static_cast<AliAODCaloCluster*>(cluster);
631 aodClus->SetLabel(&label, 1);
637 //________________________________________________________________________
638 AliPicoTrack* AliJetModelBaseTask::AddTrack(Double_t pt, Double_t eta, Double_t phi, Byte_t type, Double_t etaemc, Double_t phiemc, Bool_t ise, Int_t label)
640 // Add a track to the event.
642 const Int_t nTracks = fOutTracks->GetEntriesFast();
647 eta = GetRandomEta();
649 phi = GetRandomPhi();
652 label += fMarkMC+fMCLabelShift;
654 label -= fMarkMC+fMCLabelShift;
656 AliPicoTrack *track = new ((*fOutTracks)[nTracks]) AliPicoTrack(pt,
669 //________________________________________________________________________
670 AliAODMCParticle* AliJetModelBaseTask::AddMCParticle(AliAODMCParticle *part, Int_t origIndex)
672 const Int_t nPart = fOutMCParticles->GetEntriesFast();
674 AliAODMCParticle *aodpart = new ((*fOutMCParticles)[nPart]) AliAODMCParticle(*part);
676 if (origIndex + fMCLabelShift >= fOutMCParticlesMap->GetSize())
677 fOutMCParticlesMap->Set((origIndex + fMCLabelShift)*2);
679 fOutMCParticlesMap->AddAt(nPart, origIndex + fMCLabelShift);
680 AliDebug(2, Form("Setting bin %d to %d (fMCLabelShift=%d, origIndex=%d)",
681 origIndex + fMCLabelShift, fOutMCParticlesMap->At(origIndex + fMCLabelShift), fMCLabelShift, origIndex));
686 //________________________________________________________________________
687 void AliJetModelBaseTask::CopyCells()
694 for (Short_t i = 0; i < fCaloCells->GetNumberOfCells(); i++) {
698 Short_t cellNum = -1;
701 fCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
706 fOutCaloCells->SetCell(i, cellNum, amp, time, mclabel, efrac);
710 AliDebug(2, Form("%d cells from the current event", fAddedCells));
713 //________________________________________________________________________
714 void AliJetModelBaseTask::CopyClusters()
716 // Copy all the clusters in the new collection
720 const Int_t nClusters = fClusters->GetEntriesFast();
721 Int_t nCopiedClusters = 0;
724 for (Int_t i = 0; i < nClusters; ++i) {
725 AliESDCaloCluster *esdcluster = static_cast<AliESDCaloCluster*>(fClusters->At(i));
726 if (!esdcluster || !esdcluster->IsEMCAL())
728 AliESDCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliESDCaloCluster(*esdcluster);
730 TArrayI *labels = clus->GetLabelsArray();
738 for (Int_t i = 0; i < nClusters; ++i) {
739 AliAODCaloCluster *aodcluster = static_cast<AliAODCaloCluster*>(fClusters->At(i));
740 if (!aodcluster || !aodcluster->IsEMCAL())
742 AliAODCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliAODCaloCluster(*aodcluster);
750 //________________________________________________________________________
751 void AliJetModelBaseTask::CopyTracks()
753 // Copy all the tracks in the new collection
758 const Int_t nTracks = fTracks->GetEntriesFast();
759 Int_t nCopiedTracks = 0;
760 for (Int_t i = 0; i < nTracks; ++i) {
761 AliPicoTrack *picotrack = static_cast<AliPicoTrack*>(fTracks->At(i));
764 AliPicoTrack *track = new ((*fOutTracks)[nCopiedTracks]) AliPicoTrack(*picotrack);
765 if (!fIsMC && track->GetLabel() != 0)
771 //________________________________________________________________________
772 void AliJetModelBaseTask::CopyMCParticles()
774 // Copy all the MC particles in the new collection
779 const Int_t nPart = fMCParticles->GetEntriesFast();
780 Int_t nCopiedPart = 0;
781 for (Int_t i = 0; i < nPart; ++i) {
782 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fMCParticles->At(i));
785 new ((*fOutMCParticles)[nCopiedPart]) AliAODMCParticle(*part);
790 if (!fMCParticlesMap || !fOutMCParticlesMap)
793 if (fOutMCParticlesMap->GetSize() < fMCParticlesMap->GetSize())
794 fOutMCParticlesMap->Set(fMCParticlesMap->GetSize() * 2);
796 for (Int_t i = 0; i < fMCParticlesMap->GetSize(); i++) {
797 fOutMCParticlesMap->AddAt(fMCParticlesMap->At(i), i);
798 if (fMCParticlesMap->At(i) >= 0)
802 AliDebug(2,Form("MC particles copied. fMCLabelShift=%d",fMCLabelShift));
805 //________________________________________________________________________
806 void AliJetModelBaseTask::GetRandomCell(Double_t &eta, Double_t &phi, Int_t &absId)
811 Double_t rndEta = eta;
812 Double_t rndPhi = phi;
815 rndEta = GetRandomEta(kTRUE);
817 rndPhi = GetRandomPhi(kTRUE);
818 fGeom->GetAbsCellIdFromEtaPhi(rndEta, rndPhi, absId);
820 } while (absId == -1 && repeats < 100);
823 AliWarning(Form("Could not extract random cluster! Random eta-phi extracted more than 100 times!\n"
824 "eta [%f, %f], phi [%f, %f]\n", fEtaMin, fEtaMax, fPhiMin, fPhiMax));
832 //________________________________________________________________________
833 Double_t AliJetModelBaseTask::GetRandomEta(Bool_t emcal)
837 Double_t etamax = fEtaMax;
838 Double_t etamin = fEtaMin;
841 const Double_t EmcalMinEta = fGeom->GetArm1EtaMin();
842 const Double_t EmcalMaxEta = fGeom->GetArm1EtaMax();
844 if (etamax > EmcalMaxEta) etamax = EmcalMaxEta;
845 if (etamax < EmcalMinEta) etamax = EmcalMinEta;
846 if (etamin > EmcalMaxEta) etamin = EmcalMaxEta;
847 if (etamin < EmcalMinEta) etamin = EmcalMinEta;
850 return gRandom->Rndm() * (etamax - etamin) + etamin;
853 //________________________________________________________________________
854 Double_t AliJetModelBaseTask::GetRandomPhi(Bool_t emcal)
858 Double_t phimax = fPhiMax;
859 Double_t phimin = fPhiMin;
862 const Double_t EmcalMinPhi = fGeom->GetArm1PhiMin() * TMath::DegToRad();
863 const Double_t EmcalMaxPhi = fGeom->GetArm1PhiMax() * TMath::DegToRad();
865 if (phimax > EmcalMaxPhi) phimax = EmcalMaxPhi;
866 if (phimax < EmcalMinPhi) phimax = EmcalMinPhi;
867 if (phimin > EmcalMaxPhi) phimin = EmcalMaxPhi;
868 if (phimin < EmcalMinPhi) phimin = EmcalMinPhi;
871 return gRandom->Rndm() * (phimax - phimin) + phimin;
874 //________________________________________________________________________
875 Double_t AliJetModelBaseTask::GetRandomPt()
880 return fPtSpectrum->GetRandom();
882 return gRandom->Rndm() * (fPtMax - fPtMin) + fPtMin;
885 //________________________________________________________________________
886 void AliJetModelBaseTask::Run()