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 AliVCaloCells *tempCaloCells = 0;
172 tempCaloCells = fCaloCells;
173 fCaloCells = static_cast<AliVCaloCells*>(tempCaloCells->Clone(Form("%s_old",fCaloCells->GetName())));
181 fCaloCells = tempCaloCells;
185 //________________________________________________________________________
186 Bool_t AliJetModelBaseTask::ExecOnce()
191 gRandom = new TRandom3(0);
193 fEsdMode = InputEvent()->InheritsFrom("AliESDEvent");
195 if (fPtMax < fPtMin) {
196 AliWarning (Form("PtMax (%f) < PtMin (%f), setting PtMax = PtMin = %f", fPtMax, fPtMin, fPtMin));
200 if (fEtaMax < fEtaMin) {
201 AliWarning (Form("EtaMax (%f) < EtaMin (%f), setting EtaMax = EtaMin = %f", fEtaMax, fEtaMin, fEtaMin));
205 if (fPhiMax < fPhiMin) {
206 AliWarning (Form("PhiMax (%f) < PhiMin (%f), setting PhiMax = PhiMin = %f", fPhiMax, fPhiMin, fPhiMin));
210 if (!fCellsName.IsNull()) {
211 fCaloCells = dynamic_cast<AliVCaloCells*>(InputEvent()->FindListObject(fCellsName));
213 AliWarning(Form("%s: Couldn't retrieve calo cells with name %s!", GetName(), fCellsName.Data()));
215 else if (!fCaloCells->InheritsFrom("AliVCaloCells")) {
216 AliError(Form("%s: Collection %s does not contain a AliVCaloCells object!", GetName(), fCellsName.Data()));
221 if (!fOutCaloCells) {
222 fOutCellsName = fCellsName;
224 fOutCellsName += fSuffix;
225 if (fCopyArray || !fCaloCells) {
227 fOutCaloCells = new AliESDCaloCells(fOutCellsName,fOutCellsName);
229 fOutCaloCells = new AliAODCaloCells(fOutCellsName,fOutCellsName);
231 if (InputEvent()->FindListObject(fOutCellsName)) {
232 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCellsName.Data()));
236 InputEvent()->AddObject(fOutCaloCells);
240 fOutCaloCells = fCaloCells;
245 if (fNTracks > 0 && !fTracksName.IsNull()) {
246 fTracks = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fTracksName));
248 AliWarning(Form("%s: Couldn't retrieve tracks with name %s!", GetName(), fTracksName.Data()));
250 else if (!fTracks->GetClass()->GetBaseClass("AliPicoTrack")) {
251 AliError(Form("%s: Collection %s does not contain AliPicoTrack objects!", GetName(), fTracksName.Data()));
257 fOutTracksName = fTracksName;
259 fOutTracksName += fSuffix;
260 if (fCopyArray || !fTracks) {
261 fOutTracks = new TClonesArray("AliPicoTrack");
262 fOutTracks->SetName(fOutTracksName);
263 if (InputEvent()->FindListObject(fOutTracksName)) {
264 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutTracksName.Data()));
268 InputEvent()->AddObject(fOutTracks);
272 fOutTracks = fTracks;
277 if (fNClusters > 0 && !fCaloName.IsNull()) {
278 fClusters = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fCaloName));
281 AliWarning(Form("%s: Couldn't retrieve clusters with name %s!", GetName(), fCaloName.Data()));
283 else if (!fClusters->GetClass()->GetBaseClass("AliVCluster")) {
284 AliError(Form("%s: Collection %s does not contain AliVCluster objects!", GetName(), fCaloName.Data()));
290 fOutCaloName = fCaloName;
292 fOutCaloName += fSuffix;
293 if (fCopyArray || !fClusters) {
294 fOutClusters = new TClonesArray(fClusters->GetClass()->GetName(), fClusters->GetSize());
295 fOutClusters->SetName(fOutCaloName);
296 if (InputEvent()->FindListObject(fOutCaloName)) {
297 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutCaloName.Data()));
301 InputEvent()->AddObject(fOutClusters);
305 fOutClusters = fClusters;
310 if (!fMCParticlesName.IsNull()) {
311 fMCParticles = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fMCParticlesName));
313 AliWarning(Form("%s: Couldn't retrieve MC particles with name %s!", GetName(), fMCParticlesName.Data()));
316 if (!fMCParticles->GetClass()->GetBaseClass("AliAODMCParticle")) {
317 AliError(Form("%s: Collection %s does not contain AliAODMCParticle objects!", GetName(), fMCParticlesName.Data()));
322 fMCParticlesMap = dynamic_cast<TH1I*>(InputEvent()->FindListObject(fMCParticlesName + "_Map"));
324 if (!fMCParticlesMap) {
325 AliWarning(Form("%s: Could not retrieve map for MC particles %s! Will assume MC labels consistent with indexes...", GetName(), fMCParticlesName.Data()));
326 fMCParticlesMap = new TH1I(fMCParticlesName + "_Map", fMCParticlesName + "_Map",9999,0,1);
327 for (Int_t i = 0; i < 9999; i++) {
328 fMCParticlesMap->SetBinContent(i,i);
333 if (!fOutMCParticles) {
334 fOutMCParticlesName = fMCParticlesName;
336 fOutMCParticlesName += fSuffix;
337 if (fCopyArray || !fMCParticles) {
339 fOutMCParticles = new TClonesArray("AliAODMCParticle");
340 fOutMCParticles->SetName(fOutMCParticlesName);
341 if (InputEvent()->FindListObject(fOutMCParticlesName)) {
342 AliFatal(Form("%s: Collection %s is already present in the event!", GetName(), fOutMCParticlesName.Data()));
346 InputEvent()->AddObject(fOutMCParticles);
349 fOutMCParticlesMap = new TH1I(fOutMCParticlesName + "_Map", fOutMCParticlesName + "_Map",9999,0,1);
350 if (InputEvent()->FindListObject(fOutMCParticlesName + "_Map")) {
351 AliFatal(Form("%s: Map %s_Map is already present in the event!", GetName(), fOutMCParticlesName.Data()));
355 InputEvent()->AddObject(fOutMCParticlesMap);
359 fOutMCParticles = fMCParticles;
360 fOutMCParticlesMap = fMCParticlesMap;
365 if (!fCaloName.IsNull() || !fCellsName.IsNull()) {
367 if (fGeomName.Length() > 0) {
368 fGeom = AliEMCALGeometry::GetInstance(fGeomName);
370 AliFatal(Form("Could not get geometry with name %s!", fGeomName.Data()));
374 fGeom = AliEMCALGeometry::GetInstance();
376 AliFatal("Could not get default geometry!");
382 const Double_t EmcalMinEta = fGeom->GetArm1EtaMin();
383 const Double_t EmcalMaxEta = fGeom->GetArm1EtaMax();
384 const Double_t EmcalMinPhi = fGeom->GetArm1PhiMin() * TMath::DegToRad();
385 const Double_t EmcalMaxPhi = fGeom->GetArm1PhiMax() * TMath::DegToRad();
387 if (fEtaMax > EmcalMaxEta) fEtaMax = EmcalMaxEta;
388 if (fEtaMax < EmcalMinEta) fEtaMax = EmcalMinEta;
389 if (fEtaMin > EmcalMaxEta) fEtaMin = EmcalMaxEta;
390 if (fEtaMin < EmcalMinEta) fEtaMin = EmcalMinEta;
392 if (fPhiMax > EmcalMaxPhi) fPhiMax = EmcalMaxPhi;
393 if (fPhiMax < EmcalMinPhi) fPhiMax = EmcalMinPhi;
394 if (fPhiMin > EmcalMaxPhi) fPhiMin = EmcalMaxPhi;
395 if (fPhiMin < EmcalMinPhi) fPhiMin = EmcalMinPhi;
401 //________________________________________________________________________
402 Int_t AliJetModelBaseTask::SetNumberOfOutCells(Int_t n)
404 if (fOutCaloCells->GetNumberOfCells() < n) {
405 fOutCaloCells->DeleteContainer();
406 fOutCaloCells->CreateContainer(n);
409 fOutCaloCells->SetNumberOfCells(n);
417 //________________________________________________________________________
418 void AliJetModelBaseTask::CopyCells()
423 for (Short_t i = 0; i < fCaloCells->GetNumberOfCells(); i++) {
427 Short_t cellNum = -1;
430 fCaloCells->GetCell(i, cellNum, amp, time, mclabel, efrac);
431 fOutCaloCells->SetCell(i, cellNum, amp, time, mclabel, efrac);
434 fAddedCells = fCaloCells->GetNumberOfCells();
436 AliDebug(2, Form("%d cells from the current event", fAddedCells));
439 //________________________________________________________________________
440 Int_t AliJetModelBaseTask::AddCell(Double_t e, Double_t eta, Double_t phi)
442 // Add a cell to the event.
445 if (eta < -100 || phi < 0) {
446 GetRandomCell(eta, phi, absId);
449 fGeom->EtaPhiFromIndex(absId, eta, phi);
453 AliWarning(Form("Unable to embed cell in eta = %f, phi = %f!"
454 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
455 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
460 Double_t pt = GetRandomPt();
461 TLorentzVector nPart;
462 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
466 return AddCell(e, absId);
469 //________________________________________________________________________
470 Int_t AliJetModelBaseTask::AddCell(Double_t e, Int_t absId, Double_t time, Int_t label)
472 // Add a cell to the event.
477 label += fMCLabelShift;
479 Bool_t r = fOutCaloCells->SetCell(fAddedCells, absId, e, time, label, 0);
491 //________________________________________________________________________
492 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Double_t eta, Double_t phi, Int_t label)
494 // Add a cluster to the event.
497 if (eta < -100 || phi < 0) {
498 GetRandomCell(eta, phi, absId);
501 fGeom->EtaPhiFromIndex(absId, eta, phi);
505 AliWarning(Form("Unable to embed cluster in eta = %f, phi = %f!"
506 " Maybe the eta-phi range is not inside the EMCal acceptance (eta = [%f, %f], phi = [%f, %f])",
507 eta, phi, fEtaMin, fEtaMax, fPhiMin, fPhiMax));
512 Double_t pt = GetRandomPt();
513 TLorentzVector nPart;
514 nPart.SetPtEtaPhiM(pt, eta, phi, 0);
518 return AddCluster(e, absId, label);
521 //________________________________________________________________________
522 AliVCluster* AliJetModelBaseTask::AddCluster(Double_t e, Int_t absId, Int_t label)
524 // Add a cluster to the event.
526 const Int_t nClusters = fOutClusters->GetEntriesFast();
528 TClonesArray digits("AliEMCALDigit", 1);
530 AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(digits.New(0));
532 digit->SetIndexInList(0);
533 digit->SetType(AliEMCALDigit::kHG);
534 digit->SetAmplitude(e);
536 AliEMCALRecPoint recPoint("");
537 recPoint.AddDigit(*digit, e, kFALSE);
538 recPoint.EvalGlobalPosition(0, &digits);
541 recPoint.GetGlobalPosition(gpos);
545 AliVCluster *cluster = static_cast<AliVCluster*>(fOutClusters->New(nClusters));
546 cluster->SetType(AliVCluster::kEMCALClusterv1);
547 cluster->SetE(recPoint.GetEnergy());
548 cluster->SetPosition(g);
549 cluster->SetNCells(1);
550 UShort_t shortAbsId = absId;
551 cluster->SetCellsAbsId(&shortAbsId);
552 Double32_t fract = 1;
553 cluster->SetCellsAmplitudeFraction(&fract);
554 cluster->SetID(nClusters);
555 cluster->SetEmcCpvDistance(-1);
561 label += fMCLabelShift;
564 AliESDCaloCluster *esdClus = static_cast<AliESDCaloCluster*>(cluster);
565 TArrayI parents(1, &label);
566 esdClus->AddLabels(parents);
569 AliAODCaloCluster *aodClus = static_cast<AliAODCaloCluster*>(cluster);
570 aodClus->SetLabel(&label, 1);
576 //________________________________________________________________________
577 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)
579 // Add a track to the event.
581 const Int_t nTracks = fOutTracks->GetEntriesFast();
586 eta = GetRandomEta();
588 phi = GetRandomPhi();
593 label += fMCLabelShift;
595 AliPicoTrack *track = new ((*fOutTracks)[nTracks]) AliPicoTrack(pt,
608 //________________________________________________________________________
609 AliAODMCParticle* AliJetModelBaseTask::AddMCParticle(AliAODMCParticle *part, Int_t origIndex)
611 const Int_t nPart = fOutMCParticles->GetEntriesFast();
613 AliAODMCParticle *aodpart = new ((*fOutMCParticles)[nPart]) AliAODMCParticle(*part);
615 fOutMCParticlesMap->SetBinContent(origIndex + fMCLabelShift, nPart);
620 //________________________________________________________________________
621 void AliJetModelBaseTask::CopyClusters()
623 // Copy all the clusters in the new collection
627 const Int_t nClusters = fClusters->GetEntriesFast();
628 Int_t nCopiedClusters = 0;
631 for (Int_t i = 0; i < nClusters; ++i) {
632 AliESDCaloCluster *esdcluster = static_cast<AliESDCaloCluster*>(fClusters->At(i));
633 if (!esdcluster || !esdcluster->IsEMCAL())
635 new ((*fOutClusters)[nCopiedClusters]) AliESDCaloCluster(*esdcluster);
640 for (Int_t i = 0; i < nClusters; ++i) {
641 AliAODCaloCluster *aodcluster = static_cast<AliAODCaloCluster*>(fClusters->At(i));
642 if (!aodcluster || !aodcluster->IsEMCAL())
644 new ((*fOutClusters)[nCopiedClusters]) AliAODCaloCluster(*aodcluster);
650 //________________________________________________________________________
651 void AliJetModelBaseTask::CopyTracks()
653 // Copy all the tracks in the new collection
658 const Int_t nTracks = fTracks->GetEntriesFast();
659 Int_t nCopiedTracks = 0;
660 for (Int_t i = 0; i < nTracks; ++i) {
661 AliPicoTrack *track = static_cast<AliPicoTrack*>(fTracks->At(i));
664 new ((*fOutTracks)[nCopiedTracks]) AliPicoTrack(*track);
669 //________________________________________________________________________
670 void AliJetModelBaseTask::CopyMCParticles()
672 // Copy all the MC particles in the new collection
677 const Int_t nPart = fMCParticles->GetEntriesFast();
678 Int_t nCopiedPart = 0;
679 for (Int_t i = 0; i < nPart; ++i) {
680 AliAODMCParticle *part = static_cast<AliAODMCParticle*>(fMCParticles->At(i));
683 new ((*fOutMCParticles)[nCopiedPart]) AliAODMCParticle(*part);
688 if (!fMCParticlesMap)
693 for (Int_t i = 0; i < fMCParticlesMap->GetNbinsX()+2; i++) {
694 fOutMCParticlesMap->SetBinContent(i, fMCParticlesMap->GetBinContent(i));
695 if (fMCParticlesMap->GetBinContent(i) != 0)
699 fMCLabelShift = shift;
702 //________________________________________________________________________
703 void AliJetModelBaseTask::GetRandomCell(Double_t &eta, Double_t &phi, Int_t &absId)
708 Double_t rndEta = eta;
709 Double_t rndPhi = phi;
712 rndEta = GetRandomEta();
714 rndPhi = GetRandomPhi();
715 fGeom->GetAbsCellIdFromEtaPhi(rndEta, rndPhi, absId);
717 } while (absId == -1 && repeats < 100);
720 AliWarning(Form("Could not extract random cluster! Random eta-phi extracted more than 100 times!\n"
721 "eta [%f, %f], phi [%f, %f]\n", fEtaMin, fEtaMax, fPhiMin, fPhiMax));
729 //________________________________________________________________________
730 Double_t AliJetModelBaseTask::GetRandomEta()
734 return gRandom->Rndm() * (fEtaMax - fEtaMin) + fEtaMin;
737 //________________________________________________________________________
738 Double_t AliJetModelBaseTask::GetRandomPhi()
742 return gRandom->Rndm() * (fPhiMax - fPhiMin) + fPhiMin;
745 //________________________________________________________________________
746 Double_t AliJetModelBaseTask::GetRandomPt()
751 return fPtSpectrum->GetRandom();
753 return gRandom->Rndm() * (fPtMax - fPtMin) + fPtMin;
756 //________________________________________________________________________
757 void AliJetModelBaseTask::Run()