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