ATLAS Offline Software
DL2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "lwtnn/LightweightGraph.hh"
8 #include "lwtnn/NanReplacer.hh"
9 
11 
12 namespace {
13  const std::string jetLinkName = "jetLink";
14 }
15 
16 
17 namespace FlavorTagDiscriminants {
18 
19  // DL2
20  //
21  // TODO: make this work with more input nodes
22  DL2::DL2(const lwt::GraphConfig& graph_config,
23  const std::vector<FTagInputConfig>& inputs,
24  const std::vector<ConstituentsInputConfig>& tracks_configs,
25  const FTagOptions& options):
26  m_jetLink(jetLinkName),
27  m_input_node_name(""),
28  m_graph(new lwt::LightweightGraph(graph_config,graph_config.outputs.begin()->first)),
29  m_variable_cleaner(nullptr),
30  m_defaultValue(options.default_output_value)
31  {
32  // set up inputs
33  if (graph_config.inputs.size() > 1) {
34  throw std::logic_error("We don't currently support graphs with "
35  "more than one input");
36  } else if (graph_config.inputs.size() == 1){
37  m_input_node_name = graph_config.inputs.at(0).name;
38  m_variable_cleaner.reset(new lwt::NanReplacer(
39  graph_config.inputs.at(0).defaults,
40  lwt::rep::all));
41  }
42 
43  auto [vb, vj, ds] = dataprep::createBvarGetters(inputs);
44  m_varsFromBTag = vb;
45  m_varsFromJet = vj;
47 
48  for (auto config : tracks_configs){
49  m_tracksLoaders.push_back(std::make_shared<TracksLoader>(config, options));
50  }
51 
52  auto [decorators, dd, rd] = dataprep::createDecorators(
53  graph_config, options);
55  m_decorators = decorators;
56 
57  auto [track_validity, is_defaults, ipdd, rc] = dataprep::createIpChecker(
58  graph_config, options);
59  m_invalid_track_checker = track_validity;
60  m_is_defaults = is_defaults;
61  m_dataDependencyNames += ipdd;
62 
63  // Update dependencies and used remap from the tracks loaders.
64  for (const auto& loader : m_tracksLoaders){
65  m_dataDependencyNames += loader->getDependencies();
66  rd.merge(loader->getUsedRemap());
67  }
68  // check that all remapping was used
69  rd.merge(rc);
70  dataprep::checkForUnusedRemaps(options.remap_scalar, rd);
71  }
72 
73  void DL2::decorate(const xAOD::BTagging& btag) const {
74  auto jetLink = m_jetLink(btag);
75  if (!jetLink.isValid()) {
76  throw std::runtime_error("invalid jetLink");
77  }
78  const xAOD::Jet& jet = **jetLink;
79  decorate(jet, btag);
80  }
81  void DL2::decorate(const xAOD::Jet& jet) const {
82  decorate(jet, jet);
83  }
85  // save out things
86  for (const auto& dec: m_decorators) {
87  for (const auto& node: dec.second) {
88  // save something that is clearly wrong
89  node.second(jet) = m_defaultValue;
90  }
91  }
92  }
93 
94  void DL2::decorate(const xAOD::Jet& jet, const SG::AuxElement& btag) const {
95  using namespace internal;
96  std::vector<NamedVar> vvec;
97  for (const auto& getter: m_varsFromBTag) {
98  vvec.push_back(getter(btag));
99  }
100  for (const auto& getter: m_varsFromJet) {
101  vvec.push_back(getter(jet));
102  }
103  std::map<std::string, std::map<std::string, double> > nodes;
104  if (m_variable_cleaner) {
105  std::map<std::string, double> variables(vvec.begin(), vvec.end());
106  auto cleaned = m_variable_cleaner->replace(variables);
107 
108  // Note, you can hack in more variables to `cleaned` here.
109 
110  // put the cleaned inputs into the node structure
111  nodes[m_input_node_name] = cleaned;
112  }
113 
114  // add track sequences, check if any are invalid
115  char invalid = 0;
116  std::map<std::string, std::map<std::string, std::vector<double>>> seqs;
117 
118  for (const auto& loader : m_tracksLoaders){
119  std::map<std::string, std::vector<double>> feats;
120  std::tie(invalid, feats) = loader->getDL2Data(jet, btag, m_invalid_track_checker);
121  seqs[loader->getName()] = feats;
122  }
123 
124  for (const auto& def: m_is_defaults) {
125  def(btag) = invalid;
126  }
127  if (invalid) {
128  decorateWithDefaults(btag);
129  return;
130  }
131 
132  // save out things
133  for (const auto& dec: m_decorators) {
134  // the second argument to compute(...) is for sequences
135  auto out_vals = m_graph->compute(nodes, seqs, dec.first);
136  for (const auto& node: dec.second) {
137  node.second(btag) = out_vals.at(node.first);
138  }
139  }
140  }
141 
143  return m_dataDependencyNames;
144  }
145 }
BTaggingUtilities.h
BTagTrackIpAccessor.h
checkxAOD.ds
ds
Definition: Tools/PyUtils/bin/checkxAOD.py:258
FlavorTagDiscriminants::DL2::DL2
DL2(const lwt::GraphConfig &, const std::vector< FTagInputConfig > &, const std::vector< ConstituentsInputConfig > &={}, const FTagOptions &=FTagOptions())
Definition: DL2.cxx:22
FlavorTagDiscriminants::dataprep::createIpChecker
std::tuple< std::function< char(const internal::Tracks &)>, std::vector< SG::AuxElement::Decorator< char > >, FTagDataDependencyNames, std::set< std::string > > createIpChecker(const lwt::GraphConfig &, const FTagOptions &)
Definition: PhysicsAnalysis/JetTagging/FlavorTagDiscriminants/Root/DataPrepUtilities.cxx:420
checkCoolLatestUpdate.variables
variables
Definition: checkCoolLatestUpdate.py:13
FlavorTagDiscriminants
This file contains "getter" functions used for accessing tagger inputs from the EDM.
Definition: AssociationEnums.h:11
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:472
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
Cut::all
@ all
Definition: SUSYToolsAlg.cxx:67
FlavorTagDiscriminants::DL2::decorate
void decorate(const xAOD::BTagging &btag) const
Definition: DL2.cxx:73
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
FlavorTagDiscriminants::dataprep::createDecorators
std::tuple< std::map< std::string, internal::OutNodeFloat >, FTagDataDependencyNames, std::set< std::string > > createDecorators(const lwt::GraphConfig &config, const FTagOptions &options)
Definition: PhysicsAnalysis/JetTagging/FlavorTagDiscriminants/Root/DataPrepUtilities.cxx:382
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
FlavorTagDiscriminants::DL2::m_variable_cleaner
std::unique_ptr< lwt::NanReplacer > m_variable_cleaner
Definition: DL2.h:38
FlavorTagDiscriminants::DL2::m_defaultValue
float m_defaultValue
Definition: DL2.h:43
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
FlavorTagDiscriminants::FTagDataDependencyNames
Definition: FTagDataDependencyNames.h:12
FlavorTagDiscriminants::DL2::m_graph
std::unique_ptr< lwt::LightweightGraph > m_graph
Definition: DL2.h:37
FlavorTagDiscriminants::DL2::m_dataDependencyNames
FTagDataDependencyNames m_dataDependencyNames
Definition: DL2.h:47
lwt
Definition: NnClusterizationFactory.h:52
FlavorTagDiscriminants::DL2::m_varsFromJet
std::vector< internal::VarFromJet > m_varsFromJet
Definition: DL2.h:40
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
xAOD::BTagging_v1
Definition: BTagging_v1.h:39
python.CreateTierZeroArgdict.outputs
outputs
Definition: CreateTierZeroArgdict.py:189
FlavorTagDiscriminants::DL2::m_varsFromBTag
std::vector< internal::VarFromBTag > m_varsFromBTag
Definition: DL2.h:39
FlavorTagDiscriminants::dataprep::checkForUnusedRemaps
void checkForUnusedRemaps(const std::map< std::string, std::string > &requested, const std::set< std::string > &used)
Definition: PhysicsAnalysis/JetTagging/FlavorTagDiscriminants/Root/DataPrepUtilities.cxx:454
FlavorTagDiscriminants::DL2::m_invalid_track_checker
std::function< char(const internal::Tracks &)> m_invalid_track_checker
Definition: DL2.h:44
DL2.h
FlavorTagDiscriminants::DL2::m_jetLink
SG::AuxElement::ConstAccessor< ElementLink< xAOD::JetContainer > > m_jetLink
Definition: DL2.h:35
FlavorTagDiscriminants::FTagOptions
Definition: PhysicsAnalysis/JetTagging/FlavorTagDiscriminants/FlavorTagDiscriminants/DataPrepUtilities.h:45
library_scraper.dd
list dd
Definition: library_scraper.py:46
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
FlavorTagDiscriminants::DL2::m_tracksLoaders
std::vector< std::shared_ptr< TracksLoader > > m_tracksLoaders
Definition: DL2.h:41
FlavorTagDiscriminants::DL2::m_decorators
std::map< std::string, internal::OutNodeFloat > m_decorators
Definition: DL2.h:42
DeMoScan.first
bool first
Definition: DeMoScan.py:536
FlavorTagDiscriminants::dataprep::createBvarGetters
std::tuple< std::vector< internal::VarFromBTag >, std::vector< internal::VarFromJet >, FTagDataDependencyNames > createBvarGetters(const std::vector< FTagInputConfig > &inputs)
Definition: PhysicsAnalysis/JetTagging/FlavorTagDiscriminants/Root/DataPrepUtilities.cxx:349
FlavorTagDiscriminants::DL2::decorateWithDefaults
void decorateWithDefaults(const SG::AuxElement &) const
Definition: DL2.cxx:84
FlavorTagDiscriminants::DL2::m_input_node_name
std::string m_input_node_name
Definition: DL2.h:36
FlavorTagDiscriminants::DL2::getDataDependencyNames
const FTagDataDependencyNames & getDataDependencyNames() const
Definition: DL2.cxx:142
FlavorTagDiscriminants::DL2::m_is_defaults
std::vector< SG::AuxElement::Decorator< char > > m_is_defaults
Definition: DL2.h:45
node
Definition: memory_hooks-stdcmalloc.h:74