ATLAS Offline Software
InDetHardScatterSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 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_hardScatterDecoKey);
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  case InDet::InDetHardScatterSelectionTool::Mode::HSGN2: {
90  break;
91  }
92  default: {
93  ATH_MSG_ERROR("Unknown running mode : " << m_mode);
94  return StatusCode::FAILURE;
95  }
96  }
97 
98  // Fetch our InDetTrackSelectionTool
99  if (m_redoHardScatter && !m_trkSelectTool.empty()) {
100  m_doTrkSelection = true;
101  ATH_CHECK(m_trkSelectTool.retrieve());
102  }
103 
104  }
105 
106  // Initialize our EventInfo container and decoration reads
109 
110  // Initialize our vertex container read
111  ATH_CHECK(m_vtxContKey.initialize());
112 
113  // Initialize our jet container read if it is non empty
115 
116  // Initialize the GNN score handle if relevant mode is selected
117  ATH_CHECK(m_gnnScoreKey.initialize(m_mode == InDet::InDetHardScatterSelectionTool::Mode::HSGN2));
118 
119  return StatusCode::SUCCESS;
120 }
121 
122 // Finalize
124 {
125  ATH_MSG_DEBUG("Finalizing " << name() << "...");
126 
127  // Release our tool
128  if (m_redoHardScatter && !m_trkSelectTool.empty()) ATH_CHECK(m_trkSelectTool.release());
129 
130  return StatusCode::SUCCESS;
131 }
132 
134 {
135  ATH_MSG_DEBUG("In getHardScatter(...) for " << name() << "...");
136 
137  const EventContext& ctx = Gaudi::Hive::currentContext();
138 
139  // If we are simply returning a decoration on the EventInfo:
140  if (m_returnDeco) {
142  if (!evtInfo.isValid()) {
143  ATH_MSG_ERROR("Could not open event info, returning nullptr!");
144  return nullptr;
145  }
147  const ElementLink<xAOD::VertexContainer>& vtxLink = hardScatterDeco(*evtInfo);
148  if (!vtxLink.isValid()) {
149  ATH_MSG_ERROR("Hardscatter vertex link is not valid, returning nullptr!");
150  return nullptr;
151  }
152  return *vtxLink;
153  }
154  //::get the jet container if the mode of HS selection is sumptw
155  const xAOD::JetContainer* jetCont{nullptr};
156  if ( m_mode == InDet::InDetHardScatterSelectionTool::Mode::SumPtw ){
157  //::First get the jet containers
159  if (!jetContHandle.isValid()){
160  ATH_MSG_ERROR ("HardScatterSelectionTool configured to use jet collection "<<m_jetContKey.key()<<", but collection is not found!");
161  }
162  jetCont = jetContHandle.cptr();
163  }
164 
165  float maxsum = -999.0;
166  float max_gnn_score = -999.0;
167  const xAOD::Vertex* hardscatter = nullptr;
168  for (const auto *const vtx : *vtxCont) {
169  float sum = 0.;
170  // If we are redoing the hardscatter determination:
171  if (m_redoHardScatter) {
172  if ( m_mode == InDet::InDetHardScatterSelectionTool::Mode::SumPtw ){
173  for (const auto& trkLink : vtx->trackParticleLinks()) {
174  if (!trkLink.isValid()) continue;
175  if (m_doTrkSelection && !static_cast<bool>(m_trkSelectTool->accept(**trkLink, vtx))) continue;
176  if ( (*trkLink)->pt() >m_trkMaxPt) continue;
177  float jetPt=0.0;
178  float minDr=10.0;
179 
180  for (const xAOD::Jet* jet : *jetCont) {
181  if (jet->pt() < m_jetMinPt) {continue;} //skip jets below pT threshold
182  float DR = xAOD::P4Helpers::deltaR(*trkLink, jet);
183  if(DR<minDr) {
184  minDr = DR;
185  jetPt = jet->pt();
186  }
187  }
188  if(minDr <= m_jetTrkMaxDr){//check if jet is within dR 1.
189  sum = m_sum->add(sum, (*trkLink)->pt(),jetPt,minDr );
190  }
191  }
192  if (sum > maxsum) {
193  maxsum = sum;
194  hardscatter = vtx;
195  }
196  }
197  else if ( m_mode == InDet::InDetHardScatterSelectionTool::Mode::HSGN2 ) {
199  float gnn_score = acc_gnn(*vtx);
200  if (gnn_score > max_gnn_score) {
201  max_gnn_score = gnn_score;
202  hardscatter = vtx;
203  }
204  }
205  //::keeping Matt's (old) method for sumpt & sumpt2
206  else {
207  for (const auto& trkLink : vtx->trackParticleLinks()) {
208  if (trkLink.isValid()) {
209  if (m_doTrkSelection && !static_cast<bool>(m_trkSelectTool->accept(**trkLink, vtx))) continue;
210  sum = m_sum->add(sum, (*trkLink)->pt(),0.,0.);
211  }
212  }
213  if (sum > maxsum) {
214  maxsum = sum;
215  hardscatter = vtx;
216  }
217  }
218  }
219  // Else, return the PV:
220  else {
221  if (vtx->vertexType() == xAOD::VxType::PriVtx) {
222  hardscatter = vtx;
223  break;
224  }
225  }
226  }
227 
228  return hardscatter;
229 }
230 
232 {
233  const EventContext& ctx = Gaudi::Hive::currentContext();
235  if (!vtxCont.isValid()) {
236  ATH_MSG_ERROR("Could not open primary vertex container, returning nullptr!");
237  return nullptr;
238  }
239  return getHardScatter(vtxCont.get());
240 }
241 
243 {
244  ATH_MSG_DEBUG("In getHardScatterLink(...) for " << name() << "...");
245 
246  const EventContext& ctx = Gaudi::Hive::currentContext();
247 
248  // Get our hardscatter vertex
249  const xAOD::Vertex* hardscatter = getHardScatter(vtxCont);
250 
251  // Create our element link
252  ElementLink<xAOD::VertexContainer> hardscatterLink;
253  if (hardscatter) {
254  hardscatterLink.toContainedElement(*vtxCont, hardscatter, ctx);
255  }
256 
257  return hardscatterLink;
258 }
259 
261 {
262  const EventContext& ctx = Gaudi::Hive::currentContext();
264  if (!vtxCont.isValid()) {
265  ATH_MSG_ERROR("Could not open primary vertex container, returning empty ElementLink!");
266  return {};
267  }
268  return getHardScatterLink(vtxCont.get());
269 }
InDet::InDetHardScatterSelectionTool::getHardScatterLink
ElementLink< xAOD::VertexContainer > getHardScatterLink() const override
Definition: InDetHardScatterSelectionTool.cxx:260
InDet::InDetHardScatterSelectionTool::m_hardScatterDecoKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_hardScatterDecoKey
Definition: InDetHardScatterSelectionTool.h:142
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:136
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
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:145
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
InDet::InDetHardScatterSelectionTool::m_jetMinPt
FloatProperty m_jetMinPt
Definition: InDetHardScatterSelectionTool.h:143
InDet::InDetHardScatterSelectionTool::getHardScatter
const xAOD::Vertex * getHardScatter() const override
Definition: InDetHardScatterSelectionTool.cxx:231
InDet::InDetHardScatterSelectionTool::m_redoHardScatter
BooleanProperty m_redoHardScatter
Definition: InDetHardScatterSelectionTool.h:138
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:131
InDet::InDetHardScatterSelectionTool::m_mode
IntegerProperty m_mode
Definition: InDetHardScatterSelectionTool.h:139
InDet::InDetHardScatterSelectionTool::finalize
virtual StatusCode finalize() override
Function finalizing the tool.
Definition: InDetHardScatterSelectionTool.cxx:123
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:572
InDet::InDetHardScatterSelectionTool::m_vtxContKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vtxContKey
Definition: InDetHardScatterSelectionTool.h:135
InDet::InDetHardScatterSelectionTool::m_evtInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfoKey
xAOD::EventInfo ReadHandleKey
Definition: InDetHardScatterSelectionTool.h:134
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:141
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
CurrentContext.h
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:795
IInDetTrackSelectionTool.h
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
InDet::InDetHardScatterSelectionTool::Sum
Definition: InDetHardScatterSelectionTool.h:71
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
ReadHandle.h
Handle class for reading from StoreGate.
InDet::InDetHardScatterSelectionTool::m_trkSelectTool
ToolHandle< InDet::IInDetTrackSelectionTool > m_trkSelectTool
Definition: InDetHardScatterSelectionTool.h:140
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:128
ReadDecorHandle.h
Handle class for reading a decoration on an object.
InDet::InDetHardScatterSelectionTool::m_gnnScoreKey
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_gnnScoreKey
xAOD::EventInfo ReadDecorHandleKey
Definition: InDetHardScatterSelectionTool.h:148
TrackParticleContainer.h
InDet::InDetHardScatterSelectionTool::m_jetTrkMaxDr
FloatProperty m_jetTrkMaxDr
Definition: InDetHardScatterSelectionTool.h:144