7#include <GaudiKernel/ConcurrencyFlags.h>
10#include <boost/core/demangle.hpp>
30 : base_class(name, svc),
50 ATH_MSG_ERROR(
"Failed to create background event selector context");
51 return StatusCode::FAILURE;
60 if (!addressProvider) {
62 "Could not cast background event selector to IAddressProvider");
67 SmartIF<IAddressProvider> athPoolAP{
68 serviceLocator()->service(std::format(
"AthenaPoolAddressProviderSvc/BkgAPAPSvc_{}", name()))
72 "Could not cast AthenaPoolAddressProviderSvc to IAddressProvider");
77 SmartIF<IAddressProvider> addRemapAP{
78 serviceLocator()->service(std::format(
"AddressRemappingSvc/BkgARSvc_{}", name()))
81 ATH_MSG_WARNING(
"Could not cast AddressRemappingSvc to IAddressProvider");
86 const int n_stores = 50;
90 sgs.reserve(n_stores);
91 for (
int j = 0; j < n_stores; ++j) {
93 auto& sg = sgs.emplace_back(
94 std::format(
"StoreGateSvc/StoreGate_{}_{}_{}", name(), i, j), name());
106 auto skipEvent_callback = [
this](
109 using namespace std::chrono_literals;
112 ATH_MSG_INFO(
"Skipping " << end - begin <<
" HS events. ");
113 for (
auto iter = begin; iter < end; ++iter) {
114 const auto& evt = *iter;
117 eid.set_run_number(evt.runNum);
118 eid.set_lumi_block(evt.lbNum);
119 eid.set_event_number(evt.evtNum);
122 ATH_MSG_DEBUG(
"Skipping HS_ID " << evt.evtIdx <<
" --> skipping "
123 << n_to_skip <<
" pileup events");
124 for (std::size_t i = 0; i < n_to_skip; ++i) {
127 return StatusCode::FAILURE;
133 return StatusCode::SUCCESS;
137 return StatusCode::SUCCESS;
141 const EventContext& ctx) {
143 <<
", lumi " << ctx.eventID().lumi_block()
144 <<
", event " << ctx.eventID().event_number()
145 <<
"| hs_id " << hs_id);
149 num_mb_by_bunch.clear();
150 num_mb_by_bunch.resize(n_bunches);
154 bool sf_updated_throwaway;
155 const float beam_lumi_sf =
157 ctx.eventID().lumi_block(),
158 sf_updated_throwaway)
160 const float beam_lumi = beam_lumi_sf *
m_nPerBunch.value();
161 std::vector<float> avg_num_mb_by_bunch(n_bunches, beam_lumi);
170 avg_num_mb_by_bunch[idx] *=
m_beamInt->normFactor(bunch);
175 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
176 num_mb_by_bunch.begin(), [&prng](
float avg) {
177 return std::poisson_distribution<std::uint64_t>(avg)(prng);
180 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
181 num_mb_by_bunch.begin(), [](
float f) {
182 return static_cast<std::uint64_t>(std::round(f));
186 std::uint64_t num_mb = std::accumulate(num_mb_by_bunch.begin(), num_mb_by_bunch.end(), std::uint64_t{0});
192 std::vector<std::uint64_t>& index_array = *
m_idx_lists.get(ctx);
194 index_array.resize(num_mb);
195 std::iota(index_array.begin(), index_array.end(), 0);
207 using namespace std::chrono_literals;
208 std::chrono::steady_clock::time_point order_wait_start{};
210 const std::int64_t hs_id =
get_hs_id(ctx);
211 const std::size_t slot = ctx.slot();
215 if (stores.size() < num_to_load) {
216 ATH_MSG_INFO(
"Adding " << num_to_load - stores.size() <<
" stores");
217 stores.reserve(num_to_load);
218 for (std::size_t i = stores.size(); i < num_to_load; ++i) {
219 auto& sg = stores.emplace_back(
220 std::format(
"StoreGateSvc/StoreGate_{}_{}_{}", name(), slot, i),
229 ATH_MSG_INFO(
"Waiting to prevent out-of-order loading. Last loaded is "
231 order_wait_start = std::chrono::steady_clock::now();
233 std::this_thread::sleep_for(50ms);
235 auto wait_time = std::chrono::steady_clock::now() - order_wait_start;
236 ATH_MSG_INFO(std::format(
"Waited {:%M:%S} to prevent out-of-order loading",
241 auto start = std::chrono::steady_clock::now();
244 for (std::size_t i = 0; i < num_to_load; ++i) {
245 auto& sg = stores[i];
248 SG::CurrentEventStore::Push reader_sg_ces(sg.get());
253 return StatusCode::FAILURE;
255 IOpaqueAddress* addr =
nullptr;
259 return StatusCode::FAILURE;
261 if (addr ==
nullptr) {
263 return StatusCode::FAILURE;
269 for (
const auto* proxy_ptr : sg->proxies()) {
271 sg->proxy_exact(proxy_ptr->sgkey())->accessData();
277 ATH_MSG_INFO(std::format(
"Took {:%M:%S} to load events",
278 std::chrono::steady_clock::now() - start));
281 return StatusCode::SUCCESS;
285 std::uint64_t mb_id) {
293 throw std::logic_error(std::format(
294 "Tried to request bunch {} which is outside the range [{}, {}]", bunch,
305 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
uint32_t CLID
The Class ID type.
std::string CLIDToString(const CLID &clid)
static const std::type_info * CLIDToTypeinfo(CLID clid)
Translate between CLID and type_info.
std::vector< EvtId >::const_iterator EvtIter
ServiceHandle< StoreGateSvc > SGHandle
~OnDemandMinbiasSvc() final
Destructor.
virtual std::int64_t get_hs_id(const EventContext &ctx) const override
StatusCode initialize() final
AthService initialize.
IEvtSelector::Context * m_bkg_evt_sel_ctx
Gaudi::Property< int > m_latestDeltaBC
StatusCode beginHardScatter(const EventContext &ctx) override
StoreGateSvc * getMinbias(const EventContext &ctx, std::uint64_t mb_id) override
Gaudi::Property< bool > m_useBeamLumi
ServiceHandle< ActiveStoreSvc > m_activeStoreSvc
ServiceHandle< IEvtSelector > m_bkgEventSelector
std::mutex m_reading_batch_mtx
std::size_t getNumForBunch(const EventContext &ctx, int bunch) const override
SG::SlotSpecificObj< std::vector< std::uint64_t >, SG::InvalidSlot::Enabled > m_num_mb_by_bunch
SG::SlotSpecificObj< std::vector< std::uint64_t > > m_idx_lists
Gaudi::Property< bool > m_useBeamInt
SG::SlotSpecificObj< std::vector< SGHandle > > m_stores
OnDemandMinbiasSvc(const std::string &name, ISvcLocator *svc)
Constructor.
Gaudi::Property< bool > m_usePoisson
Gaudi::Property< std::uint64_t > m_seed
std::atomic_int64_t m_last_loaded_hs
ServiceHandle< IProxyProviderSvc > m_proxyProviderSvc
ServiceHandle< IBeamIntensity > m_beamInt
StatusCode endHardScatter(const EventContext &ctx) override
ServiceHandle< ISkipEventIdxSvc > m_skipEventIdxSvc
Gaudi::Property< float > m_nPerBunch
std::size_t calcMBRequired(std::int64_t hs_id, const EventContext &ctx)
Gaudi::Property< bool > m_onDemandMB
ServiceHandle< IBeamLuminosity > m_beamLumi
Gaudi::Property< int > m_earliestDeltaBC
The Athena Transient Store API.