Skip to content
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

Normalise bin content by bin width #127

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ namespace plotIt {
bool no_data = false;
bool override = false; // flag to plot only those which have it true (if at least one plot has it true)
bool normalized = false;
bool normalizedByBinWidth = false;
bool log_y = false;
bool log_x = false;

Expand Down
21 changes: 19 additions & 2 deletions include/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,25 @@ namespace plotIt {
float binSize = object->GetXaxis()->GetBinWidth(1);
std::string title = plot.y_axis;

boost::format formatter = get_formatter(plot.y_axis_format);
object->GetYaxis()->SetTitle((formatter % title % binSize).str().c_str());
bool isEquidistantBinning = true;
for(int i = 2; i <= object->GetXaxis()->GetNbins(); ++i) {
if(object->GetXaxis()->GetBinWidth(1)!=object->GetXaxis()->GetBinWidth(i))
isEquidistantBinning = false;
}

if(isEquidistantBinning){
boost::format formatter = get_formatter(plot.y_axis_format);
object->GetYaxis()->SetTitle((formatter % title % binSize).str().c_str());
}
else if(plot.normalizedByBinWidth){
std::string format_string = "%1%/%2%";
boost::format formatter = get_formatter(format_string);
object->GetYaxis()->SetTitle((formatter % title % "Bin width").str().c_str());
}
else{
boost::format formatter = get_formatter(plot.y_axis_format);
object->GetYaxis()->SetTitle((formatter % title % binSize).str().c_str());
}
}

if (plot.show_ratio && object->GetXaxis())
Expand Down
25 changes: 25 additions & 0 deletions src/TH1Plotter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,31 @@ namespace plotIt {
computeSystematics(mc_stacks, global_summary);
}

if (plot.normalizedByBinWidth) {
// Normalize each plot
for (auto& file: m_plotIt.getFiles()) {
if (file.type == SIGNAL) {
TH1* h = dynamic_cast<TH1*>(file.object);
h->Scale(1.,"width");
}
}

if (h_data.get()) {
h_data->Scale(1.,"width");
}

std::for_each(mc_stacks.begin(), mc_stacks.end(), [](TH1Plotter::Stacks::value_type& value) {
TIter next(value.second.stack->GetHists());
TH1* h = nullptr;
while ((h = static_cast<TH1*>(next()))) {
h->Scale(1.,"width");
}
value.second.stat_and_syst->Scale(1.,"width");
value.second.syst_only->Scale(1.,"width");
value.second.stat_only->Scale(1.,"width");
});
}

// Store all the histograms to draw, and find the one with the highest maximum
std::vector<std::pair<TObject*, std::string>> toDraw = { std::make_pair(h_data.get(), data_drawing_options) };
for (File& signal: signal_files) {
Expand Down
3 changes: 3 additions & 0 deletions src/plotIt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ namespace plotIt {
if (node["normalized"])
plot.normalized = node["normalized"].as<bool>();

if (node["normalizedByBinWidth"])
plot.normalizedByBinWidth = node["normalizedByBinWidth"].as<bool>();

if (node["no-data"])
plot.no_data = node["no-data"].as<bool>();

Expand Down