ATLAS Offline Software
NNJvtBinning.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
5 #include "nlohmann/json.hpp"
6 #include "xAODBase/IParticle.h"
7 
8 #include <algorithm>
9 #include <map>
10 #include <regex>
11 #include <stdexcept>
12 #include <string>
13 
14 namespace {
15  static constexpr float GeV = 1e3; // AnalysisBase has no SystemOfUnits.h
16 }
17 
18 namespace JetPileupTag {
19 
21  j = nlohmann::json{{"ptbin_edges", binning.ptEdges}, {"etabin_edges", binning.etaEdges}};
22  }
23 
25  j.at("ptbin_edges").get_to(binning.ptEdges);
26  j.at("etabin_edges").get_to(binning.etaEdges);
27  // pT values are stored in GeV but we should use CLHEP values wherever possible
28  for (float &value : binning.ptEdges)
29  value *= GeV;
30  }
31 
32  void to_json(nlohmann::json &j, const NNJvtCutMap &cutMap) {
33  to_json(j, cutMap.edges);
34  std::map<std::string, float> cuts;
35  for (std::size_t ptIdx = 0; ptIdx < cutMap.cutMap.size(); ++ptIdx)
36  for (std::size_t etaIdx = 0; etaIdx < cutMap.cutMap.at(ptIdx).size(); ++etaIdx)
37  cuts["(" + std::to_string(ptIdx) + ", " + std::to_string(etaIdx) + ")"] =
38  cutMap.cutMap.at(ptIdx).at(etaIdx);
39  j["cuts"] = cutMap;
40  }
41 
42  void from_json(const nlohmann::json &j, NNJvtCutMap &cutMap) {
43  j.get_to(cutMap.edges);
44  cutMap.cutMap.resize(cutMap.edges.ptEdges.size() - 1);
45  for (auto &v : cutMap.cutMap)
46  v.resize(cutMap.edges.etaEdges.size() - 1);
47  std::regex expr(R"(\‍((\d+),\s*(\d+)\))");
48  for (const auto &[bin, cut] : j["cuts"].get<std::map<std::string, float>>()) {
49  std::smatch sm;
50  if (!std::regex_match(bin, sm, expr))
51  throw std::invalid_argument("Invalid bin descriptor: " + bin);
52  cutMap.cutMap.at(std::stoi(sm[1])).at(std::stoi(sm[2])) = cut;
53  }
54  }
55 
58  is >> j;
59  return j.get<NNJvtBinning>();
60  }
61 
62  std::string NNJvtBinning::toJSON() const {
63  return nlohmann::json(*this).dump();
64  }
65 
66  bool
67  NNJvtBinning::operator()(float pt, float eta, std::size_t &ptBin, std::size_t &etaBin) const {
68  ptBin = std::distance(
69  ptEdges.begin(), std::lower_bound(ptEdges.begin(), ptEdges.end(), pt));
71  etaEdges.begin(), std::lower_bound(etaEdges.begin(), etaEdges.end(), eta));
72  // 0 => below the lowest bin edge, size() => above the highest bin edge
73  if (ptBin == 0 || ptBin == ptEdges.size())
74  ptBin = SIZE_MAX;
75  else
76  ptBin -= 1;
77  if (etaBin == 0 || etaBin == etaEdges.size())
78  etaBin = SIZE_MAX;
79  else
80  etaBin -= 1;
81 
82  return ptBin != SIZE_MAX && etaBin != SIZE_MAX;
83  }
84 
86  const xAOD::IParticle &particle, std::size_t &ptBin, std::size_t &etaBin) const {
87  return this->operator()(particle.pt(), particle.eta(), ptBin, etaBin);
88  }
89 
90  NNJvtCutMap NNJvtCutMap::fromJSON(std::istream &is) {
92  is >> j;
93  return j.get<NNJvtCutMap>();
94  }
95 
96  std::string NNJvtCutMap::toJSON() const {
97  return nlohmann::json(*this).dump();
98  }
99 
100  float NNJvtCutMap::operator()(float pt, float eta) const {
101  std::size_t ptBin, etaBin;
102  if (!edges(pt, eta, ptBin, etaBin))
103  return -1;
104  return this->operator()(ptBin, etaBin);
105  }
106 
108  std::size_t ptBin, etaBin;
109  if (!edges(particle, ptBin, etaBin))
110  return -1;
111  return this->operator()(ptBin, etaBin);
112  }
113 
114  float NNJvtCutMap::operator()(std::size_t ptBin, std::size_t etaBin) const {
115  return cutMap.at(ptBin).at(etaBin);
116  }
117 } // namespace JetPileupTag
JetPileupTag::NNJvtBinning::operator()
bool operator()(float pt, float eta, std::size_t &ptBin, std::size_t &etaBin) const
Get the correct bin for the provided pt/eta values.
Definition: NNJvtBinning.cxx:67
JetPileupTag::NNJvtCutMap::toJSON
std::string toJSON() const
Definition: NNJvtBinning.cxx:96
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
AddEmptyComponent.binning
binning
Definition: AddEmptyComponent.py:34
JetPileupTag::NNJvtCutMap
The NNJvt cut maps.
Definition: NNJvtBinning.h:50
JetPileupTag::NNJvtBinning
Helper struct to hold the bin edges for the NN Jvt cut maps.
Definition: NNJvtBinning.h:23
IParticle.h
json
nlohmann::json json
Definition: HistogramDef.cxx:9
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
test_pyathena.pt
pt
Definition: test_pyathena.py:11
bin
Definition: BinsDiffFromStripMedian.h:43
athena.value
value
Definition: athena.py:122
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
JetPileupTag::NNJvtBinning::etaEdges
std::vector< float > etaEdges
Definition: NNJvtBinning.h:25
JetPileupTag
Definition: JetVertexNNTagger.h:39
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
JetPileupTag::NNJvtCutMap::fromJSON
static NNJvtCutMap fromJSON(std::istream &is)
Definition: NNJvtBinning.cxx:90
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
JetPileupTag::NNJvtBinning::toJSON
std::string toJSON() const
Definition: NNJvtBinning.cxx:62
xAOD::etaBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap etaBin
Definition: L2StandAloneMuon_v1.cxx:148
CheckAppliedSFs.e3
e3
Definition: CheckAppliedSFs.py:264
JetPileupTag::NNJvtCutMap::edges
NNJvtBinning edges
Definition: NNJvtBinning.h:51
BindingsTest.cut
cut
This script demonstrates how to call a C++ class from Python Also how to use PyROOT is shown.
Definition: BindingsTest.py:13
plotBeamSpotVert.cuts
string cuts
Definition: plotBeamSpotVert.py:93
JetPileupTag::NNJvtCutMap::operator()
float operator()(float pt, float eta) const
Get the correct cut value for the provided pt/eta.
Definition: NNJvtBinning.cxx:100
xAOD::IParticle::pt
virtual double pt() const =0
The transverse momentum ( ) of the particle.
JetPileupTag::NNJvtBinning::ptEdges
std::vector< float > ptEdges
Definition: NNJvtBinning.h:24
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
python.PyAthena.v
v
Definition: PyAthena.py:157
JetPileupTag::to_json
void to_json(nlohmann::json &j, const NNJvtBinning &binning)
Definition: NNJvtBinning.cxx:20
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
JetPileupTag::NNJvtCutMap::cutMap
std::vector< std::vector< float > > cutMap
Definition: NNJvtBinning.h:52
JetPileupTag::GeV
constexpr float GeV
Definition: JetVertexNNTagger.h:42
JetPileupTag::from_json
void from_json(const nlohmann::json &j, NNJvtBinning &binning)
Definition: NNJvtBinning.cxx:24
JetPileupTag::NNJvtBinning::fromJSON
static NNJvtBinning fromJSON(std::istream &is)
Definition: NNJvtBinning.cxx:56
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
NNJvtBinning.h