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", 9999);
342 for (Int_t i = 0; i < 9999; 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",9999);
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 += 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 && labels[0] >= 0) {
526 AliESDCaloCluster *esdClus = dynamic_cast<AliESDCaloCluster*>(dc);
528 TArrayI parents(nlabels, labels);
529 esdClus->AddLabels(parents);
532 AliAODCaloCluster *aodClus = dynamic_cast<AliAODCaloCluster*>(dc);
534 aodClus->SetLabel(labels, nlabels);
537 else if (fMarkMC != 0) {
538 AliESDCaloCluster *esdClus = dynamic_cast<AliESDCaloCluster*>(dc);
540 TArrayI parents(1, &fMarkMC);
541 esdClus->AddLabels(parents);
544 AliAODCaloCluster *aodClus = dynamic_cast<AliAODCaloCluster*>(dc);
546 aodClus->SetLabel(&fMarkMC,1);
553 //________________________________________________________________________
554 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Double_t eta, Double_t phi, Int_t label)
556 // Add a cluster to the event.
559 if (eta < -100 || phi < 0) {
560 GetRandomCell(eta, phi, absId);
563 fGeom->EtaPhiFromIndex(absId, eta, phi);
567 AliWarning(Form("Unable to embed cluster in eta = %f, phi = %f!"
568 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
569 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
574 Double_t pt = GetRandomPt();
575 TLorentzVector nPart;
576 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
580 return AddCluster(e, absId, label);
583 //________________________________________________________________________
584 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Int_t absId, Int_t label)
586 // Add a cluster to the event.
588 const Int_t nClusters = fOutClusters->GetEntriesFast();
590 TClonesArray digits("AliEMCALDigit", 1);
592 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(digits.New(0));
594 digit->SetIndexInList(0);
595 digit->SetType(AliEMCALDigit::kHG);
596 digit->SetAmplitude(e);
598 AliEMCALRecPoint recPoint("");
599 recPoint.AddDigit(*digit, e, kFALSE);
600 recPoint.EvalGlobalPosition(0, &digits);
603 recPoint.GetGlobalPosition(gpos);
607 AliVCluster *cluster = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
608 cluster->SetType(AliVCluster::kEMCALClusterv1);
609 cluster->SetE(recPoint.GetEnergy());
610 cluster->SetPosition(g);
611 cluster->SetNCells(1);
612 UShort_t shortAbsId = absId;
613 cluster->SetCellsAbsId(&shortAbsId);
614 Double32_t fract = 1;
615 cluster->SetCellsAmplitudeFraction(&fract);
616 cluster->SetID(nClusters);
617 cluster->SetEmcCpvDistance(-1);
623 label += fMCLabelShift;
626 AliESDCaloCluster *esdClus = static_cast<AliESDCaloCluster*>(cluster);
627 TArrayI parents(1, &label);
628 esdClus->AddLabels(parents);
631 AliAODCaloCluster *aodClus = static_cast<AliAODCaloCluster*>(cluster);
632 aodClus->SetLabel(&label, 1);
638 //________________________________________________________________________
639 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)
641 // Add a track to the event.
643 const Int_t nTracks = fOutTracks->GetEntriesFast();
648 eta = GetRandomEta();
650 phi = GetRandomPhi();
655 label += fMCLabelShift;
657 AliPicoTrack *track = new ((*fOutTracks)[nTracks]) AliPicoTrack(pt,
670 //________________________________________________________________________
671 AliAODMCParticle* AliJetModelBaseTask::AddMCParticle(AliAODMCParticle *part, Int_t origIndex)
673 const Int_t nPart = fOutMCParticles->GetEntriesFast();
675 AliAODMCParticle *aodpart = new ((*fOutMCParticles)[nPart]) AliAODMCParticle(*part);
677 fOutMCParticlesMap->AddAt(nPart, origIndex + fMCLabelShift);
678 AliDebug(2, Form("Setting bin %d to %d (fMCLabelShift=%d, origIndex=%d)",
679 origIndex + fMCLabelShift, fOutMCParticlesMap->At(origIndex + fMCLabelShift), fMCLabelShift, origIndex));
684 //________________________________________________________________________
685 void AliJetModelBaseTask::CopyCells()
692 for (Short_t i = 0; i < fCaloCells->GetNumberOfCells(); i++) {
696 Short_t cellNum = -1;
699 fCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
704 fOutCaloCells->SetCell(i, cellNum, amp, time, mclabel, efrac);
708 AliDebug(2, Form("%d cells from the current event", fAddedCells));
711 //________________________________________________________________________
712 void AliJetModelBaseTask::CopyClusters()
714 // Copy all the clusters in the new collection
718 const Int_t nClusters = fClusters->GetEntriesFast();
719 Int_t nCopiedClusters = 0;
722 for (Int_t i = 0; i < nClusters; ++i) {
723 AliESDCaloCluster *esdcluster = static_cast<AliESDCaloCluster*>(fClusters->At(i));
724 if (!esdcluster || !esdcluster->IsEMCAL())
726 AliESDCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliESDCaloCluster(*esdcluster);
728 TArrayI *labels = clus->GetLabelsArray();
736 for (Int_t i = 0; i < nClusters; ++i) {
737 AliAODCaloCluster *aodcluster = static_cast<AliAODCaloCluster*>(fClusters->At(i));
738 if (!aodcluster || !aodcluster->IsEMCAL())
740 AliAODCaloCluster *clus = new ((*fOutClusters)[nCopiedClusters]) AliAODCaloCluster(*aodcluster);
748 //________________________________________________________________________
749 void AliJetModelBaseTask::CopyTracks()
751 // Copy all the tracks in the new collection
756 const Int_t nTracks = fTracks->GetEntriesFast();
757 Int_t nCopiedTracks = 0;
758 for (Int_t i = 0; i < nTracks; ++i) {
759 AliPicoTrack *picotrack = static_cast<AliPicoTrack*>(fTracks->At(i));
762 AliPicoTrack *track = new ((*fOutTracks)[nCopiedTracks]) AliPicoTrack(*picotrack);
763 if (!fIsMC && track->GetLabel() != 0)
769 //________________________________________________________________________
770 void AliJetModelBaseTask::CopyMCParticles()
772 // Copy all the MC particles in the new collection
777 const Int_t nPart = fMCParticles->GetEntriesFast();
778 Int_t nCopiedPart = 0;
779 for (Int_t i = 0; i < nPart; ++i) {
780 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fMCParticles->At(i));
783 new ((*fOutMCParticles)[nCopiedPart]) AliAODMCParticle(*part);
788 if (!fMCParticlesMap)
791 for (Int_t i = 0; i < fMCParticlesMap->GetSize(); i++) {
792 fOutMCParticlesMap->AddAt(fMCParticlesMap->At(i), i);
793 if (fMCParticlesMap->At(i) >= 0)
797 AliDebug(2,Form("MC particles copied. fMCLabelShift=%d",fMCLabelShift));
800 //________________________________________________________________________
801 void AliJetModelBaseTask::GetRandomCell(Double_t &eta, Double_t &phi, Int_t &absId)
806 Double_t rndEta = eta;
807 Double_t rndPhi = phi;
810 rndEta = GetRandomEta(kTRUE);
812 rndPhi = GetRandomPhi(kTRUE);
813 fGeom->GetAbsCellIdFromEtaPhi(rndEta, rndPhi, absId);
815 } while (absId == -1 && repeats < 100);
818 AliWarning(Form("Could not extract random cluster! Random eta-phi extracted more than 100 times!\n"
819 "eta [%f, %f], phi [%f, %f]\n", fEtaMin, fEtaMax, fPhiMin, fPhiMax));
827 //________________________________________________________________________
828 Double_t AliJetModelBaseTask::GetRandomEta(Bool_t emcal)
832 Double_t etamax = fEtaMax;
833 Double_t etamin = fEtaMin;
836 const Double_t EmcalMinEta = fGeom->GetArm1EtaMin();
837 const Double_t EmcalMaxEta = fGeom->GetArm1EtaMax();
839 if (etamax > EmcalMaxEta) etamax = EmcalMaxEta;
840 if (etamax < EmcalMinEta) etamax = EmcalMinEta;
841 if (etamin > EmcalMaxEta) etamin = EmcalMaxEta;
842 if (etamin < EmcalMinEta) etamin = EmcalMinEta;
845 return gRandom->Rndm() * (etamax - etamin) + etamin;
848 //________________________________________________________________________
849 Double_t AliJetModelBaseTask::GetRandomPhi(Bool_t emcal)
853 Double_t phimax = fPhiMax;
854 Double_t phimin = fPhiMin;
857 const Double_t EmcalMinPhi = fGeom->GetArm1PhiMin() * TMath::DegToRad();
858 const Double_t EmcalMaxPhi = fGeom->GetArm1PhiMax() * TMath::DegToRad();
860 if (phimax > EmcalMaxPhi) phimax = EmcalMaxPhi;
861 if (phimax < EmcalMinPhi) phimax = EmcalMinPhi;
862 if (phimin > EmcalMaxPhi) phimin = EmcalMaxPhi;
863 if (phimin < EmcalMinPhi) phimin = EmcalMinPhi;
866 return gRandom->Rndm() * (phimax - phimin) + phimin;
869 //________________________________________________________________________
870 Double_t AliJetModelBaseTask::GetRandomPt()
875 return fPtSpectrum->GetRandom();
877 return gRandom->Rndm() * (fPtMax - fPtMin) + fPtMin;
880 //________________________________________________________________________
881 void AliJetModelBaseTask::Run()