ATLAS Offline Software
TopMuonCPTools.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <map>
8 #include <string>
9 
10 // Top includes
12 #include "TopEvent/EventTools.h"
13 
14 // PathResolver include(s):
16 
17 // Muon include(s):
22 
23 namespace top {
24  MuonCPTools::MuonCPTools(const std::string& name) :
25  asg::AsgTool(name) {
26  declareProperty("config", m_config);
27 
28  declareProperty("MuonMomentumCalibrationTool", m_muonMomentumCalibrationTool);
29 
30  declareProperty("MuonSelectionTool", m_muonSelectionTool);
31  declareProperty("MuonSelectionToolLoose", m_muonSelectionToolLoose);
32  declareProperty("MuonSelectionToolVeryLooseVeto", m_muonSelectionToolVeryLooseVeto);
33 
34  declareProperty("MuonEfficiencyCorrectionsTool", m_muonEfficiencyCorrectionsTool);
35  declareProperty("MuonEfficiencyCorrectionsToolLoose", m_muonEfficiencyCorrectionsToolLoose);
36  declareProperty("MuonEfficiencyCorrectionsToolIso", m_muonEfficiencyCorrectionsToolIso);
37  declareProperty("MuonEfficiencyCorrectionsToolLooseIso", m_muonEfficiencyCorrectionsToolLooseIso);
38  declareProperty("MuonEfficiencyCorrectionsToolBadMuonVeto", m_muonEfficiencyCorrectionsToolBadMuonVeto);
39 
40  declareProperty("SoftMuonSelectionTool", m_softmuonSelectionTool);
41  declareProperty("SoftMuonEfficiencyCorrectionsTool", m_softmuonEfficiencyCorrectionsTool);
42 
43  }
44 
46  ATH_MSG_INFO("top::MuonCPTools initialize...");
47 
48  if (m_config->isTruthDxAOD()) {
49  ATH_MSG_INFO("top::MuonCPTools: no need to initialise anything on truth DxAOD");
50  return StatusCode::SUCCESS;
51  }
52 
53  if (!m_config->useMuons() && !m_config->useSoftMuons()) {
54  ATH_MSG_INFO("top::MuonCPTools: no need to initialise anything since not using muons");
55  return StatusCode::SUCCESS;
56  }
57 
58  if (m_config->makeAllCPTools()) {// skiping calibrations on mini-xAODs
59  top::check(setupCalibration(), "Failed to setup Muon calibration tools");
60  }
61  if (m_config->isMC()) {// scale-factors are only for MC
62  top::check(setupScaleFactors(), "Failed to setup Muon scale-factor tools");
63  }
64  return StatusCode::SUCCESS;
65  }
66 
69  m_muonSelectionTool = setupMuonSelectionTool("MuonSelectionTool",
70  m_config->muonQuality(),
71  m_config->muonEtacut(),
72  m_config->muonUseMVALowPt(),
73  m_config->muonUse2stationMuonsHighPt());
74  m_muonSelectionToolLoose = setupMuonSelectionTool("MuonSelectionToolLoose",
75  m_config->muonQualityLoose(),
76  m_config->muonEtacut(),
77  m_config->muonUseMVALowPtLoose(),
78  m_config->muonUse2stationMuonsHighPtLoose());
79  // the following is needed to make sure all muons for which d0sig is calculated are at least Loose
80  m_muonSelectionToolVeryLooseVeto = setupMuonSelectionTool("MuonSelectionToolVeryLooseVeto",
81  "Loose",
82  2.5,
83  m_config->muonUseMVALowPt(),
84  m_config->muonUse2stationMuonsHighPt());
87  m_config->muonMuonDoExtraSmearingHighPt(),
88  m_config->muonMuonDoSmearing2stationHighPt());
89  //now the soft muon part
90  if (m_config->useSoftMuons()) {
91  m_softmuonSelectionTool = setupMuonSelectionTool("SoftMuonSelectionTool",
92  m_config->softmuonQuality(),
93  m_config->softmuonEtacut(),
94  m_config->softmuonUseMVALowPt(),
95  false);
96  }
97 
98  return StatusCode::SUCCESS;
99  }
100 
102  // Setup muon SF tools
103  // However if we are running on data- we don't need these,
104  // so carry on.
105  if (!m_config->isMC()) return StatusCode::SUCCESS;
106 
107  /************************************************************
108  *
109  * Muon Scale Factors:
110  * muonSF = trigSF*effSF*isoSF*TTVASF
111  *
112  ************************************************************/
113 
114  /************************************************************
115  * Trigger Scale Factors:
116  * setup trigger SFs for nominal and 'loose' muon WPs
117  * recommendation for EOYE not to pass any isolation to tool
118  * as SFs very similar for all WPs.
119  ************************************************************/
120 
121  // In R21 now, we only need one instance of the tool
122  // and do not need to set the year as it is handled
123  // internally with PRW tool
125  = setupMuonTrigSFTool("MuonTriggerScaleFactors_R21",
126  m_config->muonQuality());
128  = setupMuonTrigSFTool("MuonTriggerScaleFactorsLoose_R21",
129  m_config->muonQualityLoose());
130 
131  /************************************************************
132  * Efficiency Scale Factors:
133  * setup muon efficiency SFs for the nominal and
134  * 'loose' muon WPs.
135  ************************************************************/
136 
137  //if !Use2stationMuonsHighPt, HighPt -> HighPt3Layers
138  //if UseMVALowPt, LowPt -> LowPtMVA
139  std::string muonQuality_name = m_config->muonQuality();
140  if (m_config->muonQuality() == "HighPt" && !(m_config->muonUse2stationMuonsHighPt()) ) muonQuality_name = "HighPt3Layers";
141  if (m_config->muonQuality() == "LowPt" && m_config->muonUseMVALowPt()) muonQuality_name = "LowPtMVA";
143  = setupMuonSFTool("MuonEfficiencyScaleFactorsTool",
144  muonQuality_name, false);
145 
146  std::string muonQualityLoose_name = m_config->muonQualityLoose();
147  if (m_config->muonQualityLoose() == "HighPt" && !(m_config->muonUse2stationMuonsHighPtLoose()) ) muonQualityLoose_name = "HighPt3Layers";
148  if (m_config->muonQualityLoose() == "LowPt" && m_config->muonUseMVALowPtLoose()) muonQualityLoose_name = "LowPtMVA";
150  = setupMuonSFTool("MuonEfficiencyScaleFactorsToolLoose",
151  muonQualityLoose_name, false);
152 
153  if (m_config->muonQuality() == "HighPt" || m_config->muonQualityLoose() == "HighPt") {
155  = setupMuonSFTool("MuonEfficiencyScaleFactorsToolBadMuonVeto",
156  "BadMuonVeto_HighPt", false);
157  }
158 
159  //now the soft muon part
160  std::string softmuonQuality_name = m_config->softmuonQuality();
161  if (m_config->softmuonQuality() == "LowPt" && m_config->softmuonUseMVALowPt()) softmuonQuality_name = "LowPtMVA";
162  if (m_config->useSoftMuons()) {
164  = setupMuonSFTool("SoftMuonEfficiencyScaleFactorsTool",
165  softmuonQuality_name, false);
166  }
167 
168  /************************************************************
169  * Isolation Scale Factors:
170  * setup muon isolation SFs for the nominal and 'loose'
171  * muons
172  *
173  * Note: if isolation WP is None, then don't setup the tool
174  ************************************************************/
175  // If we don't want isolation then we don't need the tool
176  if (m_config->muonIsolationSF() != "None") {
177  // Add iso as a suffix (see above for consistency between tools :) )
178  std::string muon_isolation = m_config->muonIsolationSF() + "Iso";
180  setupMuonSFTool("MuonEfficiencyScaleFactorsToolIso",
181  muon_isolation, true);
182  }
183 
184  // Do we have isolation on our loose muons? If not no need for the tool...
185  if (m_config->muonIsolationSFLoose() != "None") {
186  // Add iso as a suffix (see above for consistency between tools :) )
187  std::string muon_isolation = m_config->muonIsolationSFLoose() + "Iso";
189  setupMuonSFTool("MuonEfficiencyScaleFactorsToolLooseIso",
190  muon_isolation, true);
191  }
192 
193  /************************************************************
194  * Muon TTVA SF:
195  * Track-to-vertex association. This depends on whether or
196  * not we apply the tracking groups recommended impact
197  * parameter cuts to associate muon to vertex.
198  ************************************************************/
200  = setupMuonSFTool("MuonEfficiencyScaleFactorsToolTTVA",
201  "TTVA", false);
202 
203  // WARNING - The PromptLeptonIsolation scale factors are only derived with respect to the loose PID
204  // - Hence we need to fail if this has occured
205  if ((m_config->muonQuality() != "Loose" && m_config->muonIsolationSF() == "PromptLepton")
206  || (m_config->muonQualityLoose() != "Loose" && m_config->muonIsolationSFLoose() == "PromptLepton")) {
208  "Cannot use PromptLeptonIsolation on muons without using Loose quality - Scale factors are not available");
209  return StatusCode::FAILURE;
210  }
211 
212  return StatusCode::SUCCESS;
213  }
214 
216  MuonCPTools::setupMuonSelectionTool(const std::string& name, const std::string& quality, double max_eta, const bool& useMVALowPt, const bool& use2stationMuonsHighPt) {
217  std::map<std::string, int> muon_quality_map = {
218  {"Tight", 0}, {"Medium", 1}, {"Loose", 2}, {"VeryLoose", 3}, {"HighPt", 4}, {"LowPt", 5}
219  };
220  int qual_int;
221  try {
222  qual_int = muon_quality_map.at(quality);
223  } catch (const std::out_of_range& oor_exc) {
224  ATH_MSG_ERROR("\n Invalid muon quality ("
225  + quality + ") for " + name
226  + ". Valid options are: "
227  " \n\t- Tight"
228  " \n\t- Medium"
229  " \n\t- Loose"
230  " \n\t- VeryLoose"
231  " \n\t- HighPt"
232  " \n\t- LowPt");
233  throw; // Re-throw
234  }
235 
236  CP::IMuonSelectionTool* tool = nullptr;
237  if (asg::ToolStore::contains<CP::IMuonSelectionTool>(name)) {
238  tool = asg::ToolStore::get<CP::IMuonSelectionTool>(name);
239  } else {
241  top::check(asg::setProperty(tool, "IsRun3Geo", m_config->isRun3()),
242  "Failed to set IsRun3Geo for " + name);
243  top::check(asg::setProperty(tool, "MuQuality", qual_int),
244  "Failed to set MuQuality for " + name);
245  top::check(asg::setProperty(tool, "MaxEta", max_eta),
246  "Failed to set MaxEta for " + name);
247  top::check(asg::setProperty(tool, "UseMVALowPt", useMVALowPt),
248  "Failed to set UseMVALowPt for " + name + " tool");
249  top::check(asg::setProperty(tool, "Use2stationMuonsHighPt", use2stationMuonsHighPt),
250  "Failed to set Use2stationMuonsHighPt for " + name + " tool");
251  top::check(tool->initialize(), "Failed to initialize " + name);
252  }
253  return tool;
254  }
255 
257  MuonCPTools::setupMuonTrigSFTool(const std::string& name, const std::string& quality) {
259  if (asg::ToolStore::contains<CP::IMuonTriggerScaleFactors>(name)) {
260  tool = asg::ToolStore::get<CP::IMuonTriggerScaleFactors>(name);
261  } else {
263  top::check(asg::setProperty(tool, "MuonQuality", quality),
264  "Failed to set MuonQuality for " + name);
265  top::check(asg::setProperty(tool, "AllowZeroSF", false),
266  "Failed to set AllowZeroSF for " + name);
267  if (m_config->muonSFCustomInputFolderTrigger() != " ") {
268  top::check(asg::setProperty(tool, "CustomInputFolder", m_config->muonSFCustomInputFolderTrigger()),
269  "Failed to set CustomInputFolder property for MuonTriggerScaleFactors tool");
270  }
271  if (m_config->muonForcePeriod() != " ") {
272  top::check(asg::setProperty(tool, "forcePeriod", m_config->muonForcePeriod()),
273  "Failed to set forcePeriod property for MuonTriggerScaleFactors tool");
274  }
275  if (m_config->muonForceYear() != -1) {
276  top::check(asg::setProperty(tool, "forceYear", m_config->muonForceYear()),
277  "Failed to set forceYear property for MuonTriggerScaleFactors tool");
278  }
279  top::check(tool->initialize(), "Failed to init. " + name);
280  }
281  return tool;
282  }
283 
285  MuonCPTools::setupMuonSFTool(const std::string& name, const std::string& WP, const bool /*isIso*/) {
287  if (asg::ToolStore::contains<CP::IMuonEfficiencyScaleFactors>(name)) {
288  tool = asg::ToolStore::get<CP::MuonEfficiencyScaleFactors>(name);
289  } else {
291  top::check(asg::setProperty(tool, "WorkingPoint", WP),
292  "Failed to set WP for " + name + " tool");
293  top::check(asg::setProperty(tool, "CloseJetDRDecorator", "dRMuJet_AT_usingWeirdNameToAvoidUsingOnTheFlyCalculation"),
294  "Failed to set WP for " + name + " tool"); //in this way we'll only read the dR(mu,jet) from the derivation, IF the variable is there, but we'll not use on-the-fly calculation, which is tricky in AT
295  if (m_config->muonSFCustomInputFolder() != " ") {
296  top::check(asg::setProperty(tool, "CustomInputFolder", m_config->muonSFCustomInputFolder()),
297  "Failed to set CustomInputFolder property for MuonEfficiencyScaleFactors tool");
298  }
299  if (m_config->isRun3()) {
300  // the MuonEfficiencyScaleFactors tool is set up for Run 2 by default
301  top::check(asg::setProperty(tool, "CalibrationRelease", "230309_Preliminary_r22run3"),
302  "Failed to set CalibrationRelease property for MuonEfficiencyScaleFactors tool");
303  }
304  top::check(asg::setProperty(tool, "BreakDownSystematics", m_config->muonBreakDownSystematics()),
305  "Failed to set BreakDownSystematics for " + name + " tool");
306  top::check(tool->initialize(),
307  "Failed to set initialize " + name);
308  }
309  return tool;
310  }
311 
312 
314  MuonCPTools::setupMuonCalibrationAndSmearingTool(const std::string& name, const bool& doExtraSmearingHighPt, const bool& do2StationsHighPt) {
316  if (asg::ToolStore::contains<CP::IMuonCalibrationAndSmearingTool>(name)) {
317  tool = asg::ToolStore::get<CP::IMuonCalibrationAndSmearingTool>(name);
318  } else {
319  tool = new CP::MuonCalibTool(name);
320 
321  top::check(asg::setProperty(tool, "doExtraSmearing", doExtraSmearingHighPt),
322  "Failed to set doExtraSmearing for " + name + " tool");
323  top::check(asg::setProperty(tool, "do2StationsHighPt", do2StationsHighPt),
324  "Failed to set do2StationsHighPt for " + name + " tool");
325  top::check(asg::setProperty(tool, "IsRun3Geo", m_config->isRun3()),
326  "Failed to set IsRun3Geo for " + name + " tool");
327  if (m_config->isMC() && m_config->forceRandomRunNumber() > 0) {
328  top::check(asg::setProperty(tool, "useRandomRunNumber", false),
329  "Failed to set useRandomRunNumber for " + name + " tool");
330  }
331  if (!m_config->isRun3()) {
332  // the MuonCalib tool is set up for Run 3 by default
333  top::check(asg::setProperty(tool, "release", "Recs2022_08_16_Run3PreRecs"),
334  "Failed to set Run 2 release for " + name + " tool");
335  }
336  if (m_config->muonCalibMode() == "correctData_CB")
337  top::check(asg::setProperty(tool, "calibMode", CP::MuonCalibTool::CalibMode::correctData_CB), "Failed to set calibrationMode for " + name + " tool");
338  else if (m_config->muonCalibMode() == "correctData_IDMS")
339  top::check(asg::setProperty(tool, "calibMode", CP::MuonCalibTool::CalibMode::correctData_IDMS), "Failed to set calibrationMode for " + name + " tool");
340  else if (m_config->muonCalibMode() == "notCorrectData_IDMS")
341  top::check(asg::setProperty(tool, "calibMode", CP::MuonCalibTool::CalibMode::notCorrectData_IDMS), "Failed to set calibrationMode for " + name + " tool");
342  top::check(asg::setProperty(tool, "systematicScheme", m_config->muonSmearingSystematicModel()), "Failed to set systematicScheme for " + name + " tool");
343  top::check(tool->initialize(),
344  "Failed to set initialize " + name);
345  }
346  return tool;
347  }
348 } // namespace top
top::MuonCPTools::m_softmuonEfficiencyCorrectionsTool
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_softmuonEfficiencyCorrectionsTool
Definition: TopMuonCPTools.h:60
CP::MuonSelectionTool
Implementation of the muon selector tool.
Definition: MuonSelectionTool.h:31
top::MuonCPTools::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: TopMuonCPTools.h:35
MuonTriggerScaleFactors.h
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
top::MuonCPTools::setupMuonSFTool
CP::IMuonEfficiencyScaleFactors * setupMuonSFTool(const std::string &name, const std::string &WP, const bool isIso)
Definition: TopMuonCPTools.cxx:285
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MuonEfficiencyScaleFactors.h
IsoCloseByCorrectionTest.WP
WP
Definition: IsoCloseByCorrectionTest.py:56
top::MuonCPTools::m_muonEfficiencyCorrectionsToolIso
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsToolIso
Definition: TopMuonCPTools.h:53
asg
Definition: DataHandleTestTool.h:28
CP::IMuonEfficiencyScaleFactors
Definition: IMuonEfficiencyScaleFactors.h:102
CP::MuonEfficiencyScaleFactors
Definition: MuonEfficiencyScaleFactors.h:23
top::MuonCPTools::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: TopMuonCPTools.cxx:45
top::MuonCPTools::m_muonEfficiencyCorrectionsToolLooseIso
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsToolLooseIso
Definition: TopMuonCPTools.h:54
top::MuonCPTools::m_muonTriggerScaleFactorsLoose_R21
ToolHandle< CP::IMuonTriggerScaleFactors > m_muonTriggerScaleFactorsLoose_R21
Definition: TopMuonCPTools.h:49
top::MuonCPTools::m_muonEfficiencyCorrectionsToolBadMuonVeto
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsToolBadMuonVeto
Definition: TopMuonCPTools.h:57
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
CP::IMuonCalibrationAndSmearingTool
Definition: IMuonCalibrationAndSmearingTool.h:24
TopMuonCPTools.h
top::MuonCPTools::m_muonMomentumCalibrationTool
ToolHandle< CP::IMuonCalibrationAndSmearingTool > m_muonMomentumCalibrationTool
Definition: TopMuonCPTools.h:37
top::MuonCPTools::m_muonSelectionToolLoose
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionToolLoose
Definition: TopMuonCPTools.h:40
top::MuonCPTools::m_muonEfficiencyCorrectionsToolTTVA
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsToolTTVA
Definition: TopMuonCPTools.h:56
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
top::MuonCPTools::m_muonSelectionTool
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionTool
Definition: TopMuonCPTools.h:39
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
top::MuonCPTools::MuonCPTools
MuonCPTools(const std::string &name)
Definition: TopMuonCPTools.cxx:24
top::MuonCPTools::m_muonEfficiencyCorrectionsToolLoose
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsToolLoose
Definition: TopMuonCPTools.h:52
top::MuonCPTools::m_muonSelectionToolVeryLooseVeto
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionToolVeryLooseVeto
Definition: TopMuonCPTools.h:42
CP::IMuonTriggerScaleFactors
Definition: IMuonTriggerScaleFactors.h:37
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
MuonSelectionTool.h
top::MuonCPTools::m_muonTriggerScaleFactors_R21
ToolHandle< CP::IMuonTriggerScaleFactors > m_muonTriggerScaleFactors_R21
Definition: TopMuonCPTools.h:48
top::MuonCPTools::setupMuonCalibrationAndSmearingTool
CP::IMuonCalibrationAndSmearingTool * setupMuonCalibrationAndSmearingTool(const std::string &name, const bool &doExtraSmearingHighPt, const bool &do2StationsHighPt)
Definition: TopMuonCPTools.cxx:314
top::MuonCPTools::setupMuonSelectionTool
CP::IMuonSelectionTool * setupMuonSelectionTool(const std::string &name, const std::string &quality, double max_eta, const bool &useMVALowPt, const bool &use2stationMuonsHighPt)
Definition: TopMuonCPTools.cxx:216
module_driven_slicing.max_eta
max_eta
Definition: module_driven_slicing.py:166
top::MuonCPTools::setupScaleFactors
StatusCode setupScaleFactors()
Definition: TopMuonCPTools.cxx:101
PathResolver.h
CP::IMuonSelectionTool
Interface for (a) muon selector tool(s)
Definition: IMuonSelectionTool.h:25
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
CP::MuonCalibTool
Definition: MuonCalibTool.h:25
TopConfig.h
top::MuonCPTools::m_softmuonSelectionTool
ToolHandle< CP::IMuonSelectionTool > m_softmuonSelectionTool
Definition: TopMuonCPTools.h:59
top::MuonCPTools::m_muonEfficiencyCorrectionsTool
ToolHandle< CP::IMuonEfficiencyScaleFactors > m_muonEfficiencyCorrectionsTool
Definition: TopMuonCPTools.h:51
MuonCalibTool.h
CP::MuonTriggerScaleFactors
Definition: MuonTriggerScaleFactors.h:30
top::MuonCPTools::setupMuonTrigSFTool
CP::IMuonTriggerScaleFactors * setupMuonTrigSFTool(const std::string &name, const std::string &quality)
Definition: TopMuonCPTools.cxx:257
top::MuonCPTools::setupCalibration
StatusCode setupCalibration()
Definition: TopMuonCPTools.cxx:67