ATLAS Offline Software
Loading...
Searching...
No Matches
JZCombineWeight.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "JZCombineWeight.h"
6
10
11#include <fstream>
12#include <sstream>
13
14using Athena::Units::GeV;
15
19
21 std::ifstream fs(m_weightsFile);
22 try {
23 m_weightsMap = nlohmann::json::parse(fs);
24 } catch (const std::exception&) {
25 ATH_MSG_ERROR("Failed to parse input " << m_weightsFile);
26 return StatusCode::FAILURE;
27 }
28 return StatusCode::SUCCESS;
29}
30
31StatusCode JZCombineWeight::getValue(double& value) const {
32 const xAOD::EventInfo* evt {nullptr};
33 ATH_CHECK(evtStore()->retrieve(evt, "EventInfo"));
34
35 // Truth jets are only valid for MC
36 bool isMC = evt->eventTypeBitmask() & xAOD::EventInfo::IS_SIMULATION;
37 if (!isMC) { return StatusCode::SUCCESS; }
38
39 auto mu_actual = static_cast<std::size_t>(std::ceil(evt->actualInteractionsPerCrossing()));
40
41
42 // categories
43 std::vector<std::size_t> categories(m_binning.size() - 1, 0);
44
45 // HS jets
46 double pt_j0_AK4HS {5.0};
47 std::size_t idx_HS {0};
48 const xAOD::JetContainer* hs_jets {nullptr};
49 ATH_CHECK(evtStore()->retrieve(hs_jets, m_jetCollectionHS));
50 if (hs_jets->size()) {
51 auto iter_jet = std::max_element(hs_jets->begin(), hs_jets->end(),
52 [](const xAOD::Jet* j0, const xAOD::Jet* j1){ return j0->pt() < j1->pt(); });
53 pt_j0_AK4HS = (*iter_jet)->pt() / GeV;
54 idx_HS = getIndex(pt_j0_AK4HS);
55 }
56
57 ++categories[idx_HS];
58
59 // PU jets
60 std::vector<double> pt_j0_AK4PU;
61 const xAOD::JetContainer* pu_jets {nullptr};
62 ATH_CHECK(evtStore()->retrieve(pu_jets, m_jetCollectionPU));
63 std::unordered_map<int, double> leading_pts;
64 if (pu_jets->size()) {
65 for (const xAOD::Jet* pu_jet : *pu_jets) {
66 SG::ConstAccessor<int> pu_enum_acc("pileupEventNumber");
67 const uint32_t pu_enum = static_cast<uint32_t>(pu_enum_acc.withDefault(*pu_jet, 0));
68 double pu_jet_pt = pu_jet->pt() / GeV;
69 if (!leading_pts.contains(pu_enum) || leading_pts.at(pu_enum) < pu_jet_pt) {
70 leading_pts[pu_enum] = pu_jet_pt;
71 }
72 }
73 }
74 auto n_pileup_records = leading_pts.size();
75
76 for (const auto& pair : leading_pts) {
77 double pt = pair.second;
78 pt_j0_AK4PU.push_back(pt);
79 std::size_t idx_PU = getIndex(pt);
80 ++categories[idx_PU];
81 }
82
83 // Remining low pT pileups
84 categories[0] += mu_actual - n_pileup_records - 1;
85
86 // Convert to string
87 std::stringstream key_ss;
88 std::string delim {"_"};
89 for (std::size_t i = 0; i < categories.size(); ++i) {
90 if (i != 0) key_ss << delim;
91 key_ss << categories.at(i);
92 }
93 key_ss << "," << idx_HS;
94
95 const std::string& key = key_ss.str();
96
97 // categories that are not included in the weight mapping will be skipped
98 if (m_weightsMap.find(key) == m_weightsMap.end()) {
99 value = 0.0;
100 return StatusCode::SUCCESS;
101 }
102
103 for (const auto& weightName : m_weightsName) {
104 try {
105 auto wt = m_weightsMap.at(key).at(weightName).get<double>();
106 value *= wt;
107 } catch (const std::exception&) {
108 ATH_MSG_ERROR("Failed to get weight [" << weightName << "] in [" << key << "]!");
109 return StatusCode::FAILURE;
110 }
111 }
112
113 return StatusCode::SUCCESS;
114}
115
116std::size_t JZCombineWeight::getIndex(double value) const {
117 auto it = std::upper_bound(m_binning.begin(), m_binning.end(), value);
118 return std::distance(m_binning.begin(), it) - 1;
119}
120
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
static Double_t fs
Wrapper to avoid constant divisions when using units.
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
Gaudi::Property< std::string > m_weightsFile
virtual StatusCode getValue(double &value) const override
Gaudi::Property< std::vector< double > > m_binning
Gaudi::Property< std::vector< std::string > > m_weightsName
Gaudi::Property< std::string > m_jetCollectionHS
std::size_t getIndex(double value) const
virtual StatusCode initialize() override
This class provides the weight used to combine JZ sliced samples by querying JSON.
nlohmann::json m_weightsMap
Gaudi::Property< std::string > m_jetCollectionPU
Helper class to provide constant type-safe access to aux data.
const_reference_type withDefault(const ELT &e, const T &deflt) const
Fetch the variable for one element, as a const reference, with a default.
STL class.
@ IS_SIMULATION
true: simulation, false: data
Jet_v1 Jet
Definition of the current "jet version".
EventInfo_v1 EventInfo
Definition of the latest event info version.
JetContainer_v1 JetContainer
Definition of the current "jet container version".