ATLAS Offline Software
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 
13 namespace {
14  constexpr double MeVtoGeV = 1.e-3;
15 }
16 namespace 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},
34  m_key{muonContainer},
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) {
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;
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;
172  return CP::CorrectionCode::Ok;
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;
186  return CP::CorrectionCode::Ok;
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
muonContainer
xAOD::MuonContainer * muonContainer
Definition: TrigGlobEffCorrValidation.cxx:188
TestMuonSF::TriggerSFBranches::m_sys_up_SF
MuonVal::ScalarBranch< double > m_sys_up_SF
Definition: MuonSFTestHelper.h:58
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TestMuonSF::MuonSFBranches::SFSet::mcEff
MuonVal::ScalarBranch< float > mcEff
Definition: MuonSFTestHelper.h:115
TestMuonSF::TriggerSFBranches::TriggerSFBranches
TriggerSFBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonTriggerScaleFactors > &Handle, const std::string &muonContainer, const std::string &Trigger)
Definition: MuonSFTestHelper.cxx:29
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
TestMuonSF::MuonReplicaBranches::fill
bool fill(const EventContext &ctx) override
The fill method checks if enough information is provided such that the branch is cleared from the inf...
Definition: MuonSFTestHelper.cxx:208
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
MuonEfficiencyScaleFactors.h
TestMuonSF::MuonSFBranches::m_handle
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_handle
Definition: MuonSFTestHelper.h:92
TestMuonSF::MuonSFBranches::m_uncorrelate_sys
bool m_uncorrelate_sys
Definition: MuonSFTestHelper.h:93
tree
TChain * tree
Definition: tile_monitor.h:30
TestMuonSF::TriggerSFBranches::fill
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...
Definition: MuonSFTestHelper.cxx:48
TestMuonSF::MuonReplicaBranches::setMuon
void setMuon(const xAOD::Muon &muon) override final
Definition: MuonSFTestHelper.cxx:218
asg::IAsgTool
Base class for the dual-use tool interface classes.
Definition: IAsgTool.h:41
CP::MuonEfficiencyScaleFactors
Definition: MuonEfficiencyScaleFactors.h:23
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::MeVtoGeV
constexpr float MeVtoGeV
Definition: IsolationCloseByCorrectionTool.cxx:33
TestMuonSF::MuonSFBranches::SFSet
Helper struct to store scale factor, data efficiency + mc efficiency for each systematic variation.
Definition: MuonSFTestHelper.h:97
bin
Definition: BinsDiffFromStripMedian.h:43
TestMuonSF
Definition: MuonSFTestHelper.h:29
CP::SystematicSet::name
std::string name() const
returns: the systematics joined into a single string.
Definition: SystematicSet.cxx:278
asg::AsgTool::getProperty
const T * getProperty(const std::string &name) const
Get one of the tool's properties.
TestMuonSF::MuonSFBranches::SFSet::cpSet
const CP::SystematicSet cpSet
Systematic set.
Definition: MuonSFTestHelper.h:110
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
CP::SystematicVariation
Definition: SystematicVariation.h:47
TestMuonSF::MuonReplicaBranches::init
bool init() override
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
Definition: MuonSFTestHelper.cxx:200
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
MuonVal::MuonTesterBranch::tree
TTree * tree() override final
Returns the underlying TTree object.
Definition: MuonTesterBranch.cxx:53
TestMuonSF::TriggerSFBranches::m_handle
ToolHandle< CP::IMuonTriggerScaleFactors > m_handle
Definition: MuonSFTestHelper.h:53
MuonVal::MuonTesterBranch::declare_dependency
bool declare_dependency(Key &key)
Declares the ReadHandle/ ReadCondHandleKey as data dependency of the algorithm.
TestMuonSF::TriggerSFBranches::m_trigger
std::string m_trigger
Definition: MuonSFTestHelper.h:54
TestMuonSF::MuonReplicaBranches::m_handle
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_handle
Definition: MuonSFTestHelper.h:142
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
MuonVal::MuonTesterTree
Definition: MuonTesterTree.h:30
TestMuonSF::TriggerSFBranches::m_nominal_SF
MuonVal::ScalarBranch< double > m_nominal_SF
Definition: MuonSFTestHelper.h:55
TestMuonSF::TriggerSFBranches::m_sys_down_SF
MuonVal::ScalarBranch< double > m_sys_down_SF
Definition: MuonSFTestHelper.h:59
TestMuonSF::getProperty
T getProperty(const asg::IAsgTool *interface_tool, const std::string &prop_name)
Definition: MuonSFTestHelper.cxx:17
TestMuonSF::MuonSFBranches::fill
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...
Definition: MuonSFTestHelper.cxx:112
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
TestMuonSF::TriggerSFBranches::m_stat_up_SF
MuonVal::ScalarBranch< double > m_stat_up_SF
Definition: MuonSFTestHelper.h:56
python.TriggerAnalysisConfig.Trigger
def Trigger(seq)
Definition: TriggerAnalysisConfig.py:128
MuonVal::MuonTesterBranch::name
std::string name() const override final
Returns the name of the branch.
Definition: MuonTesterBranch.cxx:51
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:224
TestMuonSF::MuonSFBranches::fillSystematic
CP::CorrectionCode fillSystematic(const xAOD::Muon &muon, SFSet &set)
Definition: MuonSFTestHelper.cxx:154
MuonVal::ScalarBranch::init
bool init() override
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
MuonVal
Class to store array like branches into the n-tuples.
Definition: MuonTPMetaDataAlg.cxx:25
pmontree.code
code
Definition: pmontree.py:443
MuonVal::ScalarBranch< double >
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
TestMuonSF::MuonSFBranches::SFSet::scaleFactor
MuonVal::ScalarBranch< float > scaleFactor
Actual branches storing scaleFactor & mcEff & dataEff.
Definition: MuonSFTestHelper.h:114
TestMuonSF::MuonSFBranches::SFSet::dataEff
MuonVal::ScalarBranch< float > dataEff
Definition: MuonSFTestHelper.h:116
MuonSFTestHelper.h
TestMuonSF::MuonSFBranches::setMuon
void setMuon(const xAOD::Muon &muon) override final
Definition: MuonSFTestHelper.cxx:120
UtilFunctions.h
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
TestMuonSF::TriggerSFBranches::init
bool init() override final
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
Definition: MuonSFTestHelper.cxx:38
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
TestMuonSF::MuonSFBranches::MuonSFBranches
MuonSFBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonEfficiencyScaleFactors > &handle, const std::string &rel_name="")
Definition: MuonSFTestHelper.cxx:90
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TestMuonSF::MuonSFBranches::m_SFs
std::vector< std::unique_ptr< SFSet > > m_SFs
Definition: MuonSFTestHelper.h:121
CP::CorrectionCode
Return value from object correction CP tools.
Definition: CorrectionCode.h:31
TestMuonSF::TriggerSFBranches::m_stat_down_SF
MuonVal::ScalarBranch< double > m_stat_down_SF
Definition: MuonSFTestHelper.h:57
Prompt::Def::Var
Var
Definition: VarHolder.h:57
TestMuonSF::TriggerSFBranches::getSF
CP::CorrectionCode getSF(const xAOD::MuonContainer *muons, MuonVal::ScalarBranch< double > &Var, const CP::SystematicVariation &syst)
Definition: MuonSFTestHelper.cxx:68
TestMuonSF::TriggerSFBranches::m_key
SG::ReadHandleKey< xAOD::MuonContainer > m_key
Definition: MuonSFTestHelper.h:51
TestMuonSF::MuonReplicaBranches::m_SFs
std::map< CP::SystematicSet, std::shared_ptr< MuonVal::VectorBranch< float > > > m_SFs
Definition: MuonSFTestHelper.h:144
TestMuonSF::MuonSFBranches::init
bool init() override final
The init method checks whether the branch name has already registered to the MuonTree and tries then ...
Definition: MuonSFTestHelper.cxx:97
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
TestMuonSF::MuonReplicaBranches::MuonReplicaBranches
MuonReplicaBranches(MuonVal::MuonTesterTree &tree, const ToolHandle< CP::IMuonEfficiencyScaleFactors > &handle, const std::string &rel_name="")
Definition: MuonSFTestHelper.cxx:193
python.handimod.cc
int cc
Definition: handimod.py:523
TestMuonSF::MuonSFBranches::systName
static std::string systName(const CP::SystematicSet &set)
Dummy helper function that encodes the systname set.
Definition: MuonSFTestHelper.h:87
PlotCalibFromCool.br
br
Definition: PlotCalibFromCool.py:355
SystematicsUtil.h