Skip to content

Commit

Permalink
refactor: Use std::optional for event range end requested (#790)
Browse files Browse the repository at this point in the history
Previously, SIZE_MAX was used to indicate that the full available end range of an input file was to be used. This caused issues when no input file was present (for example in the geometry examples) and --events was not given., since it would run SIZE_MAX events (see #707).

This PR changes this to use std::optional<size_t>, so that the default is clearly communicated. The geometry example should now default to 1 if --events isn't supplied.

Fixes #707
  • Loading branch information
paulgessinger committed May 5, 2021
1 parent 8ce0f88 commit db1ce93
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -34,7 +35,7 @@ class Sequencer {
/// number of events to skip at the beginning
size_t skip = 0;
/// number of events to process, SIZE_MAX to process all available events
size_t events = SIZE_MAX;
std::optional<size_t> events = std::nullopt;
/// logging level
Acts::Logging::Level logLevel = Acts::Logging::INFO;
/// number of parallel threads to run, negative for automatic determination
Expand Down
13 changes: 8 additions & 5 deletions Examples/Framework/src/Framework/Sequencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,20 @@ ActsExamples::Sequencer::determineEventsRange() const {
return kInvalidEventsRange;
}
// events range was not defined by either the readers or user command line.
if ((beg == 0u) and (end == SIZE_MAX) and (m_cfg.events == SIZE_MAX)) {
if ((beg == 0u) and (end == SIZE_MAX) and (!m_cfg.events.has_value())) {
ACTS_ERROR("Could not determine number of events");
return kInvalidEventsRange;
}

// take user selection into account
auto begSelected = saturatedAdd(beg, m_cfg.skip);
auto endRequested = saturatedAdd(begSelected, m_cfg.events);
auto endSelected = std::min(end, endRequested);
if (end < endRequested) {
ACTS_INFO("Restrict requested number of events to available ones");
auto endSelected = end;
if (m_cfg.events.has_value()) {
auto endRequested = saturatedAdd(begSelected, m_cfg.events.value());
endSelected = std::min(end, endRequested);
if (end < endRequested) {
ACTS_INFO("Restrict requested number of events to available ones");
}
}

return {begSelected, endSelected};
Expand Down
3 changes: 2 additions & 1 deletion Examples/Run/Common/src/GeometryExampleBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ int processGeometry(int argc, char* argv[],

// Now read the standard options
auto logLevel = ActsExamples::Options::readLogLevel(vm);
auto nEvents = ActsExamples::Options::readSequencerConfig(vm).events;
size_t nEvents =
ActsExamples::Options::readSequencerConfig(vm).events.value_or(1);

// The geometry, material and decoration
auto geometry = ActsExamples::Geometry::build(vm, detector);
Expand Down
2 changes: 1 addition & 1 deletion Examples/Run/MagneticField/BFieldAccessExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int main(int argc, char* argv[]) {
// Why does this need number-of-events? If it really does emulate
// per-event access patterns this should be switched to a proper
// Sequencer-based tool. Otherwise it should be removed.
auto nEvents = ActsExamples::Options::readSequencerConfig(vm).events;
auto nEvents = ActsExamples::Options::readSequencerConfig(vm).events.value();
auto bField = ActsExamples::Options::readMagneticField(vm);

// Get the phi and eta range
Expand Down

0 comments on commit db1ce93

Please sign in to comment.