# Thread safe fill of same tree and histograms from different synchronized threads

**URL:** <https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258>\
**Category:** ROOT\
**Tags:** tree, hist\
**Created:** [June 11, 2023, 7:58am UTC](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258 "2023-06-11T07:58:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhsaren](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jhsaren/32/33617_2.png) [@jhsaren](https://root-forum.cern.ch/u/jhsaren)\
**Post date:** [June 11, 2023, 7:58am UTC](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258/1 "2023-06-11T07:58:27Z")

</div>

Hi,

_Introduction._ I have written a presort c++ analysis library to our triggerless data-acquisition system (600+ channels). I would like to also use optionally Root histograms and trees there. The natural way to increase the throughput is to use a pipeline scheme. I’m using currently taskflow library. In that, there are many worker threads accessing the same resources but of course synchronizing the threads perfectly in between. So no two threads accessing the same resources at same time.

_Question._ Are the Root histograms and tree thread safe so that I can access those from fully synchronized different threads at different times i.e. with using mutexes or similar. I have to ask this since the Root object seem to have complicated states and they interact with other global objects behind the scenes. The question can be asked also differently: Are histograms or Tree having a thread local data?

Of course, I will create the histograms and trees in one thread before going to parallel pipeline and save histograms to root file after the execution of pipeline in one single thread. Filling copies of histograms and merging them afterwards is not working solution here due to long time correlations (i.e. data must be handled in time order).

Best wishes,  
Jan

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 13, 2023, 6:27am UTC](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258/2 "2023-06-13T06:27:56Z")

</div>

Welcome to the ROOT Forum! Maybe @moneta can hep

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 13, 2023, 5:20pm UTC](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258/3 "2023-06-13T17:20:59Z")

</div>

Individual objects of type `TTree` or `TH1*` (or `TFile`) can not n be accessed from multiple thread as the same time. However once you turn on thread support in ROOT (`ROOT::EnableThreadSafety()`) distinct objects of those types can be access concurrently.

I.e. what you described will work but you might also be able to achieve even better thread scaling by using (in some cases) distinct objects rather than sharing them with several thread (We also have an infrastructure (`TBufferMerger`) that allows the merging of concurrently produced `TTree` and `TH*`

---

<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:** [June 27, 2023, 5:21pm UTC](https://root-forum.cern.ch/t/thread-safe-fill-of-same-tree-and-histograms-from-different-synchronized-threads/55258/4 "2023-06-27T17:21:02Z")

</div>

Closed on June 27, 2023, 5:21pm UTC

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