ATLAS Offline Software
Loading...
Searching...
No Matches
JZCombineWeight Class Reference

This class provides the weight used to combine JZ sliced samples by querying JSON. More...

#include <JZCombineWeight.h>

Inheritance diagram for JZCombineWeight:
Collaboration diagram for JZCombineWeight:

Public Member Functions

virtual StatusCode initialize () override
 This class provides the weight used to combine JZ sliced samples by querying JSON.
virtual StatusCode getValue (double &value) const override

Private Member Functions

std::size_t getIndex (double value) const

Private Attributes

Gaudi::Property< std::string > m_jetCollectionHS
Gaudi::Property< std::string > m_jetCollectionPU
Gaudi::Property< std::string > m_weightsFile
Gaudi::Property< std::vector< double > > m_binning
Gaudi::Property< std::vector< std::string > > m_weightsName
nlohmann::json m_weightsMap

Detailed Description

This class provides the weight used to combine JZ sliced samples by querying JSON.

Definition at line 19 of file JZCombineWeight.h.

Member Function Documentation

◆ getIndex()

std::size_t JZCombineWeight::getIndex ( double value) const
private

Definition at line 116 of file JZCombineWeight.cxx.

116 {
117 auto it = std::upper_bound(m_binning.begin(), m_binning.end(), value);
118 return std::distance(m_binning.begin(), it) - 1;
119}
Gaudi::Property< std::vector< double > > m_binning

◆ getValue()

StatusCode JZCombineWeight::getValue ( double & value) const
overridevirtual

Definition at line 31 of file JZCombineWeight.cxx.

31 {
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
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::vector< std::string > > m_weightsName
Gaudi::Property< std::string > m_jetCollectionHS
std::size_t getIndex(double value) const
nlohmann::json m_weightsMap
Gaudi::Property< std::string > m_jetCollectionPU
@ 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".
setEventNumber uint32_t

◆ initialize()

StatusCode JZCombineWeight::initialize ( )
overridevirtual

This class provides the weight used to combine JZ sliced samples by querying JSON.

Definition at line 20 of file JZCombineWeight.cxx.

20 {
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}
static Double_t fs
Gaudi::Property< std::string > m_weightsFile

Member Data Documentation

◆ m_binning

Gaudi::Property<std::vector<double> > JZCombineWeight::m_binning
private
Initial value:
{
this, "Binning", {}, "Pt binning used to define categories"}

Definition at line 36 of file JZCombineWeight.h.

36 {
37 this, "Binning", {}, "Pt binning used to define categories"};

◆ m_jetCollectionHS

Gaudi::Property<std::string> JZCombineWeight::m_jetCollectionHS
private
Initial value:
{
this, "JetCollectionHS", "AntiKt4TruthJets", "Name of the hard-scatter jet collection"}

Definition at line 30 of file JZCombineWeight.h.

30 {
31 this, "JetCollectionHS", "AntiKt4TruthJets", "Name of the hard-scatter jet collection"};

◆ m_jetCollectionPU

Gaudi::Property<std::string> JZCombineWeight::m_jetCollectionPU
private
Initial value:
{
this, "JetCollectionPU", "InTimeAntiKt4TruthJets", "Name of the pile-up jet collection"}

Definition at line 32 of file JZCombineWeight.h.

32 {
33 this, "JetCollectionPU", "InTimeAntiKt4TruthJets", "Name of the pile-up jet collection"};

◆ m_weightsFile

Gaudi::Property<std::string> JZCombineWeight::m_weightsFile
private
Initial value:
{
this, "WeightsFile", "", "Path to the weights file"}

Definition at line 34 of file JZCombineWeight.h.

34 {
35 this, "WeightsFile", "", "Path to the weights file"};

◆ m_weightsMap

nlohmann::json JZCombineWeight::m_weightsMap
private

Definition at line 41 of file JZCombineWeight.h.

◆ m_weightsName

Gaudi::Property<std::vector<std::string> > JZCombineWeight::m_weightsName
private
Initial value:
{
this, "WeightsName", {}, "Weights to be calculated - their product is used"}

Definition at line 38 of file JZCombineWeight.h.

38 {
39 this, "WeightsName", {}, "Weights to be calculated - their product is used"};

The documentation for this class was generated from the following files: