ATLAS Offline Software
InDetHardScatterSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Local include(s):
6 #include <memory>
7 
8 // FrameWork include(s):
12 
13 // EDM include(s):
16 
17 // Tool include(s)
21 // Helper classes
22 namespace {
24  class SumPt2 : public Sum {
25  public:
26  virtual float add(const float a, const float b, const float, const float) const override final {
27  return a + (b * b);
28  }
29  };
30  class SumPt : public Sum {
31  public:
32  virtual float add(const float a, const float b, const float, const float ) const override final {
33  return a + b;
34  }
35  };
36  class SumPtw : public Sum {
37  public:
38  virtual float add(const float a, const float b, const float jet_pt, const float mindR ) const override final {
39  return a + (b * b * jet_pt *jet_pt )/(mindR+0.0001);
40  }
41  };
42 } // end: namespace
43 
44 // Constructor
46  asg::AsgTool(name)
47 {}
48 
49 // Destructor
51 = default;
52 
53 // Initialize
55 {
56  // Print configuration
57  ATH_MSG_DEBUG("Initializing " << name() << "...");
58  ATH_MSG_DEBUG("Using RedoHardScatter: " << m_redoHardScatter);
59  ATH_MSG_DEBUG("Using SelectionMode: " << m_mode);
60  ATH_MSG_DEBUG("Using TrackSelectionTool: " << m_trkSelectTool);
61  ATH_MSG_DEBUG("Using ReturnDeco: " << m_returnDeco);
62  ATH_MSG_DEBUG("Using HardScatterLinkDeco: " << m_hardScatterDeco.toString());
63  ATH_MSG_DEBUG("Using VertexContainer: " << m_vtxContKey);
64  ATH_MSG_DEBUG("Using JetContainer: " << m_jetContKey);
65 
67  ATH_MSG_ERROR("RedoHardScatter and ReturnDeco cannot both be set to true.");
68  return StatusCode::FAILURE;
69  }
70 
71  // If we are re-doing the hardscatter
72  if (m_redoHardScatter) {
73  ATH_MSG_DEBUG("Setting the hard scatter strategy to: " << m_mode);
74 
75  // Determine how we will sum the track pt
76  switch (m_mode) {
77  case InDet::InDetHardScatterSelectionTool::Mode::SumPt2: {
78  m_sum = std::make_unique<::SumPt2>();
79  break;
80  }
81  case InDet::InDetHardScatterSelectionTool::Mode::SumPt: {
82  m_sum = std::make_unique<::SumPt>();
83  break;
84  }
85  case InDet::InDetHardScatterSelectionTool::Mode::SumPtw: {
86  m_sum = std::make_unique<::SumPtw>();
87  break;
88  }
89  default: {
90  ATH_MSG_ERROR("Unknown running mode : " << m_mode);
91  return StatusCode::FAILURE;
92  }
93  }
94 
95  // Fetch our InDetTrackSelectionTool
96  if (m_redoHardScatter && !m_trkSelectTool.empty()) {
97  m_doTrkSelection = true;
98  ATH_CHECK(m_trkSelectTool.retrieve());
99  }
100 
101  }
102 
103  // If we are returning a decoration
104  else if (m_returnDeco) {
105  if (m_hardScatterDeco.empty()) {
106  ATH_MSG_ERROR("Hardscatter vertex decoration cannot be empty!");
107  return StatusCode::FAILURE;
108  }
109  }
110 
111  // Initialize our EventInfo container and decoration reads
115 
116  // Initialize our vertex container read
117  ATH_CHECK(m_vtxContKey.initialize());
118 
119  // Initialize our jet container read if it is non empty
120  ATH_CHECK(m_jetContKey.initialize(!m_jetContKey.empty()));
121 
122  return StatusCode::SUCCESS;
123 }
124 
125 // Finalize
127 {
128  ATH_MSG_DEBUG("Finalizing " << name() << "...");
129 
130  // Release our tool
131  if (m_redoHardScatter && !m_trkSelectTool.empty()) ATH_CHECK(m_trkSelectTool.release());
132 
133  return StatusCode::SUCCESS;
134 }
135 
137 {
138  ATH_MSG_DEBUG("In getHardScatter(...) for " << name() << "...");
139 
140  const EventContext& ctx = Gaudi::Hive::currentContext();
141 
142  // If we are simply returning a decoration on the EventInfo:
143  if (m_returnDeco) {
145  if (!evtInfo.isValid()) {
146  ATH_MSG_ERROR("Could not open event info, returning nullptr!");
147  return nullptr;
148  }
150  const ElementLink<xAOD::VertexContainer>& vtxLink = hardScatterDeco(*evtInfo);
151  if (!vtxLink.isValid()) {
152  ATH_MSG_ERROR("Hardscatter vertex link is not valid, returning nullptr!");
153  return nullptr;
154  }
155  return *vtxLink;
156  }
157  //::get the jet container if the mode of HS selection is sumptw
158  const xAOD::JetContainer* jetCont{nullptr};
159  if ( m_mode == InDet::InDetHardScatterSelectionTool::Mode::SumPtw ){
160  //::First get the jet containers
162  if (!jetContHandle.isValid()){
163  ATH_MSG_ERROR ("HardScatterSelectionTool configured to use jet collection "<<m_jetContKey.key()<<", but collection is not found!");
164  }
165  jetCont = jetContHandle.cptr();
166  }
167 
168  float maxsum = -999.0;
169  const xAOD::Vertex* hardscatter = nullptr;
170  for (const auto *const vtx : *vtxCont) {
171  float sum = 0.;
172  // If we are redoing the hardscatter determination:
173  if (m_redoHardScatter) {
174  if ( m_mode == InDet::InDetHardScatterSelectionTool::Mode::SumPtw ){
175  for (const auto& trkLink : vtx->trackParticleLinks()) {
176  if (!trkLink.isValid()) continue;
177  if (m_doTrkSelection && !static_cast<bool>(m_trkSelectTool->accept(**trkLink, vtx))) continue;
178  if ( (*trkLink)->pt() >m_trkMaxPt) continue;
179  float jetPt=0.0;
180  float minDr=10.0;
181 
182  for (const xAOD::Jet* jet : *jetCont) {
183  if (jet->pt() < m_jetMinPt) {continue;} //skip jets below pT threshold
184  float DR = xAOD::P4Helpers::deltaR(*trkLink, jet);
185  if(DR<minDr) {
186  minDr = DR;
187  jetPt = jet->pt();
188  }
189  }
190  if(minDr <= m_jetTrkMaxDr){//check if jet is within dR 1.
191  sum = m_sum->add(sum, (*trkLink)->pt(),jetPt,minDr );
192  }
193  }
194  if (sum > maxsum) {
195  maxsum = sum;
196  hardscatter = vtx;
197  }
198  }
199  //::keeping Matt's (old) method for sumpt & sumpt2
200  else {
201  for (const auto& trkLink : vtx->trackParticleLinks()) {
202  if (trkLink.isValid()) {
203  if (m_doTrkSelection && !static_cast<bool>(m_trkSelectTool->accept(**trkLink, vtx))) continue;
204  sum = m_sum->add(sum, (*trkLink)->pt(),0.,0.);
205  }
206  }
207  if (sum > maxsum) {
208  maxsum = sum;
209  hardscatter = vtx;
210  }
211  }
212  }
213  // Else, return the PV:
214  else {
215  if (vtx->vertexType() == xAOD::VxType::PriVtx) {
216  hardscatter = vtx;
217  break;
218  }
219  }
220  }
221 
222  return hardscatter;
223 }
224 
226 {
227  const EventContext& ctx = Gaudi::Hive::currentContext();
229  if (!vtxCont.isValid()) {
230  ATH_MSG_ERROR("Could not open primary vertex container, returning nullptr!");
231  return nullptr;
232  }
233  return getHardScatter(vtxCont.get());
234 }
235 
237 {
238  ATH_MSG_DEBUG("In getHardScatterLink(...) for " << name() << "...");
239 
240  const EventContext& ctx = Gaudi::Hive::currentContext();
241 
242  // Get our hardscatter vertex
243  const xAOD::Vertex* hardscatter = getHardScatter(vtxCont);
244 
245  // Create our element link
246  ElementLink<xAOD::VertexContainer> hardscatterLink;
247  if (hardscatter) {
248  hardscatterLink.toContainedElement(*vtxCont, hardscatter, ctx);
249  }
250 
251  return hardscatterLink;
252 }
253 
255 {
256  const EventContext& ctx = Gaudi::Hive::currentContext();
258  if (!vtxCont.isValid()) {
259  ATH_MSG_ERROR("Could not open primary vertex container, returning empty ElementLink!");
260  return {};
261  }
262  return getHardScatterLink(vtxCont.get());
263 }
InDet::InDetHardScatterSelectionTool::getHardScatterLink
ElementLink< xAOD::VertexContainer > getHardScatterLink() const override
Definition: InDetHardScatterSelectionTool.cxx:254
InDet::InDetHardScatterSelectionTool::m_hardScatterDecoKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_hardScatterDecoKey
The decoration name of the ElementLink to the hardscatter vertex (applied to xAOD::EventInfo)
Definition: InDetHardScatterSelectionTool.h:147
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
InDetHardScatterSelectionTool.h
InDet::InDetHardScatterSelectionTool::m_jetContKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetContKey
Definition: InDetHardScatterSelectionTool.h:135
CurrentContext.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAODP4Helpers.h
InDet::InDetHardScatterSelectionTool::~InDetHardScatterSelectionTool
~InDetHardScatterSelectionTool()
Destructor.
asg
Definition: DataHandleTestTool.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
InDet::InDetHardScatterSelectionTool::m_trkMaxPt
FloatProperty m_trkMaxPt
Definition: InDetHardScatterSelectionTool.h:144
InDet::InDetHardScatterSelectionTool::m_jetMinPt
FloatProperty m_jetMinPt
Definition: InDetHardScatterSelectionTool.h:142
InDet::InDetHardScatterSelectionTool::getHardScatter
const xAOD::Vertex * getHardScatter() const override
Definition: InDetHardScatterSelectionTool.cxx:225
InDet::InDetHardScatterSelectionTool::m_redoHardScatter
BooleanProperty m_redoHardScatter
Definition: InDetHardScatterSelectionTool.h:137
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
xAOD::P4Helpers::deltaR
double deltaR(double rapidity1, double phi1, double rapidity2, double phi2)
from bare bare rapidity,phi
Definition: xAODP4Helpers.h:150
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::ReadHandle::get
const_pointer_type get() const
Dereference the pointer, but don't cache anything.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
InDet::InDetHardScatterSelectionTool::m_doTrkSelection
bool m_doTrkSelection
A flag which will be true if an InDetTrackSelectionTool was provided.
Definition: InDetHardScatterSelectionTool.h:130
InDet::InDetHardScatterSelectionTool::m_mode
IntegerProperty m_mode
Definition: InDetHardScatterSelectionTool.h:138
InDet::InDetHardScatterSelectionTool::finalize
virtual StatusCode finalize() override
Function finalizing the tool.
Definition: InDetHardScatterSelectionTool.cxx:126
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
InDet::InDetHardScatterSelectionTool::m_vtxContKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vtxContKey
Definition: InDetHardScatterSelectionTool.h:134
InDet::InDetHardScatterSelectionTool::m_evtInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfoKey
xAOD::EventInfo ReadHandleKey
Definition: InDetHardScatterSelectionTool.h:133
InDet::InDetHardScatterSelectionTool::initialize
virtual StatusCode initialize() override
Function initialising the tool.
Definition: InDetHardScatterSelectionTool.cxx:54
InDet::InDetHardScatterSelectionTool::m_returnDeco
BooleanProperty m_returnDeco
Definition: InDetHardScatterSelectionTool.h:140
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
InDet::InDetHardScatterSelectionTool::InDetHardScatterSelectionTool
InDetHardScatterSelectionTool(const std::string &name)
Constructor.
Definition: InDetHardScatterSelectionTool.cxx:45
add
bool add(const std::string &hname, TKey *tobj)
Definition: fastadd.cxx:55
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
IInDetTrackSelectionTool.h
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
InDet::InDetHardScatterSelectionTool::Sum
Definition: InDetHardScatterSelectionTool.h:70
InDet::InDetHardScatterSelectionTool::m_hardScatterDeco
StringProperty m_hardScatterDeco
Definition: InDetHardScatterSelectionTool.h:141
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ReadHandle.h
Handle class for reading from StoreGate.
InDet::InDetHardScatterSelectionTool::m_trkSelectTool
ToolHandle< InDet::IInDetTrackSelectionTool > m_trkSelectTool
Definition: InDetHardScatterSelectionTool.h:139
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
VertexContainer.h
a
TList * a
Definition: liststreamerinfos.cxx:10
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
InDet::InDetHardScatterSelectionTool::m_sum
std::unique_ptr< Sum > m_sum
A helper class which either sums pt2 or pt or ptw.
Definition: InDetHardScatterSelectionTool.h:127
ReadDecorHandle.h
Handle class for reading a decoration on an object.
TrackParticleContainer.h
InDet::InDetHardScatterSelectionTool::m_jetTrkMaxDr
FloatProperty m_jetTrkMaxDr
Definition: InDetHardScatterSelectionTool.h:143