ATLAS Offline Software
BatchedMinbiasSvc.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 /*
3  Copyright (C) 2022, 2023 CERN for the benefit of the ATLAS collaboration
4 */
5 
6 #ifndef PILEUPMT_BATCHEDMINBIASSVC_H
7 #define PILEUPMT_BATCHEDMINBIASSVC_H
8 
9 #include <atomic>
10 #include <deque>
11 #include <format>
12 #include <map>
13 #include <memory>
14 #include <mutex>
15 #include <string>
16 #include <vector>
17 
19 #include "Gaudi/Property.h"
20 #include "GaudiKernel/IEvtSelector.h"
21 #include "GaudiKernel/ServiceHandle.h"
22 #include "ISkipEventIdxSvc.h"
23 #include "PileUpMT/IMinbiasSvc.h"
26 #include "StoreGate/StoreGateSvc.h"
27 
28 class BatchedMinbiasSvc : public extends<AthService, IMinbiasSvc> {
29  public:
31  BatchedMinbiasSvc(const std::string& name, ISvcLocator* svc);
34 
36  StatusCode initialize() override;
37 
38  StatusCode beginHardScatter(const EventContext& ctx) override;
39  StoreGateSvc* getMinbias(const EventContext& ctx,
40  std::uint64_t mb_id) override;
41  std::size_t getNumForBunch(const EventContext& ctx, int bunch) const override;
42  virtual inline
43  std::int64_t get_hs_id(const EventContext& ctx) const override {
44  return m_skippedHSEvents.value() + ctx.evt();
45  }
46  StatusCode endHardScatter(const EventContext& ctx) override;
47 
48  private:
50  using SGHandleArray = std::vector<SGHandle>;
51  Gaudi::Property<std::uint64_t> m_seed{this, "Seed", 0,
52  "Additional seed for PRNGs"};
53  Gaudi::Property<bool> m_onDemandMB{
54  this, "OnDemandMB", false,
55  "Should minbias event contents be read on demand"};
56  Gaudi::Property<bool> m_usePoisson{this, "UsePoisson", true,
57  "Whether to use a Poisson distribution "
58  "(if False, use a delta distribution)"};
59  Gaudi::Property<bool> m_useBeamInt{
60  this, "UseBeamInt", true, "Whether to use the beam intensity service"};
61  Gaudi::Property<bool> m_useBeamLumi{
62  this, "UseBeamLumi", true, "Whether to use the beam luminosity service"};
63  Gaudi::Property<int> m_MBBatchSize{
64  this, "MBBatchSize", 10000,
65  "Number of low pT minbias events to load per batch"};
66  Gaudi::Property<int> m_NSimultaneousBatches{
67  this, "NSimultaneousBatches", 1,
68  "Max number of batches to load simultaneously"};
69  Gaudi::Property<int> m_HSBatchSize{
70  this, "HSBatchSize", 1,
71  "Number of HS events per batch (aka max reuse factor)"};
72  Gaudi::Property<int> m_skippedHSEvents{this, "SkippedHSEvents", 0,
73  "Number of skipped HS events"};
74  Gaudi::Property<float> m_nPerBunch{
75  this, "AvgMBPerBunch", 0,
76  "Average (max) number of minbias events per bunch"};
77  Gaudi::Property<int> m_earliestDeltaBC{
78  this, "EarliestDeltaBC", -32,
79  "Earliest bunch crossing to consider (as delta)"};
80  Gaudi::Property<int> m_latestDeltaBC{
81  this, "LatestDeltaBC", +6,
82  "Latest bunch crossing to consider (as delta)"};
83  Gaudi::Property<std::vector<int>> m_actualNHSEventsPerBatch{
84  this,
85  "actualNHSEventsPerBatch",
86  {},
87  "Dynamic map of actual number of HS events for each batch, in this run."};
89  this, "SkipEvtIdxSvc", "SkipEventIdxSvc",
90  "Skipped event index (run / lb num) provider"};
92  this, "BkgEventSelector", {}, "Event selector for minbias events"};
93  ServiceHandle<IBeamIntensity> m_beamInt{this, "BeamIntSvc", "FlatBM",
94  "Beam intensity service"};
96  this, "BeamLumiSvc", "LumiProfileSvc", "Beam luminosity service"};
98  this, "ActiveStoreSvc", "ActiveStoreSvc", "ActiveStoreSvc"};
99 
100  SGHandle m_spare_store{this, "StoreGateSvc",
101  std::format("StoreGateSvc/discards_{}", name()),
102  "StoreGate for discarding events"};
103  IEvtSelector::Context* m_bkg_evt_sel_ctx;
104 
105  std::vector<std::vector<std::uint64_t>> m_num_mb_by_bunch;
106  std::vector<std::vector<std::uint64_t>> m_idx_lists;
107  std::map<int, std::unique_ptr<SGHandleArray>> m_cache;
108  std::map<int, std::mutex> m_cache_mtxs; // protects m_cache entries
109  // prevents attempting to read multiple batches at once
111  std::deque<std::unique_ptr<SGHandleArray>> m_empty_caches;
112  std::mutex m_empty_caches_mtx; // protects m_empty_caches
113  std::vector<std::unique_ptr<std::atomic_int>> m_batch_use_count;
114  std::atomic_int m_last_loaded_batch;
115  int event_to_batch(std::int64_t hs_id);
116  std::size_t calcMBRequired(std::int64_t hs_id, std::size_t slot,
117  unsigned int run, unsigned int lumi,
119 };
120 
121 #endif // PILEUPMT_BATCHEDMINBIASSVC_H
ISkipEventIdxSvc.h
BatchedMinbiasSvc::m_skipEventIdxSvc
ServiceHandle< ISkipEventIdxSvc > m_skipEventIdxSvc
Definition: BatchedMinbiasSvc.h:88
BatchedMinbiasSvc::m_empty_caches
std::deque< std::unique_ptr< SGHandleArray > > m_empty_caches
Definition: BatchedMinbiasSvc.h:111
BatchedMinbiasSvc::m_earliestDeltaBC
Gaudi::Property< int > m_earliestDeltaBC
Definition: BatchedMinbiasSvc.h:77
BatchedMinbiasSvc::~BatchedMinbiasSvc
~BatchedMinbiasSvc()
Destructor.
Definition: BatchedMinbiasSvc.cxx:37
vtune_athena.format
format
Definition: vtune_athena.py:14
BatchedMinbiasSvc
Definition: BatchedMinbiasSvc.h:28
BatchedMinbiasSvc::m_MBBatchSize
Gaudi::Property< int > m_MBBatchSize
Definition: BatchedMinbiasSvc.h:63
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
BatchedMinbiasSvc::event_to_batch
int event_to_batch(std::int64_t hs_id)
Definition: BatchedMinbiasSvc.cxx:39
BatchedMinbiasSvc::getNumForBunch
std::size_t getNumForBunch(const EventContext &ctx, int bunch) const override
Definition: BatchedMinbiasSvc.cxx:406
BatchedMinbiasSvc::m_seed
Gaudi::Property< std::uint64_t > m_seed
Definition: BatchedMinbiasSvc.h:51
BatchedMinbiasSvc::m_empty_caches_mtx
std::mutex m_empty_caches_mtx
Definition: BatchedMinbiasSvc.h:112
BatchedMinbiasSvc::m_usePoisson
Gaudi::Property< bool > m_usePoisson
Definition: BatchedMinbiasSvc.h:56
BatchedMinbiasSvc::m_HSBatchSize
Gaudi::Property< int > m_HSBatchSize
Definition: BatchedMinbiasSvc.h:69
BatchedMinbiasSvc::initialize
StatusCode initialize() override
AthService initialize.
Definition: BatchedMinbiasSvc.cxx:43
BatchedMinbiasSvc::m_useBeamInt
Gaudi::Property< bool > m_useBeamInt
Definition: BatchedMinbiasSvc.h:59
BatchedMinbiasSvc::m_onDemandMB
Gaudi::Property< bool > m_onDemandMB
Definition: BatchedMinbiasSvc.h:53
BatchedMinbiasSvc::endHardScatter
StatusCode endHardScatter(const EventContext &ctx) override
Definition: BatchedMinbiasSvc.cxx:416
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:120
BatchedMinbiasSvc::m_cache_mtxs
std::map< int, std::mutex > m_cache_mtxs
Definition: BatchedMinbiasSvc.h:108
IMinbiasSvc.h
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
BatchedMinbiasSvc::m_cache
std::map< int, std::unique_ptr< SGHandleArray > > m_cache
Definition: BatchedMinbiasSvc.h:107
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
run
Definition: run.py:1
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
BatchedMinbiasSvc::m_bkg_evt_sel_ctx
IEvtSelector::Context * m_bkg_evt_sel_ctx
Definition: BatchedMinbiasSvc.h:103
BatchedMinbiasSvc::m_latestDeltaBC
Gaudi::Property< int > m_latestDeltaBC
Definition: BatchedMinbiasSvc.h:80
BatchedMinbiasSvc::m_activeStoreSvc
ServiceHandle< ActiveStoreSvc > m_activeStoreSvc
Definition: BatchedMinbiasSvc.h:97
BatchedMinbiasSvc::calcMBRequired
std::size_t calcMBRequired(std::int64_t hs_id, std::size_t slot, unsigned int run, unsigned int lumi, std::uint64_t event)
Definition: BatchedMinbiasSvc.cxx:195
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
BatchedMinbiasSvc::BatchedMinbiasSvc
BatchedMinbiasSvc(const std::string &name, ISvcLocator *svc)
Constructor.
Definition: BatchedMinbiasSvc.cxx:32
BatchedMinbiasSvc::m_batch_use_count
std::vector< std::unique_ptr< std::atomic_int > > m_batch_use_count
Definition: BatchedMinbiasSvc.h:113
BatchedMinbiasSvc::m_actualNHSEventsPerBatch
Gaudi::Property< std::vector< int > > m_actualNHSEventsPerBatch
Definition: BatchedMinbiasSvc.h:83
BatchedMinbiasSvc::m_nPerBunch
Gaudi::Property< float > m_nPerBunch
Definition: BatchedMinbiasSvc.h:74
BatchedMinbiasSvc::m_num_mb_by_bunch
std::vector< std::vector< std::uint64_t > > m_num_mb_by_bunch
Definition: BatchedMinbiasSvc.h:105
BatchedMinbiasSvc::SGHandleArray
std::vector< SGHandle > SGHandleArray
Definition: BatchedMinbiasSvc.h:50
BatchedMinbiasSvc::get_hs_id
virtual std::int64_t get_hs_id(const EventContext &ctx) const override
Definition: BatchedMinbiasSvc.h:43
BatchedMinbiasSvc::m_skippedHSEvents
Gaudi::Property< int > m_skippedHSEvents
Definition: BatchedMinbiasSvc.h:72
lumiFormat.lumi
lumi
Definition: lumiFormat.py:106
IBeamIntensity.h
provides the relative beam intensity as a function of the bunch xing.
BatchedMinbiasSvc::m_spare_store
SGHandle m_spare_store
Definition: BatchedMinbiasSvc.h:100
BatchedMinbiasSvc::m_bkgEventSelector
ServiceHandle< IEvtSelector > m_bkgEventSelector
Definition: BatchedMinbiasSvc.h:91
BatchedMinbiasSvc::m_NSimultaneousBatches
Gaudi::Property< int > m_NSimultaneousBatches
Definition: BatchedMinbiasSvc.h:66
BatchedMinbiasSvc::m_useBeamLumi
Gaudi::Property< bool > m_useBeamLumi
Definition: BatchedMinbiasSvc.h:61
AthService.h
BatchedMinbiasSvc::m_idx_lists
std::vector< std::vector< std::uint64_t > > m_idx_lists
Definition: BatchedMinbiasSvc.h:106
BatchedMinbiasSvc::m_last_loaded_batch
std::atomic_int m_last_loaded_batch
Definition: BatchedMinbiasSvc.h:114
BatchedMinbiasSvc::m_beamLumi
ServiceHandle< IBeamLuminosity > m_beamLumi
Definition: BatchedMinbiasSvc.h:95
BatchedMinbiasSvc::getMinbias
StoreGateSvc * getMinbias(const EventContext &ctx, std::uint64_t mb_id) override
Definition: BatchedMinbiasSvc.cxx:397
StoreGateSvc.h
BatchedMinbiasSvc::beginHardScatter
StatusCode beginHardScatter(const EventContext &ctx) override
Definition: BatchedMinbiasSvc.cxx:286
BatchedMinbiasSvc::m_reading_batch_mtx
std::mutex m_reading_batch_mtx
Definition: BatchedMinbiasSvc.h:110
IBeamLuminosity.h
provides the relative beam luminosity as a function of the bunch xing.
BatchedMinbiasSvc::m_beamInt
ServiceHandle< IBeamIntensity > m_beamInt
Definition: BatchedMinbiasSvc.h:93
ServiceHandle< StoreGateSvc >