ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSFTestHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4
5#ifndef XAOD_STANDALONE
9
10
12
13namespace {
14 constexpr double MeVtoGeV = 1.e-3;
15}
16namespace TestMuonSF {
17 template<typename T> T getProperty(const asg::IAsgTool* interface_tool, const std::string& prop_name) {
18 const asg::AsgTool* asg_tool = dynamic_cast<const asg::AsgTool*>(interface_tool);
19 T prop;
20 const T* HandlePtr = asg_tool->getProperty < T > (prop_name);
21 if (!HandlePtr) Error("getProperty()", "Failed to retrieve property %s ", prop_name.c_str());
22 else prop = (*HandlePtr);
23 return prop;
24 }
25
26 //############################################################
27 // TriggerSFBranches
28 //############################################################
30 const ToolHandle<CP::IMuonTriggerScaleFactors>& Handle,
31 const std::string& muonContainer,
32 const std::string& Trigger):
33 MuonVal::MuonTesterBranch{tree, "MuTriggerSF_" +Trigger},
35 m_handle{Handle},
36 m_trigger{Trigger}{
37 }
39 if (!declare_dependency(m_key)) return false;
40 if (!m_nominal_SF.init()) return false;
41 if (!m_stat_up_SF.init()) return false;
42 if (!m_stat_down_SF.init()) return false;
43 if (!m_sys_up_SF.init()) return false;
44 if (!m_sys_down_SF.init()) return false;
45 return true;
46 }
47
48 bool TriggerSFBranches::fill(const EventContext& ctx) {
50 if (!readHandle.isPresent()) {
51 ATH_MSG_FATAL("Failed to retrieve muon container "<<m_key.fullKey());
52 return false;
53 }
54 const xAOD::MuonContainer* muons{readHandle.cptr()};
56 return false;
57 } if (getSF(muons, m_stat_up_SF, CP::SystematicVariation("MUON_EFF_TrigStatUncertainty", +1)) == CP::CorrectionCode::Error) {
59 } if (getSF(muons, m_stat_down_SF, CP::SystematicVariation("MUON_EFF_TrigStatUncertainty", -1)) == CP::CorrectionCode::Error) {
61 } if (getSF(muons, m_sys_up_SF, CP::SystematicVariation("MUON_EFF_TrigSystUncertainty", +1)) == CP::CorrectionCode::Error) {
63 } if (getSF(muons, m_sys_down_SF, CP::SystematicVariation("MUON_EFF_TrigSystUncertainty", -1)) == CP::CorrectionCode::Error) {
65 }
66 return true;
67 }
70 const CP::SystematicVariation &syst) {
71 if (muons->empty()) {
73 }
74 CP::SystematicSet syst_set;
75 syst_set.insert(syst);
76 if (m_handle->applySystematicVariation(syst_set) != StatusCode::SUCCESS) {
78 }
79 double Var{1.};
80 CP::CorrectionCode code = m_handle->getTriggerScaleFactor(*muons, Var, m_trigger);
81 branch = Var;
82 return code;
83 }
84
85
86 //############################################################
87 // MuonSFBranches
88 //############################################################
89
91 const ToolHandle<CP::IMuonEfficiencyScaleFactors> &handle,
92 const std::string& relName) :
93 MuonVal::MuonTesterBranch{tree, relName + (relName.empty() ? "" : "_") +
94 getProperty<std::string>(handle.get(), "WorkingPoint")},
95 m_handle{handle} {}
96
98 auto mesf = dynamic_cast<const CP::MuonEfficiencyScaleFactors*>(m_handle.get());
99 if (!mesf) {
100 ATH_MSG_ERROR("Given tool not MuonCP::MuonEfficiencyScaleFactors");
101 return false;
102 }
103 m_uncorrelate_sys = mesf->uncorrelate_sys();
104
105 for (const auto& set : CP::make_systematics_vector(m_handle->recommendedSystematics())) {
106 m_SFs.emplace_back(std::make_unique<SFSet>(set, *this));
107 if (!m_SFs.back()->init()) return false;
108 }
109 return true;
110 }
111
112 bool MuonSFBranches::fill(const EventContext& ctx) {
113 for (auto& sys : m_SFs) {
114 if (!sys->fill(ctx)) return false;
115 }
116 return true;
117 }
118
119
123 if (!m_uncorrelate_sys) {
124 for (auto& Syst_SF : m_SFs) {
125 CP::CorrectionCode cc = fillSystematic(muon, *Syst_SF);
126 if (cc == CP::CorrectionCode::Error){
127 return;
128 }
129 }
130 } else {
131 int bin = m_handle->getUnCorrelatedSystBin(muon);
132 if (bin < 0){
133 ATH_MSG_WARNING("Did not find a valid bin for muon with pT: "<<muon.pt() *MeVtoGeV <<" GeV, eta:"<<
134 muon.eta()<<", phi: "<<muon.phi());
135 return;
136 }
137 for (auto& Syst_SF: m_SFs){
139 bool process = Syst_SF->cpSet.name().empty() ||
140 std::find_if(Syst_SF->cpSet.begin(), Syst_SF->cpSet.end(),
141 [bin](const CP::SystematicVariation& t){
142 return t.isToyVariation() && t.getToyVariation().first ==
143 static_cast<unsigned>(bin);
144 }) != Syst_SF->cpSet.end();
145 if (!process) continue;
146 CP::CorrectionCode cc = fillSystematic(muon, *Syst_SF);
147 if (cc == CP::CorrectionCode::Error) {
148 return;
149 }
150 }
151 }
152 }
153
155 SFSet& Syst_SF) {
156 if (m_handle->applySystematicVariation(Syst_SF.cpSet) != StatusCode::SUCCESS) {
157 ATH_MSG_ERROR("Failed to apply variation "<< Syst_SF.cpSet.name()<<" for "<< name());
159 }
160 float sf{1.f};
161 CP::CorrectionCode cc = m_handle->getEfficiencyScaleFactor(muon, sf);
162 if (cc == CP::CorrectionCode::Error) {
163 ATH_MSG_ERROR("Failed to retrieve the scale factor variation "<< Syst_SF.cpSet.name()<<" for "<< name());
165 }
166 Syst_SF.scaleFactor = sf;
167
169 if (std::abs(muon.eta()) > 2.5) {
170 Syst_SF.dataEff = -1;
171 Syst_SF.mcEff = -1;
173 }
174 cc = m_handle->getDataEfficiency(muon, sf);
175 if (cc == CP::CorrectionCode::Error) {
176 ATH_MSG_ERROR("Failed to retrieve the data efficiency variation "<< Syst_SF.cpSet.name()<<" for "<< name());
177 return cc;
178 }
179 Syst_SF.dataEff = sf;
180 cc = m_handle->getMCEfficiency(muon, sf);
181 if (cc == CP::CorrectionCode::Error) {
182 ATH_MSG_ERROR("Failed to retrieve the mc efficiency variation "<< Syst_SF.cpSet.name()<<" for "<< name());
184 }
185 Syst_SF.mcEff = sf;
187 }
188
189
190 //############################################################
191 // MuonReplicaBranches
192 //############################################################
194 const ToolHandle<CP::IMuonEfficiencyScaleFactors> &handle,
195 const std::string& relName) :
196 MuonVal::MuonTesterBranch{tree, relName + (relName.empty() ? "" : "_") +
197 getProperty<std::string>(handle.get(), "WorkingPoint") + "SFReplica"},
198 m_handle{handle} {
199 }
201 for (auto set : CP::make_systematics_vector(m_handle->recommendedSystematics())) {
202 auto br =std::make_shared<MuonVal::VectorBranch<float>>(tree(), name() + MuonSFBranches::systName(set));
203 m_SFs[set] = br;
204 if(!br->init()) return false;
205 }
206 return true;
207 }
208 bool MuonReplicaBranches::fill(const EventContext& ctx) {
209 for (auto& [sys, br] : m_SFs) {
210 if (!br->fill(ctx)) {
211 ATH_MSG_ERROR("Failed to fill "<<sys.name());
212 return false;
213 }
214 }
215 return true;
216 }
217
219 for (auto& [cpSys, br] : m_SFs) {
220 if (m_handle->applySystematicVariation(cpSys) != StatusCode::SUCCESS) {
221 return;
222 }
223 std::vector<float> replica{};
224 CP::CorrectionCode cc = m_handle->getEfficiencyScaleFactorReplicas(muon, replica);
225 if (cc == CP::CorrectionCode::Error) {
226 return;
227 }
228 for (auto repl : replica) {
229 br->push_back(repl);
230 }
231 }
232 }
233}
234#endif
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_WARNING(x)
xAOD::MuonContainer * muonContainer
static const Attributes_t empty
Return value from object correction CP tools.
@ Error
Some error happened during the object correction.
@ Ok
The correction was done successfully.
Class to wrap a set of SystematicVariations.
std::string name() const
returns: the systematics joined into a single string.
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
bool empty() const noexcept
Returns true if the collection is empty.
MuonTesterBranch(MuonTesterTree &tree, const std::string &br_name)
bool declare_dependency(Key &key)
Declares the ReadHandle/ ReadCondHandleKey as data dependency of the algorithm.
std::string name() const override final
Returns the name of the branch.
TTree * tree() override final
Returns the underlying TTree object.
const_pointer_type cptr()
Dereference the pointer.
bool isPresent() const
Is the referenced object present in SG?
bool init() override
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
void setMuon(const xAOD::Muon &muon) override final
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_handle
bool fill(const EventContext &ctx) override
The fill method checks if enough information is provided such that the branch is cleared from the inf...
std::map< CP::SystematicSet, std::shared_ptr< MuonVal::VectorBranch< float > > > m_SFs
MuonReplicaBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonEfficiencyScaleFactors > &handle, const std::string &rel_name="")
MuonSFBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonEfficiencyScaleFactors > &handle, const std::string &rel_name="")
static std::string systName(const CP::SystematicSet &set)
Dummy helper function that encodes the systname set.
bool init() override final
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
bool fill(const EventContext &ctx) override final
The fill method checks if enough information is provided such that the branch is cleared from the inf...
std::vector< std::unique_ptr< SFSet > > m_SFs
CP::CorrectionCode fillSystematic(const xAOD::Muon &muon, SFSet &set)
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_handle
void setMuon(const xAOD::Muon &muon) override final
bool fill(const EventContext &ctx) override final
The fill method checks if enough information is provided such that the branch is cleared from the inf...
TriggerSFBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonTriggerScaleFactors > &Handle, const std::string &muonContainer, const std::string &Trigger)
ToolHandle< CP::IMuonTriggerScaleFactors > m_handle
MuonVal::ScalarBranch< double > m_sys_down_SF
bool init() override final
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
MuonVal::ScalarBranch< double > m_nominal_SF
SG::ReadHandleKey< xAOD::MuonContainer > m_key
MuonVal::ScalarBranch< double > m_stat_up_SF
CP::CorrectionCode getSF(const xAOD::MuonContainer *muons, MuonVal::ScalarBranch< double > &Var, const CP::SystematicVariation &syst)
MuonVal::ScalarBranch< double > m_stat_down_SF
MuonVal::ScalarBranch< double > m_sys_up_SF
Base class for the dual-use tool implementation classes.
Definition AsgTool.h:47
const T * getProperty(const std::string &name) const
Get one of the tool's properties.
Base class for the dual-use tool interface classes.
Definition IAsgTool.h:41
STL class.
STL class.
const std::string process
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
constexpr float MeVtoGeV
Class to store array like branches into the n-tuples.
Definition HitValAlg.cxx:19
T getProperty(const asg::IAsgTool *interface_tool, const std::string &prop_name)
STL namespace.
Muon_v1 Muon
Reference the current persistent version:
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
Helper struct to store scale factor, data efficiency + mc efficiency for each systematic variation.
const CP::SystematicSet cpSet
Systematic set.
MuonVal::ScalarBranch< float > scaleFactor
Actual branches storing scaleFactor & mcEff & dataEff.
MuonVal::ScalarBranch< float > dataEff
MuonVal::ScalarBranch< float > mcEff