ATLAS Offline Software
TrackTruthSelectionTool.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 // InDetPhysValMonitoring includes
9 #include <cmath> // std::fabs
10 
11 
13  asg::AsgTool(name)
14  , m_accept("TrackTruthSelection")
15  , m_numTruthProcessed(0)
16  , m_numTruthPassed(0) {
17  declareInterface<IAsgSelectionTool>(this);
18 
19  // declareProperty( "Property", m_nProperty ); //example property declaration
20  declareProperty("maxEta", m_maxEta = 2.5);
21  declareProperty("minPt", m_minPt = 400);
22  declareProperty("maxPt", m_maxPt = -1);
23  declareProperty("requireOnlyPrimary", m_requireOnlyPrimary = true);
24  declareProperty("requireCharged", m_requireCharged = true);
25  declareProperty("requireStatus1", m_requireStatus1 = true);
26  declareProperty("maxProdVertRadius", m_maxProdVertRadius = 110.);
27  declareProperty("pdgId", m_pdgId = -1);
28 }
29 
31 
34  if (asg::AsgTool::initialize().isFailure()) {
35  return StatusCode::FAILURE;
36  }
37  ATH_MSG_DEBUG("Initializing " << name() << "...");
38 
39  // Define cut names and descriptions
40  m_cuts.clear();
41  if (m_maxEta > -1) {
42  m_cuts.emplace_back("eta", "Cut on (absolute) particle eta");
43  }
44  if (m_minPt > -1) {
45  m_cuts.emplace_back("min_pt", "Cut on minimum particle pT");
46  }
47  if (m_maxPt > -1) {
48  m_cuts.emplace_back("max_pt", "Cut on maximum particle pT");
49  }
50 
52  m_cuts.emplace_back("OnlyPrimary", "Cut on origin");
53  }
54 
55  if (m_requireCharged) {
56  m_cuts.emplace_back("charged", "Require charged particle");
57  }
58  if (m_requireStatus1) {
59  m_cuts.emplace_back("status_1", "Particle status=1");
60  }
61 
62  if (m_maxProdVertRadius > 0.) {
63  m_cuts.emplace_back("decay_before_pixel", "Decays before first pixel layer");
64  }
65 
66  if (m_pdgId > -1) {
67  m_cuts.emplace_back("pdgId", "Pdg Id cut"); // 3-18-16 normally enabled, disabled for testing
68  }
69  // Add cuts to the AcceptInfo
70  for (const auto& cut : m_cuts) {
71  if (m_accept.addCut(cut.first, cut.second) < 0) {
72  ATH_MSG_ERROR("Failed to add cut " << cut.first << " because the AcceptInfo object is full.");
73  return StatusCode::FAILURE;
74  }
75  }
76 
77  // Initialise counters
78  m_numTruthPassedCuts.resize(m_accept.getNCuts(), 0);
79 
80  return StatusCode::SUCCESS;
81 }
82 
83 const asg::AcceptInfo&
85  return m_accept;
86 }
87 
89 TrackTruthSelectionTool::accept(const xAOD::IParticle* p) const// Is this perhaps supposed to be xAOD::TruthParticle?
90 {
91  // Check if this is a track:
92  if (!p) {
93  ATH_MSG_ERROR("accept(...) Function received a null pointer");
94  return asg::AcceptData (&m_accept);
95  }
96  if (p->type() != xAOD::Type::TruthParticle) {
97  ATH_MSG_ERROR("accept(...) Function received a non-TruthParticle");
98  return asg::AcceptData (&m_accept);
99  }
100 
101  // Cast it to a track (we have already checked its type so we do not have to dynamic_cast):
102  const xAOD::TruthParticle* truth = static_cast< const xAOD::TruthParticle* >(p);
103 
104  // Let the specific function do the work:
105  return accept(truth);
106 }
107 
110  asg::AcceptData acceptData (&m_accept);
111 
112  // Check cuts
113  if (m_maxEta > -1) {
114  acceptData.setCutResult("eta", (p->pt() > 1e-7 ? (std::fabs(p->eta()) < m_maxEta) : false));
115  }
116  if (m_minPt > -1) {
117  acceptData.setCutResult("min_pt", (p->pt() > m_minPt));
118  }
119  if (m_maxPt > -1) {
120  acceptData.setCutResult("max_pt", (p->pt() < m_maxPt));
121  }
122  if (m_requireOnlyPrimary) {
123  acceptData.setCutResult("OnlyPrimary", (!HepMC::is_simulation_particle(p)));
124  }
125 
126  if (m_requireCharged) {
127  acceptData.setCutResult("charged", (not (p->isNeutral())));
128  }
129  if (m_requireStatus1) {
130  acceptData.setCutResult("status_1", (p->status() == 1));
131  }
132  if (m_maxProdVertRadius > 0.) {
133  acceptData.setCutResult("decay_before_pixel", (!p->hasProdVtx() || p->prodVtx()->perp() < m_maxProdVertRadius));
134  }
135  if (m_pdgId > -1) {
136  acceptData.setCutResult("pdgId", (std::fabs(p->pdgId()) == m_pdgId));// 3-18-16 normally on, disabled for testing
137  }
138  std::lock_guard<std::mutex> lock{m_mutex}; // To guard m_numTruthPassedCuts
139  // Book keep cuts
140  for (const auto& cut : m_cuts) {
141  unsigned int pos = acceptData.getCutPosition(cut.first);
142  if (acceptData.getCutResult(pos)) {
143  m_numTruthPassedCuts[pos]++;
144  }
145  }
147  if (acceptData) {
149  }
150 
151  return acceptData;
152 }
153 
156  ATH_MSG_INFO("Finalizing " << name() << "...");
157 
158  if (m_numTruthProcessed == 0) {
159  ATH_MSG_INFO("No tracks processed in selection tool.");
160  return StatusCode::SUCCESS;
161  }
163  << m_numTruthPassed * 100. / m_numTruthProcessed << "% passed all cuts.");
164  for (const auto& cut : m_cuts) {
165  ULong64_t numPassed = m_numTruthPassedCuts.at(m_accept.getCutPosition(cut.first));
166  ATH_MSG_INFO(numPassed << " = " << numPassed * 100. / m_numTruthProcessed << "% passed "
167  << cut.first << " cut.");
168  }
169 
170  return StatusCode::SUCCESS;
171 }
TrackTruthSelectionTool::m_numTruthProcessed
std::atomic< ULong64_t > m_numTruthProcessed
Definition: TrackTruthSelectionTool.h:34
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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_maxPt
float m_maxPt
Definition: TrackTruthSelectionTool.h:41
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
asg
Definition: DataHandleTestTool.h:28
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_requireCharged
bool m_requireCharged
Definition: TrackTruthSelectionTool.h:44
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:33
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:84
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
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:89
TrackTruthSelectionTool::m_requireOnlyPrimary
bool m_requireOnlyPrimary
Definition: TrackTruthSelectionTool.h:43
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:299
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:41
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_minPt
float m_minPt
Definition: TrackTruthSelectionTool.h:42
TrackTruthSelectionTool.h
TruthVertex.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
MagicNumbers.h
TrackTruthSelectionTool::m_pdgId
int m_pdgId
Definition: TrackTruthSelectionTool.h:49
TrackTruthSelectionTool::m_requireStatus1
bool m_requireStatus1
Definition: TrackTruthSelectionTool.h:45
TrackTruthSelectionTool::m_maxEta
float m_maxEta
Definition: TrackTruthSelectionTool.h:40
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
TrackTruthSelectionTool::finalize
virtual StatusCode finalize() override
Definition: TrackTruthSelectionTool.cxx:155
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
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
TrackTruthSelectionTool::m_maxProdVertRadius
double m_maxProdVertRadius
Definition: TrackTruthSelectionTool.h:48
TrackTruthSelectionTool::TrackTruthSelectionTool
TrackTruthSelectionTool(const std::string &name)
Definition: TrackTruthSelectionTool.cxx:12
asg::AcceptInfo::addCut
int addCut(const std::string &cutName, const std::string &cutDescription)
Add a cut; returning the cut position.
Definition: AcceptInfo.h:53