ATLAS Offline Software
PileUpMTAlg.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2022, 2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef PILEUPMT_PILEUPMTALG_H
6 #define PILEUPMT_PILEUPMTALG_H
7 
10 #include "PileUpMT/IMinbiasSvc.h"
15 #include "src/ISkipEventIdxSvc.h"
20 
21 // Example ROOT Includes
22 // #include "TTree.h"
23 // #include "TH1D.h"
24 #include <fmt/chrono.h>
25 #include <fmt/compile.h>
26 #include <fmt/format.h>
27 
28 #include <atomic>
29 #include <chrono>
30 #include <cstdio>
31 #include <string>
32 #include <string_view>
33 #include <thread>
34 
36  private:
37  std::FILE* m_file{};
39 
40  public:
41  atomic_output() = default;
42  void init(const std::string& filename) {
43  using namespace std::chrono;
44  std::lock_guard lck{m_mtx};
45  if (m_file != nullptr) {
46  return;
47  }
48  m_file = std::fopen(filename.c_str(), "a");
49  auto time = fmt::localtime(system_clock::to_time_t(system_clock::now()));
50  fmt::print(m_file, ("FILE CREATED ON {:%Y-%m-%d} at {:%H:%M:%S %Z}\n"),
51  time, time);
52  }
54  std::lock_guard lck{m_mtx};
55  if (m_file != nullptr) {
56  std::fclose(m_file);
57  m_file = nullptr;
58  }
59  }
60 
61  void print(const fmt::memory_buffer& str) {
62  std::lock_guard lck{m_mtx};
63  std::fwrite(str.data(), sizeof(char), str.size(), m_file);
64  std::fflush(m_file);
65  }
66 };
67 
68 class PileUpMTAlg : public AthAlgorithm {
69  public:
70  PileUpMTAlg(const std::string& name, ISvcLocator* pSvcLocator);
71  virtual ~PileUpMTAlg();
72 
74 
75  // IS EXECUTED:
76  virtual StatusCode initialize() override; // once, before any input is loaded
77  virtual StatusCode execute() override; // per event
78  virtual StatusCode finalize() override; // once, after all events processed
79 
80  // Make this algorithm clonable
81  virtual bool isClonable() const override { return true; }
82  // virtual unsigned int cardinality() const override { return m_cardinality; }
83 
92 
93  private:
94  ServiceHandle<IMinbiasSvc> m_lowptMBSvc{this, "LowPtMinbiasSvc", "",
95  "MinbiasSvc for low pT minbias"};
96  ServiceHandle<IMinbiasSvc> m_highptMBSvc{this, "HighPtMinbiasSvc", "",
97  "MinbiasSvc for high pT minbias"};
98  ServiceHandle<IMinbiasSvc> m_cavernMBSvc{this, "CavernMinbiasSvc", "",
99  "MinbiasSvc for cavern background"};
100  ServiceHandle<IMinbiasSvc> m_beamhaloMBSvc{
101  this, "BeamHaloMinbiasSvc", "", "MinbiasSvc for beam halo background"};
103  this, "BeamGasMinbiasSvc", "", "MinbiasSvc for beam gas background"};
104  ServiceHandle<IBeamIntensity> m_beamInt{this, "BeamIntSvc", "FlatBM",
105  "Beam intensity service"};
107  this, "BeamLumiSvc", "LumiProfileSvc", "Beam luminosity service"};
108  ServiceHandle<IAthRNGSvc> m_rngSvc{this, "RNGSvc", "AthRNGSvc/PileupRNG",
109  "RNG service for pile-up digitization"};
110  ToolHandle<xAODMaker::IEventInfoCnvTool> m_xAODEICnvTool{
111  this, "xAODCnvTool", "xAODMaker::EventInfoCnvTool/EventInfoCnvTool",
112  "xAOD EventInfo conversion tool"};
113  ToolHandleArray<IPileUpTool> m_puTools{
114  this, "PileUpTools", {}, "Pileup tools"};
115  Gaudi::Property<bool> m_writeTrace{this, "WriteTrace", false, "Write trace of pileup events used"};
116  Gaudi::Property<int> m_skippedHSEvents{this, "SkippedHSEvents", 0,
117  "Number of skipped HS events"};
118  Gaudi::Property<float> m_avgMu{this, "AverageMu", 200, "Average mu"};
119  Gaudi::Property<float> m_fracLowPt{this, "FracLowPt", 0.,
120  "Fraction of pileup that has low pT"};
121  Gaudi::Property<float> m_fracHighPt{this, "FracHighPt", 0.,
122  "Fraction of pileup that has high pT"};
123  Gaudi::Property<float> m_numCavern{
124  this, "NumCavern", 0., "Number of cavern background events per BC"};
125  Gaudi::Property<float> m_numBeamGas{
126  this, "NumBeamGas", 0., "Number of beam gas background events per BC"};
127  Gaudi::Property<float> m_numBeamHalo{
128  this, "NumBeamHalo", 0., "Number of beam halo background events per BC"};
129  Gaudi::Property<int> m_BCSpacing{this, "BCSpacing", 25,
130  "Bunch crossing spacing in ns"};
131  Gaudi::Property<int> m_earliestDeltaBC{
132  this, "EarliestDeltaBC", -32,
133  "Earliest bunch crossing to consider (as delta)"};
134  Gaudi::Property<int> m_latestDeltaBC{
135  this, "LatestDeltaBC", +6,
136  "Latest bunch crossing to consider (as delta)"};
138  this, "SkipEvtIdxSvc", "SkipEventIdxSvc",
139  "Skipped event index (run / lb num) provider"};
140  SG::WriteHandleKey<xAOD::EventInfo> m_evtInfoKey{this, "EventInfoKey", "OverlayEvent",
141  "Overlay EventInfo key"};
143  // Utilities
144  StatusCode get_ei(StoreGateSvc& sg,
145  std::unique_ptr<const xAOD::EventInfo>& ei,
146  std::unique_ptr<xAOD::EventAuxInfo>& eiAux,
147  bool pileup = false) const;
148  inline unsigned int get_BCID(int bc, unsigned int central_BCID) const {
149  constexpr int maxBCPerOrbit = 3564; // FIXME may need update
150  return static_cast<unsigned int>((bc + static_cast<int>(central_BCID)) %
151  maxBCPerOrbit);
152  }
153  StatusCode add_subevt(const std::vector<std::uint32_t>& bcid,
157  xAOD::EventInfo::PileUpType puType, int bc,
158  const EventContext& ctx, unsigned long subevt_id,
159  std::vector<std::uint64_t>& trace);
160  inline static atomic_output m_pileupTrace ATLAS_THREAD_SAFE{};
161  inline static bool m_skiptrace_written ATLAS_THREAD_SAFE = false;
162 };
163 
164 #endif //> !PILEUPMT_PILEUPMTALG_H
ISkipEventIdxSvc.h
ActiveStoreSvc.h
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
atomic_output::m_file
std::FILE * m_file
Definition: PileUpMTAlg.h:37
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
atomic_output::m_mtx
std::mutex m_mtx
Definition: PileUpMTAlg.h:38
initialize
void initialize()
Definition: run_EoverP.cxx:894
xAOD::EventInfo_v1::PileUpType
PileUpType
Enumerator describing the types of pileup events.
Definition: EventInfo_v1.h:264
atomic_output::init
void init(const std::string &filename)
Definition: PileUpMTAlg.h:42
python.PileUpToolsConfig.PileUpMTAlg
PileUpMTAlg
Definition: PileUpToolsConfig.py:9
atomic_output::~atomic_output
~atomic_output()
Definition: PileUpMTAlg.h:53
EventInfoContainer.h
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
PileUpMTAlg
Definition: PileUpMTAlg.h:68
AthAlgorithm.h
python.handimod.now
now
Definition: handimod.py:675
IMinbiasSvc.h
SG::WriteHandleKey< xAOD::EventInfo >
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IPileUpTool.h
a call-back interface for tools that merge pileup events information An IPileUpTool is called back fo...
PileUpMTAlg::get_BCID
unsigned int get_BCID(int bc, unsigned int central_BCID) const
Definition: PileUpMTAlg.h:148
AthAlgorithm
Definition: AthAlgorithm.h:47
python.fprint.fwrite
def fwrite(f, *args)
Definition: fprint.py:120
dqt_zlumi_pandas.pileup
pileup
Definition: dqt_zlumi_pandas.py:174
EventAuxInfo.h
atomic_output
Definition: PileUpMTAlg.h:35
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAOD::bcid
setEventNumber setTimeStamp bcid
Definition: EventInfo_v1.cxx:133
EventInfo.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
hydjet.StoreGateSvc
StoreGateSvc
Definition: hydjet.minbias.pbpb5520.r12345.job.py:40
IEventInfoCnvTool.h
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
IBeamIntensity.h
provides the relative beam intensity as a function of the bunch xing.
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
PileUpMTAlg::isClonable
virtual bool isClonable() const override
Definition: PileUpMTAlg.h:81
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
atomic_output::print
void print(const fmt::memory_buffer &str)
Definition: PileUpMTAlg.h:61
atomic_output::atomic_output
atomic_output()=default
IBeamLuminosity.h
provides the relative beam luminosity as a function of the bunch xing.
IAthRNGSvc.h
ServiceHandle< IMinbiasSvc >