ATLAS Offline Software
Loading...
Searching...
No Matches
ElectronDNNCalculator Class Reference

Used by AsgElectronSelectorTool to calculate the score of a python trained DNN using lwtnn as interface to do electron ID. More...

#include <ElectronDNNCalculator.h>

Inheritance diagram for ElectronDNNCalculator:
Collaboration diagram for ElectronDNNCalculator:

Public Member Functions

 ElectronDNNCalculator (AsgElectronSelectorTool *owner, const std::string &modelFileName, const std::string &quantileFileName, const std::vector< std::string > &variablesName)
 Constructor of the class.
 ~ElectronDNNCalculator ()
 Standard destructor.
Eigen::Matrix< float, -1, 1 > calculate (const std::vector< double > &) const
 Get the prediction of the DNN model.
bool msgLvl (const MSG::Level lvl) const
 Test the output level of the object.
MsgStream & msg () const
 The standard message stream.
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream.

Private Member Functions

double transformInput (const std::vector< double > &quantiles, double value) const
 transform the input variables according to a given QuantileTransformer.
int readQuantileTransformer (TTree *tree)
 read the bins and values of the QuantileTransformer to transform the input variables.

Private Attributes

std::unique_ptr< lwt::generic::FastGraph< float > > m_graph = 0
 DNN interface via lwtnn.
std::vector< std::vector< double > > m_quantiles
 Quantile values for each variable that needs to be transformed with the QuantileTransformer.
std::vector< double > m_references
 Reference values for the QuantileTransformer. Basically just equidistant bins between 0 and 1.
std::vector< std::string > m_variables
 Model variables.
uint m_var_size
std::function< MsgStream &()> m_msg
 the message stream we use

Detailed Description

Used by AsgElectronSelectorTool to calculate the score of a python trained DNN using lwtnn as interface to do electron ID.

Also applies a transformation to the input variables based on a QuantileTransformer.

Author
Lukas Ehrke

Definition at line 23 of file ElectronDNNCalculator.h.

Constructor & Destructor Documentation

◆ ElectronDNNCalculator()

ElectronDNNCalculator::ElectronDNNCalculator ( AsgElectronSelectorTool * owner,
const std::string & modelFileName,
const std::string & quantileFileName,
const std::vector< std::string > & variablesName )

Constructor of the class.

Definition at line 23 of file ElectronDNNCalculator.cxx.

26 :
27 asg::AsgMessagingForward(owner),
28 m_quantiles(variables.size()),
29 m_variables(variables),
30 m_var_size(variables.size())
31{
32
33 ATH_MSG_INFO("Initializing ElectronDNNCalculator...");
34 for (const auto& var : m_variables) {
35 ATH_MSG_DEBUG("VARIABLE: " << var );
36 }
37 if (modelFileName.empty()){
38 throw std::runtime_error("No file found at '" + modelFileName + "'");
39 }
40
41 // Make an input file object
42 std::ifstream inputFile;
43
44 // Open your trained model
45 ATH_MSG_INFO("Loading model: " << modelFileName.c_str());
46
47 // Create input order for the NN, the data needs to be passed in this exact order
48 lwt::InputOrder order;
49 order.scalar.emplace_back("node_0", m_variables );
50
51 // create the model
52 inputFile.open(modelFileName);
53 auto parsedGraph = lwt::parse_json_graph(inputFile);
54
55 m_graph = std::make_unique<lwt::generic::FastGraph<float>>(parsedGraph, order);
56
57 if (quantileFileName.empty()){
58 throw std::runtime_error("No file found at '" + quantileFileName + "'");
59 }
60
61 // Open quantiletransformer file
62 ATH_MSG_INFO("Loading QuantileTransformer " << quantileFileName);
63 std::unique_ptr<TFile> qtfile(TFile::Open(quantileFileName.data()));
64 if (readQuantileTransformer((TTree*)qtfile->Get("tree")) == 0){
65 throw std::runtime_error("Could not load all variables for the QuantileTransformer");
66
67 }
68}
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_INFO(x,...)
std::vector< std::vector< double > > m_quantiles
Quantile values for each variable that needs to be transformed with the QuantileTransformer.
std::unique_ptr< lwt::generic::FastGraph< float > > m_graph
DNN interface via lwtnn.
std::vector< std::string > m_variables
Model variables.
int readQuantileTransformer(TTree *tree)
read the bins and values of the QuantileTransformer to transform the input variables.
order
Configure Herwig7.

◆ ~ElectronDNNCalculator()

ElectronDNNCalculator::~ElectronDNNCalculator ( )
inline

Standard destructor.

Definition at line 33 of file ElectronDNNCalculator.h.

33{};

Member Function Documentation

◆ calculate()

Eigen::Matrix< float, -1, 1 > ElectronDNNCalculator::calculate ( const std::vector< double > & variableValues) const

Get the prediction of the DNN model.

Definition at line 71 of file ElectronDNNCalculator.cxx.

72{
73
74 if(variableValues.size() != m_var_size)
75 throw std::runtime_error("Passed vector of variables has wrong size");
76
77 // Create the input for the model
78 Eigen::VectorXf inputVector(m_variables.size());
79
80 // This has to be in the same order as the InputOrder was defined
81 for(uint i = 0; i < m_var_size; ++i){
82 inputVector(i) = transformInput(m_quantiles.at(i), variableValues.at(i));
83 }
84
85 std::vector<Eigen::VectorXf> inp;
86 inp.emplace_back(std::move(inputVector));
87
88 auto output = m_graph->compute(inp);
89 return output;
90}
unsigned int uint
double transformInput(const std::vector< double > &quantiles, double value) const
transform the input variables according to a given QuantileTransformer.
output
Definition merge.py:16

◆ msg() [1/2]

MsgStream & asg::AsgMessagingForward::msg ( ) const
inherited

The standard message stream.

Returns
A reference to the default message stream of this object.

Definition at line 24 of file AsgMessagingForward.cxx.

25 {
26 return m_msg();
27 }
std::function< MsgStream &()> m_msg
the message stream we use

◆ msg() [2/2]

MsgStream & asg::AsgMessagingForward::msg ( const MSG::Level lvl) const
inherited

The standard message stream.

Parameters
lvlThe message level to set the stream to
Returns
A reference to the default message stream, set to level "lvl"

Definition at line 29 of file AsgMessagingForward.cxx.

30 {
31 MsgStream& msg = m_msg ();
32 msg << lvl;
33 return msg;
34 }
MsgStream & msg() const
The standard message stream.

◆ msgLvl()

bool asg::AsgMessagingForward::msgLvl ( const MSG::Level lvl) const
inherited

Test the output level of the object.

Parameters
lvlThe message level to test against
Returns
boolean Indicting if messages at given level will be printed
true If messages at level "lvl" will be printed

Definition at line 11 of file AsgMessagingForward.cxx.

12 {
13 MsgStream& msg = m_msg();
14 if (msg.level() <= lvl)
15 {
16 msg << lvl;
17 return true;
18 } else
19 {
20 return false;
21 }
22 }

◆ readQuantileTransformer()

int ElectronDNNCalculator::readQuantileTransformer ( TTree * tree)
private

read the bins and values of the QuantileTransformer to transform the input variables.

Definition at line 128 of file ElectronDNNCalculator.cxx.

129{
130 int sc(1);
131 // the reference bins to which the variables will be transformed to
132 double references;
133 sc = tree->SetBranchAddress("references", &references) == -5 ? 0 : 1;
134
135 std::map<std::string, double> readVars;
136 for ( const auto& var : m_variables ){
137 sc = tree->SetBranchAddress(TString(var), &readVars[var]) == -5 ? 0 : 1;
138 }
139
140 for (int ientry = 0; ientry < tree->GetEntries(); ientry++){
141 tree->GetEntry(ientry);
142 m_references.push_back(references);
143 for(uint ivar = 0; ivar < m_var_size; ++ivar){
144 m_quantiles.at(ivar).push_back(readVars[m_variables.at(ivar)]);
145 }
146 }
147 return sc;
148}
static Double_t sc
std::vector< double > m_references
Reference values for the QuantileTransformer. Basically just equidistant bins between 0 and 1.
std::vector< reference > references
Definition hcg.cxx:525
TChain * tree

◆ transformInput()

double ElectronDNNCalculator::transformInput ( const std::vector< double > & quantiles,
double value ) const
private

transform the input variables according to a given QuantileTransformer.

Definition at line 96 of file ElectronDNNCalculator.cxx.

97{
98 int size = quantiles.size();
99
100 // if given value is outside of range of the given quantiles return min (0) or max (1) of references
101 if (value <= quantiles[0]) return m_references[0];
102 if (value >= quantiles[size-1]) return m_references[size-1];
103
104 // find the bin where the value is smaller than the next bin (going from low bins to large bins)
105 auto lowBound = std::lower_bound(quantiles.begin(), quantiles.end(), value);
106 int lowBin = lowBound - quantiles.begin() - 1;
107
108 // get x and y values on left and right side from value while going up
109 double xLup = quantiles[lowBin], yLup = m_references[lowBin], xRup = quantiles[lowBin+1], yRup = m_references[lowBin+1];
110
111 // find the bin where the value is larger than the next bin (going from large bins to low bins)
112 auto upperBound = std::upper_bound(quantiles.begin(), quantiles.end(), value);
113 int upperBin = upperBound - quantiles.begin();
114
115 // get x and y values on left and right side from value while going down
116 double xRdown = quantiles[upperBin], yRdown = m_references[upperBin], xLdown = quantiles[upperBin-1], yLdown = m_references[upperBin-1];
117
118 // calculate the gradients
119 double dydxup = ( yRup - yLup ) / ( xRup - xLup );
120 double dydxdown = ( yRdown - yLdown ) / ( xRdown - xLdown );
121
122 // average linear interpolation of up and down case
123 return 0.5 * ((yLup + dydxup * (value - xLup)) + (yLdown + dydxdown * (value - xLdown)));
124}
size_t size() const
Number of registered mappings.

Member Data Documentation

◆ m_graph

std::unique_ptr<lwt::generic::FastGraph<float> > ElectronDNNCalculator::m_graph = 0
private

DNN interface via lwtnn.

Definition at line 45 of file ElectronDNNCalculator.h.

◆ m_msg

std::function<MsgStream& ()> asg::AsgMessagingForward::m_msg
privateinherited

the message stream we use

This used to be a simple pointer to the MsgStream itself, but in AthenaMT the actual object used is local to the thread. So instead of pointing to it directly we are now using a function to look it up, which will get the thread-local object.

Definition at line 77 of file AsgMessagingForward.h.

◆ m_quantiles

std::vector<std::vector<double> > ElectronDNNCalculator::m_quantiles
private

Quantile values for each variable that needs to be transformed with the QuantileTransformer.

Definition at line 48 of file ElectronDNNCalculator.h.

◆ m_references

std::vector<double> ElectronDNNCalculator::m_references
private

Reference values for the QuantileTransformer. Basically just equidistant bins between 0 and 1.

Definition at line 50 of file ElectronDNNCalculator.h.

◆ m_var_size

uint ElectronDNNCalculator::m_var_size
private

Definition at line 53 of file ElectronDNNCalculator.h.

◆ m_variables

std::vector<std::string> ElectronDNNCalculator::m_variables
private

Model variables.

Definition at line 52 of file ElectronDNNCalculator.h.


The documentation for this class was generated from the following files: