ATLAS Offline Software
Loading...
Searching...
No Matches
CopyEventWeight.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#ifndef XAOD_ANALYSIS
6
9#include "EventInfo/EventInfo.h"
10#include "EventInfo/EventType.h"
11
12CopyEventWeight::CopyEventWeight(const std::string& name, ISvcLocator* svcLoc)
13 : GenBase(name, svcLoc)
14{
15}
16
18{
20 ATH_CHECK(m_mcWeightsKey.initialize());
21 return StatusCode::SUCCESS;
22}
23
25 // Check that the collection isn't empty
27 const size_t nEvents = events_const()->size();
28 if (nEvents == 0) {
29 ATH_MSG_WARNING("McEventCollection is empty");
30 return StatusCode::SUCCESS;
31 }
32
33 // Get the event info/type object to be filled
34 const EventInfo* pInputEvt(nullptr);
35 CHECK(evtStore()->retrieve(pInputEvt));
36 assert(pInputEvt);
37 EventType* eventType = const_cast<EventType*>(pInputEvt->event_type());
38
39 // Copy weights into EventInfo
40 const size_t nw = event_const()->weights().size();
41 if (nw == 0) ATH_MSG_WARNING("EVENT WEIGHT ARRAY EMPTY");
42 std::vector<float> weights;
43 for (size_t iw = 0; iw < nw; ++iw) {
44 ATH_MSG_DEBUG("COPYING EVENT WEIGHT " << iw << "/" << nw << ": " << event_const()->weights()[iw]);
45 eventType->set_mc_event_weight(event_const()->weights()[iw], iw, nw);
46 weights.push_back(event_const()->weights()[iw]);
47 }
48
50 mcWeights(0) = std::move(weights);
51
52 // Post-hoc debug printouts
53 ATH_MSG_DEBUG("Copied HepMC signal event weight(s) to EventInfo");
54 if (nEvents > 1) ATH_MSG_DEBUG("Ignored event weights of " << nEvents-1 << "bkg events");
55
56 return StatusCode::SUCCESS;
57}
58
59#endif
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
This class provides general information about an event.
Handle class for adding a decoration to an object.
SG::WriteDecorHandleKey< xAOD::EventInfo > m_mcWeightsKey
virtual StatusCode initialize() override
Copy the weight vector for each event.
CopyEventWeight(const std::string &name, ISvcLocator *svcLoc)
Constructor.
virtual StatusCode execute() override
size_type size() const noexcept
Returns the number of elements in the collection.
EventType * event_type()
the type of the event, e.g. simulation, testbeam, etc
This class represents the "type of event" where the type is given by one or more "characteristics".
Definition EventType.h:92
void set_mc_event_weight(float weight, unsigned int iweight=0, unsigned int nWeightsMax=0)
Add in MC weight. For more than 1 weight, add with iweight > 0.
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
Access the current event's McEventCollection (const)
Definition GenBase.h:96
const HepMC::GenEvent * event_const() const
Access the current signal event (const)
Definition GenBase.h:83
Handle class for adding a decoration to an object.
const int nEvents