ATLAS Offline Software
Loading...
Searching...
No Matches
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
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Helper class to provide constant type-safe access to aux data.
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_py_key
virtual ~BDTVertexWeightCalculator() override
const xAOD::Vertex * getVertex(const xAOD::VertexContainer &vertices) const
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_px_key
SG::ReadHandleKey< xAOD::VertexContainer > m_pointingVertexContainerKey
BDTVertexWeightCalculator(const std::string &name)
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_nphotons_good_key
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
std::unique_ptr< MVAUtils::BDT > m_bdt
std::vector< float > get_input_values(const xAOD::Vertex &vertex) const
Gaudi::Property< std::string > m_bdt_file
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_photons_pz_key
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.
Gaudi::Property< std::string > m_bdt_name
Helper class to provide constant type-safe access to aux data.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
float z() const
Returns the z position.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
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...
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)
@ PileUp
Pile-up vertex.
@ PriVtx
Primary vertex.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.
TChain * tree