ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
EvgenProdTools
src
GenWeightDeclaration.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#ifndef XAOD_ANALYSIS
6
7
#include "
EvgenProdTools/GenWeightDeclaration.h
"
8
9
#include "
EventBookkeeperTools/CutFlowSvc.h
"
10
#include "
GenInterfaces/IHepMCWeightSvc.h
"
11
12
GenWeightDeclaration::GenWeightDeclaration
(
const
std::string& name,
13
ISvcLocator* svcLoc)
14
:
GenBase
(name, svcLoc)
15
{
16
}
17
18
19
StatusCode
GenWeightDeclaration::initialize
()
20
{
21
ATH_CHECK
(
GenBase::initialize
());
22
23
// Retrieve the CutFlowSvc
24
ATH_CHECK
(
m_cutFlowSvc
.retrieve());
25
26
// Access CutFlowSvc methods
27
m_cutFlowSvcImpl
=
dynamic_cast<
CutFlowSvc
*
>
(&*
m_cutFlowSvc
);
28
if
(!
m_cutFlowSvcImpl
) {
29
ATH_MSG_ERROR
(
"Configured CutFlowSvc does not use the CutFlowSvc implementation"
);
30
return
StatusCode::FAILURE;
31
}
32
33
// Retrieve the HepMCWeightSvc
34
ATH_CHECK
(
m_hepMCWeightSvc
.retrieve());
35
return
StatusCode::SUCCESS;
36
}
37
38
39
StatusCode
GenWeightDeclaration::execute
(
const
EventContext& ctx)
40
{
41
if
(
m_weightsDeclared
) {
42
return
StatusCode::SUCCESS;
43
}
44
45
// Declare the number of generator weights to the CutFlowSvc
46
const
McEventCollection
* eventCollection =
events_const
(ctx);
47
if
(!eventCollection || eventCollection->
empty
()) {
48
ATH_MSG_ERROR
(
"Cannot declare generator weights from an empty GEN_EVENT"
);
49
return
StatusCode::FAILURE;
50
}
51
52
const
HepMC::GenEvent
*
event
= eventCollection->
front
();
53
if
(!event) {
54
ATH_MSG_ERROR
(
"Cannot declare generator weights from a null event"
);
55
return
StatusCode::FAILURE;
56
}
57
58
const
std::size_t weightCount =
event
->weights().size();
59
if
(weightCount == 0) {
60
ATH_MSG_ERROR
(
"Cannot declare an empty generator-weight vector"
);
61
return
StatusCode::FAILURE;
62
}
63
64
IHepMCWeightSvc::WeightMap
weightNames =
HepMC::weights_map
(event);
65
const
auto
runInfo
=
event
->run_info();
66
if
(
runInfo
&& !
runInfo
->weight_names().empty()) {
67
const
std::size_t rawNameCount =
runInfo
->weight_names().size();
68
if
(rawNameCount != weightCount) {
69
ATH_MSG_ERROR
(
"Generator weight names and values have different sizes: "
70
<< rawNameCount <<
" and "
<< weightCount);
71
return
StatusCode::FAILURE;
72
}
73
if
(weightNames.size() != rawNameCount) {
74
ATH_MSG_ERROR
(
"Generator weight names are not unique"
);
75
return
StatusCode::FAILURE;
76
}
77
}
78
79
// Declare the number of weights to the CutFlowSvc
80
// and the weight names to the HepMCWeightSvc
81
ATH_CHECK
(
m_cutFlowSvcImpl
->setNumberOfWeightVariations(weightCount));
82
if
(!weightNames.empty()) {
83
ATH_CHECK
(
m_hepMCWeightSvc
->setWeightNames(weightNames, ctx));
84
}
85
86
// Set the flag to avoid re-declaring weights in subsequent events
87
m_weightsDeclared
=
true
;
88
ATH_MSG_INFO
(
"Declared "
<< weightCount
89
<<
" generator weight slot"
<< (weightCount == 1 ?
""
:
"s"
)
90
<<
" from the first event"
);
91
return
StatusCode::SUCCESS;
92
}
93
94
#endif
ATH_CHECK
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition
AthCheckMacros.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
CutFlowSvc.h
GenWeightDeclaration.h
IHepMCWeightSvc.h
CutFlowSvc
This implements the methods for ICutFlowSvc.
Definition
CutFlowSvc.h:47
DataVector::front
const T * front() const
Access the first element in the collection as an rvalue.
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
GenBase::initialize
virtual StatusCode initialize() override
Definition
GenBase.cxx:17
GenBase::GenBase
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition
GenBase.cxx:11
GenBase::events_const
const McEventCollection * events_const(const EventContext &ctx) const
Access the current event's McEventCollection (const).
Definition
GenBase.h:93
GenWeightDeclaration::m_weightsDeclared
bool m_weightsDeclared
Definition
GenWeightDeclaration.h:50
GenWeightDeclaration::initialize
virtual StatusCode initialize() override
Definition
GenWeightDeclaration.cxx:19
GenWeightDeclaration::execute
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
Definition
GenWeightDeclaration.cxx:39
GenWeightDeclaration::m_hepMCWeightSvc
ServiceHandle< IHepMCWeightSvc > m_hepMCWeightSvc
Definition
GenWeightDeclaration.h:45
GenWeightDeclaration::m_cutFlowSvc
ServiceHandle< ICutFlowSvc > m_cutFlowSvc
Definition
GenWeightDeclaration.h:37
GenWeightDeclaration::m_cutFlowSvcImpl
CutFlowSvc * m_cutFlowSvcImpl
Definition
GenWeightDeclaration.h:43
GenWeightDeclaration::GenWeightDeclaration
GenWeightDeclaration(const std::string &name, ISvcLocator *svcLoc)
Definition
GenWeightDeclaration.cxx:12
IHepMCWeightSvc::WeightMap
std::map< std::string, std::size_t > WeightMap
Definition
IHepMCWeightSvc.h:22
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
Definition
McEventCollection.h:31
HepMC::weights_map
std::map< std::string, std::size_t > weights_map(const HepMC3::GenEvent *e)
Definition
GenEvent.h:346
HepMC::GenEvent
HepMC3::GenEvent GenEvent
Definition
GenEvent.h:39
runInfo
Definition
runInfo.py:1
Generated on
for ATLAS Offline Software by
1.17.0