Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Public Member Functions | Private Attributes | List of all members
SkipEventIdxSvc Class Reference

#include <SkipEventIdxSvc.h>

Inheritance diagram for SkipEventIdxSvc:
Collaboration diagram for SkipEventIdxSvc:

Public Member Functions

 SkipEventIdxSvc (const std::string &name, ISvcLocator *svc)
 
 ~SkipEventIdxSvc () override=default
 
StatusCode initialize () override
 
StatusCode start () override
 
StatusCode registerCallback (std::function< StatusCode(EvtIter, EvtIter)> &&callback) override
 
void handle (const Incident &inc) override
 

Private Attributes

int m_initial_skip_events {}
 
std::vector< EvtId > m_events {}
 
std::vector< std::function< StatusCode(EvtIter, EvtIter)> > m_callbacks {}
 
bool m_started = false
 

Detailed Description

Definition at line 13 of file SkipEventIdxSvc.h.

Constructor & Destructor Documentation

◆ SkipEventIdxSvc()

SkipEventIdxSvc::SkipEventIdxSvc ( const std::string &  name,
ISvcLocator *  svc 
)

Definition at line 54 of file SkipEventIdxSvc.cxx.

55  : base_class(name, svc) {}

◆ ~SkipEventIdxSvc()

SkipEventIdxSvc::~SkipEventIdxSvc ( )
overridedefault

Member Function Documentation

◆ handle()

void SkipEventIdxSvc::handle ( const Incident &  inc)
override

Definition at line 223 of file SkipEventIdxSvc.cxx.

223  {
224  if (inc.type() != "BeginRun"s && inc.type() != "SkipEvents"s) {
225  return;
226  }
227  ATH_MSG_DEBUG("Received incident of type " << inc.type() << " from "
228  << inc.source());
229  if (inc.type() == "SkipEvents"s) {
230  ATH_MSG_DEBUG("Running callbacks");
231  const auto& cInc =
232  dynamic_cast<const ContextIncident<std::tuple<int, int>>&>(inc);
233  auto begin = m_events.cbegin() + std::get<0>(cInc.tag());
234  auto end = m_events.cbegin() + std::get<1>(cInc.tag()) +
235  1; // plus 1 for 1 after end
236  for (auto&& fn : m_callbacks) {
237  if (!std::invoke(fn, begin, end).isSuccess()) {
238  throw std::runtime_error(
239  "A skipEvent callback returned a failure error code!");
240  }
241  }
242  }
243 }

◆ initialize()

StatusCode SkipEventIdxSvc::initialize ( )
override

Definition at line 57 of file SkipEventIdxSvc.cxx.

57  {
58  // This is a special service that runs through all the input events ahead of
59  // time, and records the run and lb numbers
60  ATH_MSG_INFO("Initializing SkipEventIdxSvc");
61  SmartIF<IProperty> propMgr(serviceLocator());
62  std::string evt_sel_name{};
63  ATH_CHECK(propMgr->getProperty("EvtSel", evt_sel_name));
64  if (evt_sel_name.empty()) {
65  // No event selector
66  //
67  ATH_MSG_WARNING("No event selector");
68  m_started = true;
69  return StatusCode::SUCCESS;
70  }
71  auto sg = serviceLocator()->service<StoreGateSvc>("StoreGateSvc/StoreGateSvc",
72  false);
73  auto evtSel = serviceLocator()->service<IEvtSelector>(evt_sel_name, false);
74  if (!sg.isValid() || !evtSel.isValid()) {
75  ATH_MSG_WARNING("Event selector or storegate is invalid");
76  return StatusCode::FAILURE;
77  }
78 
79  auto modSvc = serviceLocator()->service<IEvtIdModifierSvc>(
80  "EvtIdModifierSvc/EvtIdModifierSvc", false);
81  std::uint64_t evts_skipped_before_mod = 0;
82  std::vector<EvtId> modifier_evts{};
83  if (modSvc.isValid()) {
84  // Steal it's configuration
85  try {
86  evts_skipped_before_mod =
87  getProp<std::uint64_t&>(modSvc, "SkipEvents").value();
88  ATH_MSG_INFO("Skipping " << evts_skipped_before_mod
89  << " events before modifying");
90  std::vector<std::uint64_t> lst =
91  getProp<std::vector<std::uint64_t>&>(modSvc, "Modifiers").value();
92  ATH_MSG_DEBUG("Printing EvtId modifier config. There are "
93  << lst.size() / 6 << " entries.");
94  std::string config_str{};
95  auto config_str_iter = std::back_inserter(config_str);
96  modifier_evts =
97  lst | rv::chunk(6) |
98  rv::for_each([&config_str_iter](const auto& rec) {
99  const int mod_bitset = rec[5];
100  const bool mod_run_num = mod_bitset & 1;
101  const bool mod_evt_num = mod_bitset & (1 << 1);
102  const bool mod_lb_num = mod_bitset & (1 << 3);
103 
104  const std::uint64_t runNum = mod_run_num ? rec[0] : 0;
105  const std::uint64_t evtNum = mod_evt_num ? rec[1] : 0;
106  const std::uint64_t lbNum = mod_lb_num ? rec[3] : 0;
107  const std::uint64_t numEvts = rec[4];
108 
109  std::format_to(config_str_iter,
110  "Run: {} [{:c}] LB: {} [{:c}] EVT: {} [{:c}] "
111  "NumEvts: {}\n",
112  runNum, mod_run_num ? 'Y' : 'N', lbNum,
113  mod_lb_num ? 'Y' : 'N', evtNum,
114  mod_evt_num ? 'Y' : 'N', numEvts);
115  return ranges::yield_from(
116  rv::repeat_n(EvtId{static_cast<uint32_t>(runNum),
117  static_cast<uint32_t>(lbNum), evtNum},
118  numEvts));
119  }) |
120  ranges::to<std::vector<EvtId>>;
121  ATH_MSG_DEBUG(config_str);
122  } catch (const std::bad_cast&) {
123  ATH_MSG_ERROR("Wrong type for property of EvtIdModifierSvc.");
124  }
125  } else {
126  ATH_MSG_INFO("No EvtIdModifierSvc found");
127  }
128 
129  m_initial_skip_events = getProp<int>(evtSel, "SkipEvents");
130  ATH_CHECK(setProp(evtSel, "SkipEvents", "0"));
131  IEvtSelector::Context* ctx = nullptr;
132  ATH_CHECK(sg->clearStore());
133  ATH_CHECK(evtSel->createContext(ctx));
134 
135  std::uint64_t idx = 0;
136  ATH_CHECK(dynamic_cast<Service*>(evtSel.get())->start());
137  while (evtSel->next(*ctx).isSuccess()) {
138  EvtId evt_id{};
139  // Load event
140  IOpaqueAddress* addr = nullptr;
141  ATH_CHECK(evtSel->createAddress(*ctx, addr));
142  ATH_CHECK(sg->recordAddress(addr));
143  ATH_CHECK(sg->loadEventProxies());
144 
145  // Read EventInfo
146  // We don't look for the legacy EventInfo, only an Input attribute list and
147  // if that doesn't exist, and xAOD::EventInfo
148  std::vector<std::string> attr_lists;
149  sg->keys<AthenaAttributeList> (attr_lists);
150  const auto* attr_list_p =
151  sg->tryConstRetrieve<AthenaAttributeList>("Input");
152  if (attr_list_p != nullptr && attr_list_p->size() > 6) {
153  try {
154  const AthenaAttributeList& attr_list = *attr_list_p;
155  const auto runNum = attr_list["RunNumber"].data<unsigned>();
156  const auto evtNum = attr_list["EventNumber"].data<unsigned long long>();
157  const auto lbNum = attr_list["LumiBlockN"].data<unsigned>();
158  evt_id = EvtId{runNum, lbNum, evtNum};
159  } catch (...) {
160  }
161  }
162  ATH_CHECK(sg->clearStore());
163  if (idx >= evts_skipped_before_mod && !modifier_evts.empty()) {
164  const std::size_t mod_idx =
165  (idx - evts_skipped_before_mod) % modifier_evts.size();
166  evt_id.runNum = modifier_evts[mod_idx].runNum != 0U
167  ? modifier_evts[mod_idx].runNum
168  : evt_id.runNum;
169  evt_id.lbNum = modifier_evts[mod_idx].lbNum != 0U
170  ? modifier_evts[mod_idx].lbNum
171  : evt_id.lbNum;
172  evt_id.evtNum = modifier_evts[mod_idx].evtNum != 0U
173  ? modifier_evts[mod_idx].evtNum
174  : evt_id.evtNum;
175  }
176  evt_id.evtIdx = idx;
177  m_events.push_back(evt_id);
178  ++idx;
179  }
180 
181  // reset current storegate
182  ATH_MSG_INFO("Setting SkipEvents back to " << m_initial_skip_events
183  << " and rewinding");
184  ATH_CHECK(
185  setProp(evtSel, "SkipEvents", std::format("{}", m_initial_skip_events)));
186  ATH_CHECK(evtSel->rewind(*ctx));
187  ATH_MSG_INFO("Recorded a total of " << m_events.size() << " events");
188 
189  // Register as incident handler
190  ServiceHandle<IIncidentSvc> incident_svc("IncidentSvc", name());
191  ATH_CHECK(incident_svc.retrieve());
192  incident_svc->addListener(this, "BeginRun");
193  incident_svc->addListener(this, "SkipEvents");
194  return StatusCode::SUCCESS;
195 }

◆ registerCallback()

StatusCode SkipEventIdxSvc::registerCallback ( std::function< StatusCode(EvtIter, EvtIter)> &&  callback)
override

Definition at line 209 of file SkipEventIdxSvc.cxx.

210  {
211  m_callbacks.push_back(callback);
212  // Call callback immediately for events skipped in EventSelector if already
213  // started
214  if (m_started && !m_events.empty()) {
215  auto begin = m_events.cbegin();
216  auto end = m_events.cbegin() +
217  std::min(std::size_t(m_initial_skip_events), m_events.size());
218  ATH_CHECK(std::invoke(callback, begin, end));
219  }
220  return StatusCode::SUCCESS;
221 }

◆ start()

StatusCode SkipEventIdxSvc::start ( )
override

Definition at line 197 of file SkipEventIdxSvc.cxx.

197  {
198  ATH_MSG_INFO("Starting SkipEventIdxSvc");
199  // Call callbacks if any have already been added
200  for (auto&& callback : m_callbacks) {
201  auto begin = m_events.cbegin();
202  auto end = m_events.cbegin() + m_initial_skip_events;
203  ATH_CHECK(std::invoke(callback, begin, end));
204  }
205  m_started = true;
206  return StatusCode::SUCCESS;
207 }

Member Data Documentation

◆ m_callbacks

std::vector<std::function<StatusCode(EvtIter, EvtIter)> > SkipEventIdxSvc::m_callbacks {}
private

Definition at line 28 of file SkipEventIdxSvc.h.

◆ m_events

std::vector<EvtId> SkipEventIdxSvc::m_events {}
private

Definition at line 27 of file SkipEventIdxSvc.h.

◆ m_initial_skip_events

int SkipEventIdxSvc::m_initial_skip_events {}
private

Definition at line 26 of file SkipEventIdxSvc.h.

◆ m_started

bool SkipEventIdxSvc::m_started = false
private

Definition at line 29 of file SkipEventIdxSvc.h.


The documentation for this class was generated from the following files:
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
runLayerRecalibration.callback
callback
Definition: runLayerRecalibration.py:64
athena.value
value
Definition: athena.py:124
skel.runNum
runNum
Definition: skel.ABtoEVGEN.py:137
IEvtIdModifierSvc
Definition: IEvtIdModifierSvc.h:37
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
SkipEventIdxSvc::m_initial_skip_events
int m_initial_skip_events
Definition: SkipEventIdxSvc.h:26
python.getCurrentFolderTag.fn
fn
Definition: getCurrentFolderTag.py:79
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SkipEventIdxSvc::m_callbacks
std::vector< std::function< StatusCode(EvtIter, EvtIter)> > m_callbacks
Definition: SkipEventIdxSvc.h:28
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthenaAttributeList
An AttributeList represents a logical row of attributes in a metadata table. The name and type of eac...
Definition: PersistentDataModel/PersistentDataModel/AthenaAttributeList.h:45
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
hydjet.StoreGateSvc
StoreGateSvc
Definition: hydjet.minbias.pbpb5520.r12345.job.py:40
SkipEventIdxSvc::m_events
std::vector< EvtId > m_events
Definition: SkipEventIdxSvc.h:27
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
SkipEventIdxSvc::m_started
bool m_started
Definition: SkipEventIdxSvc.h:29
ServiceHandle< IIncidentSvc >