ATLAS Offline Software
JetVertexNNTagger.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <fstream>
8 #include <regex>
9 #include <map>
10 
12 #include "xAODJet/JetContainer.h"
15 #include "xAODCore/ShallowCopy.h"
17 
20 
21 #include "lwtnn/generic/FastGraph.hh"
22 #include "lwtnn/parse_json.hh"
23 #include "lwtnn/Stack.hh"
24 
25 using xAOD::JetContainer;
26 
27 namespace JetPileupTag {
28 
30  : asg::AsgTool(name) {}
31 
33 
35  {
36 
37  ATH_MSG_DEBUG("Initializing...");
38 
39  if(m_jetContainerName.empty()){
40  ATH_MSG_ERROR("JetVertexTaggerTool needs to have its input jet container configured!");
41  return StatusCode::FAILURE;
42  }
43 
44  // Use the Path Resolver to find the jvt file and retrieve the likelihood histogram
45  std::string configPath = PathResolverFindCalibFile(m_NNConfigDir+"/"+m_NNParamFileName);
46  ATH_MSG_INFO(" Reading JVT NN file from:\n " << m_NNParamFileName << "\n");
47  ATH_MSG_INFO(" resolved in :\n " << configPath << "\n\n");
48 
49  std::ifstream fconfig( configPath.c_str() );
50  if ( !fconfig.is_open() ) {
51  ATH_MSG_ERROR( "Error opening config file: " << m_NNParamFileName );
52  ATH_MSG_ERROR( "Are you sure that the file exists at this path?" );
53  return StatusCode::FAILURE;
54  }
55 
56  ATH_MSG_INFO("\n Reading JVT likelihood histogram from:\n " << configPath << "\n\n");
57  lwt::GraphConfig cfg = lwt::parse_json_graph( fconfig );
58  // FastGraph is initialised with the order in which inputs will be
59  // provided, to avoid map lookup
60  lwt::InputOrder order;
61  lwt::order_t node_order;
62  std::vector<std::string> inputs = {"Rpt","JVFCorr","ptbin","etabin"};
63  // Single input block
64  node_order.emplace_back(cfg.inputs[0].name,inputs);
65  order.scalar = node_order;
66  ATH_MSG_DEBUG( "Network NLayers: " << cfg.layers.size() );
67 
68  // Primarily using double to take advantage of build_vector later
69  m_lwnn = std::make_unique<lwt::generic::FastGraph<double> >(cfg,order);
70 
71  std::string cutsPath = PathResolverFindCalibFile(m_NNConfigDir+"/"+m_NNCutFileName);
72  ATH_MSG_INFO(" Reading JVT NN cut file from:\n " << m_NNCutFileName << "\n");
73  ATH_MSG_INFO(" resolved in :\n " << cutsPath << "\n\n");
74  std::ifstream fcuts( cutsPath.c_str() );
75  if ( !fcuts.is_open() ) {
76  ATH_MSG_ERROR( "Error opening cuts file: " << m_NNCutFileName );
77  ATH_MSG_ERROR( "Are you sure that the file exists at this path?" );
78  return StatusCode::FAILURE;
79  }
81 
84  m_jvtKey = m_jetContainerName + "." + m_jvtKey.key();
85  if (!m_rptKey.empty())
86  m_rptKey = m_jetContainerName + "." + m_rptKey.key();
87  if (!m_passJvtKey.empty())
89 
90  ATH_CHECK(m_vertexContainer_key.initialize());
91 #ifndef XAOD_STANDALONE
93  // The user has promised that this will be produced by the same alg.
94  // Tell the scheduler to ignore it to avoid circular dependencies.
97  }
99  // For analysis applications, may not enforce scheduling via data deps
103  }
104 #endif
105  ATH_CHECK(m_jvfCorrKey.initialize());
106  ATH_CHECK(m_sumPtTrkKey.initialize());
107  ATH_CHECK(m_jvtKey.initialize());
108  ATH_CHECK(m_rptKey.initialize(SG::AllowEmpty));
110 
111  return StatusCode::SUCCESS;
112  }
113 
114 
115 
116  float JetVertexNNTagger::evaluateJvt(float rpt, float jvfcorr, size_t ptbin, size_t etabin) const
117  {
118 
119  lwt::VectorX<double> inputvals = lwt::build_vector({rpt,jvfcorr,static_cast<double>(ptbin),static_cast<double>(etabin)});
120  std::vector<lwt::VectorX<double> > scalars{inputvals};
121  lwt::VectorX<double> output = m_lwnn->compute(scalars);
122 
123  return output(0);
124 
125  }
126 
127 
129  {
130 
131  for ( const xAOD::Vertex* vertex : vertices ) {
132  if(vertex->vertexType() == xAOD::VxType::PriVtx) {
133  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << " Found HS vertex at index: "<< vertex->index());
134  return vertex;
135  }
136  }
137  if (vertices.size()==1) {
138  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << " Found no HS vertex, return dummy");
139  if (vertices.back()->vertexType() == xAOD::VxType::NoVtx)
140  return vertices.back();
141  }
142  ATH_MSG_VERBOSE("No vertex found in container.");
143  return nullptr;
144  }
145 
147  {
148 
149  // Grab vertices for index bookkeeping
151  const xAOD::VertexContainer& vertices = *vertexHandle;
152  ATH_MSG_DEBUG("Successfully retrieved VertexContainer: " << m_vertexContainer_key.key());
153 
154  const xAOD::Vertex *HSvertex = findHSVertex(vertices);
155  if(!HSvertex) {
156  ATH_MSG_WARNING("Invalid primary vertex found, will not continue decorating with JVT.");
157  return StatusCode::FAILURE;
158  }
159 
165 
166  static constexpr float invalidJvt = -1;
167  static constexpr float invalidRpt = 0;
168  static constexpr char invalidPassJvt = true;
169 
170 
171  for(const xAOD::Jet* jet : jetCont) {
172  float jvt = invalidJvt;
173  float rpt = invalidRpt;
174  char passJvt = invalidPassJvt;
175  if (HSvertex->vertexType() == xAOD::VxType::PriVtx) {
176  // Calculate RpT and JVFCorr
177  // Default JVFcorr to -1 when no tracks are associated.
178  float jvfcorr = jvfCorrHandle(*jet);
179  std::vector<float> sumpttrk = sumPtTrkHandle(*jet);
180  rpt = sumpttrk[HSvertex->index() - vertices[0]->index()]/jet->pt();
181 
182  size_t ptbin, etabin;
183  if (jet->pt() <= m_maxpt_for_cut && m_cutMap.edges(*jet, ptbin, etabin)) {
184  jvt = evaluateJvt(rpt, jvfcorr, ptbin, etabin);
185  float jvtCut = m_cutMap(ptbin, etabin);
186  passJvt = jvt > jvtCut;
187 
188  ATH_MSG_VERBOSE("Jet with pt " << jet->pt() << ", eta " << jet->eta() );
189  ATH_MSG_VERBOSE(" --> ptbin " << ptbin << ", etabin " << etabin);
190  ATH_MSG_VERBOSE(" --> inputs: corrJVF " << jvfcorr << ", rpt " << rpt );
191  ATH_MSG_VERBOSE("JVT cut for ptbin " << ptbin << ", etabin " << etabin << " = " << jvtCut);
192  ATH_MSG_VERBOSE("Evaluated JVT = " << jvt << ", jet " << (passJvt ? "passes" :"fails") << " working point" );
193  }
194  }
195 
196  // Decorate jet
197  jvtHandle(*jet) = jvt;
198  if (!rptHandle.key().empty())
199  rptHandle(*jet) = rpt;
200  if (!passJvtHandle.key().empty())
201  passJvtHandle(*jet) = passJvt;
202  }
203 
204  return StatusCode::SUCCESS;
205  }
206 }
ShallowCopy.h
JetPileupTag::JetVertexNNTagger::m_maxpt_for_cut
Gaudi::Property< float > m_maxpt_for_cut
Definition: JetVertexNNTagger.h:86
JetPileupTag::JetVertexNNTagger::evaluateJvt
float evaluateJvt(float rpt, float jvfcorr, size_t ptbin, size_t etabin) const
Definition: JetVertexNNTagger.cxx:116
JetPileupTag::JetVertexNNTagger::m_jvtKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_jvtKey
Definition: JetVertexNNTagger.h:93
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
JetPileupTag::JetVertexNNTagger::m_cutMap
NNJvtCutMap m_cutMap
Definition: JetVertexNNTagger.h:74
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
JetPileupTag::JetVertexNNTagger::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetVertexNNTagger.cxx:34
JetVertexNNTagger.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::renounce
std::enable_if_t< std::is_void_v< std::result_of_t< decltype(&T::renounce)(T)> > &&!std::is_base_of_v< SG::VarHandleKeyArray, T > &&std::is_base_of_v< Gaudi::DataHandle, T >, void > renounce(T &h)
Definition: AthCommonDataStore.h:380
asg
Definition: DataHandleTestTool.h:28
JetPileupTag::JetVertexNNTagger::JetVertexNNTagger
JetVertexNNTagger(const std::string &name)
Constructor with a tool name.
Definition: JetVertexNNTagger.cxx:29
JetPileupTag::JetVertexNNTagger::findHSVertex
const xAOD::Vertex * findHSVertex(const xAOD::VertexContainer &vertices) const
Definition: JetVertexNNTagger.cxx:128
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::Vertex_v1::vertexType
VxType::VertexType vertexType() const
The type of the vertex.
JetPileupTag
Definition: JetVertexNNTagger.h:39
xAOD::VxType::NoVtx
@ NoVtx
Dummy vertex. TrackParticle was not used in vertex fit.
Definition: TrackingPrimitives.h:570
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
JetPileupTag::JetVertexNNTagger::m_suppressOutputDeps
Gaudi::Property< bool > m_suppressOutputDeps
Definition: JetVertexNNTagger.h:79
JetPileupTag::NNJvtCutMap::fromJSON
static NNJvtCutMap fromJSON(std::istream &is)
Definition: NNJvtBinning.cxx:90
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
JetPileupTag::JetVertexNNTagger::m_vertexContainer_key
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexContainer_key
Definition: JetVertexNNTagger.h:90
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
mc.order
order
Configure Herwig7.
Definition: mc.Herwig7_Dijet.py:12
JetPileupTag::JetVertexNNTagger::m_rptKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_rptKey
Definition: JetVertexNNTagger.h:94
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
lwt::atlas::order_t
std::vector< std::pair< std::string, std::vector< std::string > > > order_t
Definition: InputOrder.h:25
JetPileupTag::NNJvtCutMap::edges
NNJvtBinning edges
Definition: NNJvtBinning.h:51
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
JetPileupTag::JetVertexNNTagger::~JetVertexNNTagger
~JetVertexNNTagger()
Destructor.
lwtDev::build_vector
VectorXd build_vector(const std::vector< double > &bias)
Definition: Stack.cxx:760
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
merge.output
output
Definition: merge.py:17
PathResolver.h
JetPileupTag::JetVertexNNTagger::m_jetContainerName
Gaudi::Property< std::string > m_jetContainerName
Definition: JetVertexNNTagger.h:77
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ReadHandle.h
Handle class for reading from StoreGate.
WriteCaloSwCorrections.cfg
cfg
Definition: WriteCaloSwCorrections.py:23
JetPileupTag::JetVertexNNTagger::m_NNConfigDir
Gaudi::Property< std::string > m_NNConfigDir
Definition: JetVertexNNTagger.h:82
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
lwtDev::parse_json_graph
GraphConfig parse_json_graph(std::istream &json)
Definition: parse_json.cxx:71
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
JetPileupTag::JetVertexNNTagger::m_suppressInputDeps
Gaudi::Property< bool > m_suppressInputDeps
Definition: JetVertexNNTagger.h:78
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
JetContainer.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
JetPileupTag::JetVertexNNTagger::m_jvfCorrKey
SG::ReadDecorHandleKey< xAOD::JetContainer > m_jvfCorrKey
Definition: JetVertexNNTagger.h:91
JetPileupTag::JetVertexNNTagger::m_passJvtKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_passJvtKey
Definition: JetVertexNNTagger.h:95
JetPileupTag::JetVertexNNTagger::m_sumPtTrkKey
SG::ReadDecorHandleKey< xAOD::JetContainer > m_sumPtTrkKey
Definition: JetVertexNNTagger.h:92
ReadDecorHandle.h
Handle class for reading a decoration on an object.
JetAuxContainer.h
IParticleHelpers.h
JetPileupTag::JetVertexNNTagger::m_NNCutFileName
Gaudi::Property< std::string > m_NNCutFileName
Definition: JetVertexNNTagger.h:84
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
JetPileupTag::JetVertexNNTagger::decorate
virtual StatusCode decorate(const xAOD::JetContainer &jetCont) const override
Decorate a jet collection without otherwise modifying it.
Definition: JetVertexNNTagger.cxx:146
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
JetPileupTag::JetVertexNNTagger::m_lwnn
std::unique_ptr< lwt::generic::FastGraph< double > > m_lwnn
Internal members for interpreting jet inputs and NN configuration.
Definition: JetVertexNNTagger.h:72
JetPileupTag::JetVertexNNTagger::m_NNParamFileName
Gaudi::Property< std::string > m_NNParamFileName
Definition: JetVertexNNTagger.h:83