ATLAS Offline Software
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
HepMCWeightSvc Class Reference

Service to read/write HepMC's WeightContainer key names from/to IOVMetaDataContainers author: will buttinger , NLAA. More...

#include <HepMCWeightSvc.h>

Inheritance diagram for HepMCWeightSvc:
Collaboration diagram for HepMCWeightSvc:

Classes

struct  WeightInfo
 Cached sets of weights. More...
 

Public Member Functions

virtual StatusCode initialize () override
 Standard Gaudi initialize. More...
 
virtual StatusCode setWeightNames (const WeightMap &weightNames, const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Record weight names to metadata if none have yet been set. More...
 
virtual WeightMap weightNames (const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Return the current weight names. More...
 
virtual std::vector< std::string > weightNameVec (const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Return the current weight names. More...
 

Private Member Functions

unsigned long getChanNum (const EventContext &ctx) const
 Return the ‘channel number’ for the current event. More...
 
unsigned int loadWeights (unsigned long chanNum)
 Try to load weight names from metadata. More...
 
unsigned int getWeightIndex (const EventContext &ctx)
 Return the index in m_weights for the current event. More...
 

Private Attributes

PublicToolHandle< IIOVDbMetaDataToolm_metaDataTool { this, "IOVDbMetaDataTool", "IOVDbMetaDataTool" }
 Handle to metadata tool. More...
 
Gaudi::Property< bool > m_enabled { this, "Enable", true, "If false, will return failure on loadWeights" }
 
WeightInfo m_weights [NWEIGHTS]
 
size_t m_nextWeight = 0
 Index of next set of weights to overwrite. More...
 
SG::ReadHandleKey< xAOD::EventInfom_eventInfoKey { this, "EventInfoKey", "EventInfo", "SG key for EventInfo object." }
 Used to get MC channel for the current event. More...
 
std::mutex m_mutex
 Serialize access to this service. More...
 

Static Private Attributes

static const unsigned int NWEIGHTS = 8
 Array of weights. More...
 

Detailed Description

Service to read/write HepMC's WeightContainer key names from/to IOVMetaDataContainers author: will buttinger , NLAA.

Definition at line 24 of file HepMCWeightSvc.h.

Member Function Documentation

◆ getChanNum()

unsigned long HepMCWeightSvc::getChanNum ( const EventContext &  ctx) const
private

Return the ‘channel number’ for the current event.

Parameters
ctxCurrent event context.

Either the MC channel number or the run number if that hasn't been set.

Definition at line 134 of file HepMCWeightSvc.cxx.

135 {
136  // Try first to get the MC channel number from EventInfo.
137  SG::ReadHandle ei (m_eventInfoKey, ctx);
138  if (ei.isValid()) {
139  unsigned long chanNum = ei->mcChannelNumber();
140  if (chanNum > 0) return chanNum;
141  }
142  // Fall back to run number.
143  return ctx.eventID().run_number();
144 }

◆ getWeightIndex()

unsigned int HepMCWeightSvc::getWeightIndex ( const EventContext &  ctx)
private

Return the index in m_weights for the current event.

Parameters
ctxThe current event context.

Returns NWEIGHTS on failure.

Definition at line 225 of file HepMCWeightSvc.cxx.

226 {
227  unsigned long chanNum = getChanNum (ctx);
228  if (chanNum != 0) {
229  for (size_t i = 0; i < NWEIGHTS; i++) {
230  if (m_weights[i].m_chanNum == chanNum) {
231  return i;
232  }
233  }
234  return loadWeights (chanNum);
235  }
236  return NWEIGHTS;
237 }

◆ initialize()

StatusCode HepMCWeightSvc::initialize ( )
overridevirtual

Standard Gaudi initialize.

Definition at line 18 of file HepMCWeightSvc.cxx.

19 {
21  return StatusCode::SUCCESS;
22 }

◆ loadWeights()

unsigned int HepMCWeightSvc::loadWeights ( unsigned long  chanNum)
private

Try to load weight names from metadata.

Parameters
chanNumChannel number for which to load weights.

Returns the index in m_weights of the loaded weights.

Definition at line 153 of file HepMCWeightSvc.cxx.

154 {
155  // Must be called holding the service mutex.
156 
157  if (!m_enabled) return NWEIGHTS;
158 
159  std::map<std::string, int> in;
160 
161  Athena::ToolLock toolLock (*m_metaDataTool);
162  const IOVMetaDataContainer* cont = m_metaDataTool->findMetaDataContainer ("/Generation/Parameters");
163 
164  // Return quietly if container isn't there.
165  if (cont) {
166  if (cont->payloadContainer()->size() == 0) return NWEIGHTS;
167 
168  unsigned long chanNumRead = chanNum;
169  // If there is only one collection of weights, then we just load that one.
170  if (cont->payloadContainer()->at(0)->size()==1) {
171  chanNumRead = cont->payloadContainer()->at(0)->chanNum(0);
172  }
173 
174  const coral::Attribute& attr = cont->payloadContainer()->at(0)->attributeList(chanNumRead)["HepMCWeightNames"];
175 
176  int version = 1;
177  try {
178  version = cont->payloadContainer()->at(0)->attributeList(chanNumRead)["HepMCWeightSvcVersion"].data<int>();
179  } catch(const coral::AttributeListException&) {
180  version = 1; //no version available so assume version 1
181  }
182 
183  std::string weightNames = attr.data<std::string>();
184 
185  //protect against malformed weightnames - see ATLMCPROD-3103
186  //this actually only came about because the Gaudi::Utils::toString method was not used
187  //in setWeightNames below. I've now corrected that, but now the fear is that these
188  //replacements will interfere with those too!
189  //so use a versioning system now
190  if(version==1) {
191  weightNames = std::regex_replace(weightNames, std::regex(R"(\{')"), "{\"");
192  weightNames = std::regex_replace(weightNames, std::regex(R"(':)"), "\":");
193  weightNames = std::regex_replace(weightNames, std::regex(R"(, ')"), ", \"");
194  }
195 
196  ATH_MSG_DEBUG("Loading weightnames: " << weightNames);
197 
198  if( Gaudi::Parsers::parse(in,weightNames).isFailure() ) return NWEIGHTS;
199  }
200 
201  size_t iweight = m_nextWeight;
202  if (++m_nextWeight >= NWEIGHTS) {
203  m_nextWeight = 0;
204  }
205 
206  WeightInfo& w = m_weights[iweight];
207  w.m_chanNum = chanNum;
208  w.m_weightNames.clear();
209 
210  for(const auto& i : in) {
211  w.m_weightNames[i.first] = i.second;
212  }
213  w.fillVec();
214 
215  return iweight;
216 }

◆ setWeightNames()

StatusCode HepMCWeightSvc::setWeightNames ( const WeightMap &  weightNames,
const EventContext &  ctx = Gaudi::Hive::currentContext() 
)
overridevirtual

Record weight names to metadata if none have yet been set.

Parameters
weightNamesMap of names to record.
ctxCurrent event context.

Does nothing if weight names have already been set or read.

Definition at line 33 of file HepMCWeightSvc.cxx.

35 {
36  std::scoped_lock lock (m_mutex);
37 
38  // Ignore any attempt to set 'nothing' for the weight names.
39  if (weightNames.size()==0 ||
40  (weightNames.size()==1 && weightNames.begin()->first=="0") )
41  {
42  return StatusCode::SUCCESS;
43  }
44 
45  if (m_weights[0].m_chanNum != 0 || m_nextWeight != 0) {
46  // We only allow setting of weightNames ONCE! ...
47  // if the weights have ever been loaded, we will not allow
48  // them to be set again!
49  // Effectively means the weights are only set in evgen jobs.
50  return StatusCode::SUCCESS;
51  }
52 
53  unsigned int chanNum = ctx.eventID().run_number();
54 
55  WeightInfo& w = m_weights[m_nextWeight++];
56  w.m_chanNum = chanNum;
57  w.m_weightNames = weightNames;
58  w.fillVec();
59 
60  // Create and register the metadata containter with these weight names
61  // Use the MetaDataTool to do this
62  ATH_CHECK( m_metaDataTool->registerFolder("/Generation/Parameters","Metadata created during Event Generation") );
63 
64  // Create a new attributelist collection for it ...
65  auto cont = std::make_unique<CondAttrListCollection> (true /* use regular timestamps, not run-lumiblock timestamps */) ;
66 
67  // Create a single attribute list.
69 
70  // Store as strings ... when read back in we use a gaudi parser to parse the list
71  myAttributes.extend("HepMCWeightNames","string");
72  myAttributes.extend("HepMCWeightSvcVersion","int");
73  myAttributes["HepMCWeightSvcVersion"].data<int>() = 2;
74 
75  std::string stringToStore = Gaudi::Utils::toString( weightNames );
76 
77  myAttributes["HepMCWeightNames"].data<std::string>() = stringToStore;
78 
79  // Use the run-number as the 'channel' ... all weightnames should be the same for the same channel
80  bool add_status = cont->add(chanNum, myAttributes);
81  if (!add_status) {
82  ATH_MSG_INFO("Failed to add AttributeList for weight " << stringToStore);
83  }
84 
85  ATH_MSG_INFO("Storing /Generation/Parameters :: WeightNames = " << stringToStore);
86 
87  // And assign it to 'channel 0' .. consistent with other metadata
88  ATH_CHECK( m_metaDataTool->addPayload("/Generation/Parameters", cont.release()) );
89 
90  return StatusCode::SUCCESS;
91 }

◆ weightNames()

HepMCWeightSvc::WeightMap HepMCWeightSvc::weightNames ( const EventContext &  ctx = Gaudi::Hive::currentContext())
overridevirtual

Return the current weight names.

Definition at line 98 of file HepMCWeightSvc.cxx.

99 {
100  std::scoped_lock lock (m_mutex);
101  size_t i = getWeightIndex (ctx);
102  if (i < NWEIGHTS) {
103  return m_weights[i].m_weightNames;
104  }
105 
106  ATH_MSG_WARNING("Unable to load weightnames from metadata ... do not trust the weightnames!");
107  return WeightMap();
108 }

◆ weightNameVec()

std::vector< std::string > HepMCWeightSvc::weightNameVec ( const EventContext &  ctx = Gaudi::Hive::currentContext())
overridevirtual

Return the current weight names.

Definition at line 115 of file HepMCWeightSvc.cxx.

116 {
117  std::scoped_lock lock (m_mutex);
118  size_t i = getWeightIndex (ctx);
119  if (i < NWEIGHTS) {
120  return m_weights[i].m_weightNameVec;
121  }
122 
123  ATH_MSG_WARNING("Unable to load weightnames from metadata ... do not trust the weightnames!");
124  return std::vector<std::string>();
125 }

Member Data Documentation

◆ m_enabled

Gaudi::Property<bool> HepMCWeightSvc::m_enabled { this, "Enable", true, "If false, will return failure on loadWeights" }
private

Definition at line 96 of file HepMCWeightSvc.h.

◆ m_eventInfoKey

SG::ReadHandleKey<xAOD::EventInfo> HepMCWeightSvc::m_eventInfoKey { this, "EventInfoKey", "EventInfo", "SG key for EventInfo object." }
private

Used to get MC channel for the current event.

Definition at line 126 of file HepMCWeightSvc.h.

◆ m_metaDataTool

PublicToolHandle<IIOVDbMetaDataTool> HepMCWeightSvc::m_metaDataTool { this, "IOVDbMetaDataTool", "IOVDbMetaDataTool" }
private

Handle to metadata tool.

Definition at line 91 of file HepMCWeightSvc.h.

◆ m_mutex

std::mutex HepMCWeightSvc::m_mutex
private

Serialize access to this service.

Definition at line 130 of file HepMCWeightSvc.h.

◆ m_nextWeight

size_t HepMCWeightSvc::m_nextWeight = 0
private

Index of next set of weights to overwrite.

Definition at line 123 of file HepMCWeightSvc.h.

◆ m_weights

WeightInfo HepMCWeightSvc::m_weights[NWEIGHTS]
private

Definition at line 120 of file HepMCWeightSvc.h.

◆ NWEIGHTS

const unsigned int HepMCWeightSvc::NWEIGHTS = 8
staticprivate

Array of weights.

Definition at line 119 of file HepMCWeightSvc.h.


The documentation for this class was generated from the following files:
IOVMetaDataContainer::payloadContainer
const IOVPayloadContainer * payloadContainer() const
Access to payload container.
Definition: IOVMetaDataContainer.h:141
IOVPayloadContainer::at
CondAttrListCollection * at(unsigned int i) const
Element access.
Definition: IOVPayloadContainer.h:128
HepMCWeightSvc::m_nextWeight
size_t m_nextWeight
Index of next set of weights to overwrite.
Definition: HepMCWeightSvc.h:123
IOVMetaDataContainer
This class is a container for conditions data. It is intended to be used to store conditions data fro...
Definition: IOVMetaDataContainer.h:37
Gaudi::Parsers::parse
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:284
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HepMCWeightSvc::NWEIGHTS
static const unsigned int NWEIGHTS
Array of weights.
Definition: HepMCWeightSvc.h:119
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
HepMCWeightSvc::m_enabled
Gaudi::Property< bool > m_enabled
Definition: HepMCWeightSvc.h:97
HepMCWeightSvc::m_metaDataTool
PublicToolHandle< IIOVDbMetaDataTool > m_metaDataTool
Handle to metadata tool.
Definition: HepMCWeightSvc.h:92
HepMCWeightSvc::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Used to get MC channel for the current event.
Definition: HepMCWeightSvc.h:127
IOVPayloadContainer::size
size_type size() const
size of payload vector
Definition: IOVPayloadContainer.h:121
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
HepMCWeightSvc::loadWeights
unsigned int loadWeights(unsigned long chanNum)
Try to load weight names from metadata.
Definition: HepMCWeightSvc.cxx:153
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
checkCoolLatestUpdate.chanNum
chanNum
Definition: checkCoolLatestUpdate.py:27
HepMCWeightSvc::m_weights
WeightInfo m_weights[NWEIGHTS]
Definition: HepMCWeightSvc.h:120
lumiFormat.i
int i
Definition: lumiFormat.py:85
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CondAttrListCollection::chanNum
ChanNum chanNum(unsigned int index) const
channel number for index: (index = 0 to size-1)
Definition: CondAttrListCollection.h:384
Athena::ToolLock
RAII helper for acquiring the lock of an ILockableTool.
Definition: ILockableTool.h:43
HepMCWeightSvc::WeightInfo::m_weightNameVec
std::vector< std::string > m_weightNameVec
Sorted vector of weight names.
Definition: HepMCWeightSvc.h:112
CondAttrListCollection::attributeList
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
Definition: CondAttrListCollection.h:401
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
HepMCWeightSvc::getWeightIndex
unsigned int getWeightIndex(const EventContext &ctx)
Return the index in m_weights for the current event.
Definition: HepMCWeightSvc.cxx:225
HepMCWeightSvc::WeightInfo::m_weightNames
WeightMap m_weightNames
Map of weight names.
Definition: HepMCWeightSvc.h:109
CondAttrListCollection::size
size_type size() const
number of Chan/AttributeList pairs
Definition: CondAttrListCollection.h:322
get_generator_info.version
version
Definition: get_generator_info.py:33
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HepMCWeightSvc::getChanNum
unsigned long getChanNum(const EventContext &ctx) const
Return the ‘channel number’ for the current event.
Definition: HepMCWeightSvc.cxx:134
HepMCWeightSvc::weightNames
virtual WeightMap weightNames(const EventContext &ctx=Gaudi::Hive::currentContext()) override
Return the current weight names.
Definition: HepMCWeightSvc.cxx:98
HepMCWeightSvc::m_mutex
std::mutex m_mutex
Serialize access to this service.
Definition: HepMCWeightSvc.h:130
CondAttrListCollection::AttributeList
coral::AttributeList AttributeList
Definition: CondAttrListCollection.h:56
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200