ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
InnerDetector
InDetRecTools
InDetPVNNSelection
Root
PVNN.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#include "
InDetPVNNSelection/PVNN.h
"
6
#include "
FlavorTagInference/SaltModel.h
"
7
#include "
FlavorTagInference/SaltModelGraphConfig.h
"
8
9
#include "
PathResolver/PathResolver.h
"
10
11
#include "
InDetPVNNSelection/TracksLoader.h
"
12
#include "
InDetPVNNSelection/JetsLoader.h
"
13
#include "
InDetPVNNSelection/PhotonsLoader.h
"
14
#include "
InDetPVNNSelection/ElectronsLoader.h
"
15
#include "
InDetPVNNSelection/MuonsLoader.h
"
16
#include "
InDetPVNNSelection/IParticlesLoader.h
"
17
#include "
AthenaBaseComps/AthMsgStreamMacros.h
"
18
19
#include <algorithm>
20
#include <cfenv>
21
#include <cmath>
22
#include <array>
23
#include <sstream>
24
#include <stdexcept>
25
26
namespace
InDetPVNNSelection
{
27
28
PVNN::PVNN
(
const
std::string& nn_file):
29
m_saltModel
(nullptr),
30
m_modelPath
(),
31
m_input_node_name
()
32
{
33
34
// Load and initialize the neural network model from the given file path.
35
std::string fullPathToOnnxFile =
PathResolverFindCalibFile
(nn_file);
36
m_modelPath
= fullPathToOnnxFile;
37
m_saltModel
= std::make_shared<FlavorTagInference::SaltModel>(fullPathToOnnxFile);
38
auto
graph_config =
m_saltModel
->getGraphConfig();
39
40
// Vertex tensor key for runInference: must match InputNodeConfig::name in embedded pvnn_config
41
// (same metadata SaltModel uses for graph_config).
42
std::vector<std::string> input_node_names;
43
input_node_names.reserve(graph_config.inputs.size());
44
for
(
const
auto
&
node
: graph_config.inputs) {
45
input_node_names.push_back(
node
.
name
);
46
}
47
48
if
(std::find(input_node_names.begin(), input_node_names.end(),
"vertice_features"
) != input_node_names.end()) {
49
m_input_node_name
=
"vertice_features"
;
50
}
else
if
(std::find(input_node_names.begin(), input_node_names.end(),
"vertex_features"
) != input_node_names.end()) {
51
m_input_node_name
=
"vertex_features"
;
52
}
else
if
(graph_config.inputs.size() == 1) {
53
m_input_node_name
= graph_config.inputs.front().name;
54
}
else
{
55
std::ostringstream
msg
;
56
msg
<<
"Unsupported scalar vertex input name in model '"
<< nn_file <<
"'. Graph config inputs: "
;
57
for
(
const
auto
& name : input_node_names) {
58
msg
<< name <<
" "
;
59
}
60
throw
std::runtime_error(
msg
.str());
61
}
62
63
// Create configuration objects for data preprocessing.
64
auto
[inputs, constituents_configs] =
dataprep::createGetterConfig
(graph_config);
65
66
for
(
const
auto
& config : constituents_configs){
67
switch
(config.type){
68
case
ConstituentsType::TRACK
:
69
m_constituentsLoaders
.push_back(std::make_shared<TracksLoader>(config));
70
break
;
71
case
ConstituentsType::ELECTRON
:
72
m_constituentsLoaders
.push_back(std::make_shared<ElectronsLoader>(config));
73
break
;
74
case
ConstituentsType::MUON
:
75
m_constituentsLoaders
.push_back(std::make_shared<MuonsLoader>(config));
76
break
;
77
case
ConstituentsType::JET
:
78
m_constituentsLoaders
.push_back(std::make_shared<JetsLoader>(config));
79
break
;
80
case
ConstituentsType::PHOTON
:
81
m_constituentsLoaders
.push_back(std::make_shared<PhotonsLoader>(config));
82
break
;
83
case
ConstituentsType::IPARTICLE
:
84
m_constituentsLoaders
.push_back(std::make_shared<IParticlesLoader>(config));
85
break
;
86
}
87
}
88
89
m_varsFromVertex
=
dataprep::createVertexVarGetters
(inputs);
90
91
// Retrieve the configuration for the model outputs.
92
FlavorTagInference::OutputConfig
pvnn_output_config =
m_saltModel
->getOutputConfig();
93
94
for
(
const
auto
& outNode : pvnn_output_config) {
95
// the node's output name will be used to define the decoration name
96
std::string dec_name = outNode.name;
97
m_decorators
.vertexFloat.emplace_back(outNode.name,
Dec<float>
(dec_name));
98
if
(outNode.name ==
"salt_phsvertex"
) {
99
m_decorators
.vertexFloat.emplace_back(
"salt_phsvertex"
,
Dec<float>
(
"HSGN2_phsvertex"
));
100
}
101
}
102
}
103
104
PVNN::~PVNN
() =
default
;
105
106
float
PVNN::decorate
(
const
xAOD::Vertex
& vertex)
const
{
107
/* Main function for decorating a vertex with PVNN outputs. */
108
using namespace
internal
;
109
110
// prepare input
111
// -------------
112
FlavorTagInference::InputMap
pvnn_input;
113
114
std::vector<float> vertex_feat;
115
vertex_feat.reserve(
m_varsFromVertex
.size());
116
for
(
const
auto
& getter:
m_varsFromVertex
) {
117
vertex_feat.push_back(getter(vertex).second);
118
}
119
std::vector<int64_t> vertexfeat_dim = {1,
static_cast<
int64_t
>
(vertex_feat.size())};
120
121
for
(
const
auto
& value : vertex_feat) {
122
if
(!std::isfinite(value)) {
123
throw
std::runtime_error(
"Non-finite scalar vertex input before runInference"
);
124
}
125
}
126
127
FlavorTagInference::Inputs vertex_info (vertex_feat, vertexfeat_dim);
128
pvnn_input.insert({
m_input_node_name
, vertex_info});
129
// Provide common scalar aliases to absorb metadata/model naming mismatches.
130
constexpr
std::array<const char*, 4> scalar_input_aliases = {
131
"vertice_features"
,
"vertex_features"
,
"vertice_var"
,
"vertex_var"
132
};
133
for
(
const
char
* alias : scalar_input_aliases) {
134
pvnn_input.insert({alias, vertex_info});
135
}
136
137
for
(
const
auto
& loader :
m_constituentsLoaders
){
138
auto
[sequence_name, sequence_data, sequence_constituents] = loader->getData(vertex);
139
pvnn_input.insert({sequence_name, sequence_data});
140
const
std::string legacy_sequence_name = loader->getName();
141
if
(legacy_sequence_name != sequence_name) {
142
pvnn_input.insert({legacy_sequence_name, sequence_data});
143
}
144
}
145
146
// run inference
147
// -------------
148
// FPE warning are hidden but should be resolved.
149
// Related JIRA Ticket : https://its.cern.ch/jira/browse/ATLASRECTS-8386
150
std::feclearexcept(FE_ALL_EXCEPT);
151
152
const
FlavorTagInference::InferenceOutput
inference_output =
153
m_saltModel
->runInference(pvnn_input);
154
const
auto
& out_f = inference_output.
singleFloat
;
155
156
// Get PVNN score
157
// ----------------
158
float
score = -999;
159
FlavorTagInference::OutputConfig
pvnn_output_config =
m_saltModel
->getOutputConfig();
160
161
for
(
const
auto
& outNode : pvnn_output_config) {
162
std::string score_name = outNode.name;
163
164
if
(outNode.name.find(
"_phsvertex"
) != std::string::npos) {
165
score = out_f.at(score_name);
166
}
167
}
168
std::feclearexcept(FE_ALL_EXCEPT);
169
170
return
score;
171
172
}
// end of decorate()
173
174
}
// end of namespace InDetPVNNSelection
175
AthMsgStreamMacros.h
SaltModel.h
IParticlesLoader.h
ElectronsLoader.h
MuonsLoader.h
TracksLoader.h
JetsLoader.h
PVNN.h
PathResolver.h
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition
PathResolver.cxx:325
PhotonsLoader.h
SaltModelGraphConfig.h
InDetPVNNSelection::PVNN::m_decorators
Decorators m_decorators
Definition
PVNN.h:76
InDetPVNNSelection::PVNN::m_saltModel
std::shared_ptr< const FlavorTagInference::SaltModel > m_saltModel
Definition
PVNN.h:71
InDetPVNNSelection::PVNN::decorate
virtual float decorate(const xAOD::Vertex &vertex) const
Definition
PVNN.cxx:106
InDetPVNNSelection::PVNN::m_modelPath
std::string m_modelPath
Definition
PVNN.h:72
InDetPVNNSelection::PVNN::~PVNN
virtual ~PVNN()
InDetPVNNSelection::PVNN::m_constituentsLoaders
std::vector< std::shared_ptr< IConstituentsLoader > > m_constituentsLoaders
Definition
PVNN.h:75
InDetPVNNSelection::PVNN::m_input_node_name
std::string m_input_node_name
Definition
PVNN.h:73
InDetPVNNSelection::PVNN::Dec
SG::Decorator< T > Dec
Definition
PVNN.h:59
InDetPVNNSelection::PVNN::PVNN
PVNN(const std::string &nnFile)
Definition
PVNN.cxx:28
InDetPVNNSelection::PVNN::m_varsFromVertex
std::vector< internal::VarFromVertex > m_varsFromVertex
Definition
PVNN.h:74
node
Definition
node.h:24
node::name
void name(const std::string &n)
Definition
node.h:40
FlavorTagInference::OutputConfig
std::vector< SaltModelOutput > OutputConfig
Definition
ISaltModel.h:38
FlavorTagInference::InputMap
std::map< std::string, Inputs, std::less<> > InputMap
Definition
ISaltModel.h:37
InDetPVNNSelection::dataprep::createGetterConfig
std::tuple< std::vector< PVNNInputConfig >, std::vector< ConstituentsInputConfig > > createGetterConfig(FlavorTagInference::SaltModelGraphConfig::GraphConfig &graph_config)
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/Root/DataPrepUtilities.cxx:118
InDetPVNNSelection::dataprep::createVertexVarGetters
std::vector< internal::VarFromVertex > createVertexVarGetters(const std::vector< PVNNInputConfig > &inputs)
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/Root/DataPrepUtilities.cxx:171
InDetPVNNSelection::internal
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/DataPrepUtilities.h:43
InDetPVNNSelection
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:22
InDetPVNNSelection::ConstituentsType::MUON
@ MUON
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:35
InDetPVNNSelection::ConstituentsType::ELECTRON
@ ELECTRON
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:34
InDetPVNNSelection::ConstituentsType::TRACK
@ TRACK
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:33
InDetPVNNSelection::ConstituentsType::IPARTICLE
@ IPARTICLE
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:32
InDetPVNNSelection::ConstituentsType::PHOTON
@ PHOTON
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:37
InDetPVNNSelection::ConstituentsType::JET
@ JET
Definition
InnerDetector/InDetRecTools/InDetPVNNSelection/InDetPVNNSelection/ConstituentsLoader.h:36
xAOD::Vertex
Vertex_v1 Vertex
Define the latest version of the vertex class.
Definition
Event/xAOD/xAODTracking/xAODTracking/Vertex.h:16
FlavorTagInference::InferenceOutput
Definition
ISaltModel.h:40
FlavorTagInference::InferenceOutput::singleFloat
std::map< std::string, float > singleFloat
Definition
ISaltModel.h:41
msg
MsgStream & msg
Definition
testRead.cxx:32
Generated on
for ATLAS Offline Software by
1.17.0