ATLAS Offline Software
MuonObjectCollectionMaker.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 // $Id: MuonObjectCollectionMaker.cxx 810751 2017-09-29 14:41:39Z iconnell $
9 #include "TopEvent/EventTools.h"
10 
13 #include "xAODMuon/MuonContainer.h"
15 #include "xAODCore/ShallowCopy.h"
19 
20 namespace top {
22  asg::AsgTool(name),
23  m_config(nullptr),
24 
25  m_specifiedSystematics(),
26  m_recommendedSystematics(),
27 
28  m_calibrationTool("MuonMomentumCalibrationTool"),
29  m_muonSelectionToolVeryLooseVeto("MuonSelectionToolVeryLooseVeto"),
30  m_muonSelectionTool("MuonSelectionTool"),
31  m_muonSelectionToolLoose("MuonSelectionToolLoose")
32  {
33  declareProperty("config", m_config);
34 
35  declareProperty("MuonMomentumCalibrationTool", m_calibrationTool);
36  declareProperty("MuonSelectionToolVeryLooseVeto", m_muonSelectionToolVeryLooseVeto);
37  declareProperty("MuonSelectionTool", m_muonSelectionTool);
38  declareProperty("MuonSelectionToolLoose", m_muonSelectionToolLoose);
39  }
40 
42  ATH_MSG_INFO(" top::MuonObjectCollectionMaker initialize");
43 
44  for (const std::string& WP : m_config->muonIsolationWPs()) {
45  m_muonIsolationTools[WP] = ToolHandle<CP::IIsolationSelectionTool>("IsolationTool_MuonWP_" + WP);
46  top::check(m_muonIsolationTools[WP].retrieve(), "Failed to retrieve muon isolation tool for WP " + WP);
47  }
48 
49  top::check(m_muonSelectionToolVeryLooseVeto.retrieve(), "Failed to retrieve Selection Tool");
50  top::check(m_muonSelectionTool.retrieve(), "Failed to retrieve Selection Tool");
51  top::check(m_muonSelectionToolLoose.retrieve(), "Failed to retrieve Selection Tool");
52 
54  const std:: string& syststr = m_config->systematics();
55  std::set<std::string> syst;
56 
57  if (!m_config->isSystNominal(syststr) && !m_config->isSystAll(syststr)) {
58  bool ok = m_config->getSystematicsList(syststr, syst);
59  if (!ok) {
60  ATH_MSG_ERROR(" top::MuonObjectCollectionMaker could not determine systematic list");
61  return StatusCode::FAILURE;
62  }
63  //here the idea is that if the user specifies AllXXX, we leave syst as an empty string, so that all recommended CP
64  // systematics are then used
65  if (m_config->contains(syst, "AllMuons")) {
66  syst.clear();
67  }
68  }
69 
71 
72  m_config->systematicsMuons(specifiedSystematics());
73 
75 
76  ATH_MSG_INFO(" top::MuonObjectCollectionMaker completed initialize");
77  return StatusCode::SUCCESS;
78  }
79 
81 
82  static const SG::AuxElement::ConstAccessor<int> chamberIndex("chamberIndex");
83  static const SG::AuxElement::ConstAccessor<short> PLV_TrackJetNTrack("PromptLeptonInput_TrackJetNTrack");
84  static const SG::AuxElement::ConstAccessor<float> PLV_DRlj("PromptLeptonInput_DRlj");
85  static const SG::AuxElement::ConstAccessor<float> PLV_PtRel("PromptLeptonInput_PtRel");
86  static const SG::AuxElement::ConstAccessor<float> PLV_PtFrac("PromptLeptonInput_PtFrac");
87  static const SG::AuxElement::ConstAccessor<float> PLV_PromptLeptonVeto("PromptLeptonVeto");
88 // static SG::AuxElement::Decorator<float> byhand_LowPtPLV("LowPtPLV");
89 
90  const xAOD::EventInfo* eventInfo(nullptr);
91 
92  top::check(evtStore()->retrieve(eventInfo, m_config->sgKeyEventInfo()), "Failed to retrieve EventInfo");
93  float beam_pos_sigma_x = eventInfo->beamPosSigmaX();
94  float beam_pos_sigma_y = eventInfo->beamPosSigmaY();
95  float beam_pos_sigma_xy = eventInfo->beamPosSigmaXY();
96 
98  const xAOD::MuonContainer* xaod(nullptr);
99  top::check(evtStore()->retrieve(xaod, m_config->sgKeyMuons()), "Failed to retrieve Muons");
100 
102  for (auto systematic : m_specifiedSystematics) {
104  if (executeNominal && !m_config->isSystNominal(m_config->systematicName(systematic.hash()))) continue;
105  if (!executeNominal && m_config->isSystNominal(m_config->systematicName(systematic.hash()))) continue;
106 
108  top::check(m_calibrationTool->applySystematicVariation(systematic), "Failed to applySystematicVariation");
109 
111  std::pair< xAOD::MuonContainer*,
112  xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer(*xaod);
113 
115  for (auto muon : *(shallow_xaod_copy.first)) {
116 
118  if (m_isFirstCheckForLowPtMVA && (m_config->muonUseMVALowPt() || m_config->muonUseMVALowPtLoose() || m_config->softmuonUseMVALowPt()) && muon->nMuonSegments()>0 && muon->muonSegment(0) ) {
119  if (!chamberIndex.isAvailable(*(muon->muonSegment(0)))) {
120  ATH_MSG_ERROR("MuonSegmentsAuxDyn.chamberIndex is not available in your derivation, so UseMVALowPt cannot be performed.");
121  ATH_MSG_ERROR("Please turn OFF UseMVALowPt or use more recent p-tag");
122  ATH_MSG_ERROR("AnalysisTop will crash soon...");
123  throw std::runtime_error("Missing MuonSegmentsAux.chamberIndex variable");
124  }
125  else m_isFirstCheckForLowPtMVA = false;
126  }
127 
129  if (muon->primaryTrackParticle()) {
130  top::check(m_calibrationTool->applyCorrection(*muon), "Failed to applyCorrection");
131 
132  // don't do the decorations unless the muons are at least Loose
133  // this is because it may fail if the muons are at just VeryLoose
134  if (m_muonSelectionToolVeryLooseVeto->accept(*muon) || (m_config->muonUseLowPt() && m_muonSelectionTool->accept(*muon))|| (m_config->muonUseLowPtLoose() && m_muonSelectionToolLoose->accept(*muon)))
135  {
136  double d0sig = xAOD::TrackingHelpers::d0significance(muon->primaryTrackParticle(),
137  beam_pos_sigma_x,
138  beam_pos_sigma_y,
139  beam_pos_sigma_xy);
140  muon->auxdecor<float>("d0sig") = d0sig;
141 
142  if (eventInfo->isAvailable<float>("AnalysisTop_PRIVTX_z_position")) {
143  float vtx_z = eventInfo->auxdata<float>("AnalysisTop_PRIVTX_z_position");
144  float delta_z0 = muon->primaryTrackParticle()->z0() + muon->primaryTrackParticle()->vz() - vtx_z;
145  muon->auxdecor<float>("delta_z0") = delta_z0;
146  muon->auxdecor<float>("delta_z0_sintheta") = delta_z0 * std::sin(muon->primaryTrackParticle()->theta());
147  }
148  }
149  }
150 
152  for (const auto& muonIsoWP : m_muonIsolationTools) {
153  const char passIsol = (muonIsoWP.second->accept(*muon)) ? 1 : 0;
154  muon->auxdecor<char>("AnalysisTop_Isol_" + muonIsoWP.first) = passIsol;
155  }
156  } // end loop over muons
157 
159  bool setLinks = xAOD::setOriginalObjectLink(*xaod, *shallow_xaod_copy.first);
160  if (!setLinks) ATH_MSG_ERROR(" Cannot set original object links for muons, MET recalculation may struggle");
161 
163  std::string outputSGKey = m_config->sgKeyMuons(systematic.hash());
164  std::string outputSGKeyAux = outputSGKey + "Aux.";
165 
166  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
167  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
168  if (!save || !saveAux) {
169  return StatusCode::FAILURE;
170  }
171  } // Loop over all systematics
172 
173  return StatusCode::SUCCESS;
174  }
175 
178  for (auto systematic : m_specifiedSystematics) {
179  const xAOD::MuonContainer* xaod(nullptr);
180  top::check(evtStore()->retrieve(xaod, m_config->sgKeyMuons(systematic.hash())), "Failed to retrieve Muons");
181 
182  ATH_MSG_INFO(" Muons with sgKey = " << m_config->sgKeyMuons(systematic.hash()));
183  for (auto muon : *xaod) {
184  ATH_MSG_INFO(" MU pT , eta = " << muon->pt() << " , " << muon->eta());
185  }
186  }
187 
188  return StatusCode::SUCCESS;
189  }
190 
191  void MuonObjectCollectionMaker::specifiedSystematics(const std::set<std::string>& specifiedSystematics) {
193  const std::vector<CP::SystematicSet> systList = CP::make_systematics_vector(
194  m_calibrationTool->recommendedSystematics());
195 
196  for (auto s : systList) {
197 
198  if(!m_config->getTreeFilter()->filterTree(s.name())) continue; // Applying tree filter
199  m_recommendedSystematics.push_back(s);
200  if (s.name() == "") {
201  m_specifiedSystematics.push_back(s);
202  }
203 
205  if (m_config->isMC()) {
207  if (!m_config->isSystNominal(m_config->systematics())) {
208  if (specifiedSystematics.size() == 0) {
209  m_specifiedSystematics.push_back(s);
210  }
211  if (specifiedSystematics.size() > 0) {
212  for (auto i : specifiedSystematics) {
214  if (!filter.filterTree(s.name())) {
215  m_specifiedSystematics.push_back(s);
216  }
217  }
218  }
219  }
220  }
221  }
223  m_recommendedSystematics.unique();
224  m_specifiedSystematics.sort();
225  m_specifiedSystematics.unique();
226  }
227 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ShallowCopy.h
top::MuonObjectCollectionMaker::m_specifiedSystematics
std::list< CP::SystematicSet > m_specifiedSystematics
Definition: MuonObjectCollectionMaker.h:72
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
top::MuonObjectCollectionMaker::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: MuonObjectCollectionMaker.h:70
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
TrackParticlexAODHelpers.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
top::MuonObjectCollectionMaker::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: MuonObjectCollectionMaker.cxx:41
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
IsoCloseByCorrectionTest.WP
WP
Definition: IsoCloseByCorrectionTest.py:56
top::MuonObjectCollectionMaker::m_calibrationTool
ToolHandle< CP::IMuonCalibrationAndSmearingTool > m_calibrationTool
Definition: MuonObjectCollectionMaker.h:75
TreeFilter.h
xAOD::TrackingHelpers::d0significance
double d0significance(const xAOD::TrackParticle *tp, double d0_uncert_beam_spot_2)
Definition: TrackParticlexAODHelpers.cxx:42
xAOD::ShallowAuxContainer
Class creating a shallow copy of an existing auxiliary container.
Definition: ShallowAuxContainer.h:54
asg
Definition: DataHandleTestTool.h:28
xAOD::EventInfo_v1::beamPosSigmaX
float beamPosSigmaX() const
The width of the beam spot in the X direction.
top::MuonObjectCollectionMaker::printout
StatusCode printout()
Definition: MuonObjectCollectionMaker.cxx:176
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
top::MuonObjectCollectionMaker::m_isFirstCheckForLowPtMVA
bool m_isFirstCheckForLowPtMVA
Definition: MuonObjectCollectionMaker.h:83
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
covarianceTool.filter
filter
Definition: covarianceTool.py:514
top::TreeFilter
Definition: TreeFilter.h:13
MuonAuxContainer.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
checkTP.save
def save(self, fileName="./columbo.out")
Definition: checkTP.py:178
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
top::MuonObjectCollectionMaker::m_muonIsolationTools
std::unordered_map< std::string, ToolHandle< CP::IIsolationSelectionTool > > m_muonIsolationTools
– Isolation –///
Definition: MuonObjectCollectionMaker.h:78
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
SG::AuxElement::auxdata
Accessor< T, ALLOC >::reference_type auxdata(const std::string &name)
Fetch an aux data variable, as a non-const reference.
xAOD::EventInfo_v1::beamPosSigmaY
float beamPosSigmaY() const
The width of the beam spot in the Y direction.
SG::AuxElement::isAvailable
bool isAvailable(const std::string &name, const std::string &clsname="") const
Check if an aux variable is available for reading.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
TopConfig.h
top::MuonObjectCollectionMaker::m_muonSelectionToolVeryLooseVeto
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionToolVeryLooseVeto
Definition: MuonObjectCollectionMaker.h:81
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
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
MuonContainer.h
MuonObjectCollectionMaker.h
xAOD::EventInfo_v1::beamPosSigmaXY
float beamPosSigmaXY() const
The beam spot shape's X-Y correlation.
xAOD::setOriginalObjectLink
bool setOriginalObjectLink(const IParticle &original, IParticle &copy)
This function should be used by CP tools when they make a deep copy of an object in their correctedCo...
Definition: IParticleHelpers.cxx:30
top::MuonObjectCollectionMaker::m_recommendedSystematics
std::list< CP::SystematicSet > m_recommendedSystematics
Definition: MuonObjectCollectionMaker.h:73
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
IParticleHelpers.h
top::MuonObjectCollectionMaker::execute
StatusCode execute(bool)
Definition: MuonObjectCollectionMaker.cxx:80
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
top::MuonObjectCollectionMaker::m_muonSelectionToolLoose
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionToolLoose
Definition: MuonObjectCollectionMaker.h:81
top::MuonObjectCollectionMaker::m_muonSelectionTool
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionTool
Definition: MuonObjectCollectionMaker.h:81
top::MuonObjectCollectionMaker::specifiedSystematics
virtual const std::list< CP::SystematicSet > & specifiedSystematics() const
Definition: MuonObjectCollectionMaker.h:62
AuxElement.h
Base class for elements of a container that can have aux data.
top::MuonObjectCollectionMaker::MuonObjectCollectionMaker
MuonObjectCollectionMaker(const std::string &name)
Definition: MuonObjectCollectionMaker.cxx:21
SystematicsUtil.h