From ab8c0cc22d9d9c7c3df2f1458dd0f4fc085d788f Mon Sep 17 00:00:00 2001 From: Vincenzo Eduardo Padulano Date: Sat, 12 Sep 2026 09:58:15 +0200 Subject: [PATCH] Add another graphics example And enforce usage of ROOT macros for interactive graphics examples --- course/basic/graphics/README.md | 2 +- ...as.cpp => example_01_interactive_canvas.C} | 5 -- .../example_02_histogram_live_update.C | 47 +++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) rename course/basic/graphics/{example_01_interactive_canvas.cpp => example_01_interactive_canvas.C} (90%) create mode 100644 course/basic/graphics/example_02_histogram_live_update.C diff --git a/course/basic/graphics/README.md b/course/basic/graphics/README.md index 18a0762..215a3f9 100644 --- a/course/basic/graphics/README.md +++ b/course/basic/graphics/README.md @@ -1,5 +1,5 @@ This directory contains examples using ROOT graphics interactively, i.e. creating a canvas and visualising data. Run the examples as: ``` -$: root -l .cpp +$: root -l .C ``` diff --git a/course/basic/graphics/example_01_interactive_canvas.cpp b/course/basic/graphics/example_01_interactive_canvas.C similarity index 90% rename from course/basic/graphics/example_01_interactive_canvas.cpp rename to course/basic/graphics/example_01_interactive_canvas.C index 916a01c..9ee2957 100644 --- a/course/basic/graphics/example_01_interactive_canvas.cpp +++ b/course/basic/graphics/example_01_interactive_canvas.C @@ -17,8 +17,3 @@ void example_01_interactive_canvas() { h.DrawClone(); canvas->Draw(); } - -int main() { - example_01_interactive_canvas(); - return 0; -} diff --git a/course/basic/graphics/example_02_histogram_live_update.C b/course/basic/graphics/example_02_histogram_live_update.C new file mode 100644 index 0000000..4010e56 --- /dev/null +++ b/course/basic/graphics/example_02_histogram_live_update.C @@ -0,0 +1,47 @@ +#include // ROOT colours and markers +#include +#include + +#include + +// We create a unique_ptr to a canvas at global scope to avoid losing the canvas +// object after the end of the main function +auto canvas = std::make_unique("c", "c", 800, 600); + +void example_02_histogram_live_update() { + // Generate and draw the first histogram + TH1D h_1{"h_1", "h_1", 100, 0, 10}; + h_1.SetFillColor(kGray); + TH1D h_2{"h_2", "h_2", 100, 0, 10}; + h_2.SetMarkerStyle(kFullCross); + + h_1.Draw(); + // Draw error bars and markers with E1P + // Overlay histogram on same canvas with same + h_2.Draw("E1P same"); + + std::mt19937 rng{std::random_device{}()}; + + std::normal_distribution gaus{6, 0.5}; + std::exponential_distribution exp{0.5}; + + constexpr auto n_fills{10000}; + constexpr auto n_update{10}; + for (auto i = 0; i < n_fills; i++) { + auto val_gaus = gaus(rng); + auto val_exp = exp(rng); + h_1.Fill(val_gaus); + h_1.Fill(val_exp); + h_2.Fill(val_gaus); + + if (i % n_update == 0) { + canvas->ModifiedUpdate(); + } + } + + // We draw them once more after the update loop to ensure they will survive + // past the end of the function + h_1.DrawClone(); + h_2.DrawClone(); + canvas->Draw(); +}