-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[hist] Add tutorial for profile histograms #23243
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-profile
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.
+123
−1
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
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,90 @@ | ||
| /// \file | ||
| /// \ingroup tutorial_histv7 | ||
| /// | ||
| /// Profile histograms with RProfile. | ||
| /// | ||
| /// \macro_code | ||
| /// \macro_output | ||
| /// | ||
| /// \date July 2026 | ||
| /// \author The ROOT Team | ||
|
|
||
| #include <ROOT/RBinIndex.hxx> | ||
| #include <ROOT/RProfile.hxx> | ||
| #include <ROOT/RRegularAxis.hxx> | ||
|
|
||
| #include <cstddef> | ||
| #include <iostream> | ||
| #include <random> | ||
|
|
||
| void hist010_RProfile() | ||
| { | ||
| // Create an axis that can be used for multiple histograms. | ||
| ROOT::Experimental::RRegularAxis axis(40, {0.0, 20.0}); | ||
|
|
||
| // Create a profile histogram and fill with random values. | ||
| ROOT::Experimental::RProfile profile(40, {0.0, 20.0}); | ||
|
|
||
| std::mt19937 gen; | ||
| // Create a first normal distribution with mean 10.0 and stddev 4.0. | ||
| std::normal_distribution normal1(10.0, 4.0); | ||
| // Create a second normal distribution for the value. | ||
| std::normal_distribution normal2(40.0, 10.0); | ||
| for (std::size_t i = 0; i < 1000; i++) { | ||
| double x = normal1(gen); | ||
| double v = normal2(gen); | ||
| profile.Fill(x, v); | ||
| } | ||
|
|
||
| // Print (some of) the global statistics. | ||
| std::cout << "entries = " << profile.GetNEntries() << "\n"; | ||
| std::cout << "binned mean = " << profile.ComputeMean(0); | ||
| std::cout << ", stddev = " << profile.ComputeStdDev(0); | ||
| std::cout << "\n"; | ||
|
|
||
| // "Draw" the entries of the profile histogram with ASCII characters. The height is hard-coded to work for this | ||
| // tutorial. | ||
| for (int row = 8; row > 0; row--) { | ||
| auto print = [&](ROOT::Experimental::RBinIndex bin) { | ||
| const auto &content = profile.GetBinContent(bin); | ||
| static constexpr int Scale = 10; | ||
| std::cout << (content.fSum >= (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"; | ||
| } | ||
|
|
||
| std::cout << "\n"; | ||
| std::cout << "value mean = " << profile.ComputeMean(1); | ||
| std::cout << ", stddev = " << profile.ComputeStdDev(1); | ||
| std::cout << "\n"; | ||
|
|
||
| auto printBin = [&](ROOT::Experimental::RBinIndex bin) { | ||
| const auto &content = profile.GetBinContent(bin); | ||
| std::cout << "entries = " << content.fSum << ", mean = " << content.ComputeMean(); | ||
| }; | ||
|
|
||
| std::cout << "underflow bin: "; | ||
| printBin(ROOT::Experimental::RBinIndex::Underflow()); | ||
| std::cout << "\n"; | ||
|
|
||
| std::cout << "bin #5: "; | ||
| printBin(5); | ||
| std::cout << "\n"; | ||
|
|
||
| std::cout << "bin #10: "; | ||
| printBin(10); | ||
| 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I wasn't sure what to print. Initially I wanted to do "errors", but there are several definitions so I kept it to the minimum... Let me know if you would like to see something else printed.