ATLAS Offline Software
TrackTruthSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // InDetPhysValMonitoring includes
9 #include <cmath> // std::fabs
10 
11 
13  asg::AsgTool(name) {
14  declareInterface<IAsgSelectionTool>(this);
15 }
16 
18 
21  if (asg::AsgTool::initialize().isFailure()) {
22  return StatusCode::FAILURE;
23  }
24  ATH_MSG_DEBUG("Initializing " << name() << "...");
25 
26  // Define cut names and descriptions
27  m_cuts.clear();
28  if (m_maxEta > -1) {
29  m_cuts.emplace_back("eta", "Cut on (absolute) particle eta");
30  }
31  if (m_minPt > -1) {
32  m_cuts.emplace_back("min_pt", "Cut on minimum particle pT");
33  }
34  if (m_maxPt > -1) {
35  m_cuts.emplace_back("max_pt", "Cut on maximum particle pT");
36  }
37 
39  m_cuts.emplace_back("OnlyPrimary", "Cut on origin");
40  }
41 
42  if (m_requireCharged) {
43  m_cuts.emplace_back("charged", "Require charged particle");
44  }
45  if (m_requireStable) {
46  m_cuts.emplace_back("stable", "Particle stable");
47  }
48 
49  if (m_maxProdVertRadius > 0.) {
50  m_cuts.emplace_back("decay_before_pixel", "Decays before first pixel layer");
51  }
52 
53  if (m_pdgId > -1) {
54  m_cuts.emplace_back("pdgId", "Pdg Id cut"); // 3-18-16 normally enabled, disabled for testing
55  }
56  // Add cuts to the AcceptInfo
57  for (const auto& cut : m_cuts) {
58  if (m_accept.addCut(cut.first, cut.second) < 0) {
59  ATH_MSG_ERROR("Failed to add cut " << cut.first << " because the AcceptInfo object is full.");
60  return StatusCode::FAILURE;
61  }
62  }
63 
64  // Initialise counters
65  m_numTruthPassedCuts.resize(m_accept.getNCuts(), 0);
66 
67  return StatusCode::SUCCESS;
68 }
69 
70 const asg::AcceptInfo&
72  return m_accept;
73 }
74 
76 TrackTruthSelectionTool::accept(const xAOD::IParticle* p) const// Is this perhaps supposed to be xAOD::TruthParticle?
77 {
78  // Check if this is a track:
79  if (!p) {
80  ATH_MSG_ERROR("accept(...) Function received a null pointer");
81  return asg::AcceptData (&m_accept);
82  }
83  if (p->type() != xAOD::Type::TruthParticle) {
84  ATH_MSG_ERROR("accept(...) Function received a non-TruthParticle");
85  return asg::AcceptData (&m_accept);
86  }
87 
88  // Cast it to a track (we have already checked its type so we do not have to dynamic_cast):
89  const xAOD::TruthParticle* truth = static_cast< const xAOD::TruthParticle* >(p);
90 
91  // Let the specific function do the work:
92  return accept(truth);
93 }
94 
97  asg::AcceptData acceptData (&m_accept);
98 
99  // Check cuts
100  if (m_maxEta > -1) {
101  acceptData.setCutResult("eta", (p->pt() > 1e-7 ? (std::fabs(p->eta()) < m_maxEta) : false));
102  }
103  if (m_minPt > -1) {
104  acceptData.setCutResult("min_pt", (p->pt() > m_minPt));
105  }
106  if (m_maxPt > -1) {
107  acceptData.setCutResult("max_pt", (p->pt() < m_maxPt));
108  }
109  if (m_requireOnlyPrimary) {
110  acceptData.setCutResult("OnlyPrimary", (!HepMC::is_simulation_particle(p)));
111  }
112 
113  if (m_requireCharged) {
114  acceptData.setCutResult("charged", (not (p->isNeutral())));
115  }
116  if (m_requireStable) {
117  acceptData.setCutResult("stable", (MC::isStable(p)));
118  }
119  if (m_maxProdVertRadius > 0.) {
120  acceptData.setCutResult("decay_before_pixel", (!p->hasProdVtx() || p->prodVtx()->perp() < m_maxProdVertRadius));
121  }
122  if (m_pdgId > -1) {
123  acceptData.setCutResult("pdgId", (std::fabs(p->pdgId()) == m_pdgId));// 3-18-16 normally on, disabled for testing
124  }
125  std::lock_guard<std::mutex> lock{m_mutex}; // To guard m_numTruthPassedCuts
126  // Book keep cuts
127  for (const auto& cut : m_cuts) {
128  unsigned int pos = acceptData.getCutPosition(cut.first);
129  if (acceptData.getCutResult(pos)) {
130  m_numTruthPassedCuts[pos]++;
131  }
132  }
134  if (acceptData) {
136  }
137 
138  return acceptData;
139 }
140 
143  ATH_MSG_INFO("Finalizing " << name() << "...");
144 
145  if (m_numTruthProcessed == 0) {
146  ATH_MSG_INFO("No tracks processed in selection tool.");
147  return StatusCode::SUCCESS;
148  }
150  << m_numTruthPassed * 100. / m_numTruthProcessed << "% passed all cuts.");
151  for (const auto& cut : m_cuts) {
152  ULong64_t numPassed = m_numTruthPassedCuts.at(m_accept.getCutPosition(cut.first));
153  ATH_MSG_INFO(numPassed << " = " << numPassed * 100. / m_numTruthProcessed << "% passed "
154  << cut.first << " cut.");
155  }
156 
157  return StatusCode::SUCCESS;
158 }
TrackTruthSelectionTool::m_numTruthProcessed
std::atomic< ULong64_t > m_numTruthProcessed
Definition: TrackTruthSelectionTool.h:34
TrackTruthSelectionTool::m_maxPt
FloatProperty m_maxPt
Definition: TrackTruthSelectionTool.h:41
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TrackTruthSelectionTool::m_accept
asg::AcceptInfo m_accept
Definition: TrackTruthSelectionTool.h:32
TrackTruthSelectionTool::m_requireStable
BooleanProperty m_requireStable
Definition: TrackTruthSelectionTool.h:45
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
asg
Definition: DataHandleTestTool.h:28
TrackTruthSelectionTool::m_pdgId
IntegerProperty m_pdgId
Definition: TrackTruthSelectionTool.h:49
TrackTruthSelectionTool::~TrackTruthSelectionTool
virtual ~TrackTruthSelectionTool()
asg::AcceptInfo::getCutPosition
unsigned int getCutPosition(const std::string &cutName) const
Get the bit position of a cut.
Definition: AcceptInfo.h:73
TrackTruthSelectionTool::m_numTruthPassed
std::atomic< ULong64_t > m_numTruthPassed
Definition: TrackTruthSelectionTool.h:35
TrackTruthSelectionTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: TrackTruthSelectionTool.cxx:20
TrackTruthSelectionTool::getAcceptInfo
virtual const asg::AcceptInfo & getAcceptInfo() const override
Declare the interface ID for this pure-virtual interface class to the Athena framework.
Definition: TrackTruthSelectionTool.cxx:71
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
asg::AcceptData::getCutPosition
unsigned int getCutPosition(const std::string &cutName) const
Get the bit position of a cut.
Definition: AcceptData.h:71
TrackTruthSelectionTool::accept
virtual asg::AcceptData accept(const xAOD::IParticle *p) const override
The main accept method: the actual cuts are applied here.
Definition: TrackTruthSelectionTool.cxx:76
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
asg::AcceptInfo
Definition: AcceptInfo.h:28
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:355
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
asg::AcceptInfo::getNCuts
unsigned int getNCuts() const
Get the number of cuts defined.
Definition: AcceptInfo.h:46
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
BindingsTest.cut
cut
This script demonstrates how to call a C++ class from Python Also how to use PyROOT is shown.
Definition: BindingsTest.py:13
TrackTruthSelectionTool::m_requireCharged
BooleanProperty m_requireCharged
Definition: TrackTruthSelectionTool.h:44
TrackTruthSelectionTool.h
TrackTruthSelectionTool::m_maxProdVertRadius
FloatProperty m_maxProdVertRadius
Definition: TrackTruthSelectionTool.h:48
TruthVertex.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrackTruthSelectionTool::m_requireOnlyPrimary
BooleanProperty m_requireOnlyPrimary
Definition: TrackTruthSelectionTool.h:43
TrackTruthSelectionTool::m_maxEta
FloatProperty m_maxEta
Definition: TrackTruthSelectionTool.h:40
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
TrackTruthSelectionTool::finalize
virtual StatusCode finalize() override
Definition: TrackTruthSelectionTool.cxx:142
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
asg::AcceptData::setCutResult
void setCutResult(const std::string &cutName, bool cutResult)
Set the result of a cut, based on the cut name (safer)
Definition: AcceptData.h:134
asg::AcceptData::getCutResult
bool getCutResult(const std::string &cutName) const
Get the result of a cut, based on the cut name (safer)
Definition: AcceptData.h:98
TrackTruthSelectionTool::m_mutex
std::mutex m_mutex
Definition: TrackTruthSelectionTool.h:37
asg::AcceptData
Definition: AcceptData.h:30
TrackTruthSelectionTool::m_cuts
std::vector< std::pair< std::string, std::string > > m_cuts
Definition: TrackTruthSelectionTool.h:33
asg::AsgTool::initialize
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: AsgTool.h:133
HepMCHelpers.h
TrackTruthSelectionTool::TrackTruthSelectionTool
TrackTruthSelectionTool(const std::string &name)
Definition: TrackTruthSelectionTool.cxx:12
TrackTruthSelectionTool::m_minPt
FloatProperty m_minPt
Definition: TrackTruthSelectionTool.h:42
asg::AcceptInfo::addCut
int addCut(const std::string &cutName, const std::string &cutDescription)
Add a cut; returning the cut position.
Definition: AcceptInfo.h:53