Logo ROOT  
Reference Guide
mtbb101_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 processes and how it's possible to write simultaneously multiple files. The operation performed in this case is the creation of random gaussian numbers.

// Some useful constants and functions
// Total amount of numbers
const UInt_t nNumbers = 20000000U;
// The number of workers
const UInt_t nThreads = 4U;
// We split the work in equal parts
const auto workSize = nNumbers / nThreads;
// A simple function to fill ntuples randomly
void fillRandom(TNtuple &ntuple, TRandom3 &rndm, UInt_t n)
{
for (auto i : ROOT::TSeqI(n))
ntuple.Fill(rndm.Gaus());
}
Int_t mtbb101_fillNtuples()
{
// No nuisance for batch execution
gROOT->SetBatch();
// Perform the operation sequentially ---------------------------------------
// Create a random generator and and Ntuple to hold the numbers
TRandom3 rndm(1);
TFile ofile("mtbb101_singleCore.root", "RECREATE");
TNtuple randomNumbers("singleCore", "Random Numbers", "r");
fillRandom(randomNumbers, rndm, nNumbers);
randomNumbers.Write();
ofile.Close();
// We now go MP! ------------------------------------------------------------
// 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("mtbb101_multiCore_%u.root", workerID), "RECREATE");
TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
fillRandom(workerRandomNumbers, workerRndm, workSize);
workerRandomNumbers.Write();
return 0;
};
// Create the pool of workers
ROOT::TThreadExecutor pool(nThreads);
// Fill the pool with work
pool.Map(workItem, ROOT::TSeqI(nThreads));
return 0;
}
Date
January 2016
Author
Danilo Piparo

Definition in file mtbb101_fillNtuples.C.

n
const Int_t n
Definition: legend1.C:16
TRandom::Gaus
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:263
TNtuple
Definition: TNtuple.h:28
Form
char * Form(const char *fmt,...)
ROOT::TThreadExecutor
This class provides a simple interface to execute the same task multiple times in parallel,...
Definition: TThreadExecutor.hxx:41
Int_t
int Int_t
Definition: RtypesCore.h:45
TRandom3
Definition: TRandom3.h:27
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TFile
Definition: TFile.h:54
ROOT::TSeq
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
TNtuple::Fill
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:169
gROOT
#define gROOT
Definition: TROOT.h:406