Producing ROOT files in parallel

Hello there,

I’m attempting to generate multiple TFile’s on multiple concurrent threads of a single C++ application (I’m using std::thread). However, when I create a TTree, TCanvas or TH1 object on one thread, it automatically registers to one of the TFiles created by other threads.

Is there a way to turn this behavior off as it has no sense in a parallel context? I would like to explicitly specify the object-to-file association.

Thank you for any advice.

Cheers,
Petr

Hi Petr,

ROOT supports parallel creation of files and objects contained (1 file per thread). You can start from this tutorial, based on std threads: root.cern.ch/doc/master/mt101__ … es_8C.html
Let us know how it goes.

Cheers,
Danilo

1 Like

Thanks for the advice.

The creation works quite well. However, in several isolated cases, I am encountering buffer flushing issues. I have created a separate thread for that.

Cheers,
Petr

Ciao Danilo,
I was looking around for some info about the parallel handling of TFiles.
One thing that you write everywhere but I’m a bit confused about is that ROOT manages correctly TFiles in a MT environment as long as each thread manages one file.
What I’m a bit confused about is whether you need to use a unique TFile object per thread or if the underlying file has to be unique.
In the example you post here it seems that every worker has to have its own independent file, which is not particularly useful in most cases I’m working on while somewhere else, namely the description of DataFrame, for example, it seems that different TFiles can be connected safely to the same underline filesystem object, at least for the read operations. It is not clear whether the same would apply to a write operations but in some other forum posts read and write are always mentioned on equal footing.
Can you clarify, please, whether it is safe to write safely on different TFile objects from different threads when they are connected to the same underlying filesystem file?

Cheers
Stefano

Hi,

One thing that you write everywhere but I’m a bit confused about is that ROOT manages correctly TFiles in a MT environment as long as each thread manages one file.

That is correct. If the file is being read, the underlying file must not be unique. If it is being written, it must in general. If you need to write to the same file from different threads, it is still possible, but through TBufferMerger, which is what RDataFrame uses intrenally to Snapshot a dataset in MT mode.

I hope this helps.

Cheers,
D