-
Couldn't load subscription status.
- Fork 1.4k
[hist] Add tutorial for weighted filling #20178
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
Open
hahnjo
wants to merge
2
commits into
root-project:master
Choose a base branch
from
hahnjo:hist-tutorial-weighted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,119 @@ | ||
| /// \file | ||
| /// \ingroup tutorial_histv7 | ||
| /// | ||
| /// Weighted filling of RHist and RBinWithError bin content type. | ||
| /// | ||
| /// \macro_code | ||
| /// \macro_output | ||
| /// | ||
| /// \date October 2025 | ||
| /// \author The ROOT Team | ||
|
|
||
| #include <ROOT/RBinIndex.hxx> | ||
| #include <ROOT/RBinWithError.hxx> | ||
| #include <ROOT/RHist.hxx> | ||
| #include <ROOT/RRegularAxis.hxx> | ||
| #include <ROOT/RWeight.hxx> | ||
|
|
||
| #include <cstddef> | ||
| #include <iostream> | ||
| #include <random> | ||
| #include <variant> | ||
|
|
||
| // It is currently not possible to directly draw an RHist, so this function implements an output with ASCII characters. | ||
| static void DrawHistogram(const ROOT::Experimental::RHist<double> &hist) | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // Get the axis object from the histogram. | ||
| auto &axis = std::get<ROOT::Experimental::RRegularAxis>(hist.GetAxes()[0]); | ||
|
|
||
| // Print (some of) the global statistics. | ||
| std::cout << "entries = " << hist.GetNEntries(); | ||
| std::cout << ", mean = " << hist.ComputeMean(); | ||
| std::cout << ", stddev = " << hist.ComputeStdDev(); | ||
| std::cout << "\n"; | ||
|
|
||
| // "Draw" the histogram with ASCII characters. The height is hard-coded to work for this tutorial. | ||
| for (int row = 15; row > 0; row--) { | ||
| auto print = [&](ROOT::Experimental::RBinIndex bin) { | ||
| auto value = hist.GetBinContent(bin); | ||
| static constexpr int Scale = 100; | ||
| std::cout << (value >= (row * Scale) ? '*' : ' '); | ||
| }; | ||
|
|
||
| // First the underflow bin, separated by a vertical bar. | ||
| print(ROOT::Experimental::RBinIndex::Underflow()); | ||
| std::cout << '|'; | ||
|
|
||
| // Now iterate the normal bins and print a '*' if the value is sufficiently large. | ||
| for (auto bin : axis.GetNormalRange()) { | ||
| print(bin); | ||
| } | ||
|
|
||
| // Finally the overflow bin after a separating vertical bar. | ||
| std::cout << '|'; | ||
| print(ROOT::Experimental::RBinIndex::Overflow()); | ||
| std::cout << "\n"; | ||
| } | ||
| } | ||
|
|
||
| void hist002_RHist_weighted() | ||
| { | ||
| // Create an axis that can be used for multiple histograms. | ||
| ROOT::Experimental::RRegularAxis axis(40, {0.0, 20.0}); | ||
|
|
||
| // Create two histograms, one of which will be filled with weighted entries. | ||
| ROOT::Experimental::RHist<double> hist1({axis}); | ||
| ROOT::Experimental::RHist<double> hist2({axis}); | ||
|
|
||
| // Create a normal distribution with mean 10.0 and stddev 5.0. | ||
| std::mt19937 gen; | ||
| std::normal_distribution normal(10.0, 5.0); | ||
| for (std::size_t i = 0; i < 25000; i++) { | ||
| hist1.Fill(normal(gen)); | ||
| double value = normal(gen); | ||
| double weight = 0.2 + 0.008 * value * value; | ||
| hist2.Fill(value, ROOT::Experimental::RWeight(weight)); | ||
| } | ||
|
|
||
| // "Draw" the histograms with ASCII characters. | ||
| std::cout << "hist1 with expected mean = " << normal.mean() << "\n"; | ||
| DrawHistogram(hist1); | ||
| std::cout << "\n"; | ||
|
|
||
| std::cout << "hist2 with distorted normal distribution\n"; | ||
| DrawHistogram(hist2); | ||
| std::cout << "\n"; | ||
|
|
||
| // Create and fill a third histogram with the special RBinWithError bin content type. | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // In addition to the sum of weights, it tracks the sum of weights squared to compute the bin errors. | ||
| ROOT::Experimental::RHist<ROOT::Experimental::RBinWithError> hist3({axis}); | ||
| for (std::size_t i = 0; i < 25000; i++) { | ||
| double value = normal(gen); | ||
| double weight = 0.2 + 0.008 * value * value; | ||
| hist3.Fill(value, ROOT::Experimental::RWeight(weight)); | ||
| } | ||
|
|
||
| // Visualize the computed bin errors with ASCII characters. | ||
| std::cout << "bin errors of hist3 (not to scale)\n"; | ||
| for (int row = 15; row > 0; row--) { | ||
| auto print = [&](ROOT::Experimental::RBinIndex bin) { | ||
| auto error = std::sqrt(hist3.GetBinContent(bin).fSum2); | ||
| static constexpr int Scale = 5; | ||
| std::cout << (error >= (row * Scale) ? '*' : ' '); | ||
| }; | ||
|
|
||
| // First the underflow bin, separated by a vertical bar. | ||
| print(ROOT::Experimental::RBinIndex::Underflow()); | ||
| std::cout << '|'; | ||
|
|
||
| // Now iterate the normal bins and print a '*' if the value is sufficiently large. | ||
| for (auto bin : axis.GetNormalRange()) { | ||
| print(bin); | ||
| } | ||
|
|
||
| // Finally the overflow bin after a separating vertical bar. | ||
| std::cout << '|'; | ||
| print(ROOT::Experimental::RBinIndex::Overflow()); | ||
| std::cout << "\n"; | ||
| } | ||
| } | ||
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,8 +1,9 @@ | ||
| \defgroup tutorial_histv7 Histogram tutorials | ||
| \defgroup tutorial_histv7 RHist tutorials | ||
| \ingroup tutorial_hist | ||
|
|
||
| Examples demonstrating ROOT's histogram package. | ||
|
|
||
| | **Tutorial** | **Description** | | ||
| |---|---| | ||
| | hist001_RHist_basics.C | Basics of RHist, including filling and adding them. | | ||
| | hist002_RHist_weighted.C | Weighted filling of RHist and RBinWithError bin content type. | |
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.