ATLAS Offline Software
BDTVertexWeightCalculator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 #include "MVAUtils/BDT.h"
9 #include "TFile.h"
10 #include "TTree.h"
11 #include "TVector3.h"
12 #include "xAODTracking/Vertex.h"
17 
18 #include <cmath>
19 #include <limits>
20 
22  : asg::AsgTool(name) { }
23 
25 
27  ATH_MSG_DEBUG("Initializing " << name() << "...");
28 
30  ATH_CHECK(m_nphotons_good_key.initialize());
31  ATH_CHECK(m_photons_px_key.initialize());
32  ATH_CHECK(m_photons_py_key.initialize());
33  ATH_CHECK(m_photons_pz_key.initialize());
34 
36 
37  return StatusCode::SUCCESS;
38 }
39 
41  const std::string fullPathToFile = PathResolverFindCalibFile(m_bdt_file);
42  if (fullPathToFile.empty()) {
43  ATH_MSG_ERROR("Can not find BDT file: '" << m_bdt_file
44  << "'. Please check if the "
45  "file exists.");
46  return StatusCode::FAILURE;
47  }
48 
49  std::unique_ptr<TFile> rootFile(TFile::Open(fullPathToFile.c_str(), "READ"));
50  if (!rootFile) {
51  ATH_MSG_ERROR("Can not open BDT file root file: '"
52  << fullPathToFile
53  << "'. Please check if the file "
54  "exists and is not corrupted.");
55 
56  return StatusCode::FAILURE;
57  }
58  std::unique_ptr<TTree> tree((TTree*)rootFile->Get(m_bdt_name.value().c_str()));
59  if (!tree) {
60  ATH_MSG_ERROR("Can not find BDT tree '" << m_bdt_name << "' in file: '"
61  << fullPathToFile << "'.");
62  }
63 
64  ATH_MSG_INFO("Loading BDT '" << m_bdt_name << "' from file: '"
65  << fullPathToFile << "'.");
66  m_bdt = std::make_unique<MVAUtils::BDT>(tree.get());
67 
68  return StatusCode::SUCCESS;
69 }
70 
72  const xAOD::Vertex& vertex) const {
73  SG::ReadHandle<xAOD::VertexContainer> pointingVertexContainer{
75  if (pointingVertexContainer->size() != 1) {
76  ATH_MSG_ERROR("Pointing vertex container has size "
77  << pointingVertexContainer->size()
78  << " instead of 1");
79  return {};
80  }
81  const xAOD::Vertex* pointingVertex = pointingVertexContainer->front();
82 
83  const float zcommon = pointingVertex->z();
84  const float zcommon_error =
85  std::sqrt(pointingVertex->covariancePosition()(2, 2));
86 
87  auto acc_nph_good = SG::makeHandle<unsigned int>(m_nphotons_good_key);
88  const float nph_good = acc_nph_good(*pointingVertex);
89  const float vtx_sumpt = xAOD::PVHelpers::getVertexSumPt(&vertex, 1);
90  const float vtx_sumpt2 =xAOD::PVHelpers::getVertexSumPt(&vertex, 2);
91  const float vtx_z = vertex.z();
92 
93  const float deltaz_sig_photons = std::abs(vtx_z - zcommon) / zcommon_error;
94 
95  auto acc_photons_px = SG::makeHandle<float>(m_photons_px_key);
96  auto acc_photons_py = SG::makeHandle<float>(m_photons_py_key);
97  auto acc_photons_pz = SG::makeHandle<float>(m_photons_pz_key);
98  TVector3 photon_sump(acc_photons_px(*pointingVertex),
99  acc_photons_py(*pointingVertex),
100  acc_photons_pz(*pointingVertex));
101  TLorentzVector vertex_p4 = xAOD::PVHelpers::getVertexMomentum(&vertex, true);
102  const float deltaphi_sig_photons = vertex_p4.Vect().DeltaPhi(photon_sump);
103 
104  ATH_MSG_DEBUG("Input variables: nph_good: "
105  << nph_good << ", vtx_sumpt: " << vtx_sumpt
106  << ", vtx_sumpt2: " << vtx_sumpt2
107  << ", deltaz_sig_photons: " << deltaz_sig_photons
108  << ", deltaphi_sig_photons: " << deltaphi_sig_photons);
109 
110  return {nph_good, vtx_sumpt, vtx_sumpt2, deltaz_sig_photons,
111  deltaphi_sig_photons};
112 }
113 
115  const xAOD::Vertex& vertex) const {
116  if (vertex.vertexType() != xAOD::VxType::VertexType::PriVtx and
117  vertex.vertexType() != xAOD::VxType::VertexType::PileUp) {
118  return 0.;
119  }
120  const std::vector<float> input_values = get_input_values(vertex);
121  if (input_values.empty()) { return 0.; }
122  return m_bdt->GetClassification(input_values);
123 }
124 
126  const xAOD::VertexContainer& vertices) const {
127  float best_score = std::numeric_limits<float>::lowest();
128  const xAOD::Vertex* vertex = nullptr;
129  for (const xAOD::Vertex* v : vertices) {
130  if (v == nullptr) {
131  ATH_MSG_WARNING("Null vertex in container");
132  continue;
133  }
134  if (v->vertexType() == xAOD::VxType::PriVtx) {
135  static const SG::ConstAccessor<float> acc ("score");
136  const float score =
137  acc.isAvailable(*v) ? acc(*v) : estimateSignalCompatibility(*v);
138  if (score > best_score) {
139  best_score = score;
140  vertex = v;
141  }
142  }
143  }
144  if (vertex == nullptr) {
145  ATH_MSG_WARNING("No vertex found");
146  }
147  return vertex;
148 }
BDTVertexWeightCalculator::m_photons_px_key
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_px_key
Definition: BDTVertexWeightCalculator.h:55
xAOD::PVHelpers::getVertexSumPt
float getVertexSumPt(const xAOD::Vertex *vertex, int power=1, bool useAux=true)
Loop over track particles associated with vertex and return scalar sum of pT^power in GeV (from auxda...
Definition: PhotonVertexHelpers.cxx:215
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
BDTVertexWeightCalculator::BDTVertexWeightCalculator
BDTVertexWeightCalculator(const std::string &name)
Definition: BDTVertexWeightCalculator.cxx:21
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
tree
TChain * tree
Definition: tile_monitor.h:30
asg
Definition: DataHandleTestTool.h:28
SG::ConstAccessor< float >
BDTVertexWeightCalculator::m_bdt_file
Gaudi::Property< std::string > m_bdt_file
Definition: BDTVertexWeightCalculator.h:61
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
BDTVertexWeightCalculator::initialize_BDT
StatusCode initialize_BDT()
Definition: BDTVertexWeightCalculator.cxx:40
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
BDTVertexWeightCalculator::m_nphotons_good_key
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_nphotons_good_key
Definition: BDTVertexWeightCalculator.h:53
BDTVertexWeightCalculator.h
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
BDTVertexWeightCalculator::estimateSignalCompatibility
virtual double estimateSignalCompatibility(const xAOD::Vertex &vertex) const override
Estimate the compatibility of the vertex with a hard scatter vertex, with respect to pileup vertices.
Definition: BDTVertexWeightCalculator.cxx:114
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
makeComparison.rootFile
rootFile
Definition: makeComparison.py:27
BDTVertexWeightCalculator::get_input_values
std::vector< float > get_input_values(const xAOD::Vertex &vertex) const
Definition: BDTVertexWeightCalculator.cxx:71
xAOD::Vertex_v1::z
float z() const
Returns the z position.
xAOD::PVHelpers::getVertexMomentum
TLorentzVector getVertexMomentum(const xAOD::Vertex *vertex, bool useAux=true, const std::string &derivationPrefix="")
Return vector sum of tracks associated with vertex (from auxdata if available and useAux = true)
Definition: PhotonVertexHelpers.cxx:175
BDT.h
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
Vertex.h
PhotonVertexHelpers.h
xAOD::VxType::PileUp
@ PileUp
Pile-up vertex.
Definition: TrackingPrimitives.h:573
PathResolver.h
BDTVertexWeightCalculator::~BDTVertexWeightCalculator
virtual ~BDTVertexWeightCalculator() override
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ReadHandle.h
Handle class for reading from StoreGate.
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
xAOD::score
@ score
Definition: TrackingPrimitives.h:513
BDTVertexWeightCalculator::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: BDTVertexWeightCalculator.cxx:26
python.PyAthena.v
v
Definition: PyAthena.py:157
BDTVertexWeightCalculator::m_photons_pz_key
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_pz_key
Definition: BDTVertexWeightCalculator.h:59
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
VertexContainer.h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ReadDecorHandle.h
Handle class for reading a decoration on an object.
BDTVertexWeightCalculator::getVertex
const xAOD::Vertex * getVertex(const xAOD::VertexContainer &vertices) const
Definition: BDTVertexWeightCalculator.cxx:125
BDTVertexWeightCalculator::m_bdt_name
Gaudi::Property< std::string > m_bdt_name
Definition: BDTVertexWeightCalculator.h:63
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
BDTVertexWeightCalculator::m_bdt
std::unique_ptr< MVAUtils::BDT > m_bdt
Definition: BDTVertexWeightCalculator.h:66
BDTVertexWeightCalculator::m_photons_py_key
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_py_key
Definition: BDTVertexWeightCalculator.h:57
BDTVertexWeightCalculator::m_pointingVertexContainerKey
SG::ReadHandleKey< xAOD::VertexContainer > m_pointingVertexContainerKey
Definition: BDTVertexWeightCalculator.h:50