7#include <GaudiKernel/ConcurrencyFlags.h>
10#include <boost/core/demangle.hpp>
14#include <range/v3/algorithm.hpp>
15#include <range/v3/numeric/accumulate.hpp>
16#include <range/v3/to_container.hpp>
17#include <range/v3/view.hpp>
33 : base_class(name, svc),
53 ATH_MSG_ERROR(
"Failed to create background event selector context");
54 return StatusCode::FAILURE;
63 if (!addressProvider) {
65 "Could not cast background event selector to IAddressProvider");
70 SmartIF<IAddressProvider> athPoolAP{
71 serviceLocator()->service(std::format(
"AthenaPoolAddressProviderSvc/BkgAPAPSvc_{}", name()))
75 "Could not cast AthenaPoolAddressProviderSvc to IAddressProvider");
80 SmartIF<IAddressProvider> addRemapAP{
81 serviceLocator()->service(std::format(
"AddressRemappingSvc/BkgARSvc_{}", name()))
84 ATH_MSG_WARNING(
"Could not cast AddressRemappingSvc to IAddressProvider");
89 const int n_stores = 50;
93 sgs.reserve(n_stores);
94 for (
int j = 0; j < n_stores; ++j) {
96 auto& sg = sgs.emplace_back(
97 std::format(
"StoreGateSvc/StoreGate_{}_{}_{}", name(), i, j), name());
109 auto skipEvent_callback = [
this](
112 using namespace std::chrono_literals;
115 ATH_MSG_INFO(
"Skipping " << end - begin <<
" HS events. ");
116 for (
auto iter = begin; iter < end; ++iter) {
117 const auto& evt = *iter;
120 eid.set_run_number(evt.runNum);
121 eid.set_lumi_block(evt.lbNum);
122 eid.set_event_number(evt.evtNum);
125 ATH_MSG_DEBUG(
"Skipping HS_ID " << evt.evtIdx <<
" --> skipping "
126 << n_to_skip <<
" pileup events");
127 for (std::size_t i = 0; i < n_to_skip; ++i) {
130 return StatusCode::FAILURE;
136 return StatusCode::SUCCESS;
140 return StatusCode::SUCCESS;
144 const EventContext& ctx) {
146 <<
", lumi " << ctx.eventID().lumi_block()
147 <<
", event " << ctx.eventID().event_number()
148 <<
"| hs_id " << hs_id);
152 num_mb_by_bunch.clear();
153 num_mb_by_bunch.resize(n_bunches);
157 bool sf_updated_throwaway;
158 const float beam_lumi_sf =
160 ctx.eventID().lumi_block(),
161 sf_updated_throwaway)
163 const float beam_lumi = beam_lumi_sf *
m_nPerBunch.value();
164 std::vector<float> avg_num_mb_by_bunch(n_bunches, beam_lumi);
173 avg_num_mb_by_bunch[idx] *=
m_beamInt->normFactor(bunch);
178 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
179 num_mb_by_bunch.begin(), [&prng](
float avg) {
180 return std::poisson_distribution<std::uint64_t>(avg)(prng);
183 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
184 num_mb_by_bunch.begin(), [](
float f) {
185 return static_cast<std::uint64_t>(std::round(f));
189 std::uint64_t num_mb = ranges::accumulate(num_mb_by_bunch, 0UL);
195 std::vector<std::uint64_t>& index_array = *
m_idx_lists.get(ctx);
197 index_array.resize(num_mb);
198 std::iota(index_array.begin(), index_array.end(), 0);
210 using namespace std::chrono_literals;
211 std::chrono::steady_clock::time_point order_wait_start{};
213 const std::int64_t hs_id =
get_hs_id(ctx);
214 const std::size_t slot = ctx.slot();
218 if (stores.size() < num_to_load) {
219 ATH_MSG_INFO(
"Adding " << num_to_load - stores.size() <<
" stores");
220 stores.reserve(num_to_load);
221 for (std::size_t i = stores.size(); i < num_to_load; ++i) {
222 auto& sg = stores.emplace_back(
223 std::format(
"StoreGateSvc/StoreGate_{}_{}_{}", name(), slot, i),
232 ATH_MSG_INFO(
"Waiting to prevent out-of-order loading. Last loaded is "
234 order_wait_start = std::chrono::steady_clock::now();
236 std::this_thread::sleep_for(50ms);
238 auto wait_time = std::chrono::steady_clock::now() - order_wait_start;
239 ATH_MSG_INFO(std::format(
"Waited {:%M:%S} to prevent out-of-order loading",
244 auto start = std::chrono::steady_clock::now();
247 for (std::size_t i = 0; i < num_to_load; ++i) {
248 auto& sg = stores[i];
251 SG::CurrentEventStore::Push reader_sg_ces(sg.get());
256 return StatusCode::FAILURE;
258 IOpaqueAddress* addr =
nullptr;
262 return StatusCode::FAILURE;
264 if (addr ==
nullptr) {
266 return StatusCode::FAILURE;
272 for (
const auto* proxy_ptr : sg->proxies()) {
274 sg->proxy_exact(proxy_ptr->sgkey())->accessData();
280 ATH_MSG_INFO(std::format(
"Took {:%M:%S} to load events",
281 std::chrono::steady_clock::now() - start));
284 return StatusCode::SUCCESS;
288 std::uint64_t mb_id) {
296 throw std::logic_error(std::format(
297 "Tried to request bunch {} which is outside the range [{}, {}]", bunch,
308 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.