ATLAS Offline Software
Loading...
Searching...
No Matches
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.
virtual StatusCode setWeightNames (const WeightMap &weightNames, const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Record weight names to metadata if none have yet been set.
virtual WeightMap weightNames (const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Return the current weight names.
virtual std::vector< std::string > weightNameVec (const EventContext &ctx=Gaudi::Hive::currentContext()) override
 Return the current weight names.

Private Member Functions

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

Private Attributes

PublicToolHandle< IIOVDbMetaDataToolm_metaDataTool { this, "IOVDbMetaDataTool", "IOVDbMetaDataTool" }
 Handle to metadata tool.
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.
SG::ReadHandleKey< xAOD::EventInfom_eventInfoKey { this, "EventInfoKey", "EventInfo", "SG key for EventInfo object." }
 Used to get MC channel for the current event.
std::mutex m_mutex
 Serialize access to this service.

Static Private Attributes

static const unsigned int NWEIGHTS = 8
 Array of weights.

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}
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Used to get MC channel for the current event.

◆ 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}
static const unsigned int NWEIGHTS
Array of weights.
WeightInfo m_weights[NWEIGHTS]
unsigned long getChanNum(const EventContext &ctx) const
Return the ‘channel number’ for the current event.
unsigned int loadWeights(unsigned long chanNum)
Try to load weight names from metadata.

◆ initialize()

StatusCode HepMCWeightSvc::initialize ( )
overridevirtual

Standard Gaudi initialize.

Definition at line 18 of file HepMCWeightSvc.cxx.

19{
20 ATH_CHECK( m_eventInfoKey.initialize() );
21 return StatusCode::SUCCESS;
22}
#define ATH_CHECK
Evaluate an expression and check for errors.

◆ 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}
#define ATH_MSG_DEBUG(x)
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
ChanNum chanNum(unsigned int index) const
channel number for index: (index = 0 to size-1)
size_type size() const
number of Chan/AttributeList pairs
Gaudi::Property< bool > m_enabled
size_t m_nextWeight
Index of next set of weights to overwrite.
virtual WeightMap weightNames(const EventContext &ctx=Gaudi::Hive::currentContext()) override
Return the current weight names.
PublicToolHandle< IIOVDbMetaDataTool > m_metaDataTool
Handle to metadata tool.
const IOVPayloadContainer * payloadContainer() const
Access to payload container.
size_type size() const
size of payload vector
CondAttrListCollection * at(unsigned int i) const
Element access.
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
Cached sets of weights.

◆ 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
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}
#define ATH_MSG_INFO(x)
coral::AttributeList AttributeList
std::mutex m_mutex
Serialize access to this service.

◆ 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}
#define ATH_MSG_WARNING(x)
unsigned int getWeightIndex(const EventContext &ctx)
Return the index in m_weights for the current event.

◆ 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.

97{ this, "Enable", true, "If false, will return failure on loadWeights" };

◆ 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.

127{ this, "EventInfoKey", "EventInfo", "SG key for EventInfo object." };

◆ m_metaDataTool

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

Handle to metadata tool.

Definition at line 91 of file HepMCWeightSvc.h.

92{ this, "IOVDbMetaDataTool", "IOVDbMetaDataTool" };

◆ 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: