ATLAS Offline Software
D3PDMCTruthClassifier.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
14 #include "D3PDMCTruthClassifier.h"
19 
20 #include "Particle/TrackParticle.h"
21 #include "xAODEgamma/Electron.h"
22 #include "xAODEgamma/Photon.h"
23 #include "xAODMuon/Muon.h"
24 #include "xAODJet/Jet.h"
26 
27 #include <vector>
28 
29 
30 namespace D3PD {
31 
32 
40  const std::string& name,
41  const IInterface* parent)
43  m_sg ("StoreGateSvc", name)
44 {
45 }
46 
47 
52 {
54  CHECK( m_sg.retrieve() );
55  return StatusCode::SUCCESS;
56 }
57 
58 
66 {
67  // Try to find the name of the truth map container,
68  // given the name of the TrackParticleContainer.
69  bool got_names = false;
70  std::string tp_truth_name, tp_name;
71  if (el->trackParticleLink().isValid()) {
72  tp_name = el->trackParticleLink().dataID();
73  got_names = get_tptruth_name (tp_name, tp_truth_name);
74  }
75 
76  // If we found the truth map container, diddle the properties
77  // for the base tool (remembering the original values).
78  std::string old_tp_truth_name, old_tp_name;
79  if (got_names) {
80  getProperty1 ("TrackParticleTruthCollection", old_tp_truth_name);
81  getProperty1 ("TrackParticleContainerName", old_tp_name);
82  setProperty1 ("TrackParticleTruthCollection", tp_truth_name);
83  setProperty1 ("TrackParticleContainerName", tp_name);
84  }
85 
86  // Call the base tool.
90 
91  // Restore original properties, if we changed them.
92  if (got_names) {
93  setProperty1 ("TrackParticleTruthCollection", old_tp_truth_name);
94  setProperty1 ("TrackParticleContainerName", old_tp_name);
95  }
96 
97  return ret;
98 }
99 
100 
108 {
109  // Try to find the name of the truth map container,
110  // given the name of the TrackParticleContainer.
111  bool got_names = false;
112  std::string tp_truth_name, tp_name;
113  const xAOD::Vertex* vx = ph->vertex();
114  if (vx) {
116  for (auto trklink : links) {
117  if (trklink.isValid()) {
118  tp_name = trklink.dataID();
119  got_names = get_tptruth_name (tp_name, tp_truth_name);
120  if (got_names) break;
121  }
122  }
123  }
124 
125  // If we found the truth map container, diddle the properties
126  // for the base tool (remembering the original values).
127  std::string old_tp_truth_name, old_tp_name;
128  if (got_names) {
129  getProperty1 ("TrackParticleTruthCollection", old_tp_truth_name);
130  getProperty1 ("TrackParticleContainerName", old_tp_name);
131  setProperty1 ("TrackParticleTruthCollection", tp_truth_name);
132  setProperty1 ("TrackParticleContainerName", tp_name);
133  }
134 
135  // Call the base tool.
139 
140  // Restore original properties, if we changed them.
141  if (got_names) {
142  setProperty1 ("TrackParticleTruthCollection", old_tp_truth_name);
143  setProperty1 ("TrackParticleContainerName", old_tp_name);
144  }
145 
146  return ret;
147 }
148 
149 
156 {
157  // Avoid using forward muons that have a tracklet associated to them:
158  if( mu->muonType() == xAOD::Muon::SiliconAssociatedForwardMuon ) {
159  return std::make_pair( MCTruthPartClassifier::Unknown, MCTruthPartClassifier::NonDefined );
160  }
161 
162  // For all other muons just use the base class's function:
164 }
165 
166 
174 bool D3PDMCTruthClassifier::get_tptruth_name (const std::string& tp_name,
175  std::string& tptruth_name)
176 {
177  // Check all TrackParticleTruthCollections to try to find one
178  // pointing at the desired TrackParticle container.
179  std::vector<std::string> keys;
181 
182  for (size_t i = 0; i < keys.size(); i++) {
183  const TrackParticleTruthCollection* tptc = 0;
184  if (m_sg->retrieve (tptc, keys[i]).isFailure() || !tptc)
185  {
186  ATH_MSG_WARNING( "Can't retrieve TrackParticleTruthCollection/"
187  << keys[i] );
188  continue;
189  }
190  tptruth_name = keys[i];
191 
192  std::string target_tp_name;
193  if (tptc->trackParticleContainerLink().isDefault()) {
194  // Older files didn't have this link. Hardcode this case.
195  target_tp_name = "TrackParticleCandidate";
196  }
197  else if (!tptc->trackParticleContainerLink().isValid())
198  continue;
199  else
200  target_tp_name = tptc->trackParticleContainerLink().dataID();
201 
202  if (tp_name == target_tp_name)
203  return true;
204  }
205  return false;
206 }
207 
208 
214 void D3PDMCTruthClassifier::getProperty1 (const std::string& pname,
215  std::string& value)
216 {
217  StatusCode sc = getProperty (pname, value);
218  if (sc.isFailure())
219  REPORT_ERROR (sc) << "Can't get property " << pname << "\n";
220 }
221 
222 
228 void D3PDMCTruthClassifier::setProperty1 (const std::string& pname,
229  const std::string& value)
230 {
231  StatusCode sc = setProperty (pname, value);
232  if (sc.isFailure())
233  REPORT_ERROR (sc) << "Can't set property " << pname << "\n";
234 }
235 
236 
237 } // namespace D3PD
D3PDMCTruthClassifier.h
Work around MCTruthClassifier brain-damage.
Jet.h
D3PD::D3PDMCTruthClassifier::getProperty1
void getProperty1(const std::string &pname, std::string &value)
Helper to retrieve the value of a Gaudi property.
Definition: D3PDMCTruthClassifier.cxx:214
REPORT_ERROR
#define REPORT_ERROR(SC)
Report an error.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:355
TrackParticle.h
MCTruthClassifier::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: MCTruthClassifier.h:101
Muon.h
xAOD::Vertex_v1::trackParticleLinks
const TrackParticleLinks_t & trackParticleLinks() const
Get all the particles associated with the vertex.
D3PD::D3PDMCTruthClassifier::D3PDMCTruthClassifier
D3PDMCTruthClassifier(const std::string &type, const std::string &name, const IInterface *parent)
Standard Gaudi tool constructor.
Definition: D3PDMCTruthClassifier.cxx:39
TrackParticleTruthCollection.h
athena.value
value
Definition: athena.py:122
asg::AsgTool::getProperty
const T * getProperty(const std::string &name) const
Get one of the tool's properties.
D3PD::D3PDMCTruthClassifier::particleTruthClassifier
virtual std::pair< MCTruthPartClassifier::ParticleType, MCTruthPartClassifier::ParticleOrigin > particleTruthClassifier(const xAOD::Electron *el)
Run the classifier for an electron.
Definition: D3PDMCTruthClassifier.cxx:65
D3PD::D3PDMCTruthClassifier::initialize
virtual StatusCode initialize()
Standard Gaudi initialize method.
Definition: D3PDMCTruthClassifier.cxx:51
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
NonDefined
@ NonDefined
Definition: TruthClasses.h:52
D3PD
Block filler tool for noisy FEB information.
Definition: InnerDetector/InDetMonitoring/InDetGlobalMonitoring/macros/EnhancedPrimaryVertexMonitoring/TrigD3PD/ChainGroup.h:21
TrackParticleTruthCollection::trackParticleContainerLink
DataLink< Rec::TrackParticleContainer > trackParticleContainerLink() const
Definition: TrackParticleTruthCollection.h:26
D3PD::D3PDMCTruthClassifier::setProperty1
void setProperty1(const std::string &pname, const std::string &value)
Helper to set the value of a Gaudi property.
Definition: D3PDMCTruthClassifier.cxx:228
lumiFormat.i
int i
Definition: lumiFormat.py:92
ret
T ret(T t)
Definition: rootspy.cxx:260
Photon.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DMTest::links
links
Definition: CLinks_v1.cxx:22
ParticleOrigin
ParticleOrigin
Definition: TruthClasses.h:51
test_pyathena.parent
parent
Definition: test_pyathena.py:15
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
VxTrackAtVertex.h
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
xAOD::Vertex_v1::TrackParticleLinks_t
std::vector< ElementLink< xAOD::TrackParticleContainer > > TrackParticleLinks_t
Type for the associated track particles.
Definition: Vertex_v1.h:128
LinkToTrackParticleBase.h
D3PD::D3PDMCTruthClassifier::m_sg
ServiceHandle< StoreGateSvc > m_sg
The StoreGate service.
Definition: D3PDMCTruthClassifier.h:119
D3PD::D3PDMCTruthClassifier::get_tptruth_name
bool get_tptruth_name(const std::string &tp_name, std::string &tptruth_name)
Try to find a TrackParticleTruthCollection pointing at TP_NAME.
Definition: D3PDMCTruthClassifier.cxx:174
DataLinkBase::isDefault
bool isDefault() const
Test to see if we're in the default state.
Definition: DataLinkBase.cxx:31
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
errorcheck.h
Helpers for checking error return status codes and reporting errors.
xAOD::Electron_v1
Definition: Electron_v1.h:34
MCTruthClassifier
Definition: MCTruthClassifier.h:56
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
xAOD::Photon_v1
Definition: Photon_v1.h:37
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
TrackParticleTruthCollection
Definition: TrackParticleTruthCollection.h:18
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
Electron.h
MCTruthClassifier::particleTruthClassifier
virtual std::pair< MCTruthPartClassifier::ParticleType, MCTruthPartClassifier::ParticleOrigin > particleTruthClassifier(const xAOD::TruthParticle *, MCTruthPartClassifier::Info *info=nullptr) const override
Definition: MCTruthClassifierGen.cxx:77
ParticleType
ParticleType
Definition: TruthClasses.h:8
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
xAOD::Photon_v1::vertex
const xAOD::Vertex * vertex(size_t index=0) const
Pointer to the xAOD::Vertex/es that match the photon candidate.
Definition: Photon_v1.cxx:46
TrackParticleContainer.h