ATLAS Offline Software
FoldDecoratorAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "FoldDecoratorAlg.h"
6 
9 
10 #include <nlohmann/json.hpp>
11 
12 #include <random>
13 
14 namespace FlavorTagDiscriminants {
15 
17  const std::string& name, ISvcLocator* svcloc):
19  {
20  }
21 
23  {
24  ATH_CHECK(m_mcEventNumberKey.initialize());
25  if (m_jetCollection.empty()) {
26  ATH_MSG_ERROR("jet collection not specified");
27  return StatusCode::FAILURE;
28  }
29  m_hashKey = m_jetCollection + "." + m_hashKey.key();
30  ATH_CHECK(m_hashKey.initialize());
31 
32  // other entropy sources, mostly counts
33  for (auto& key: m_jetAssociations) {
34  key = m_jetCollection + "." + key.key();
35  }
36  ATH_CHECK(m_jetAssociations.initialize());
37  for (auto& key: m_jetInts) {
38  key = m_jetCollection + "." + key.key();
39  }
40  ATH_CHECK(m_jetInts.initialize());
41 
42  // set up some salts for other sources of entropy
43  std::map<std::string, uint32_t> fullSeeds;
44  for (const auto& [k, v]: m_jetVarSeeds) {
45  fullSeeds[m_jetCollection + "." + k] = v;
46  }
47  auto addHashKeys = [this, &fullSeeds](auto keys) {
48  for (auto& key: keys) {
49  uint32_t salt = m_salt;
50  if (auto h = fullSeeds.extract(key.key())) {
51  salt = std::mt19937(h.mapped())();
52  }
53  this->m_hashedKeys[key.key()] = salt;
54  }
55  };
56  addHashKeys(m_jetAssociations);
57  addHashKeys(m_jetInts);
58  if (!fullSeeds.empty()) {
59  for (const auto& [k, v]: fullSeeds) {
60  ATH_MSG_ERROR("unused salt for jet variable " << k);
61  }
62  return StatusCode::FAILURE;
63  }
64 
65  // more sources from constituents
66  std::set<std::string> associations;
67  for (const auto& key: m_jetAssociations) associations.insert(key.key());
68  using charmap_t = std::map<std::string,std::vector<std::string>>;
70  for (const auto& [k, vlist]: chars.get<charmap_t>()) {
71  uint32_t salt = m_salt;
72  std::string key = m_jetCollection + "." + k;
73  if (!associations.count(key)) {
74  ATH_MSG_ERROR("Constituent " << key << " is not read from the jet");
75  return StatusCode::FAILURE;
76  }
77  for (const auto& v: vlist) {
78  if (auto h = m_constituentSeeds.value().extract(v)) {
79  salt = std::mt19937(h.mapped())();
80  }
81  m_chars[key].emplace_back(salt,v);
82  }
83  }
84  if (!m_constituentSeeds.empty()) {
85  for (const auto& [k, v]: m_constituentSeeds) {
86  ATH_MSG_ERROR("unused salt for constituent variable " << k);
87  }
88  return StatusCode::FAILURE;
89  }
90 
91  return StatusCode::SUCCESS;
92  }
93 
94  StatusCode FoldDecoratorAlg::execute(const EventContext& cxt) const {
96  m_mcEventNumberKey, cxt);
98  m_hashKey, cxt);
99  auto hjetassoc = m_jetAssociations.makeHandles(cxt);
100  auto hjetint = m_jetInts.makeHandles(cxt);
101  uint32_t number = hnumber(*hnumber);
102  auto event_hash = std::mt19937(number ^ m_salt)();
103 
104  // get more entropy from jet variables
105  auto getSalt = [this, event_hash](const auto& handle) {
106  return this->m_hashedKeys.at(handle.decorKey()) ^ event_hash;
107  };
108  for (const auto* jet: *hhash) {
109  uint32_t jet_hash = 0;
110  for (const auto& assoc: hjetassoc) {
111  const auto& iplc = assoc(*jet);
112  jet_hash ^= std::mt19937( iplc.size() ^ getSalt(assoc))();
113  if (m_chars.count(assoc.decorKey())) {
114  for (const auto& [salt, acc]: m_chars.at(assoc.decorKey())) {
115  unsigned int count = 0;
116  for (const auto& lnk: iplc) count += acc(**lnk);
117  jet_hash ^= std::mt19937(count ^ salt ^ event_hash)();
118  }
119  }
120  }
121  for (const auto& hint: hjetint) {
122  jet_hash ^= std::mt19937( hint(*jet) ^ getSalt(hint))();
123  }
124  hhash(*jet) = event_hash ^ jet_hash;
125  }
126  return StatusCode::SUCCESS;
127  }
128 
129 } // end namespace FlavorTagDiscriminants
FlavorTagDiscriminants::FoldDecoratorAlg::initialize
virtual StatusCode initialize() override
Definition: FoldDecoratorAlg.cxx:22
FlavorTagDiscriminants::FoldDecoratorAlg::m_chars
std::unordered_map< std::string, std::vector< SaltedCReader > > m_chars
Definition: FoldDecoratorAlg.h:62
FlavorTagDiscriminants
This file contains "getter" functions used for accessing tagger inputs from the EDM.
Definition: AssociationEnums.h:11
FlavorTagDiscriminants::FoldDecoratorAlg::m_jetVarSeeds
Gaudi::Property< std::map< std::string, uint32_t > > m_jetVarSeeds
Definition: FoldDecoratorAlg.h:47
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1040
FlavorTagDiscriminants::FoldDecoratorAlg::FoldDecoratorAlg
FoldDecoratorAlg(const std::string &name, ISvcLocator *svcloc)
Definition: FoldDecoratorAlg.cxx:16
FlavorTagDiscriminants::FoldDecoratorAlg::m_hashKey
SG::WriteDecorHandleKey< JC > m_hashKey
Definition: FoldDecoratorAlg.h:44
FoldDecoratorAlg.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
FlavorTagDiscriminants::FoldDecoratorAlg::m_salt
Gaudi::Property< uint32_t > m_salt
Definition: FoldDecoratorAlg.h:32
FlavorTagDiscriminants::FoldDecoratorAlg::m_hashedKeys
std::unordered_map< std::string, uint32_t > m_hashedKeys
Definition: FoldDecoratorAlg.h:58
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
WriteDecorHandle.h
Handle class for adding a decoration to an object.
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FlavorTagDiscriminants::FoldDecoratorAlg::m_jetCollection
Gaudi::Property< std::string > m_jetCollection
Definition: FoldDecoratorAlg.h:35
python.selection.number
number
Definition: selection.py:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
FlavorTagDiscriminants::FoldDecoratorAlg::m_jetAssociations
SG::ReadDecorHandleKeyArray< JC, IPLV > m_jetAssociations
Definition: FoldDecoratorAlg.h:38
python.PyAthena.v
v
Definition: PyAthena.py:157
FlavorTagDiscriminants::FoldDecoratorAlg::m_constituentSeeds
Gaudi::Property< std::map< std::string, uint32_t > > m_constituentSeeds
Definition: FoldDecoratorAlg.h:53
h
FlavorTagDiscriminants::FoldDecoratorAlg::m_mcEventNumberKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_mcEventNumberKey
Definition: FoldDecoratorAlg.h:29
FlavorTagDiscriminants::FoldDecoratorAlg::m_constituentChars
Gaudi::Property< std::string > m_constituentChars
Definition: FoldDecoratorAlg.h:50
ReadDecorHandle.h
Handle class for reading a decoration on an object.
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
FlavorTagDiscriminants::FoldDecoratorAlg::m_jetInts
SG::ReadDecorHandleKeyArray< JC, int > m_jetInts
Definition: FoldDecoratorAlg.h:41
FlavorTagDiscriminants::FoldDecoratorAlg::execute
virtual StatusCode execute(const EventContext &cxt) const override
Definition: FoldDecoratorAlg.cxx:94
fitman.k
k
Definition: fitman.py:528
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37