-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[hist] Implement initial RAxes
#19596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e30d9e
[hist] Make RVariableBinAxis constructor explicit
hahnjo ba4b04f
[hist] Default-initialize invalid RLinearizedIndex
hahnjo b2e001e
[hist] Implement initial RAxes
hahnjo 3204de7
[hist] Add microbenchmarks for RAxes::ComputeGlobalIndex
hahnjo 309d88a
[hist] Throw on unimplemented axis type
hahnjo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
add_executable(hist_benchmark_axes hist_benchmark_axes.cxx) | ||
target_link_libraries(hist_benchmark_axes ROOTHist benchmark::benchmark) | ||
|
||
add_executable(hist_benchmark_regular hist_benchmark_regular.cxx) | ||
target_link_libraries(hist_benchmark_regular ROOTHist benchmark::benchmark) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <ROOT/RAxes.hxx> | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
#include <random> | ||
#include <tuple> | ||
#include <vector> | ||
|
||
struct RAxes_Regular1 : public benchmark::Fixture { | ||
// The objects are stored and constructed in the fixture to avoid compiler optimizations in the benchmark body taking | ||
// advantage of the (constant) constructor parameters. | ||
ROOT::Experimental::RRegularAxis axis{20, 0.0, 1.0}; | ||
ROOT::Experimental::Internal::RAxes axes{{axis}}; | ||
std::vector<double> fNumbers; | ||
|
||
// Avoid GCC warning | ||
using benchmark::Fixture::SetUp; | ||
void SetUp(benchmark::State &state) final | ||
{ | ||
std::mt19937 gen; | ||
std::uniform_real_distribution<> dis; | ||
fNumbers.resize(state.range(0)); | ||
for (std::size_t i = 0; i < fNumbers.size(); i++) { | ||
fNumbers[i] = dis(gen); | ||
} | ||
} | ||
}; | ||
|
||
BENCHMARK_DEFINE_F(RAxes_Regular1, ComputeGlobalIndex)(benchmark::State &state) | ||
{ | ||
for (auto _ : state) { | ||
for (double number : fNumbers) { | ||
benchmark::DoNotOptimize(axes.ComputeGlobalIndex(std::make_tuple(number))); | ||
} | ||
} | ||
} | ||
BENCHMARK_REGISTER_F(RAxes_Regular1, ComputeGlobalIndex)->Range(0, 32768); | ||
|
||
struct RAxes_Regular2 : public benchmark::Fixture { | ||
// The objects are stored and constructed in the fixture to avoid compiler optimizations in the benchmark body taking | ||
// advantage of the (constant) constructor parameters. | ||
ROOT::Experimental::RRegularAxis axis{20, 0.0, 1.0}; | ||
ROOT::Experimental::Internal::RAxes axes{{axis, axis}}; | ||
std::vector<double> fNumbers; | ||
|
||
// Avoid GCC warning | ||
using benchmark::Fixture::SetUp; | ||
void SetUp(benchmark::State &state) final | ||
{ | ||
std::mt19937 gen; | ||
std::uniform_real_distribution<> dis; | ||
fNumbers.resize(2 * state.range(0)); | ||
for (std::size_t i = 0; i < fNumbers.size(); i++) { | ||
fNumbers[i] = dis(gen); | ||
} | ||
} | ||
}; | ||
|
||
BENCHMARK_DEFINE_F(RAxes_Regular2, ComputeGlobalIndex)(benchmark::State &state) | ||
{ | ||
for (auto _ : state) { | ||
for (std::size_t i = 0; i < fNumbers.size(); i += 2) { | ||
benchmark::DoNotOptimize(axes.ComputeGlobalIndex(std::make_tuple(fNumbers[i], fNumbers[i + 1]))); | ||
} | ||
} | ||
} | ||
BENCHMARK_REGISTER_F(RAxes_Regular2, ComputeGlobalIndex)->Range(0, 32768); | ||
|
||
BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
set(histv7_headers | ||
ROOT/RAxes.hxx | ||
ROOT/RLinearizedIndex.hxx | ||
ROOT/RRegularAxis.hxx | ||
ROOT/RVariableBinAxis.hxx | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#pragma link C++ class ROOT::Experimental::RRegularAxis-; | ||
#pragma link C++ class ROOT::Experimental::RVariableBinAxis-; | ||
#pragma link C++ class ROOT::Experimental::Internal::RAxes-; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/// \file | ||
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback | ||
/// is welcome! | ||
|
||
#ifndef ROOT_RAxes | ||
#define ROOT_RAxes | ||
|
||
#include "RLinearizedIndex.hxx" | ||
#include "RRegularAxis.hxx" | ||
#include "RVariableBinAxis.hxx" | ||
|
||
#include <stdexcept> | ||
#include <tuple> | ||
#include <utility> | ||
#include <variant> | ||
#include <vector> | ||
|
||
class TBuffer; | ||
|
||
namespace ROOT { | ||
namespace Experimental { | ||
namespace Internal { | ||
|
||
/** | ||
Bin configurations for all dimensions of a histogram. | ||
*/ | ||
class RAxes final { | ||
public: | ||
using AxisVariant = std::variant<RRegularAxis, RVariableBinAxis>; | ||
|
||
private: | ||
std::vector<AxisVariant> fAxes; | ||
|
||
public: | ||
/// \param[in] axes the axis objects, must have size > 0 | ||
explicit RAxes(std::vector<AxisVariant> axes) : fAxes(std::move(axes)) | ||
{ | ||
if (fAxes.empty()) { | ||
throw std::invalid_argument("must have at least 1 axis object"); | ||
} | ||
} | ||
|
||
std::size_t GetNumDimensions() const { return fAxes.size(); } | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const std::vector<AxisVariant> &Get() const { return fAxes; } | ||
|
||
friend bool operator==(const RAxes &lhs, const RAxes &rhs) { return lhs.fAxes == rhs.fAxes; } | ||
|
||
/// Compute the total number of bins for all axes. | ||
/// | ||
/// It is the product of each dimension's total number of bins. | ||
/// | ||
/// \return the total number of bins | ||
std::size_t ComputeTotalNumBins() const | ||
jblomer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
std::size_t totalNumBins = 1; | ||
for (auto &&axis : fAxes) { | ||
if (auto *regular = std::get_if<RRegularAxis>(&axis)) { | ||
totalNumBins *= regular->GetTotalNumBins(); | ||
} else if (auto *variable = std::get_if<RVariableBinAxis>(&axis)) { | ||
totalNumBins *= variable->GetTotalNumBins(); | ||
} else { | ||
throw std::logic_error("unimplemented axis type"); | ||
} | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return totalNumBins; | ||
} | ||
|
||
private: | ||
template <std::size_t I, typename... A> | ||
RLinearizedIndex ComputeGlobalIndex(std::size_t index, const std::tuple<A...> &args) const | ||
{ | ||
const auto &axis = fAxes[I]; | ||
RLinearizedIndex linIndex; | ||
if (auto *regular = std::get_if<RRegularAxis>(&axis)) { | ||
index *= regular->GetTotalNumBins(); | ||
linIndex = regular->ComputeLinearizedIndex(std::get<I>(args)); | ||
} else if (auto *variable = std::get_if<RVariableBinAxis>(&axis)) { | ||
index *= variable->GetTotalNumBins(); | ||
linIndex = variable->ComputeLinearizedIndex(std::get<I>(args)); | ||
} else { | ||
throw std::logic_error("unimplemented axis type"); | ||
} | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!linIndex.fValid) { | ||
return {0, false}; | ||
} | ||
index += linIndex.fIndex; | ||
if constexpr (I + 1 < sizeof...(A)) { | ||
return ComputeGlobalIndex<I + 1>(index, args); | ||
} | ||
return {index, true}; | ||
} | ||
|
||
public: | ||
/// Compute the global index for all axes. | ||
/// | ||
/// \param[in] args the arguments | ||
/// \return the global index that may be invalid | ||
template <typename... A> | ||
RLinearizedIndex ComputeGlobalIndex(const std::tuple<A...> &args) const | ||
{ | ||
if (sizeof...(A) != fAxes.size()) { | ||
throw std::invalid_argument("invalid number of arguments to ComputeGlobalIndex"); | ||
} | ||
return ComputeGlobalIndex<0, A...>(0, args); | ||
} | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// ROOT Streamer function to throw when trying to store an object of this class. | ||
void Streamer(TBuffer &) { throw std::runtime_error("unable to store RAxes"); } | ||
}; | ||
|
||
} // namespace Internal | ||
} // namespace Experimental | ||
} // namespace ROOT | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#include "hist_test.hxx" | ||
|
||
#include <stdexcept> | ||
#include <tuple> | ||
#include <variant> | ||
#include <vector> | ||
|
||
TEST(RAxes, Constructor) | ||
{ | ||
static constexpr std::size_t BinsX = 20; | ||
const RRegularAxis regularAxis(BinsX, 0, BinsX); | ||
static constexpr std::size_t BinsY = 30; | ||
std::vector<double> bins; | ||
for (std::size_t i = 0; i < BinsY; i++) { | ||
bins.push_back(i); | ||
} | ||
bins.push_back(BinsY); | ||
const RVariableBinAxis variableBinAxis(bins); | ||
|
||
RAxes axes({regularAxis, variableBinAxis}); | ||
EXPECT_EQ(axes.GetNumDimensions(), 2); | ||
const auto &v = axes.Get(); | ||
ASSERT_EQ(v.size(), 2); | ||
EXPECT_EQ(v[0].index(), 0); | ||
EXPECT_EQ(v[1].index(), 1); | ||
EXPECT_TRUE(std::get_if<RRegularAxis>(&v[0]) != nullptr); | ||
EXPECT_TRUE(std::get_if<RVariableBinAxis>(&v[1]) != nullptr); | ||
|
||
std::vector<RAxes::AxisVariant> newAxes{variableBinAxis, regularAxis}; | ||
axes = RAxes(newAxes); | ||
EXPECT_EQ(axes.GetNumDimensions(), 2); | ||
|
||
EXPECT_THROW(RAxes({}), std::invalid_argument); | ||
} | ||
|
||
TEST(RAxes, Equality) | ||
{ | ||
static constexpr std::size_t BinsX = 20; | ||
const RRegularAxis regularAxis(BinsX, 0, BinsX); | ||
static constexpr std::size_t BinsY = 30; | ||
std::vector<double> bins; | ||
for (std::size_t i = 0; i < BinsY; i++) { | ||
bins.push_back(i); | ||
} | ||
bins.push_back(BinsY); | ||
const RVariableBinAxis variableBinAxis(bins); | ||
|
||
const RAxes axesA({regularAxis, variableBinAxis}); | ||
const RAxes axesA2({regularAxis, variableBinAxis}); | ||
const RAxes axesB({variableBinAxis, regularAxis}); | ||
const RAxes axesC({regularAxis}); | ||
const RAxes axesD({variableBinAxis}); | ||
|
||
EXPECT_TRUE(axesA == axesA); | ||
EXPECT_TRUE(axesA == axesA2); | ||
EXPECT_TRUE(axesA2 == axesA); | ||
|
||
EXPECT_FALSE(axesA == axesB); | ||
EXPECT_FALSE(axesA == axesC); | ||
EXPECT_FALSE(axesA == axesD); | ||
|
||
EXPECT_FALSE(axesB == axesC); | ||
EXPECT_FALSE(axesB == axesD); | ||
|
||
EXPECT_FALSE(axesC == axesD); | ||
} | ||
|
||
TEST(RAxes, ComputeTotalNumBins) | ||
{ | ||
static constexpr std::size_t BinsX = 20; | ||
const RRegularAxis regularAxis(BinsX, 0, BinsX); | ||
static constexpr std::size_t BinsY = 30; | ||
std::vector<double> bins; | ||
for (std::size_t i = 0; i < BinsY; i++) { | ||
bins.push_back(i); | ||
} | ||
bins.push_back(BinsY); | ||
const RVariableBinAxis variableBinAxis(bins); | ||
const RAxes axes({regularAxis, variableBinAxis}); | ||
|
||
// Both axes include underflow and overflow bins. | ||
EXPECT_EQ(axes.ComputeTotalNumBins(), (BinsX + 2) * (BinsY + 2)); | ||
} | ||
|
||
TEST(RAxes, ComputeGlobalIndex) | ||
{ | ||
static constexpr std::size_t BinsX = 20; | ||
const RRegularAxis regularAxis(BinsX, 0, BinsX); | ||
static constexpr std::size_t BinsY = 30; | ||
std::vector<double> bins; | ||
for (std::size_t i = 0; i < BinsY; i++) { | ||
bins.push_back(i); | ||
} | ||
bins.push_back(BinsY); | ||
const RVariableBinAxis variableBinAxis(bins); | ||
const RAxes axes({regularAxis, variableBinAxis}); | ||
|
||
{ | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 2.5)); | ||
EXPECT_EQ(globalIndex.fIndex, 1 * (BinsY + 2) + 2); | ||
EXPECT_TRUE(globalIndex.fValid); | ||
} | ||
|
||
{ | ||
// Underflow bin of the first axis. | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(-1, 2.5)); | ||
EXPECT_EQ(globalIndex.fIndex, BinsX * (BinsY + 2) + 2); | ||
EXPECT_TRUE(globalIndex.fValid); | ||
} | ||
|
||
{ | ||
// Overflow bin of the second axis. | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 42)); | ||
EXPECT_EQ(globalIndex.fIndex, 1 * (BinsY + 2) + BinsY + 1); | ||
EXPECT_TRUE(globalIndex.fValid); | ||
} | ||
} | ||
|
||
TEST(RAxes, ComputeGlobalIndexNoFlowBins) | ||
{ | ||
static constexpr std::size_t BinsX = 20; | ||
const RRegularAxis regularAxis(BinsX, 0, BinsX, /*enableFlowBins=*/false); | ||
static constexpr std::size_t BinsY = 30; | ||
std::vector<double> bins; | ||
for (std::size_t i = 0; i < BinsY; i++) { | ||
bins.push_back(i); | ||
} | ||
bins.push_back(BinsY); | ||
const RVariableBinAxis variableBinAxis(bins, /*enableFlowBins=*/false); | ||
const RAxes axes({regularAxis, variableBinAxis}); | ||
ASSERT_EQ(axes.ComputeTotalNumBins(), BinsX * BinsY); | ||
|
||
{ | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 2.5)); | ||
EXPECT_EQ(globalIndex.fIndex, 1 * BinsY + 2); | ||
EXPECT_TRUE(globalIndex.fValid); | ||
} | ||
|
||
{ | ||
// Underflow bin of the first axis. | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(-1, 2.5)); | ||
EXPECT_EQ(globalIndex.fIndex, 0); | ||
EXPECT_FALSE(globalIndex.fValid); | ||
} | ||
|
||
{ | ||
// Overflow bin of the second axis. | ||
const auto globalIndex = axes.ComputeGlobalIndex(std::make_tuple(1.5, 42)); | ||
EXPECT_EQ(globalIndex.fIndex, 0); | ||
EXPECT_FALSE(globalIndex.fValid); | ||
} | ||
} | ||
|
||
TEST(RAxes, ComputeGlobalIndexInvalidNumberOfArguments) | ||
{ | ||
static constexpr std::size_t Bins = 20; | ||
const RRegularAxis axis(Bins, 0, Bins); | ||
const RAxes axes1({axis}); | ||
ASSERT_EQ(axes1.GetNumDimensions(), 1); | ||
const RAxes axes2({axis, axis}); | ||
ASSERT_EQ(axes2.GetNumDimensions(), 2); | ||
|
||
EXPECT_NO_THROW(axes1.ComputeGlobalIndex(std::make_tuple(1))); | ||
EXPECT_THROW(axes1.ComputeGlobalIndex(std::make_tuple(1, 2)), std::invalid_argument); | ||
|
||
EXPECT_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1)), std::invalid_argument); | ||
EXPECT_NO_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1, 2))); | ||
EXPECT_THROW(axes2.ComputeGlobalIndex(std::make_tuple(1, 2, 3)), std::invalid_argument); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.