Skip to content

[PWGDQ] HistogramManager: resolve each class's histograms once, not on every fill - #18971

Merged
axti98 merged 1 commit into
AliceO2Group:masterfrom
ktf:pr18971
Nov 30, 2026
Merged

axti98 merged 1 commit into
AliceO2Group:masterfrom
ktf:pr18971

Conversation

@ktf

@ktf ktf commented Nov 28, 2026

Copy link
Copy Markdown
Member

FillHistClass looked up the class's list by name, then walked it with a
TIter and dynamic_cast every histogram to find its type, on every call. In
the dielectron trains 814771-3 and 814961-3 this was 35-39% of the CPU.

The histograms of a class, the variables each one is filled with and its
dimension are now resolved on the first fill of the class and kept; adding
a histogram or changing the main list clears the cache. The fill itself is
unchanged.

The 814771 workflow on one of its input files (2 GB, arm64 Mac):
401 s -> 159 s user CPU, the same peak memory, and an AnalysisResults.root
with all 1803 TH1/TProfile and 63 THn identical bin by bin (contents,
errors, entries, statistics).

@github-actions

Copy link
Copy Markdown

O2 linter results: ❌ 0 errors, ⚠️ 28 warnings, 🔕 0 disabled

@alibuild

Copy link
Copy Markdown
Collaborator

Error while checking build/O2Physics/code-check for 7d18ac0 at 2026-11-28 15:21:

## sw/BUILD/O2Physics-code-check-latest/log
--
========== List of issues found ==========
++ echo 'To reproduce the results below locally, run: '\''aliBuild build O2Physics-code-check -e ALIBUILD_BASE_HASH=upstream/master'\''.'
To reproduce the results below locally, run: 'aliBuild build O2Physics-code-check -e ALIBUILD_BASE_HASH=upstream/master'.
++ echo 'To apply all automatic fixes, add the '\''-e O2PHYSICS_CHECKER_FIX=1'\'' option.'
To apply all automatic fixes, add the '-e O2PHYSICS_CHECKER_FIX=1' option.
++ echo 'See the documentation for more details at: <https://aliceo2group.github.io/analysis-framework/docs/tools/#running-with-o2physics-code-check>.'
See the documentation for more details at: <https://aliceo2group.github.io/analysis-framework/docs/tools/#running-with-o2physics-code-check>.
++ echo 'Found 10 errors and 0 warnings.'
Found 10 errors and 0 warnings.
++ [[ 10 -gt 0 ]]
++ cat /sw/BUILD/023c6131def1c472c0a87c90dc374e7ff0c4e0e1/O2Physics-code-check/errors.txt
PWGDQ/Core/HistogramManager.cxx:800:40: error: no header providing "std::string_view" is directly included [misc-include-cleaner,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:831:48: error: no header providing "std::move" is directly included [misc-include-cleaner,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:882:29: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:897:24: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:915:29: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:922:24: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:940:29: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:947:24: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.cxx:961:18: error: do not use static_cast to downcast from a base to a derived class; use dynamic_cast instead [cppcoreguidelines-pro-type-static-cast-downcast,-warnings-as-errors]
PWGDQ/Core/HistogramManager.h:124:5: error: no header providing "size_t" is directly included [misc-include-cleaner,-warnings-as-errors]
++ [[ 0 -gt 0 ]]
++ [[ 10 -gt 0 ]]
++ exit 1
--

Full log here.

@axti98 axti98 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Giulio, thanks a lot for this change, this is a very nice improvement!

In principal I am fine with this implementation, I just have a question on the architecture: You store the histograms in the a std::vector where the histogram is a TObject*. Later you downcast each entry via a static_cast. This is for sure a possible solution, but have you considered whether one could cache the already casted pointers? Then we could avoid the not-so-fancy static_casts.

//
// the histograms of a class, cast and paired with their variables on the first fill of the class
//
if (auto it = fHistsToFill.find(std::string_view(className)); it != fHistsToFill.end()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add header for string_view in HistogramManager.cxx

Comment thread PWGDQ/Core/HistogramManager.cxx Resolved
Comment thread PWGDQ/Core/HistogramManager.h Resolved
Comment thread PWGDQ/Core/HistogramManager.cxx Resolved
Comment thread PWGDQ/Core/HistogramManager.cxx Resolved
Comment thread PWGDQ/Core/HistogramManager.cxx Resolved
Comment thread PWGDQ/Core/HistogramManager.cxx Resolved
…n every fill

FillHistClass looked up the class's list by name, then walked it with a
TIter and dynamic_cast every histogram to find its type, on every call. In
the dielectron trains 814771-3 and 814961-3 this was 35-39% of the CPU.

The histograms of a class, the variables each one is filled with and its
dimension are now resolved on the first fill of the class and kept; adding
a histogram or changing the main list clears the cache. The fill itself is
unchanged.

The 814771 workflow on one of its input files (2 GB, arm64 Mac):
401 s -> 159 s user CPU, the same peak memory, and an AnalysisResults.root
with all 1803 TH1/TProfile and 63 THn identical bin by bin (contents,
errors, entries, statistics).
@ktf

ktf commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@axti98 thank you. I have pushed a new version with most of your comments addressed.

One could provide an update which uses and std::variant<> to avoid the static_casts. It would be way more complex PR, and I suggest to do it in a second iteration.

Moreover, I would consider moving everything to the Framework provided histogram registry, if you are open to large changes.

@ktf
ktf requested a review from axti98 November 29, 2026 16:16
@axti98

axti98 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

@ktf

Okay if it would be too complex we can leave it like this for now.

@axti98
axti98 merged commit 9ba4d7b into AliceO2Group:master Nov 30, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants