ATLAS Offline Software
Loading...
Searching...
No Matches
BatchedMinbiasSvc.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#include "BatchedMinbiasSvc.h"
6
7#include <GaudiKernel/ConcurrencyFlags.h>
8
9#include <algorithm>
10#include <boost/core/demangle.hpp>
11#include <chrono>
12#include <cmath>
13#include <format>
14#include <random>
15#include <range/v3/algorithm/stable_sort.hpp>
16#include <range/v3/numeric/accumulate.hpp>
17#include <range/v3/to_container.hpp>
18#include <range/v3/view.hpp>
19#include <thread>
20
25
26namespace rv = ranges::views;
27
28inline std::string CLIDToString(const CLID& clid) {
29 return boost::core::demangle(CLIDRegistry::CLIDToTypeinfo(clid)->name());
30}
31
32BatchedMinbiasSvc::BatchedMinbiasSvc(const std::string& name, ISvcLocator* svc)
33 : base_class(name, svc),
34 m_bkg_evt_sel_ctx(nullptr),
36
38
39int BatchedMinbiasSvc::event_to_batch(std::int64_t hs_id) {
40 return int(hs_id / m_HSBatchSize.value());
41}
42
44 ATH_CHECK(m_skipEventIdxSvc.retrieve());
45 ATH_CHECK(m_beamInt.retrieve());
46 ATH_CHECK(m_beamLumi.retrieve());
47 std::size_t n_concurrent =
48 Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents();
49 m_idx_lists.clear();
50 m_idx_lists.resize(n_concurrent);
51
52 m_num_mb_by_bunch.clear();
53 m_num_mb_by_bunch.resize(n_concurrent);
54
55 m_cache.clear();
56 m_empty_caches.clear();
57 m_batch_use_count.clear();
58 m_batch_use_count.reserve(m_actualNHSEventsPerBatch.value().size());
59 for (std::size_t i = 0; i < m_actualNHSEventsPerBatch.value().size(); ++i) {
60 m_batch_use_count.emplace_back(std::make_unique<std::atomic_int>(0));
61 }
62 ATH_CHECK(m_bkgEventSelector.retrieve());
63 ATH_CHECK(m_activeStoreSvc.retrieve());
64 // Setup context
65 if (!m_bkgEventSelector->createContext(m_bkg_evt_sel_ctx).isSuccess()) {
66 ATH_MSG_ERROR("Failed to create background event selector context");
67 return StatusCode::FAILURE;
68 }
69 ATH_CHECK(SmartIF<IService>(m_bkgEventSelector.get())->start());
70
71 // Setup proxy provider
72 SmartIF<IProxyProviderSvc> proxyProviderSvc{
73 serviceLocator()->service(std::format("ProxyProviderSvc/BkgPPSvc_{}", name()))
74 };
75 ATH_CHECK(proxyProviderSvc.isValid());
76
77 // Setup Address Providers
78 SmartIF<IAddressProvider> addressProvider{m_bkgEventSelector.get()};
79 if (!addressProvider) {
81 "Could not cast background event selector to IAddressProvider");
82 } else {
83 proxyProviderSvc->addProvider(addressProvider);
84 }
85 // AthenaPoolAddressProviderSvc
86 SmartIF<IAddressProvider> athPoolAP{
87 serviceLocator()->service(std::format("AthenaPoolAddressProviderSvc/BkgAPAPSvc_{}", name()))
88 };
89 if (!athPoolAP) {
91 "Could not cast AthenaPoolAddressProviderSvc to IAddressProvider");
92 } else {
93 proxyProviderSvc->addProvider(athPoolAP);
94 }
95 // AddressRemappingSvc
96 SmartIF<IAddressProvider> addRemapAP{
97 serviceLocator()->service(std::format("AddressRemappingSvc/BkgARSvc_{}", name()))
98 };
99 if (!addRemapAP) {
100 ATH_MSG_WARNING("Could not cast AddressRemappingSvc to IAddressProvider");
101 } else {
102 proxyProviderSvc->addProvider(addRemapAP);
103 }
104
105 int mbBatchSize = m_MBBatchSize.value();
106 // setup NSimultaneousBatches vectors of MBBatchSize StoreGates in
107 // m_empty_caches
108 for (int i = 0; i < m_NSimultaneousBatches.value(); ++i) {
109 auto& sgs = m_empty_caches.emplace_back(std::make_unique<SGHandleArray>());
110 sgs->reserve(mbBatchSize);
111 for (int j = 0; j < mbBatchSize; ++j) {
112 // creates / retrieves a different StoreGateSvc for each slot
113 auto& sg = sgs->emplace_back(
114 std::format("StoreGateSvc/StoreGate_{}_{}_{}", name(), i, j), name());
115 ATH_CHECK(sg.retrieve());
116 sg->setStoreID(StoreID::PILEUP_STORE);
117 sg->setProxyProviderSvc(proxyProviderSvc);
118 }
119 }
120
121 // Setup the spare store for event skipping
122 ATH_CHECK(m_spare_store.retrieve());
124 m_spare_store->setProxyProviderSvc(proxyProviderSvc);
125
126 // Setup the callback for event skipping
127 auto skipEvent_callback = [this, mbBatchSize](
129 ISkipEventIdxSvc::EvtIter end) -> StatusCode {
130 using namespace std::chrono_literals;
131 auto evts = ranges::make_subrange(begin, end);
132 ATH_MSG_INFO("Skipping " << end - begin << " HS events.");
133 auto batches_all =
134 evts | rv::transform([this](const ISkipEventIdxSvc::EvtId& evt) {
135 return event_to_batch(evt.evtIdx);
136 });
137 std::vector<std::tuple<int, int>> batches_with_counts{};
138 // Produce a list of batches, and how many times they appear
139 for (int batch : batches_all) {
140 // First entry
141 if (batches_with_counts.empty()) {
142 batches_with_counts.emplace_back(batch, 1);
143 continue;
144 }
145 // Subsequent entries
146 auto& last_entry = batches_with_counts.back();
147 if (batch == std::get<0>(last_entry)) {
148 std::get<1>(last_entry) += 1;
149 continue;
150 }
151 batches_with_counts.emplace_back(batch, 1);
152 }
153
154 // Discard batches
155 const int hs_batch_size = m_HSBatchSize.value();
156 auto* const old_store = m_activeStoreSvc->activeStore();
157 m_activeStoreSvc->setStore(m_spare_store.get());
158 ATH_CHECK(m_spare_store->clearStore());
159 for (const auto& [batch, count] : batches_with_counts) {
160 if (m_cache.count(batch) != 0) {
161 // batch is currently loaded, just update the use count
162 m_batch_use_count[batch]->fetch_add(count);
163 continue;
164 }
165 // force ordering in background stream
166 while (m_last_loaded_batch < batch - 1) {
167 std::this_thread::sleep_for(50ms);
168 }
169 // if we aren't skipping all the hardscatters in the batch, do nothing
170 if ((m_batch_use_count[batch]->fetch_add(count) + count) <
171 hs_batch_size) {
172 continue;
173 }
174 // otherwise discard the batch
175 ATH_MSG_INFO("Discarding batch " << batch);
176 std::unique_lock lck{m_reading_batch_mtx};
177 if (!m_bkgEventSelector->next(*m_bkg_evt_sel_ctx, mbBatchSize)
178 .isSuccess()) {
179 ATH_MSG_INFO("Ran out of background events");
180 return StatusCode::FAILURE;
181 }
182 // increment counters
183 m_last_loaded_batch.fetch_add(1);
184 }
185 ATH_CHECK(m_spare_store->clearStore());
186 m_activeStoreSvc->setStore(old_store);
187 return StatusCode::SUCCESS;
188 };
189
190 // register callback
191 ATH_CHECK(m_skipEventIdxSvc->registerCallback(skipEvent_callback));
192 return StatusCode::SUCCESS;
193}
194
195std::size_t BatchedMinbiasSvc::calcMBRequired(std::int64_t hs_id,
196 std::size_t slot,
197 unsigned int run,
198 unsigned int lumi,
199 std::uint64_t event) {
200 const int n_bunches = m_latestDeltaBC.value() - m_earliestDeltaBC.value() + 1;
201 FastReseededPRNG prng{m_seed.value(), hs_id};
202
203 // First apply the beam luminosity SF
204 bool sf_updated_throwaway;
205 const float beam_lumi_sf =
206 m_useBeamLumi ? m_beamLumi->scaleFactor(run, lumi, sf_updated_throwaway)
207 : 1.f;
208 std::vector<float> avg_num_mb_by_bunch(n_bunches,
209 beam_lumi_sf * m_nPerBunch.value());
210 // Now update using beam intensities
211 if (m_useBeamInt) {
212 // Supposed to be once per event, but ends up running once per minbias type
213 // per event now
214 m_beamInt->selectT0(run, event);
215 for (int bunch = m_earliestDeltaBC.value();
216 bunch <= m_latestDeltaBC.value(); ++bunch) {
217 std::size_t idx = bunch - m_earliestDeltaBC.value();
218 avg_num_mb_by_bunch[idx] *= m_beamInt->normFactor(bunch);
219 }
220 }
221
222 std::vector<std::uint64_t>& num_mb_by_bunch = m_num_mb_by_bunch[slot];
223 num_mb_by_bunch.clear();
224 num_mb_by_bunch.resize(n_bunches);
225
226 if (m_usePoisson) {
227 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
228 num_mb_by_bunch.begin(), [&prng](float avg) {
229 return std::poisson_distribution<std::uint64_t>(avg)(prng);
230 });
231 } else {
232 std::transform(avg_num_mb_by_bunch.begin(), avg_num_mb_by_bunch.end(),
233 num_mb_by_bunch.begin(), [](float f) {
234 return static_cast<std::uint64_t>(std::round(f));
235 });
236 }
237
238 std::uint64_t num_mb = ranges::accumulate(num_mb_by_bunch, 0UL);
239 std::vector<std::uint64_t>& index_array = m_idx_lists[slot];
240 const std::uint64_t mbBatchSize = m_MBBatchSize.value();
241 // Prevent running out of events
242 if (num_mb > mbBatchSize) {
243 const int center_bunch = -m_earliestDeltaBC.value();
244 auto indices =
245 rv::iota(0ULL, num_mb_by_bunch.size()) |
246 rv::filter([center_bunch, &num_mb_by_bunch](int idx) {
247 bool good = idx != center_bunch; // filter out the central bunch
248 good =
249 good && num_mb_by_bunch[idx] > 0; // filter out unfilled bunches
250 return good;
251 }) |
252 ranges::to<std::vector>;
253 // sort by distance from central bunch
254 ranges::stable_sort(indices, std::greater{},
255 [center_bunch](std::size_t idx) {
256 return std::size_t(std::abs(int(idx) - center_bunch));
257 });
258 // subtract from bunches until we aren't using too many events
259 for (auto idx : indices) {
260 const std::uint64_t max_to_subtract = num_mb - mbBatchSize;
261 const std::uint64_t num_subtracted =
262 std::min(max_to_subtract, num_mb_by_bunch[idx]);
263 num_mb_by_bunch[idx] -= num_subtracted;
264 num_mb -= num_subtracted;
265 if (num_mb <= mbBatchSize) {
266 break;
267 }
268 }
269 // Print an error anyway so we can fix the job
270 ATH_MSG_ERROR("We need " << num_mb << " events but the batch size is "
271 << mbBatchSize << ". Restricting to "
272 << mbBatchSize << " events!");
273 }
274 index_array = rv::ints(0, int(mbBatchSize)) | rv::sample(num_mb, prng) |
275 ranges::to<std::vector<std::uint64_t>>;
276 ranges::shuffle(index_array, prng);
277 ATH_MSG_DEBUG("HS ID " << hs_id << " uses " << num_mb << " events");
278 // Disabled until C++ 23 range formatting can be used
279 // if (m_HSBatchSize <= 1) {
280 // ATH_MSG_DEBUG(fmt::format("\t\tBy bunch: [{}]\n", fmt::join(num_mb_by_bunch, ", "))
281 // << fmt::format("\t\tOrder: [{}]", fmt::join(index_array, ", ")));
282 // }
283 return num_mb;
284}
285
286StatusCode BatchedMinbiasSvc::beginHardScatter(const EventContext& ctx) {
287 using namespace std::chrono_literals;
288 bool first_wait = true;
289 std::chrono::steady_clock::time_point cache_wait_start{};
290 std::chrono::steady_clock::time_point order_wait_start{};
291 const std::int64_t hs_id = get_hs_id(ctx);
292 const int batch = event_to_batch(hs_id);
293 calcMBRequired(hs_id, ctx.slot(),
294 ctx.eventID().run_number(), // don't need the total, only
295 ctx.eventID().lumi_block(), // need to populate the arrays
296 ctx.eventID().event_number());
297 while (true) {
298 if (m_cache.count(batch) != 0) {
299 // batch already loaded
300 // mutex prevents returning when batch is partially loaded
301 m_cache_mtxs[batch].lock();
302 m_cache_mtxs[batch].unlock();
303 return StatusCode::SUCCESS;
304 }
305 // prevent batches loading out-of-order
306 if (m_last_loaded_batch < (batch - 1)) {
307 ATH_MSG_INFO("Waiting to prevent out-of-order loading of batches");
308 order_wait_start = std::chrono::steady_clock::now();
309 while (m_last_loaded_batch < (batch - 1)) {
310 std::this_thread::sleep_for(50ms);
311 }
312 auto wait_time = std::chrono::steady_clock::now() - order_wait_start;
313 ATH_MSG_INFO(std::format(
314 "Waited {:%M:%S} to prevent out-of-order loading", wait_time));
315 }
316 // See if there are any free caches
317 // Using try_lock here to avoid reading same batch twice
318 std::unique_lock<std::mutex> empty_caches_lock (m_empty_caches_mtx,
319 std::try_to_lock);
320 if (empty_caches_lock.owns_lock()) {
321 if (m_empty_caches.empty()) {
322 // Unlock mutex if we got the lock but there were no free caches
323 empty_caches_lock.unlock();
324 if (first_wait) {
325 ATH_MSG_INFO("Waiting for a free cache");
326 first_wait = false;
327 cache_wait_start = std::chrono::steady_clock::now();
328 }
329 // Wait 100ms then try again
330 std::this_thread::sleep_for(100ms);
331 continue;
332 }
333 if (!first_wait) {
334 auto wait_time = std::chrono::steady_clock::now() - cache_wait_start;
336 std::format("Waited {:%M:%S} for a free cache", wait_time));
337 }
338 std::scoped_lock reading{m_cache_mtxs[batch], m_reading_batch_mtx};
339 if (m_HSBatchSize != 0) {
340 ATH_MSG_INFO("Reading next batch in event " << ctx.evt() << ", slot "
341 << ctx.slot() << " (hs_id "
342 << hs_id << ")");
343 }
344 auto start_time = std::chrono::system_clock::now();
345 m_cache[batch] = std::move(m_empty_caches.front());
346 m_empty_caches.pop_front();
347 // Remember old store to reset later
348 auto* old_store = m_activeStoreSvc->activeStore();
349 for (auto&& sg : *m_cache[batch]) {
350 // Change active store
351 m_activeStoreSvc->setStore(sg.get());
352 SG::CurrentEventStore::Push reader_sg_ces(sg.get());
353 // Read next event
354 ATH_CHECK(sg->clearStore(true));
355 if (!(m_bkgEventSelector->next(*m_bkg_evt_sel_ctx)).isSuccess()) {
356 ATH_MSG_FATAL("Ran out of minbias events");
357 return StatusCode::FAILURE;
358 }
359 IOpaqueAddress* addr = nullptr;
360 if (!m_bkgEventSelector->createAddress(*m_bkg_evt_sel_ctx, addr)
361 .isSuccess()) {
362 ATH_MSG_WARNING("Failed to create address. No more events?");
363 return StatusCode::FAILURE;
364 }
365 if (addr == nullptr) {
366 ATH_MSG_WARNING("createAddress returned nullptr. No more events?");
367 return StatusCode::FAILURE;
368 }
369 ATH_CHECK(sg->recordAddress(addr));
370 ATH_CHECK(sg->loadEventProxies());
371 // Read data now if desired
372 if (!m_onDemandMB) {
373 for (const auto* proxy_ptr : sg->proxies()) {
374 if (!proxy_ptr->isValid()) {
375 continue;
376 }
377
378 // Sort of a const_cast, then ->accessData()
379 sg->proxy_exact(proxy_ptr->sgkey())->accessData();
380 }
381 }
382 }
383 // Reset active store
384 m_activeStoreSvc->setStore(old_store);
385 if (m_HSBatchSize != 0) {
386 ATH_MSG_INFO(std::format(
387 "Reading {} events took {:%OMm %OSs}", m_cache[batch]->size(),
388 std::chrono::system_clock::now() - start_time));
389 }
390 m_last_loaded_batch.exchange(batch);
391 return StatusCode::SUCCESS;
392 }
393 }
394 return StatusCode::SUCCESS;
395}
396
398 std::uint64_t mb_id) {
399 const std::int64_t hs_id = get_hs_id(ctx);
400 const std::size_t slot = ctx.slot();
401 const std::size_t index = m_idx_lists.at(slot).at(mb_id);
402 const int batch = event_to_batch(hs_id);
403 return m_cache[batch]->at(index).get();
404}
405
406std::size_t BatchedMinbiasSvc::getNumForBunch(const EventContext& ctx,
407 int bunch) const {
408 if (bunch < m_earliestDeltaBC.value() || bunch > m_latestDeltaBC.value()) {
409 throw std::logic_error(std::format(
410 "Tried to request bunch {} which is outside the range [{}, {}]", bunch,
411 m_earliestDeltaBC.value(), m_latestDeltaBC.value()));
412 }
413 return m_num_mb_by_bunch.at(ctx.slot()).at(bunch - m_earliestDeltaBC.value());
414}
415
416StatusCode BatchedMinbiasSvc::endHardScatter(const EventContext& ctx) {
417 using namespace std::chrono_literals;
418 const std::int64_t hs_id = get_hs_id(ctx);
419 const int batch = event_to_batch(hs_id);
420 const int uses = m_batch_use_count[batch]->fetch_add(1) + 1;
421
422 // If we're done with every event in the batch, clear the stores and return
423 // them
424 if (uses == m_HSBatchSize.value()) {
425 std::unique_ptr temp = std::move(m_cache[batch]);
426 m_cache.erase(batch);
427 for (auto&& sg : *temp) {
428 ATH_CHECK(sg->clearStore());
429 }
430 std::lock_guard lg{m_empty_caches_mtx};
431 m_empty_caches.emplace_back(std::move(temp));
432 } else {
433 ATH_MSG_DEBUG("BATCH " << batch << ": " << uses << " uses out of "
434 << m_HSBatchSize << " "
436 }
437 return StatusCode::SUCCESS;
438}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::string CLIDToString(const CLID &clid)
uint32_t CLID
The Class ID type.
Hold a pointer to the current event store.
StatusCode initialize() override
AthService initialize.
ServiceHandle< IEvtSelector > m_bkgEventSelector
Gaudi::Property< std::uint64_t > m_seed
Gaudi::Property< float > m_nPerBunch
Gaudi::Property< bool > m_useBeamLumi
std::vector< std::vector< std::uint64_t > > m_idx_lists
std::map< int, std::unique_ptr< SGHandleArray > > m_cache
ServiceHandle< IBeamIntensity > m_beamInt
~BatchedMinbiasSvc()
Destructor.
Gaudi::Property< std::vector< int > > m_actualNHSEventsPerBatch
Gaudi::Property< bool > m_onDemandMB
int event_to_batch(std::int64_t hs_id)
StoreGateSvc * getMinbias(const EventContext &ctx, std::uint64_t mb_id) override
Gaudi::Property< int > m_earliestDeltaBC
std::vector< std::unique_ptr< std::atomic_int > > m_batch_use_count
Gaudi::Property< bool > m_usePoisson
std::vector< std::vector< std::uint64_t > > m_num_mb_by_bunch
ServiceHandle< ActiveStoreSvc > m_activeStoreSvc
Gaudi::Property< int > m_latestDeltaBC
std::deque< std::unique_ptr< SGHandleArray > > m_empty_caches
std::mutex m_reading_batch_mtx
std::size_t getNumForBunch(const EventContext &ctx, int bunch) const override
StatusCode endHardScatter(const EventContext &ctx) override
Gaudi::Property< bool > m_useBeamInt
std::mutex m_empty_caches_mtx
Gaudi::Property< int > m_MBBatchSize
BatchedMinbiasSvc(const std::string &name, ISvcLocator *svc)
Constructor.
ServiceHandle< ISkipEventIdxSvc > m_skipEventIdxSvc
IEvtSelector::Context * m_bkg_evt_sel_ctx
virtual std::int64_t get_hs_id(const EventContext &ctx) const override
Gaudi::Property< int > m_HSBatchSize
std::map< int, std::mutex > m_cache_mtxs
std::size_t calcMBRequired(std::int64_t hs_id, std::size_t slot, unsigned int run, unsigned int lumi, std::uint64_t event)
StatusCode beginHardScatter(const EventContext &ctx) override
std::atomic_int m_last_loaded_batch
ServiceHandle< IBeamLuminosity > m_beamLumi
Gaudi::Property< int > m_NSimultaneousBatches
static const std::type_info * CLIDToTypeinfo(CLID clid)
Translate between CLID and type_info.
std::vector< EvtId >::const_iterator EvtIter
Temporarily change the current store.
The Athena Transient Store API.
@ PILEUP_STORE
Definition StoreID.h:31
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
Definition index.py:1
Definition run.py:1