Skip to content

Commit

Permalink
feat: Configurable and more flexible binning for CKFPerformanceWriter (
Browse files Browse the repository at this point in the history
…#2379)

This allows for a more flexible binning for the `PlotTools`, and exposes this to python. For example, a binning can be configured like this:

```python
binningCfg = {
    "Pt" : acts.examples.Binning("pT", list(np.logspace(-2,2,40))),
    "Eta" : acts.examples.Binning("eta", 40, -4, 4),
    "Phi" : acts.examples.Binning("phi", 100, -3.15, 3.15),
}

s.addWriter(
    acts.examples.CKFPerformanceWriter(
		...
        effPlotToolConfig = acts.examples.EffPlotToolConfig(self.binningCfg),
    )
)
```
  • Loading branch information
benjaminhuth committed Aug 29, 2023
1 parent e529c52 commit 3e77d7d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
39 changes: 28 additions & 11 deletions Examples/Framework/include/ActsExamples/Utilities/Helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2019 CERN for the benefit of the Acts project
// Copyright (C) 2019-2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -29,16 +29,33 @@ namespace ActsExamples {

namespace PlotHelpers {
/// @brief Nested binning struct for booking plots
struct Binning {
Binning() = default;

Binning(std::string bTitle, int bins, float bMin, float bMax)
: title(std::move(bTitle)), nBins(bins), min(bMin), max(bMax){};

std::string title; ///< title to be displayed
int nBins = 0; ///< number of bins
float min = 0; ///< minimum value
float max = 0; ///< maximum value
class Binning {
public:
Binning() : m_bins({0.0}) {}

Binning(std::string title, int bins, double bMin, double bMax)
: m_title(std::move(title)) {
const auto step = (bMax - bMin) / bins;
m_bins.resize(bins + 1);
std::generate(m_bins.begin(), m_bins.end(), [&, v = bMin]() mutable {
auto r = v;
v += step;
return r;
});
}

Binning(std::string title, std::vector<double> bins)
: m_title(std::move(title)), m_bins(std::move(bins)){};

const auto& title() const { return m_title; }
auto nBins() const { return m_bins.size() - 1; }
const double* data() const { return m_bins.data(); }
auto low() const { return m_bins.front(); }
auto high() const { return m_bins.back(); }

private:
std::string m_title;
std::vector<double> m_bins;
};

/// @brief book a 1D histogram
Expand Down
34 changes: 17 additions & 17 deletions Examples/Framework/src/Utilities/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2019 CERN for the benefit of the Acts project
// Copyright (C) 2019-2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -23,21 +23,21 @@ namespace ActsExamples {
namespace PlotHelpers {
TH1F* bookHisto(const char* histName, const char* histTitle,
const Binning& varBinning) {
TH1F* hist = new TH1F(histName, histTitle, varBinning.nBins, varBinning.min,
varBinning.max);
hist->GetXaxis()->SetTitle(varBinning.title.c_str());
TH1F* hist =
new TH1F(histName, histTitle, varBinning.nBins(), varBinning.data());
hist->GetXaxis()->SetTitle(varBinning.title().c_str());
hist->GetYaxis()->SetTitle("Entries");
hist->Sumw2();
return hist;
}

TH2F* bookHisto(const char* histName, const char* histTitle,
const Binning& varXBinning, const Binning& varYBinning) {
TH2F* hist = new TH2F(histName, histTitle, varXBinning.nBins, varXBinning.min,
varXBinning.max, varYBinning.nBins, varYBinning.min,
varYBinning.max);
hist->GetXaxis()->SetTitle(varXBinning.title.c_str());
hist->GetYaxis()->SetTitle(varYBinning.title.c_str());
TH2F* hist =
new TH2F(histName, histTitle, varXBinning.nBins(), varXBinning.data(),
varYBinning.nBins(), varYBinning.data());
hist->GetXaxis()->SetTitle(varXBinning.title().c_str());
hist->GetYaxis()->SetTitle(varYBinning.title().c_str());
hist->Sumw2();
return hist;
}
Expand Down Expand Up @@ -70,16 +70,16 @@ void anaHisto(TH1D* inputHist, int j, TH1F* meanHist, TH1F* widthHist) {

TEfficiency* bookEff(const char* effName, const char* effTitle,
const Binning& varBinning) {
TEfficiency* efficiency = new TEfficiency(effName, effTitle, varBinning.nBins,
varBinning.min, varBinning.max);
TEfficiency* efficiency =
new TEfficiency(effName, effTitle, varBinning.nBins(), varBinning.data());
return efficiency;
}

TEfficiency* bookEff(const char* effName, const char* effTitle,
const Binning& varXBinning, const Binning& varYBinning) {
TEfficiency* efficiency = new TEfficiency(
effName, effTitle, varXBinning.nBins, varXBinning.min, varXBinning.max,
varYBinning.nBins, varYBinning.min, varYBinning.max);
effName, effTitle, varXBinning.nBins(), varXBinning.data(),
varYBinning.nBins(), varYBinning.data());
return efficiency;
}

Expand All @@ -96,10 +96,10 @@ void fillEff(TEfficiency* efficiency, float xValue, float yValue, bool status) {
TProfile* bookProf(const char* profName, const char* profTitle,
const Binning& varXBinning, const Binning& varYBinning) {
TProfile* prof =
new TProfile(profName, profTitle, varXBinning.nBins, varXBinning.min,
varXBinning.max, varYBinning.min, varYBinning.max);
prof->GetXaxis()->SetTitle(varXBinning.title.c_str());
prof->GetYaxis()->SetTitle(varYBinning.title.c_str());
new TProfile(profName, profTitle, varXBinning.nBins(), varXBinning.data(),
varYBinning.low(), varYBinning.high());
prof->GetXaxis()->SetTitle(varXBinning.title().c_str());
prof->GetYaxis()->SetTitle(varYBinning.title().c_str());
return prof;
}

Expand Down
6 changes: 3 additions & 3 deletions Examples/Framework/src/Validation/ResPlotTool.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2019 CERN for the benefit of the Acts project
// Copyright (C) 2019-2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -226,7 +226,7 @@ void ActsExamples::ResPlotTool::refinement(
for (unsigned int parID = 0; parID < Acts::eBoundSize; parID++) {
std::string parName = m_cfg.paramNames.at(parID);
// refine the plots vs eta
for (int j = 1; j <= bEta.nBins; j++) {
for (int j = 1; j <= static_cast<int>(bEta.nBins()); j++) {
TH1D* temp_res = resPlotCache.res_vs_eta.at(parName)->ProjectionY(
Form("%s_projy_bin%d", "Residual_vs_eta_Histo", j), j, j);
PlotHelpers::anaHisto(temp_res, j,
Expand All @@ -241,7 +241,7 @@ void ActsExamples::ResPlotTool::refinement(
}

// refine the plots vs pT
for (int j = 1; j <= bPt.nBins; j++) {
for (int j = 1; j <= static_cast<int>(bPt.nBins()); j++) {
TH1D* temp_res = resPlotCache.res_vs_pT.at(parName)->ProjectionY(
Form("%s_projy_bin%d", "Residual_vs_pT_Histo", j), j, j);
PlotHelpers::anaHisto(temp_res, j, resPlotCache.resMean_vs_pT.at(parName),
Expand Down
22 changes: 21 additions & 1 deletion Examples/Python/src/Output.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2021 CERN for the benefit of the Acts project
// Copyright (C) 2021-2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -145,6 +145,26 @@ void addOutput(Context& ctx) {
ACTS_PYTHON_STRUCT_END();
}

// Bindings for the binning in e.g., CKFPerformanceWriter
{
py::class_<PlotHelpers::Binning>(mex, "Binning")
.def(py::init<std::string, int, double, double>(), "title"_a, "bins"_a,
"bMin"_a, "bMax"_a)
.def(py::init<std::string, std::vector<double>>(), "title"_a, "bins"_a);

py::class_<EffPlotTool::Config>(mex, "EffPlotToolConfig")
.def(py::init<std::map<std::string, PlotHelpers::Binning>>(),
"varBinning"_a);

py::class_<FakeRatePlotTool::Config>(mex, "FakeRatePlotToolConfig")
.def(py::init<std::map<std::string, PlotHelpers::Binning>>(),
"varBinning"_a);

py::class_<DuplicationPlotTool::Config>(mex, "DuplicationPlotToolConfig")
.def(py::init<std::map<std::string, PlotHelpers::Binning>>(),
"varBinning"_a);
}

// ROOT WRITERS
ACTS_PYTHON_DECLARE_WRITER(ActsExamples::RootPropagationStepsWriter, mex,
"RootPropagationStepsWriter", collection, filePath,
Expand Down

0 comments on commit 3e77d7d

Please sign in to comment.