ATLAS Offline Software
SoftMuonObjectCollectionMaker.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3  */
4 
8 #include "TopEvent/EventTools.h"
9 
12 #include "xAODMuon/MuonContainer.h"
14 #include "xAODCore/ShallowCopy.h"
18 
19 namespace top {
21  asg::AsgTool(name),
22  m_config(nullptr),
23 
24  m_specifiedSystematics(),
25  m_recommendedSystematics(),
26 
27  m_calibrationTool("MuonMomentumCalibrationTool"),
28  m_muonSelectionToolVeryLooseVeto("MuonSelectionToolVeryLooseVeto"),
29  m_softmuonSelectionTool("SoftMuonSelectionTool"){
30  declareProperty("config", m_config);
31 
32  declareProperty("MuonMomentumCalibrationTool", m_calibrationTool);
33  declareProperty("MuonSelectionToolVeryLooseVeto", m_muonSelectionToolVeryLooseVeto);
34  declareProperty("SoftMuonSelectionTool", m_softmuonSelectionTool);
35  }
36 
38  ATH_MSG_INFO(" top::SoftMuonObjectCollectionMaker initialize");
39 
40  top::check(m_calibrationTool.retrieve(), "Failed to retrieve muon calibration tool");
41  top::check(m_muonSelectionToolVeryLooseVeto.retrieve(), "Failed to retrieve Selection Tool");
42  top::check(m_softmuonSelectionTool.retrieve(),"Failed to retrieve Selection Tool");
43 
45  const std:: string& syststr = m_config->systematics();
46  std::set<std::string> syst;
47 
48  if (!m_config->isSystNominal(syststr) && !m_config->isSystAll(syststr)) {
49  bool ok = m_config->getSystematicsList(syststr, syst);
50  if (!ok) {
51  ATH_MSG_ERROR(" top::SoftMuonObjectCollectionMaker could not determine systematic list");
52  return StatusCode::FAILURE;
53  }
54  //here the idea is that if the user specifies AllXXX, we leave syst as an empty string, so that all recommended CP
55  // systematics are then used
56  if (m_config->contains(syst, "AllMuons")) {
57  syst.clear();
58  }
59  if (m_config->contains(syst, "AllSoftMuons")) {
60  syst.clear();
61  }
62  }
63 
65 
66  m_config->systematicsSoftMuons(specifiedSystematics());
67 
68  ATH_MSG_INFO(" top::SoftMuonObjectCollectionMaker completed initialize");
69  return StatusCode::SUCCESS;
70  }
71 
73  const xAOD::EventInfo* eventInfo(nullptr);
74 
75  top::check(evtStore()->retrieve(eventInfo, m_config->sgKeyEventInfo()), "Failed to retrieve EventInfo");
76  const float beam_pos_sigma_x = eventInfo->beamPosSigmaX();
77  const float beam_pos_sigma_y = eventInfo->beamPosSigmaY();
78  const float beam_pos_sigma_xy = eventInfo->beamPosSigmaXY();
79 
81  const xAOD::MuonContainer* xaod(nullptr);
82 
83  top::check(evtStore()->retrieve(xaod, m_config->sgKeyMuons()), "Failed to retrieve Soft Muons"); //we use sgKeyMuons and not sgKeySoftMuons on purpose to use the same collection for muons and soft muons
84 
86  for (auto systematic : m_specifiedSystematics) {
88  if (executeNominal && !m_config->isSystNominal(m_config->systematicName(systematic.hash()))) continue;
89  if (!executeNominal && m_config->isSystNominal(m_config->systematicName(systematic.hash()))) continue;
90 
92  top::check(m_calibrationTool->applySystematicVariation(systematic), "Failed to applySystematicVariation");
93 
95  std::pair< xAOD::MuonContainer*,
96  xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer(*xaod);
97 
99  for (xAOD::Muon* muon : *(shallow_xaod_copy.first)) {
100 
102  if (muon->primaryTrackParticle()) {
103  top::check(m_calibrationTool->applyCorrection(*muon), "Failed to applyCorrection");
104 
105  // don't do the decorations unless the muons are at least Loose
106  // this is because it may fail if the muons are at just VeryLoose
107  if (m_muonSelectionToolVeryLooseVeto->accept(*muon)||(m_config->softmuonUseLowPt() && m_softmuonSelectionTool->accept(*muon))) {
108  double d0sig = xAOD::TrackingHelpers::d0significance(muon->primaryTrackParticle(),
109  beam_pos_sigma_x,
110  beam_pos_sigma_y,
111  beam_pos_sigma_xy);
112  muon->auxdecor<float>("d0sig") = d0sig;
113 
114  if (eventInfo->isAvailable<float>("AnalysisTop_PRIVTX_z_position")) {
115  float vtx_z = eventInfo->auxdata<float>("AnalysisTop_PRIVTX_z_position");
116  float delta_z0 = muon->primaryTrackParticle()->z0() + muon->primaryTrackParticle()->vz() - vtx_z;
117  muon->auxdecor<float>("delta_z0") = delta_z0;
118  muon->auxdecor<float>("delta_z0_sintheta") = delta_z0 * std::sin(muon->primaryTrackParticle()->theta());
119  }
120  }
121  }//end of if (muon->primaryTrackParticle())
122 
123  }//end of loop on muons
124 
126  bool setLinks = xAOD::setOriginalObjectLink(*xaod, *shallow_xaod_copy.first);
127  if (!setLinks) {
128  ATH_MSG_ERROR(" Cannot set original object links for soft muons");
129  return StatusCode::FAILURE;
130  }
131 
133  std::string outputSGKey = m_config->sgKeySoftMuons(systematic.hash());
134  std::string outputSGKeyAux = outputSGKey + "Aux.";
135 
136  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
137  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
138  if (!save || !saveAux) {
139  return StatusCode::FAILURE;
140  }
141  } // Loop over all systematics
142 
143  return StatusCode::SUCCESS;
144  }
145 
148  for (auto systematic : m_specifiedSystematics) {
149  const xAOD::MuonContainer* xaod(nullptr);
150  top::check(evtStore()->retrieve(xaod, m_config->sgKeySoftMuons(
151  systematic.hash())), "Failed to retrieve Soft Muons");
152 
153  ATH_MSG_INFO(" Soft Muons with sgKey = " << m_config->sgKeySoftMuons(systematic.hash()));
154  for (auto muon : *xaod) {
155  ATH_MSG_INFO(" SOFT MU pT , eta = " << muon->pt() << " , " << muon->eta());
156  }
157  }
158 
159  return StatusCode::SUCCESS;
160  }
161 
162  void SoftMuonObjectCollectionMaker::specifiedSystematics(const std::set<std::string>& specifiedSystematics) {
164  const std::vector<CP::SystematicSet> systList = CP::make_systematics_vector(
165  m_calibrationTool->recommendedSystematics());
166 
167  for (const CP::SystematicSet& s : systList) {
168 
169  if(!m_config->getTreeFilter()->filterTree(s.name())) continue; // Applying tree filter
170  m_recommendedSystematics.push_back(s);
171  if (s.name() == "") {
172  m_specifiedSystematics.push_back(s);
173  }
174 
176  if (m_config->isMC()) {
178  if (!m_config->isSystNominal(m_config->systematics())) {
179  if (specifiedSystematics.size() == 0) {
180  m_specifiedSystematics.push_back(s);
181  }
182  if (specifiedSystematics.size() > 0) {
183  for (auto i : specifiedSystematics) {
185  if (!filter.filterTree(s.name())) {
186  m_specifiedSystematics.push_back(s);
187  }
188  }
189  }
190  }
191  }
192  }
194  m_recommendedSystematics.unique();
195  m_specifiedSystematics.sort();
196  m_specifiedSystematics.unique();
197  }
198 
199 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ShallowCopy.h
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
SoftMuonObjectCollectionMaker.h
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
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
top::SoftMuonObjectCollectionMaker::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: SoftMuonObjectCollectionMaker.cxx:37
TreeFilter.h
xAOD::TrackingHelpers::d0significance
double d0significance(const xAOD::TrackParticle *tp, double d0_uncert_beam_spot_2)
Definition: TrackParticlexAODHelpers.cxx:42
top::SoftMuonObjectCollectionMaker::m_recommendedSystematics
std::list< CP::SystematicSet > m_recommendedSystematics
Definition: SoftMuonObjectCollectionMaker.h:67
xAOD::ShallowAuxContainer
Class creating a shallow copy of an existing auxiliary container.
Definition: ShallowAuxContainer.h:54
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
xAOD::EventInfo_v1::beamPosSigmaX
float beamPosSigmaX() const
The width of the beam spot in the X direction.
top::SoftMuonObjectCollectionMaker::specifiedSystematics
virtual const std::list< CP::SystematicSet > & specifiedSystematics() const
Definition: SoftMuonObjectCollectionMaker.h:56
top::SoftMuonObjectCollectionMaker::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: SoftMuonObjectCollectionMaker.h:65
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
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
top::SoftMuonObjectCollectionMaker::SoftMuonObjectCollectionMaker
SoftMuonObjectCollectionMaker(const std::string &name)
Definition: SoftMuonObjectCollectionMaker.cxx:20
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
top::SoftMuonObjectCollectionMaker::m_calibrationTool
ToolHandle< CP::IMuonCalibrationAndSmearingTool > m_calibrationTool
Definition: SoftMuonObjectCollectionMaker.h:69
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
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
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::SoftMuonObjectCollectionMaker::m_muonSelectionToolVeryLooseVeto
ToolHandle< CP::IMuonSelectionTool > m_muonSelectionToolVeryLooseVeto
Definition: SoftMuonObjectCollectionMaker.h:73
IParticleHelpers.h
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
top::SoftMuonObjectCollectionMaker::execute
StatusCode execute(bool)
Definition: SoftMuonObjectCollectionMaker.cxx:72
top::SoftMuonObjectCollectionMaker::m_specifiedSystematics
std::list< CP::SystematicSet > m_specifiedSystematics
Definition: SoftMuonObjectCollectionMaker.h:66
top::SoftMuonObjectCollectionMaker::printout
StatusCode printout()
Definition: SoftMuonObjectCollectionMaker.cxx:146
AuxElement.h
Base class for elements of a container that can have aux data.
top::SoftMuonObjectCollectionMaker::m_softmuonSelectionTool
ToolHandle< CP::IMuonSelectionTool > m_softmuonSelectionTool
Definition: SoftMuonObjectCollectionMaker.h:73
SystematicsUtil.h