ATLAS Offline Software
Loading...
Searching...
No Matches
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
8
11
13 ISvcLocator* svcLoc)
14 : GenBase(name, svcLoc)
15{
16}
17
18
20{
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
39StatusCode GenWeightDeclaration::execute(const EventContext& ctx)
40{
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
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
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
This implements the methods for ICutFlowSvc.
Definition CutFlowSvc.h:47
const T * front() const
Access the first element in the collection as an rvalue.
bool empty() const noexcept
Returns true if the collection is empty.
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
const McEventCollection * events_const(const EventContext &ctx) const
Access the current event's McEventCollection (const).
Definition GenBase.h:93
virtual StatusCode initialize() override
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
ServiceHandle< IHepMCWeightSvc > m_hepMCWeightSvc
ServiceHandle< ICutFlowSvc > m_cutFlowSvc
GenWeightDeclaration(const std::string &name, ISvcLocator *svcLoc)
std::map< std::string, std::size_t > WeightMap
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
std::map< std::string, std::size_t > weights_map(const HepMC3::GenEvent *e)
Definition GenEvent.h:346
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39