ATLAS Offline Software
TestIsolationCloseByCorrAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 // Local include(s):
9 #include <StoreGate/ReadHandle.h>
10 
12 
13 namespace CP {
14 
15  TestIsolationCloseByCorrAlg::TestIsolationCloseByCorrAlg(const std::string& name, ISvcLocator* svcLoc) :
16  AthHistogramAlgorithm(name, svcLoc) {}
17 
19  ATH_CHECK(m_isoSelectorTool.retrieve());
20  ATH_CHECK(m_isoCloseByCorrTool.retrieve());
21 
22  ATH_CHECK(m_muonKey.initialize(!m_muonKey.empty()));
23  ATH_CHECK(m_elecKey.initialize(!m_elecKey.empty()));
24  ATH_CHECK(m_photKey.initialize(!m_photKey.empty()));
27  unsigned int writeOpts = wOpts::writePileUp | wOpts::writeBeamSpot;
28  if (m_isMC) writeOpts |= wOpts::isMC;
29  m_tree.addBranch(std::make_shared<MuonVal::EventInfoBranch>(m_tree, writeOpts));
30 
31  auto add_correctionHelper = [this](std::shared_ptr<IsoCorrectionTestHelper> helper) {
32  if (!m_selDecoration.empty()) helper->SetSelectionDecorator(m_selDecoration.value());
33  if (!m_isoDecoration.empty()) helper->SetIsolationDecorator(m_isoDecoration.value());
34  if (!m_backup_prefix.empty()) helper->SetBackupPreFix(m_backup_prefix.value());
35  if (!m_updatedIsoDeco.empty()) helper->SetUpdatedIsoDecorator(m_updatedIsoDeco.value());
37  };
38  if (!m_elecKey.empty()) {
39  m_ele_helper = std::make_shared<IsoCorrectionTestHelper>(m_tree, "Electrons", m_isoSelectorTool->getElectronWPs());
40  add_correctionHelper(m_ele_helper);
41  }
42  if (!m_muonKey.empty()) {
43  m_muo_helper = std::make_shared<IsoCorrectionTestHelper>(m_tree, "Muons", m_isoSelectorTool->getMuonWPs());
44  add_correctionHelper(m_muo_helper);
45  }
46  if (!m_photKey.empty()) {
47  m_pho_helper = std::make_shared<IsoCorrectionTestHelper>(m_tree, "Photons", m_isoSelectorTool->getPhotonWPs());
48  add_correctionHelper(m_pho_helper);
49  }
50  if (!m_muonSelTool.empty()) ATH_CHECK(m_muonSelTool.retrieve());
51  if (!m_elecSelTool.empty()) ATH_CHECK(m_elecSelTool.retrieve());
52  if (!m_photSelTool.empty()) ATH_CHECK(m_photSelTool.retrieve());
53  if (!m_selDecoration.empty()) m_selDecorator = std::make_unique<CharDecorator>(m_selDecoration);
54  if (!m_isoDecoration.empty()) m_isoDecorator = std::make_unique<CharDecorator>(m_isoDecoration);
55  ATH_CHECK(m_tree.init(this));
56  return StatusCode::SUCCESS;
57  }
60  return StatusCode::SUCCESS;
61  }
62  template <class TARGET_TYPE, class CONT_TYPE, class COPY_TYPE>
65  std::pair<std::unique_ptr<COPY_TYPE>,
66  std::unique_ptr<xAOD::ShallowAuxContainer>>& cont) const {
67  if (key.empty()) {
68  ATH_MSG_DEBUG("No key given. Assume it's no required to load the container");
69  return StatusCode::SUCCESS;
70  }
71  SG::ReadHandle<CONT_TYPE> readHandle{key, ctx};
72  if (!readHandle.isValid()) {
73  ATH_MSG_FATAL("Failed to load container " << key.fullKey());
74  return StatusCode::FAILURE;
75  }
76 
77  cont = xAOD::shallowCopyContainer(dynamic_cast<const TARGET_TYPE&> (*readHandle),ctx);
78  if (!m_selDecorator && !m_isoDecorator) return StatusCode::SUCCESS;
79  std::unique_ptr<COPY_TYPE>& elems = cont.first;
80  for (auto part : *(elems.get()) ) {
81  if (m_selDecorator) (*m_selDecorator)(*part) = passSelection(ctx, part);
82  if (m_isoDecorator) (*m_isoDecorator)(*part) = true && m_isoSelectorTool->accept(*part);
83  }
84 
85  return StatusCode::SUCCESS;
86  }
87 
88  bool TestIsolationCloseByCorrAlg::passSelection(const EventContext&, const xAOD::Muon* muon) const {
89  return muon->pt() >= m_mu_min_pt && (m_mu_max_eta < 0. || std::abs(muon->eta()) < m_mu_max_eta) &&
90  (m_muonSelTool.empty() || m_muonSelTool->accept(*muon));
91  }
92  bool TestIsolationCloseByCorrAlg::passSelection(const EventContext& ctx, const xAOD::Egamma* egamm) const {
93  if (egamm->type() == xAOD::Type::ObjectType::Electron) {
94  return egamm->pt() >= m_el_min_pt && (m_el_max_eta < 0. || std::abs(egamm->eta()) < m_el_max_eta) &&
95  (m_elecSelTool.empty() || m_elecSelTool->accept(ctx, egamm));
96  }
98  return egamm->pt() >= m_ph_min_pt && (m_ph_max_eta < 0. || std::abs(egamm->eta()) < m_ph_max_eta) &&
99  (m_photSelTool.empty() || m_photSelTool->accept(ctx, egamm));
100  }
101 
103  const EventContext& ctx = Gaudi::Hive::currentContext();
104  //
105  xAOD::ElectronContainer* Electrons = nullptr;
106  std::pair<std::unique_ptr<xAOD::ElectronContainer>, std::unique_ptr<xAOD::ShallowAuxContainer>> ElShallow;
107  ATH_CHECK(loadContainer<xAOD::ElectronContainer>(ctx, m_elecKey, ElShallow));
108  Electrons = ElShallow.first.get();
109  //
110  xAOD::PhotonContainer* Photons = nullptr;
111  std::pair<std::unique_ptr<xAOD::PhotonContainer>, std::unique_ptr<xAOD::ShallowAuxContainer>> PhShallow;
112  ATH_CHECK(loadContainer<xAOD::PhotonContainer>(ctx, m_photKey, PhShallow));
113  Photons = PhShallow.first.get();
114  //
115  xAOD::MuonContainer* Muons = nullptr;
116  std::pair<std::unique_ptr<xAOD::MuonContainer>, std::unique_ptr<xAOD::ShallowAuxContainer>> MuonsShallow;
117  ATH_CHECK(loadContainer<xAOD::MuonContainer>(ctx, m_muonKey, MuonsShallow));
118  Muons = MuonsShallow.first.get();
119 
120  // Okay everything is defined for the preselection of the algorithm. lets pass the things towards the IsoCorrectionTool
121  if (m_isoCloseByCorrTool->getCloseByIsoCorrection(ctx, Electrons, Muons, Photons).code() == CorrectionCode::Error) {
122  ATH_MSG_ERROR("Something weird happened with the tool");
123  return StatusCode::FAILURE;
124  }
125  // The isoCorrectionTool has now corrected everything using close-by objects satisfiyng the dec_PassQuality criteria
126  // The name of the decorator is set via the 'SelectionDecorator' property of the tool
127  // Optionally one can also define that the tool shall only objects surviving the overlap removal without changing the initial
128  // decorator Use therefore the 'PassOverlapDecorator' property to define the decorators name If you define the 'BackupPrefix'
129  // property then the original values are stored before correction <Prefix>_<IsolationCone> The final result whether the object
130  // passes the isolation criteria now can be stored in the 'IsolationSelectionDecorator' e.g. 'CorrectedIso'
131 
132  // parse the associated muon clusters to the tool
133  ClusterSet muon_clusters;
134  PflowSet pflows;
135 
136 
138  TrackSet selected_trks{}, expected_trks{};
139  if (!m_polTrkKey.empty()) {
141  if (!closeTrkColl.isValid()) {
142  ATH_MSG_FATAL("Failed to load " << m_polTrkKey.fullKey() << " from storegate");
143  return StatusCode::FAILURE;
144  }
145  for (const xAOD::TrackParticle* trk : *closeTrkColl) { selected_trks.emplace(trk); }
147  correction_tool()->loadPrimaryParticles(Electrons, cache);
148  correction_tool()->loadPrimaryParticles(Muons, cache);
149  correction_tool()->loadPrimaryParticles(Photons, cache);
150  correction_tool()->loadAssociatedObjects(ctx, cache);
151  expected_trks = std::move(cache.tracks);
152  muon_clusters = std::move(cache.clusters);
153  pflows = std::move(cache.flows);
154  }
155 
156  // Store everything in the final ntuples
157  auto fill_helper = [&](std::shared_ptr<IsoCorrectionTestHelper> helper, const xAOD::IParticleContainer* parts) -> StatusCode{
158  if (!helper) return StatusCode::SUCCESS;
159  helper->SetClusters(muon_clusters);
160  helper->SetFlowElements(pflows);
161  return helper->Fill(parts);
162  };
163  ATH_CHECK(fill_helper(m_ele_helper, Electrons));
164  ATH_CHECK(fill_helper(m_muo_helper, Muons));
165  ATH_CHECK(fill_helper(m_pho_helper, Photons));
166  ATH_CHECK(m_tree.fill(ctx));
167  return StatusCode::SUCCESS;
168  }
170  return dynamic_cast<const CP::IsolationCloseByCorrectionTool*>(m_isoCloseByCorrTool.get());
171  }
172 } // namespace CP
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
xAOD::Egamma_v1::type
virtual Type::ObjectType type() const override=0
The type of the object as a simple enumeration, remains pure virtual in e/gamma.
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
CP::TestIsolationCloseByCorrAlg::m_ph_min_pt
Gaudi::Property< float > m_ph_min_pt
Definition: TestIsolationCloseByCorrAlg.h:97
MuonVal::MuonTesterTree::init
StatusCode init(OWNER *instance)
Initialize method.
TestIsolationCloseByCorrAlg.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
CP::PflowSet
std::set< FlowElementPtr > PflowSet
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:74
CP::TestIsolationCloseByCorrAlg::m_updatedIsoDeco
Gaudi::Property< std::string > m_updatedIsoDeco
Definition: TestIsolationCloseByCorrAlg.h:71
CP::TestIsolationCloseByCorrAlg::m_pho_helper
std::shared_ptr< IsoCorrectionTestHelper > m_pho_helper
Definition: TestIsolationCloseByCorrAlg.h:91
CP::IsolationCloseByCorrectionTool::ObjectCache::clusters
ClusterSet clusters
Definition: IsolationCloseByCorrectionTool.h:91
CP::TestIsolationCloseByCorrAlg::m_muo_helper
std::shared_ptr< IsoCorrectionTestHelper > m_muo_helper
Definition: TestIsolationCloseByCorrAlg.h:90
CP::TrackSet
std::set< TrackPtr > TrackSet
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:72
DataVector::get
const T * get(size_type n) const
Access an element, as an rvalue.
CP::TestIsolationCloseByCorrAlg::m_selDecorator
SelectionDecorator m_selDecorator
Definition: TestIsolationCloseByCorrAlg.h:75
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
CP::TestIsolationCloseByCorrAlg::m_backup_prefix
Gaudi::Property< std::string > m_backup_prefix
Definition: TestIsolationCloseByCorrAlg.h:72
CP::IsolationCloseByCorrectionTool::loadAssociatedObjects
void loadAssociatedObjects(const EventContext &ctx, ObjectCache &cache) const
Load all associated tracks / clusters / flow elements into the cache.
Definition: IsolationCloseByCorrectionTool.cxx:161
Defs.h
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
CP::TestIsolationCloseByCorrAlg::m_isoDecorator
SelectionDecorator m_isoDecorator
Definition: TestIsolationCloseByCorrAlg.h:76
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
CP::TestIsolationCloseByCorrAlg::finalize
StatusCode finalize() override
Definition: TestIsolationCloseByCorrAlg.cxx:58
CP::TestIsolationCloseByCorrAlg::execute
StatusCode execute() override
Definition: TestIsolationCloseByCorrAlg.cxx:102
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
CP::TestIsolationCloseByCorrAlg::m_tree
MuonVal::MuonTesterTree m_tree
Definition: TestIsolationCloseByCorrAlg.h:88
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
runBeamSpotCalibration.helper
helper
Definition: runBeamSpotCalibration.py:112
CP::TestIsolationCloseByCorrAlg::m_isMC
Gaudi::Property< bool > m_isMC
Definition: TestIsolationCloseByCorrAlg.h:100
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
CP::TestIsolationCloseByCorrAlg::m_photSelTool
ToolHandle< IAsgPhotonIsEMSelector > m_photSelTool
Definition: TestIsolationCloseByCorrAlg.h:81
CP::IsolationCloseByCorrectionTool::loadPrimaryParticles
void loadPrimaryParticles(const xAOD::IParticleContainer *container, ObjectCache &cache) const
Filter all electrons/muons/photons from the collection which pass the selection decoration.
Definition: IsolationCloseByCorrectionTool.cxx:136
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CP::TestIsolationCloseByCorrAlg::m_el_max_eta
Gaudi::Property< float > m_el_max_eta
Definition: TestIsolationCloseByCorrAlg.h:96
CP::TestIsolationCloseByCorrAlg::loadContainer
StatusCode loadContainer(const EventContext &ctx, const SG::ReadHandleKey< CONT_TYPE > &key, std::pair< std::unique_ptr< COPY_TYPE >, std::unique_ptr< xAOD::ShallowAuxContainer >> &cont) const
Definition: TestIsolationCloseByCorrAlg.cxx:63
CP::TestIsolationCloseByCorrAlg::m_isoCloseByCorrTool
ToolHandle< CP::IIsolationCloseByCorrectionTool > m_isoCloseByCorrTool
Definition: TestIsolationCloseByCorrAlg.h:85
CP::IsolationCloseByCorrectionTool::ObjectCache::flows
PflowSet flows
Definition: IsolationCloseByCorrectionTool.h:92
CP::IsolationCloseByCorrectionTool
Definition: IsolationCloseByCorrectionTool.h:37
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IsoVariableHelper.h
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
CP::TestIsolationCloseByCorrAlg::m_el_min_pt
Gaudi::Property< float > m_el_min_pt
Definition: TestIsolationCloseByCorrAlg.h:95
CP::TestIsolationCloseByCorrAlg::m_polTrkKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_polTrkKey
Optionally the algorithm can test the behaviour of the tracks selected by the IsoCloseByCorrectionTrk...
Definition: TestIsolationCloseByCorrAlg.h:65
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthHistogramAlgorithm
Definition: AthHistogramAlgorithm.h:32
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
TestMacroHelpers.h
CP::TestIsolationCloseByCorrAlg::m_ele_helper
std::shared_ptr< IsoCorrectionTestHelper > m_ele_helper
Definition: TestIsolationCloseByCorrAlg.h:89
MuonVal::EventInfoBranch::WriteOpts
WriteOpts
Definition: EventInfoBranch.h:18
CP::TestIsolationCloseByCorrAlg::m_muonKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonKey
Input containers.
Definition: TestIsolationCloseByCorrAlg.h:61
CP::TestIsolationCloseByCorrAlg::m_ph_max_eta
Gaudi::Property< float > m_ph_max_eta
Definition: TestIsolationCloseByCorrAlg.h:98
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
CP::TestIsolationCloseByCorrAlg::m_mu_max_eta
Gaudi::Property< float > m_mu_max_eta
Definition: TestIsolationCloseByCorrAlg.h:94
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
CP::IsolationCloseByCorrectionTool::ObjectCache
Definition: IsolationCloseByCorrectionTool.h:85
CP::ClusterSet
std::set< CaloClusterPtr > ClusterSet
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:73
CP::IsolationCloseByCorrectionTool::ObjectCache::tracks
TrackSet tracks
Definition: IsolationCloseByCorrectionTool.h:90
CP::TestIsolationCloseByCorrAlg::TestIsolationCloseByCorrAlg
TestIsolationCloseByCorrAlg(const std::string &name, ISvcLocator *svcLoc)
Definition: TestIsolationCloseByCorrAlg.cxx:15
CP::TestIsolationCloseByCorrAlg::initialize
StatusCode initialize() override
Definition: TestIsolationCloseByCorrAlg.cxx:18
MuonVal::MuonTesterTree::fill
bool fill(const EventContext &ctx)
Fills the tree per call.
Definition: MuonTesterTree.cxx:89
MuonVal::MuonTesterTree::write
StatusCode write()
Finally write the TTree objects.
Definition: MuonTesterTree.cxx:178
EventInfoRead.isMC
isMC
Definition: EventInfoRead.py:11
xAOD::Egamma_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: Egamma_v1.cxx:65
doL1CaloHVCorrections.parts
parts
Definition: doL1CaloHVCorrections.py:334
xAOD::Egamma_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition: Egamma_v1.cxx:70
CP::TestIsolationCloseByCorrAlg::m_isoDecoration
Gaudi::Property< std::string > m_isoDecoration
Definition: TestIsolationCloseByCorrAlg.h:70
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
CP::TestIsolationCloseByCorrAlg::passSelection
bool passSelection(const EventContext &ctx, const xAOD::Muon *muon) const
Definition: TestIsolationCloseByCorrAlg.cxx:88
ReadHandle.h
Handle class for reading from StoreGate.
CP::TestIsolationCloseByCorrAlg::m_muonSelTool
ToolHandle< CP::IMuonSelectionTool > m_muonSelTool
Optionally the user can also parse the elec / muon / photon selection tools.
Definition: TestIsolationCloseByCorrAlg.h:79
CP::TestIsolationCloseByCorrAlg::correction_tool
const CP::IsolationCloseByCorrectionTool * correction_tool() const
Definition: TestIsolationCloseByCorrAlg.cxx:169
CP::TestIsolationCloseByCorrAlg::m_mu_min_pt
Gaudi::Property< float > m_mu_min_pt
Definition: TestIsolationCloseByCorrAlg.h:93
CP::TestIsolationCloseByCorrAlg::m_elecSelTool
ToolHandle< IAsgElectronLikelihoodTool > m_elecSelTool
Definition: TestIsolationCloseByCorrAlg.h:80
CP::TestIsolationCloseByCorrAlg::m_isoSelectorTool
ToolHandle< CP::IIsolationSelectionTool > m_isoSelectorTool
Definition: TestIsolationCloseByCorrAlg.h:86
CP::TestIsolationCloseByCorrAlg::m_elecKey
SG::ReadHandleKey< xAOD::EgammaContainer > m_elecKey
Definition: TestIsolationCloseByCorrAlg.h:62
MuonVal::MuonTesterTree::addBranch
bool addBranch(std::shared_ptr< IMuonTesterBranch > branch)
Branch is added to the tree without transferring the ownership.
Definition: MuonTesterTree.cxx:61
CP::TestIsolationCloseByCorrAlg::m_photKey
SG::ReadHandleKey< xAOD::EgammaContainer > m_photKey
Definition: TestIsolationCloseByCorrAlg.h:63
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
CP::TestIsolationCloseByCorrAlg::m_selDecoration
Gaudi::Property< std::string > m_selDecoration
Definition: TestIsolationCloseByCorrAlg.h:67