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.
const UInt_t nNumbers = 20000000U;
const auto workSize = nNumbers / nWorkers;
{
for (
auto i :
ROOT::TSeqI(
n))
ntuple.Fill(rndm.Gaus());
}
Int_t mt101_fillNtuples()
{
TFile ofile(
"mt101_singleCore.root",
"RECREATE");
TNtuple randomNumbers(
"singleCore",
"Random Numbers",
"r");
fillRandom(randomNumbers, rndm, nNumbers);
randomNumbers.Write();
ofile.Close();
auto workItem = [](
UInt_t workerID) {
TFile ofile(
Form(
"mt101_multiCore_%u.root", workerID),
"RECREATE");
TNtuple workerRandomNumbers(
"multiCore",
"Random Numbers",
"r");
fillRandom(workerRandomNumbers, workerRndm, workSize);
workerRandomNumbers.Write();
return 0;
};
std::vector<std::thread> workers;
for (
auto workerID :
ROOT::
TSeqI(nWorkers)) {
workers.emplace_back(workItem, workerID);
}
for (auto &&worker : workers)
worker.join();
return 0;
}
char * Form(const char *fmt,...)
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
A simple TTree restricted to a list of float variables only.
Random number generator class based on M.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
- Date
- January 2016
- Author
- Danilo Piparo
Definition in file mt101_fillNtuples.C.