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 FlavorTagInference {
15 
17  const std::string& name, ISvcLocator* svcloc):
19  {
20  }
21 
23  {
24  ATH_CHECK(m_mcEventNumberKey.initialize());
25  // event info is usually not decorated, renounce the dependency
27  if (m_jetCollection.empty()) {
28  ATH_MSG_ERROR("jet collection not specified");
29  return StatusCode::FAILURE;
30  }
31  m_hashKey = m_jetCollection + "." + m_hashKey.key();
32  ATH_CHECK(m_hashKey.initialize());
33 
34  // other entropy sources, mostly counts
35  for (auto& key: m_jetAssociations) {
36  key = m_jetCollection + "." + key.key();
37  }
38  ATH_CHECK(m_jetAssociations.initialize());
39  // most the associations also aren't decorated, renounce these
40  // dependencies too
42  for (auto& key: m_jetInts) {
43  key = m_jetCollection + "." + key.key();
44  }
45  ATH_CHECK(m_jetInts.initialize());
46  // same with jetInts: usually not decorated
48 
49  // set up some salts for other sources of entropy
50  std::map<std::string, uint32_t> fullSeeds;
51  for (const auto& [k, v]: m_jetVarSeeds) {
52  fullSeeds[m_jetCollection + "." + k] = v;
53  }
54  auto addHashKeys = [this, &fullSeeds](auto keys) {
55  for (auto& key: keys) {
56  uint32_t salt = m_salt;
57  if (auto h = fullSeeds.extract(key.key())) {
58  salt = std::mt19937(h.mapped())();
59  }
60  this->m_hashedKeys[key.key()] = salt;
61  }
62  };
63  addHashKeys(m_jetAssociations);
64  addHashKeys(m_jetInts);
65  if (!fullSeeds.empty()) {
66  for (const auto& [k, v]: fullSeeds) {
67  ATH_MSG_ERROR("unused salt for jet variable " << k);
68  }
69  return StatusCode::FAILURE;
70  }
71 
72  // more sources from constituents
73  std::set<std::string> associations;
74  for (const auto& key: m_jetAssociations) associations.insert(key.key());
75  using charmap_t = std::map<std::string,std::vector<std::string>>;
77  for (const auto& [k, vlist]: chars.get<charmap_t>()) {
78  uint32_t salt = m_salt;
79  std::string key = m_jetCollection + "." + k;
80  if (!associations.count(key)) {
81  ATH_MSG_ERROR("Constituent " << key << " is not read from the jet");
82  return StatusCode::FAILURE;
83  }
84  for (const auto& v: vlist) {
85  if (auto h = m_constituentSeeds.value().extract(v)) {
86  salt = std::mt19937(h.mapped())();
87  }
88  m_chars[key].emplace_back(salt,v);
89  }
90  }
91  if (!m_constituentSeeds.empty()) {
92  for (const auto& [k, v]: m_constituentSeeds) {
93  ATH_MSG_ERROR("unused salt for constituent variable " << k);
94  }
95  return StatusCode::FAILURE;
96  }
97 
98  return StatusCode::SUCCESS;
99  }
100 
101  StatusCode FoldDecoratorAlg::execute(const EventContext& cxt) const {
103  m_mcEventNumberKey, cxt);
105  m_hashKey, cxt);
106  auto hjetassoc = m_jetAssociations.makeHandles(cxt);
107  auto hjetint = m_jetInts.makeHandles(cxt);
108  uint32_t number = hnumber(*hnumber);
109  auto event_hash = std::mt19937(number ^ m_salt)();
110 
111  // get more entropy from jet variables
112  auto getSalt = [this, event_hash](const auto& handle) {
113  return this->m_hashedKeys.at(handle.decorKey()) ^ event_hash;
114  };
115  for (const auto* jet: *hhash) {
116  uint32_t jet_hash = 0;
117  for (const auto& assoc: hjetassoc) {
118  const auto& iplc = assoc(*jet);
119  jet_hash ^= std::mt19937( iplc.size() ^ getSalt(assoc))();
120  if (m_chars.count(assoc.decorKey())) {
121  for (const auto& [salt, acc]: m_chars.at(assoc.decorKey())) {
122  unsigned int count = 0;
123  for (const auto& lnk: iplc) count += acc(**lnk);
124  jet_hash ^= std::mt19937(count ^ salt ^ event_hash)();
125  }
126  }
127  }
128  for (const auto& hint: hjetint) {
129  jet_hash ^= std::mt19937( hint(*jet) ^ getSalt(hint))();
130  }
131  hhash(*jet) = event_hash ^ jet_hash;
132  }
133  return StatusCode::SUCCESS;
134  }
135 
136 } // end namespace FlavorTagInference
FlavorTagInference
This file contains "getter" functions used for accessing tagger inputs from the EDM.
Definition: PhysicsAnalysis/JetTagging/FlavorTagInference/FlavorTagInference/ConstituentsLoader.h:27
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
FlavorTagInference::FoldDecoratorAlg::m_jetAssociations
SG::ReadDecorHandleKeyArray< JC, IPLV > m_jetAssociations
Definition: FoldDecoratorAlg.h:38
FlavorTagInference::FoldDecoratorAlg::m_jetCollection
Gaudi::Property< std::string > m_jetCollection
Definition: FoldDecoratorAlg.h:35
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1113
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::renounce
std::enable_if_t< std::is_void_v< std::result_of_t< decltype(&T::renounce)(T)> > &&!std::is_base_of_v< SG::VarHandleKeyArray, T > &&std::is_base_of_v< Gaudi::DataHandle, T >, void > renounce(T &h)
Definition: AthCommonDataStore.h:380
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::renounceArray
void renounceArray(SG::VarHandleKeyArray &handlesArray)
remove all handles from I/O resolution
Definition: AthCommonDataStore.h:364
FoldDecoratorAlg.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:84
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
FlavorTagInference::FoldDecoratorAlg::m_hashKey
SG::WriteDecorHandleKey< JC > m_hashKey
Definition: FoldDecoratorAlg.h:44
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
FlavorTagInference::FoldDecoratorAlg::initialize
virtual StatusCode initialize() override
Definition: FoldDecoratorAlg.cxx:22
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
FlavorTagInference::FoldDecoratorAlg::m_jetInts
SG::ReadDecorHandleKeyArray< JC, int > m_jetInts
Definition: FoldDecoratorAlg.h:41
FlavorTagInference::FoldDecoratorAlg::m_hashedKeys
std::unordered_map< std::string, uint32_t > m_hashedKeys
Definition: FoldDecoratorAlg.h:58
FlavorTagInference::FoldDecoratorAlg::m_salt
Gaudi::Property< uint32_t > m_salt
Definition: FoldDecoratorAlg.h:32
FlavorTagInference::FoldDecoratorAlg::m_constituentSeeds
Gaudi::Property< std::map< std::string, uint32_t > > m_constituentSeeds
Definition: FoldDecoratorAlg.h:53
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:100
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
FlavorTagInference::FoldDecoratorAlg::FoldDecoratorAlg
FoldDecoratorAlg(const std::string &name, ISvcLocator *svcloc)
Definition: FoldDecoratorAlg.cxx:16
FlavorTagInference::FoldDecoratorAlg::m_chars
std::unordered_map< std::string, std::vector< SaltedCReader > > m_chars
Definition: FoldDecoratorAlg.h:62
python.selection.number
number
Definition: selection.py:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
python.PyAthena.v
v
Definition: PyAthena.py:154
h
FlavorTagInference::FoldDecoratorAlg::m_mcEventNumberKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_mcEventNumberKey
Definition: FoldDecoratorAlg.h:29
ReadDecorHandle.h
Handle class for reading a decoration on an object.
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:801
FlavorTagInference::FoldDecoratorAlg::m_constituentChars
Gaudi::Property< std::string > m_constituentChars
Definition: FoldDecoratorAlg.h:50
FlavorTagInference::FoldDecoratorAlg::m_jetVarSeeds
Gaudi::Property< std::map< std::string, uint32_t > > m_jetVarSeeds
Definition: FoldDecoratorAlg.h:47
fitman.k
k
Definition: fitman.py:528
FlavorTagInference::FoldDecoratorAlg::execute
virtual StatusCode execute(const EventContext &cxt) const override
Definition: FoldDecoratorAlg.cxx:101
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37