Multi-threaded output of TH* histograms via TBufferMerger

ROOT Version: 6.30/08
Platform: AlmaLinux 8.10
Compiler: GNU 8.5.0 via CMake 3.26.5


I am writing a program which uses TTreeProcessorMT for multi-threaded processing of an input tree and TBufferMerger for output in this multi-threaded context. I can easily create a TTree and fill it from multiple threads. This works, and I have done this a few different times already.

However, when I try to create and fill a TH1I histogram alongside the tree, it fails to appear in the output file. As far as I’m aware, I should just be able to create and fill the histogram in the same way as I do for the tree, at least this is what I would do for a normal TFile:

auto f = merger.GetFile();
TTree t(“t”, “t”);
t.Branch(“whatever”, &whatever);

TH1I h(“h”, “h”, 100, -1., 1.);

I would think it should be simple for a TBufferMerger to merge histograms from different threads by just adding them, so I am confused as to why this doesn’t work. Additionally, I found a sort of related thread which implies that this should be possible. Is there some detail that I’m missing here, or some other solution/workaround?

I created a little macro to replicate this issue, and tried to keep the general framework of how my larger program works. It requires a ROOT file as input, the file path for which is specified in the program, but it doesn’t matter what file is used.

tbuffermerger_hist.C (1.3 KB)

Random guess:
Maybe do f->cd() before creating the hist ?
or alternatively hist.SetDirectory(…)

Other thing you could try is to do hist.Write();

I tried all of these things, but the histogram still remains absent from the output file. I was able to create a subdirectory during this testing, but again the subdirectory remained empty.