# Multi-threaded output of TH\* histograms via TBufferMerger

**URL:** <https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089>\
**Category:** ROOT\
**Tags:** hist\
**Created:** [August 28, 2025, 9:59pm UTC](https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089 "2025-08-28T21:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crafter312](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crafter312/32/36890_2.png) [@crafter312](https://root-forum.cern.ch/u/crafter312)\
**Post date:** [August 28, 2025, 9:59pm UTC](https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089/1 "2025-08-28T21:59:46Z")

</div>

_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`:

```cpp
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](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258/3) 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](https://root-forum.cern.ch/uploads/short-url/monYZUajFv44FKsDETp2KbBc0lx.C) (1.3 KB)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 29, 2025, 7:18am UTC](https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089/2 "2025-08-29T07:18:15Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![crafter312](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crafter312/32/36890_2.png) [@crafter312](https://root-forum.cern.ch/u/crafter312)\
**Post date:** [August 29, 2025, 5:10pm UTC](https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089/3 "2025-08-29T17:10:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 12, 2025, 5:11pm UTC](https://root-forum.cern.ch/t/multi-threaded-output-of-th-histograms-via-tbuffermerger/64089/4 "2025-09-12T17:11:04Z")

</div>

Closed on September 12, 2025, 5:11pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
