Logo ROOT  
Reference Guide
mt101_fillNtuples.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Fill n-tuples in distinct workers.

This tutorial illustrates the basics of how it's possible with ROOT to offload heavy operations on multiple threads and how it's possible to write simultaneously multiple files. The operation performed in this case is the creation of random gaussian numbers. NOTE: this code can be executed in a macro, ACLiC'ed or not, but not yet at the command line prompt.

// Some useful constants and functions
// Total amount of numbers
const UInt_t nNumbers = 20000000U;
// The number of workers
const UInt_t nWorkers = 4U;
// We split the work in equal parts
const auto workSize = nNumbers / nWorkers;
// A simple function to fill ntuples randomly
void fillRandom(TNtuple &ntuple, TRandom3 &rndm, UInt_t n)
for (auto i : ROOT::TSeqI(n))
Int_t mt101_fillNtuples()
// No nuisance for batch execution
// Perform the operation sequentially ---------------------------------------
// Create a random generator and and Ntuple to hold the numbers
TRandom3 rndm(1);
TFile ofile("mt101_singleCore.root", "RECREATE");
TNtuple randomNumbers("singleCore", "Random Numbers", "r");
fillRandom(randomNumbers, rndm, nNumbers);
// We now go MT! ------------------------------------------------------------
// The first, fundamental operation to be performed in order to make ROOT
// thread-aware.
// We define our work item
auto workItem = [](UInt_t workerID) {
// One generator, file and ntuple per worker
TRandom3 workerRndm(workerID); // Change the seed
TFile ofile(Form("mt101_multiCore_%u.root", workerID), "RECREATE");
TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
fillRandom(workerRandomNumbers, workerRndm, workSize);
return 0;
// Create the collection which will hold the threads, our "pool"
std::vector<std::thread> workers;
// Fill the "pool" with workers
for (auto workerID : ROOT::TSeqI(nWorkers)) {
workers.emplace_back(workItem, workerID);
// Now join them
for (auto &&worker : workers)
return 0;
int Int_t
Definition: RtypesCore.h:45
unsigned int UInt_t
Definition: RtypesCore.h:46
#define gROOT
Definition: TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2468
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:67
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:169
Random number generator class based on M.
Definition: TRandom3.h:27
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:274
const Int_t n
Definition: legend1.C:16
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
Definition: TROOT.cxx:493
January 2016
Danilo Piparo

Definition in file mt101_fillNtuples.C.