Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PMGTruthWeightTool.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 // EDM include(s):
6 #ifndef XAOD_STANDALONE
8 #endif
9 
10 // Local include(s):
12 #include <regex>
13 #include <PMGTools/WeightHelpers.h>
16 
18 
19 
20 namespace PMGTools
21 {
23  : asg::AsgMetadataTool(name)
24  {
25  declareProperty("MetaObjectName", m_metaName = "TruthMetaData");
26  }
27 
28 
30  {
31  ATH_MSG_DEBUG("Initialising...");
32 
33  ATH_MSG_INFO("Attempting to retrieve truth meta data from the first file...");
34 
35  // Clear cached weights
37 
39  m_calibCache.initialize (std::move (affSysts),
40  [this] (const CP::SystematicSet& sys,
41  std::size_t& idx) {
42  if (!sys.name().empty()) ATH_MSG_WARNING("Mapping for " << sys.name() << " missing, setting to index 0.");
43  idx = 0;
44  return StatusCode::SUCCESS;
45  });
46 
47  // Try to load MC channel number from file metadata
49 
50  if (m_mcChannelNumber == uint32_t(-1)) {
51  ATH_MSG_WARNING("... MC channel number could not be loaded");
52  } else {
53  ATH_MSG_INFO("... MC channel number identified as " << m_mcChannelNumber);
54  }
55 
56  // Start by trying to load metadata from the store
57  m_metaDataContainer = nullptr;
58  if (inputMetaStore()->contains<xAOD::TruthMetaDataContainer>(m_metaName)) {
60  ATH_MSG_INFO("Loaded xAOD::TruthMetaDataContainer");
61 
62  // Check for incorrectly stored McChannelNumber
64  for (auto truthMetaData : *m_metaDataContainer) {
65  if (truthMetaData->mcChannelNumber() != 0) { m_useChannelZeroInMetaData = false; }
66  }
67  // If we only have one metadata item take MC channel from there if needed
68  if (m_mcChannelNumber == uint32_t(-1) && m_metaDataContainer->size() == 1) {
69  m_mcChannelNumber = m_metaDataContainer->at(0)->mcChannelNumber();
70  ATH_MSG_WARNING("... MC channel number taken from the metadata as " << m_mcChannelNumber);
71  }
72  if (m_useChannelZeroInMetaData) { ATH_MSG_WARNING("MC channel number in TruthMetaData is invalid - assuming that channel 0 has the correct information."); }
73 
74  // Load metadata from TruthMetaDataContainer if we have a valid channel number or if we're going to use 0 anyway
75  // ... otherwise wait until we can load a channel number from EventInfo
77  if (loadMetaData().isFailure()) {
78  ATH_MSG_ERROR("Could not load metadata for MC channel number " << m_mcChannelNumber);
79  return StatusCode::FAILURE;
80  }
81  }
82  } else {
83  // ... now try to load the weight container using the POOL metadata (not possible in AnalysisBase)
84  // see https://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/AthAnalysisBase#How_to_read_the_truth_weight_nam
85  if (loadPOOLMetaData().isFailure()) {
86  ATH_MSG_ERROR("Could not load POOL HepMCWeightNames");
87  return StatusCode::FAILURE;
88  }
89  }
90 
91  // Add the affecting systematics to the global registry
93  if (!registry.registerSystematics(*this)) {
94  ATH_MSG_ERROR("unable to register the systematics");
95  return StatusCode::FAILURE;
96  }
97 
98  ATH_MSG_DEBUG("Successfully initialized!");
99 
100  return StatusCode::SUCCESS;
101  }
102 
103 
104  const std::vector<std::string>& PMGTruthWeightTool::getWeightNames() const {
105  return m_weightNames;
106  }
107 
108 
109  float PMGTruthWeightTool::getWeight(const xAOD::EventInfo* evtInfo, const std::string& weightName) const {
110  // Return appropriate weight from EventInfo: this should be identical to the TruthEvent
111  try {
112  return evtInfo->mcEventWeight(m_weightIndices.at(weightName));
113  } catch (const std::out_of_range& e) {
114  // Before throwing an exception, try to recover with bad naming conventions
115  std::string strippedName = std::regex_replace(weightName, std::regex(" "), "_");
116  std::transform(strippedName.begin(), strippedName.end(), strippedName.begin(),
117  [](unsigned char c){ return std::tolower(c); });
118  for (const std::string &weight : m_weightNames) {
119  std::string modifiedName = std::regex_replace(weight, std::regex(" "), "_");
120  std::transform(modifiedName.begin(), modifiedName.end(), modifiedName.begin(),
121  [](unsigned char c){ return std::tolower(c); });
122  if (strippedName == modifiedName){
123  ATH_MSG_WARNING("Using weight name \"" << weight << "\" instead of requested \"" << weightName << "\"");
124  return getWeight(evtInfo, weight);
125  }
126  }
127  ATH_MSG_FATAL("Weight \"" + weightName + "\" could not be found");
128  throw std::runtime_error(name() + ": Weight \"" + weightName + "\" could not be found");
129  }
130  }
131 
132 
133  bool PMGTruthWeightTool::hasWeight(const std::string& weightName) const {
134  return (m_weightIndices.count(weightName) > 0);
135  }
136 
137 
139  {
140  const std::size_t *res;
142  return evtInfo->mcEventWeight(*res);
143  }
144 
145 
147  {
148  const std::size_t *res;
150  return *res;
151  }
152 
153 
155  {
157  }
158 
159 
161  {
162  return affectingSystematics();
163  }
164 
165 
167  mcChannelNumber = static_cast<uint32_t>(-1);
168 
169  // Try to load MC channel number from file metadata
170  ATH_MSG_INFO("Attempting to retrieve MC channel number...");
171  const xAOD::FileMetaData *fmd = nullptr;
172  if (inputMetaStore()->contains<xAOD::FileMetaData>("FileMetaData")) {
173  ATH_CHECK(inputMetaStore()->retrieve(fmd, "FileMetaData"));
174  float fltChannelNumber(-1);
175  if (fmd->value(xAOD::FileMetaData::mcProcID, fltChannelNumber)) {
176  mcChannelNumber = static_cast<uint32_t>(fltChannelNumber);
177  return StatusCode::SUCCESS;
178  }
179  }
180  return StatusCode::FAILURE;
181  }
182 
184  {
185  // Detect possible MC channel number change
188 
190  ATH_MSG_ERROR("MC channel number from a new file does not match the previously processed files.");
191  return StatusCode::FAILURE;
192  }
193 
194  return StatusCode::SUCCESS;
195  }
196 
197 
199  // Find the correct truth meta data object
200  uint32_t targetChannelNumber = (m_useChannelZeroInMetaData ? 0 : m_mcChannelNumber);
201  ATH_MSG_INFO("Attempting to load weight meta data from xAOD TruthMetaData for channel " << targetChannelNumber);
202  auto itTruthMetaDataPtr = std::find_if(m_metaDataContainer->begin(), m_metaDataContainer->end(),
203  [targetChannelNumber] (const auto& it) { return it->mcChannelNumber() == targetChannelNumber; }
204  );
205 
206  // If no such object is found then return
207  if (itTruthMetaDataPtr == m_metaDataContainer->end()) {
208  ATH_MSG_ERROR("Could not load weight meta data!");
209  return StatusCode::FAILURE;
210  }
211 
212  // Update cached weight data
213  const std::vector<std::string> &truthWeightNames = (*itTruthMetaDataPtr)->weightNames();
214  for(std::size_t idx = 0; idx < truthWeightNames.size(); ++idx ) {
215  m_weightNames.push_back(truthWeightNames.at(idx));
216  m_weightIndices[truthWeightNames.at(idx)] = idx;
217 
218  std::string sysName = weightNameWithPrefix(truthWeightNames.at(idx));
219  if (!sysName.empty()) {
221  }
222 
223  ANA_MSG_VERBOSE(" " << truthWeightNames.at(idx) << " " << sysName);
224  }
225  return this->validateWeightLocationCaches();
226  }
227 
228 
230  // AnalysisBase can only use the xAOD::TruthMetaDataContainer, so skip this
231 #ifdef XAOD_STANDALONE
232  return StatusCode::SUCCESS;
233 #else
234  ATH_MSG_INFO("Looking for POOL HepMC IOVMetaData...");
235  std::map<std::string, int> hepMCWeightNamesMap;
236  if (AAH::retrieveMetadata("/Generation/Parameters", "HepMCWeightNames", hepMCWeightNamesMap, inputMetaStore()).isFailure()) {
237  ATH_MSG_FATAL("Cannot access metadata " << m_metaName << " and failed to get names from IOVMetadata");
238  return StatusCode::FAILURE;
239  }
240 
241  // Use input map to fill the index map and the weight names
242  ATH_MSG_INFO("Attempting to load weight meta data from HepMC IOVMetaData container");
243  for (auto& kv : hepMCWeightNamesMap) {
244  m_weightNames.push_back(kv.first);
245  m_weightIndices[kv.first] = kv.second;
246 
247  std::string sysName = weightNameWithPrefix(kv.first);
248  if (!sysName.empty()) {
250  }
251 
252  ANA_MSG_VERBOSE(" " << kv.first << " " << sysName);
253  }
254  return this->validateWeightLocationCaches();
255 #endif // XAOD_STANDALONE
256  }
257 
258 
260  // Validate weight caches against one another
261  if (m_weightNames.size() != m_weightIndices.size()) {
262  ATH_MSG_ERROR("Found " << m_weightNames.size() << " but " << m_weightIndices.size() << " weight indices!");
263  return StatusCode::FAILURE;
264  }
265 
266  ATH_MSG_INFO("Successfully loaded information about " << m_weightNames.size() << " weights");
267  return StatusCode::SUCCESS;
268  }
269 
270 
272  m_weightNames.clear();
273  m_weightIndices.clear();
274  }
275 
276 } // namespace PMGTools
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
python.Dso.registry
registry
Definition: Control/AthenaServices/python/Dso.py:159
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:93
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
WeightHelpers.h
Postprocess generator weight names to be name/ROOT friendly.
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CP::SystematicsCache::initialize
void initialize(CP::SystematicSet val_affectingSystematics, std::function< StatusCode(const CP::SystematicSet &sys, CalibData &result)> val_calc) noexcept
initialize with the list of affecting systematics and a function to calculate the calibration data fo...
Definition: SystematicsCache.h:158
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PMGTools::PMGTruthWeightTool::PMGTruthWeightTool
PMGTruthWeightTool(const std::string &name)
Create a proper constructor for Athena.
Definition: PMGTruthWeightTool.cxx:22
PMGTools::PMGTruthWeightTool::hasWeight
virtual bool hasWeight(const std::string &weightName) const override
Implements interface from IPMGTruthWeightTool.
Definition: PMGTruthWeightTool.cxx:133
skel.it
it
Definition: skel.GENtoEVGEN.py:407
asg
Definition: DataHandleTestTool.h:28
PMGTools::PMGTruthWeightTool::m_weightIndices
std::unordered_map< std::string, size_t > m_weightIndices
Weight names to indices of available weights in this file.
Definition: PMGTruthWeightTool.h:113
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
asg::AsgMetadataTool::inputMetaStore
MetaStorePtr_t inputMetaStore() const
Accessor for the input metadata store.
Definition: AsgMetadataTool.cxx:88
PMGTools::PMGTruthWeightTool::initialize
virtual StatusCode initialize() override
Function initialising the tool.
Definition: PMGTruthWeightTool.cxx:29
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
PMGTools::PMGTruthWeightTool::m_calibCache
CP::SystematicsCache< std::size_t > m_calibCache
Systematics to indices of available weights.
Definition: PMGTruthWeightTool.h:116
PMGTools::PMGTruthWeightTool::loadMetaData
StatusCode loadMetaData()
Loads weight information from xAOD::TruthMetaDataContainer.
Definition: PMGTruthWeightTool.cxx:198
CP::SystematicVariation
Definition: SystematicVariation.h:47
PMGTruthWeightTool.h
xAOD::mcChannelNumber
mcChannelNumber
Definition: EventInfo_v1.cxx:197
PMGTools::PMGTruthWeightTool::clearWeightLocationCaches
void clearWeightLocationCaches()
Clear caches.
Definition: PMGTruthWeightTool.cxx:271
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
xAOD::FileMetaData_v1::value
bool value(MetaDataType type, std::string &val) const
Get a pre-defined string value out of the object.
Definition: FileMetaData_v1.cxx:195
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
ANA_CHECK_THROW
#define ANA_CHECK_THROW(EXP)
check whether the given expression was successful, throwing an exception on failure
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:339
PMGTools::PMGTruthWeightTool::beginInputFile
virtual StatusCode beginInputFile() override
Function called when a new input file is opened.
Definition: PMGTruthWeightTool.cxx:183
PMGTools::PMGTruthWeightTool::getMCChannelNumber
StatusCode getMCChannelNumber(uint32_t &mcChannelNumber)
Helper function for retrieving MC channel number from file metadata.
Definition: PMGTruthWeightTool.cxx:166
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SystematicRegistry.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
PMGTools::PMGTruthWeightTool::m_useChannelZeroInMetaData
bool m_useChannelZeroInMetaData
Flag to indicate whether the xAOD::TruthMetaData objects have incorrect McChannelNumber.
Definition: PMGTruthWeightTool.h:107
PMGTools
Tool providing sample cross-sections and k-factors etc.
Definition: AnalysisCommon/PMGTools/PMGTools/IPMGSherpaVjetsSysTool.h:16
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
PMGTools::PMGTruthWeightTool::getSysWeight
virtual float getSysWeight(const xAOD::EventInfo *evtInfo, const CP::SystematicSet &sys) const override
Implements interface from IPMGTruthWeightTool.
Definition: PMGTruthWeightTool.cxx:138
PMGTools::PMGTruthWeightTool::recommendedSystematics
virtual CP::SystematicSet recommendedSystematics() const override
Implements interface from ISystematicsTool.
Definition: PMGTruthWeightTool.cxx:160
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
PMGTools::PMGTruthWeightTool::getSysWeightIndex
virtual size_t getSysWeightIndex(const CP::SystematicSet &sys) const override
Implements interface from IPMGTruthWeightTool.
Definition: PMGTruthWeightTool.cxx:146
xAOD::FileMetaData_v1::mcProcID
@ mcProcID
Same as mc_channel_number [float].
Definition: FileMetaData_v1.h:74
PMGTools::weightNameWithPrefix
std::string weightNameWithPrefix(const std::string &name)
cleanup the weight name and add prefix
Definition: WeightHelpers.cxx:72
xAOD::FileMetaData_v1
Class holding file-level metadata about an xAOD file.
Definition: FileMetaData_v1.h:34
PMGTools::PMGTruthWeightTool::m_metaName
std::string m_metaName
Stores the meta data record name.
Definition: PMGTruthWeightTool.h:98
PMGTools::PMGTruthWeightTool::loadPOOLMetaData
StatusCode loadPOOLMetaData()
Loads weight information from POOL using HepMCWeightNames.
Definition: PMGTruthWeightTool.cxx:229
ANA_MSG_VERBOSE
#define ANA_MSG_VERBOSE(xmsg)
Macro printing verbose messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:286
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
PMGTools::PMGTruthWeightTool::m_metaDataContainer
const xAOD::TruthMetaDataContainer * m_metaDataContainer
Ptr to the meta data container for this file.
Definition: PMGTruthWeightTool.h:104
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
AthAnalysisHelper.h
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
PMGTools::PMGTruthWeightTool::m_mcChannelNumber
uint32_t m_mcChannelNumber
Current MC channel number.
Definition: PMGTruthWeightTool.h:101
FileMetaData.h
PMGTools::PMGTruthWeightTool::m_weightNames
std::vector< std::string > m_weightNames
Available weight names for this file.
Definition: PMGTruthWeightTool.h:110
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CP::SystematicRegistry
This module implements the central registry for handling systematic uncertainties with CP tools.
Definition: SystematicRegistry.h:25
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
PMGTools::PMGTruthWeightTool::validateWeightLocationCaches
StatusCode validateWeightLocationCaches()
Validate weight caches.
Definition: PMGTruthWeightTool.cxx:259
xAOD::EventInfo_v1::mcEventWeight
float mcEventWeight(size_t i=0) const
The weight of one specific MC event used in the simulation.
Definition: EventInfo_v1.cxx:203
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
CP::SystematicsCache::affectingSystematics
const CP::SystematicSet & affectingSystematics() const noexcept
the list of affecting systematics
Definition: SystematicsCache.h:170
PMGTools::PMGTruthWeightTool::getWeightNames
virtual const std::vector< std::string > & getWeightNames() const override
Implements interface from IPMGTruthWeightTool.
Definition: PMGTruthWeightTool.cxx:104
CP::SystematicsCache::add
StatusCode add(const CP::SystematicSet &sys, CalibData value)
add an individual systematic variation
Definition: SystematicsCache.h:255
CP::SystematicsCache::get
StatusCode get(const CP::SystematicSet &sys, const CalibData *&result) const
get the pointer to the cached calibration data for the given systematic, calculating it if necessary
Definition: SystematicsCache.h:189
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
PMGTools::PMGTruthWeightTool::affectingSystematics
virtual CP::SystematicSet affectingSystematics() const override
Implements interface from ISystematicsTool.
Definition: PMGTruthWeightTool.cxx:154
PMGTools::PMGTruthWeightTool::getWeight
virtual float getWeight(const xAOD::EventInfo *evtInfo, const std::string &weightName) const override
Implements interface from IPMGTruthWeightTool.
Definition: PMGTruthWeightTool.cxx:109
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
CP::SystematicRegistry::getInstance
static SystematicRegistry & getInstance()
Get the singleton instance of the registry for the curren thread.
Definition: SystematicRegistry.cxx:25
AthAnalysisHelper::retrieveMetadata
static std::string retrieveMetadata(const std::string &folder, const std::string &key, const ServiceHandle< StoreGateSvc > &inputMetaStore)
method that always returns as a string you can use from, e.g, pyROOT with evt = ROOT....
Definition: AthAnalysisHelper.h:254