ATLAS Offline Software
Loading...
Searching...
No Matches
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"
7
8#include <algorithm>
9#include <map>
10#include <regex>
11#include <stdexcept>
12#include <string>
13
14namespace {
15 static constexpr float GeV = 1e3; // AnalysisBase has no SystemOfUnits.h
16}
17
18namespace JetPileupTag {
19
20 void to_json(nlohmann::json &j, const NNJvtBinning &binning) {
21 j = nlohmann::json{{"ptbin_edges", binning.ptEdges}, {"etabin_edges", binning.etaEdges}};
22 }
23
24 void from_json(const nlohmann::json &j, NNJvtBinning &binning) {
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
57 nlohmann::json j;
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));
70 etaBin = std::distance(
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 // Use lowest pt bin for any jets with lower pt
74 if (ptBin == 0)
75 ptBin = 1;
76 if (ptBin == ptEdges.size())
77 ptBin = SIZE_MAX;
78 else
79 ptBin -= 1;
80 if (etaBin == 0 || etaBin == etaEdges.size())
81 etaBin = SIZE_MAX;
82 else
83 etaBin -= 1;
84
85 return ptBin != SIZE_MAX && etaBin != SIZE_MAX;
86 }
87
89 const xAOD::IParticle &particle, std::size_t &ptBin, std::size_t &etaBin) const {
90 return this->operator()(particle.pt(), particle.eta(), ptBin, etaBin);
91 }
92
94 nlohmann::json j;
95 is >> j;
96 return j.get<NNJvtCutMap>();
97 }
98
99 std::string NNJvtCutMap::toJSON() const {
100 return nlohmann::json(*this).dump();
101 }
102
103 float NNJvtCutMap::operator()(float pt, float eta) const {
104 std::size_t ptBin, etaBin;
105 if (!edges(pt, eta, ptBin, etaBin))
106 return -1;
107 return this->operator()(ptBin, etaBin);
108 }
109
110 float NNJvtCutMap::operator()(const xAOD::IParticle &particle) const {
111 std::size_t ptBin, etaBin;
112 if (!edges(particle, ptBin, etaBin))
113 return -1;
114 return this->operator()(ptBin, etaBin);
115 }
116
117 float NNJvtCutMap::operator()(std::size_t ptBin, std::size_t etaBin) const {
118 return cutMap.at(ptBin).at(etaBin);
119 }
120} // namespace JetPileupTag
Scalar eta() const
pseudorapidity method
Helpers for reading NN Jvt network binnings and results from an input file.
Class providing the definition of the 4-vector interface.
virtual double pt() const =0
The transverse momentum ( ) of the particle.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
void to_json(nlohmann::json &j, const NNJvtBinning &binning)
constexpr float GeV
void from_json(const nlohmann::json &j, NNJvtBinning &binning)
Helper struct to hold the bin edges for the NN Jvt cut maps.
std::vector< float > ptEdges
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.
std::vector< float > etaEdges
std::string toJSON() const
static NNJvtBinning fromJSON(std::istream &is)
The NNJvt cut maps.
std::string toJSON() const
static NNJvtCutMap fromJSON(std::istream &is)
float operator()(float pt, float eta) const
Get the correct cut value for the provided pt/eta.
std::vector< std::vector< float > > cutMap