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"
29 ClassImp(AliJetModelBaseTask)
31 //________________________________________________________________________
32 AliJetModelBaseTask::AliJetModelBaseTask() :
33 AliAnalysisTaskSE("AliJetModelBaseTask"),
42 fOutMCParticlesName(),
47 fPhiMax(TMath::Pi() * 2),
69 fOutMCParticlesMap(0),
74 // Default constructor.
77 //________________________________________________________________________
78 AliJetModelBaseTask::AliJetModelBaseTask(const char *name, Bool_t drawqa) :
79 AliAnalysisTaskSE(name),
81 fTracksName("PicoTracks"),
82 fOutTracksName("PicoTracksEmbedded"),
83 fCaloName("CaloClustersCorr"),
84 fOutCaloName("CaloClustersCorrEmbedded"),
88 fOutMCParticlesName(""),
93 fPhiMax(TMath::Pi() * 2),
115 fOutMCParticlesMap(0),
120 // Standard constructor.
123 DefineOutput(1, TList::Class());
127 //________________________________________________________________________
128 AliJetModelBaseTask::~AliJetModelBaseTask()
133 //________________________________________________________________________
134 void AliJetModelBaseTask::UserCreateOutputObjects()
136 // Create user output.
141 fOutput = new TList();
144 PostData(1, fOutput);
147 //________________________________________________________________________
148 void AliJetModelBaseTask::UserExec(Option_t *)
150 // Execute per event.
153 fIsInit = ExecOnce();
160 fOutTracks->Delete();
162 fOutClusters->Delete();
164 fOutMCParticles->Delete();
167 // Reset name (it is cleared each event by the analysis manager)
168 if (fOutMCParticlesMap) {
169 new (fOutMCParticlesMap) TH1I(fOutMCParticlesName + "_Map", fOutMCParticlesName + "_Map",9999,0,1);
170 fOutMCParticlesMap->TArrayI::Reset(-1);
173 AliVCaloCells *tempCaloCells = 0;
178 tempCaloCells = fCaloCells;
179 fCaloCells = static_cast<AliVCaloCells*>(tempCaloCells->Clone(Form("%s_old",fCaloCells->GetName())));
187 fCaloCells = tempCaloCells;
191 //________________________________________________________________________
192 Bool_t AliJetModelBaseTask::ExecOnce()
197 gRandom = new TRandom3(0);
199 fEsdMode = InputEvent()->InheritsFrom("AliESDEvent");
201 if (fPtMax < fPtMin) {
202 AliWarning (Form("PtMax (%f) < PtMin (%f), setting PtMax = PtMin = %f", fPtMax, fPtMin, fPtMin));
206 if (fEtaMax < fEtaMin) {
207 AliWarning (Form("EtaMax (%f) < EtaMin (%f), setting EtaMax = EtaMin = %f", fEtaMax, fEtaMin, fEtaMin));
211 if (fPhiMax < fPhiMin) {
212 AliWarning (Form("PhiMax (%f) < PhiMin (%f), setting PhiMax = PhiMin = %f", fPhiMax, fPhiMin, fPhiMin));
216 if (!fCellsName.IsNull()) {
217 fCaloCells = dynamic_cast<AliVCaloCells*>(InputEvent()->FindListObject(fCellsName));
219 AliWarning(Form("%s: Couldn't retrieve calo cells with name %s!", GetName(), fCellsName.Data()));
221 else if (!fCaloCells->InheritsFrom("AliVCaloCells")) {
222 AliError(Form("%s: Collection %s does not contain a AliVCaloCells object!", GetName(), fCellsName.Data()));
227 if (!fOutCaloCells) {
228 fOutCellsName = fCellsName;
230 fOutCellsName += fSuffix;
231 if (fCopyArray || !fCaloCells) {
233 fOutCaloCells = new AliESDCaloCells(fOutCellsName,fOutCellsName);
235 fOutCaloCells = new AliAODCaloCells(fOutCellsName,fOutCellsName);
237 if (InputEvent()->FindListObject(fOutCellsName)) {
238 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCellsName.Data()));
242 InputEvent()->AddObject(fOutCaloCells);
246 fOutCaloCells = fCaloCells;
251 if (fNTracks > 0 && !fTracksName.IsNull()) {
252 fTracks = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fTracksName));
254 AliWarning(Form("%s: Couldn't retrieve tracks with name %s!", GetName(), fTracksName.Data()));
256 else if (!fTracks->GetClass()->GetBaseClass("AliPicoTrack")) {
257 AliError(Form("%s: Collection %s does not contain AliPicoTrack objects!", GetName(), fTracksName.Data()));
263 fOutTracksName = fTracksName;
265 fOutTracksName += fSuffix;
266 if (fCopyArray || !fTracks) {
267 fOutTracks = new TClonesArray("AliPicoTrack");
268 fOutTracks->SetName(fOutTracksName);
269 if (InputEvent()->FindListObject(fOutTracksName)) {
270 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutTracksName.Data()));
274 InputEvent()->AddObject(fOutTracks);
278 fOutTracks = fTracks;
283 if (fNClusters > 0 && !fCaloName.IsNull()) {
284 fClusters = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fCaloName));
287 AliWarning(Form("%s: Couldn't retrieve clusters with name %s!", GetName(), fCaloName.Data()));
289 else if (!fClusters->GetClass()->GetBaseClass("AliVCluster")) {
290 AliError(Form("%s: Collection %s does not contain AliVCluster objects!", GetName(), fCaloName.Data()));
296 fOutCaloName = fCaloName;
298 fOutCaloName += fSuffix;
299 if (fCopyArray || !fClusters) {
300 fOutClusters = new TClonesArray(fClusters->GetClass()->GetName(), fClusters->GetSize());
301 fOutClusters->SetName(fOutCaloName);
302 if (InputEvent()->FindListObject(fOutCaloName)) {
303 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCaloName.Data()));
307 InputEvent()->AddObject(fOutClusters);
311 fOutClusters = fClusters;
316 if (!fMCParticlesName.IsNull()) {
317 fMCParticles = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fMCParticlesName));
319 AliWarning(Form("%s: Couldn't retrieve MC particles with name %s!", GetName(), fMCParticlesName.Data()));
322 if (!fMCParticles->GetClass()->GetBaseClass("AliAODMCParticle")) {
323 AliError(Form("%s: Collection %s does not contain AliAODMCParticle objects!", GetName(), fMCParticlesName.Data()));
328 fMCParticlesMap = dynamic_cast<TH1I*>(InputEvent()->FindListObject(fMCParticlesName + "_Map"));
330 if (!fMCParticlesMap) {
331 AliWarning(Form("%s: Could not retrieve map for MC particles %s! Will assume MC labels consistent with indexes...", GetName(), fMCParticlesName.Data()));
332 fMCParticlesMap = new TH1I(fMCParticlesName + "_Map", fMCParticlesName + "_Map",9999,0,1);
333 for (Int_t i = 0; i < 9999; i++) {
334 fMCParticlesMap->SetBinContent(i,i);
339 if (!fOutMCParticles) {
340 fOutMCParticlesName = fMCParticlesName;
342 fOutMCParticlesName += fSuffix;
343 if (fCopyArray || !fMCParticles) {
345 fOutMCParticles = new TClonesArray("AliAODMCParticle");
346 fOutMCParticles->SetName(fOutMCParticlesName);
347 if (InputEvent()->FindListObject(fOutMCParticlesName)) {
348 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutMCParticlesName.Data()));
352 InputEvent()->AddObject(fOutMCParticles);
355 fOutMCParticlesMap = new TH1I(fOutMCParticlesName + "_Map", fOutMCParticlesName + "_Map",9999,0,1);
356 if (InputEvent()->FindListObject(fOutMCParticlesName + "_Map")) {
357 AliFatal(Form("%s: Map %s_Map is already present in the event!", GetName(), fOutMCParticlesName.Data()));
361 InputEvent()->AddObject(fOutMCParticlesMap);
365 fOutMCParticles = fMCParticles;
366 fOutMCParticlesMap = fMCParticlesMap;
371 if (!fCaloName.IsNull() || !fCellsName.IsNull()) {
373 if (fGeomName.Length() > 0) {
374 fGeom = AliEMCALGeometry::GetInstance(fGeomName);
376 AliFatal(Form("Could not get geometry with name %s!", fGeomName.Data()));
380 fGeom = AliEMCALGeometry::GetInstance();
382 AliFatal("Could not get default geometry!");
388 const Double_t EmcalMinEta = fGeom->GetArm1EtaMin();
389 const Double_t EmcalMaxEta = fGeom->GetArm1EtaMax();
390 const Double_t EmcalMinPhi = fGeom->GetArm1PhiMin() * TMath::DegToRad();
391 const Double_t EmcalMaxPhi = fGeom->GetArm1PhiMax() * TMath::DegToRad();
393 if (fEtaMax > EmcalMaxEta) fEtaMax = EmcalMaxEta;
394 if (fEtaMax < EmcalMinEta) fEtaMax = EmcalMinEta;
395 if (fEtaMin > EmcalMaxEta) fEtaMin = EmcalMaxEta;
396 if (fEtaMin < EmcalMinEta) fEtaMin = EmcalMinEta;
398 if (fPhiMax > EmcalMaxPhi) fPhiMax = EmcalMaxPhi;
399 if (fPhiMax < EmcalMinPhi) fPhiMax = EmcalMinPhi;
400 if (fPhiMin > EmcalMaxPhi) fPhiMin = EmcalMaxPhi;
401 if (fPhiMin < EmcalMinPhi) fPhiMin = EmcalMinPhi;
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 void AliJetModelBaseTask::CopyCells()
429 for (Short_t i = 0; i < fCaloCells->GetNumberOfCells(); i++) {
433 Short_t cellNum = -1;
436 fCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
437 fOutCaloCells->SetCell(i, cellNum, amp, time, mclabel, efrac);
440 fAddedCells = fCaloCells->GetNumberOfCells();
442 AliDebug(2, Form("%d cells from the current event", fAddedCells));
445 //________________________________________________________________________
446 Int_t AliJetModelBaseTask::AddCell(Double_t e, Double_t eta, Double_t phi)
448 // Add a cell to the event.
451 if (eta < -100 || phi < 0) {
452 GetRandomCell(eta, phi, absId);
455 fGeom->EtaPhiFromIndex(absId, eta, phi);
459 AliWarning(Form("Unable to embed cell in eta = %f, phi = %f!"
460 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
461 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
466 Double_t pt = GetRandomPt();
467 TLorentzVector nPart;
468 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
472 return AddCell(e, absId);
475 //________________________________________________________________________
476 Int_t AliJetModelBaseTask::AddCell(Double_t e, Int_t absId, Double_t time, Int_t label)
478 // Add a cell to the event.
483 label += fMCLabelShift;
485 Bool_t r = fOutCaloCells->SetCell(fAddedCells, absId, e, time, label, 0);
497 //________________________________________________________________________
498 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Double_t eta, Double_t phi, Int_t label)
500 // Add a cluster to the event.
503 if (eta < -100 || phi < 0) {
504 GetRandomCell(eta, phi, absId);
507 fGeom->EtaPhiFromIndex(absId, eta, phi);
511 AliWarning(Form("Unable to embed cluster in eta = %f, phi = %f!"
512 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
513 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
518 Double_t pt = GetRandomPt();
519 TLorentzVector nPart;
520 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
524 return AddCluster(e, absId, label);
527 //________________________________________________________________________
528 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Int_t absId, Int_t label)
530 // Add a cluster to the event.
532 const Int_t nClusters = fOutClusters->GetEntriesFast();
534 TClonesArray digits("AliEMCALDigit", 1);
536 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(digits.New(0));
538 digit->SetIndexInList(0);
539 digit->SetType(AliEMCALDigit::kHG);
540 digit->SetAmplitude(e);
542 AliEMCALRecPoint recPoint("");
543 recPoint.AddDigit(*digit, e, kFALSE);
544 recPoint.EvalGlobalPosition(0, &digits);
547 recPoint.GetGlobalPosition(gpos);
551 AliVCluster *cluster = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
552 cluster->SetType(AliVCluster::kEMCALClusterv1);
553 cluster->SetE(recPoint.GetEnergy());
554 cluster->SetPosition(g);
555 cluster->SetNCells(1);
556 UShort_t shortAbsId = absId;
557 cluster->SetCellsAbsId(&shortAbsId);
558 Double32_t fract = 1;
559 cluster->SetCellsAmplitudeFraction(&fract);
560 cluster->SetID(nClusters);
561 cluster->SetEmcCpvDistance(-1);
567 label += fMCLabelShift;
570 AliESDCaloCluster *esdClus = static_cast<AliESDCaloCluster*>(cluster);
571 TArrayI parents(1, &label);
572 esdClus->AddLabels(parents);
575 AliAODCaloCluster *aodClus = static_cast<AliAODCaloCluster*>(cluster);
576 aodClus->SetLabel(&label, 1);
582 //________________________________________________________________________
583 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)
585 // Add a track to the event.
587 const Int_t nTracks = fOutTracks->GetEntriesFast();
592 eta = GetRandomEta();
594 phi = GetRandomPhi();
599 label += fMCLabelShift;
601 AliPicoTrack *track = new ((*fOutTracks)[nTracks]) AliPicoTrack(pt,
614 //________________________________________________________________________
615 AliAODMCParticle* AliJetModelBaseTask::AddMCParticle(AliAODMCParticle *part, Int_t origIndex)
617 const Int_t nPart = fOutMCParticles->GetEntriesFast();
619 AliAODMCParticle *aodpart = new ((*fOutMCParticles)[nPart]) AliAODMCParticle(*part);
621 fOutMCParticlesMap->SetBinContent(origIndex + fMCLabelShift, nPart);
622 AliDebug(2, Form("Setting bin %d to %d (fMCLabelShift=%d, origIndex=%d)",
623 origIndex + fMCLabelShift, fOutMCParticlesMap->At(origIndex + fMCLabelShift), fMCLabelShift, origIndex));
628 //________________________________________________________________________
629 void AliJetModelBaseTask::CopyClusters()
631 // Copy all the clusters in the new collection
635 const Int_t nClusters = fClusters->GetEntriesFast();
636 Int_t nCopiedClusters = 0;
639 for (Int_t i = 0; i < nClusters; ++i) {
640 AliESDCaloCluster *esdcluster = static_cast<AliESDCaloCluster*>(fClusters->At(i));
641 if (!esdcluster || !esdcluster->IsEMCAL())
643 new ((*fOutClusters)[nCopiedClusters]) AliESDCaloCluster(*esdcluster);
648 for (Int_t i = 0; i < nClusters; ++i) {
649 AliAODCaloCluster *aodcluster = static_cast<AliAODCaloCluster*>(fClusters->At(i));
650 if (!aodcluster || !aodcluster->IsEMCAL())
652 new ((*fOutClusters)[nCopiedClusters]) AliAODCaloCluster(*aodcluster);
658 //________________________________________________________________________
659 void AliJetModelBaseTask::CopyTracks()
661 // Copy all the tracks in the new collection
666 const Int_t nTracks = fTracks->GetEntriesFast();
667 Int_t nCopiedTracks = 0;
668 for (Int_t i = 0; i < nTracks; ++i) {
669 AliPicoTrack *track = static_cast<AliPicoTrack*>(fTracks->At(i));
672 new ((*fOutTracks)[nCopiedTracks]) AliPicoTrack(*track);
677 //________________________________________________________________________
678 void AliJetModelBaseTask::CopyMCParticles()
680 // Copy all the MC particles in the new collection
685 const Int_t nPart = fMCParticles->GetEntriesFast();
686 Int_t nCopiedPart = 0;
687 for (Int_t i = 0; i < nPart; ++i) {
688 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fMCParticles->At(i));
691 new ((*fOutMCParticles)[nCopiedPart]) AliAODMCParticle(*part);
696 if (!fMCParticlesMap)
699 for (Int_t i = 0; i < fMCParticlesMap->GetNbinsX()+2; i++) {
700 fOutMCParticlesMap->SetBinContent(i, fMCParticlesMap->GetBinContent(i));
701 if (fMCParticlesMap->GetBinContent(i) != 0)
705 AliDebug(2,Form("MC particles copied. fMCLabelShift=%d",fMCLabelShift));
708 //________________________________________________________________________
709 void AliJetModelBaseTask::GetRandomCell(Double_t &eta, Double_t &phi, Int_t &absId)
714 Double_t rndEta = eta;
715 Double_t rndPhi = phi;
718 rndEta = GetRandomEta();
720 rndPhi = GetRandomPhi();
721 fGeom->GetAbsCellIdFromEtaPhi(rndEta, rndPhi, absId);
723 } while (absId == -1 && repeats < 100);
726 AliWarning(Form("Could not extract random cluster! Random eta-phi extracted more than 100 times!\n"
727 "eta [%f, %f], phi [%f, %f]\n", fEtaMin, fEtaMax, fPhiMin, fPhiMax));
735 //________________________________________________________________________
736 Double_t AliJetModelBaseTask::GetRandomEta()
740 return gRandom->Rndm() * (fEtaMax - fEtaMin) + fEtaMin;
743 //________________________________________________________________________
744 Double_t AliJetModelBaseTask::GetRandomPhi()
748 return gRandom->Rndm() * (fPhiMax - fPhiMin) + fPhiMin;
751 //________________________________________________________________________
752 Double_t AliJetModelBaseTask::GetRandomPt()
757 return fPtSpectrum->GetRandom();
759 return gRandom->Rndm() * (fPtMax - fPtMin) + fPtMin;
762 //________________________________________________________________________
763 void AliJetModelBaseTask::Run()