-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[hist] Add first basic tutorial #20025
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
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,92 @@ | ||
/// \file | ||
/// \ingroup tutorial_histv7 | ||
/// | ||
/// Basics of RHist, including filling and adding them. | ||
/// | ||
/// \macro_code | ||
/// | ||
/// \date September 2025 | ||
/// \author The ROOT Team | ||
|
||
#include <ROOT/RBinIndex.hxx> | ||
#include <ROOT/RHist.hxx> | ||
#include <ROOT/RRegularAxis.hxx> | ||
|
||
#include <cstddef> | ||
#include <iostream> | ||
#include <random> | ||
#include <variant> | ||
hahnjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// It is currently not possible to directly draw RHist's, so this function implements an output with ASCII characters. | ||
static void DrawHistogram(const ROOT::Experimental::RHist<int> &hist) | ||
{ | ||
// 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 = 10; | ||
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 hist001_RHist_basics() | ||
{ | ||
// Create an axis that can be used for multiple histograms. | ||
ROOT::Experimental::RRegularAxis axis(40, 0, 20); | ||
|
||
// Create a first histograms and fill with random values. | ||
ROOT::Experimental::RHist<int> hist1({axis}); | ||
|
||
// Create a normal distribution with mean 5.0 and stddev 2.0. | ||
std::mt19937 gen; | ||
std::normal_distribution normal1(5.0, 2.0); | ||
for (std::size_t i = 0; i < 1000; i++) { | ||
hist1.Fill(normal1(gen)); | ||
} | ||
|
||
// "Draw" the histogram with ASCII characters. | ||
std::cout << "hist1 with expected mean = " << normal1.mean() << "\n"; | ||
DrawHistogram(hist1); | ||
std::cout << "\n"; | ||
|
||
// Create a second histogram and fill with random values of a different distribution. | ||
ROOT::Experimental::RHist<int> hist2({axis}); | ||
std::normal_distribution normal2(13.0, 4.0); | ||
for (std::size_t i = 0; i < 1500; i++) { | ||
hist2.Fill(normal2(gen)); | ||
} | ||
std::cout << "hist2 with expected mean = " << normal2.mean() << "\n"; | ||
DrawHistogram(hist2); | ||
std::cout << "\n"; | ||
|
||
// Create a third, merged histogram from the two previous two. | ||
ROOT::Experimental::RHist<int> hist3({axis}); | ||
hist3.Add(hist1); | ||
hist3.Add(hist2); | ||
std::cout << "hist3 with expected entries = " << (hist1.GetNEntries() + hist2.GetNEntries()) << "\n"; | ||
DrawHistogram(hist3); | ||
} |
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,8 @@ | ||
\defgroup tutorial_histv7 Histogram tutorials | ||
\ingroup tutorial_hist | ||
|
||
Examples demonstrating ROOT's histogram package. | ||
|
||
| **Tutorial** | **Description** | | ||
|---|---| | ||
| hist001_RHist_basics.C | Basics of RHist, including filling and adding them. | |
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.