Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mtbb101_fillNtuples.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_multicore
3/// \notebook
4/// Fill n-tuples in distinct workers.
5/// This tutorial illustrates the basics of how it's possible with ROOT to
6/// offload heavy operations on multiple processes and how it's possible to write
7/// simultaneously multiple files. The operation performed in this case is the
8/// creation of random gaussian numbers.
9///
10/// \macro_code
11///
12/// \date January 2016
13/// \author Danilo Piparo
14
15// Some useful constants and functions
16
17// Total amount of numbers
18const UInt_t nNumbers = 20000000U;
19
20// The number of workers
21const UInt_t nThreads = 4U;
22
23// We split the work in equal parts
24const auto workSize = nNumbers / nThreads;
25
26// A simple function to fill ntuples randomly
27void fillRandom(TNtuple &ntuple, TRandom3 &rndm, UInt_t n)
28{
29 for (auto i : ROOT::TSeqI(n))
30 ntuple.Fill(rndm.Gaus());
31}
32
33Int_t mtbb101_fillNtuples()
34{
35 // No nuisance for batch execution
36 gROOT->SetBatch();
37
38 // Perform the operation sequentially ---------------------------------------
39
40 // Create a random generator and and Ntuple to hold the numbers
41 TRandom3 rndm(1);
42 TFile ofile("mtbb101_singleCore.root", "RECREATE");
43 TNtuple randomNumbers("singleCore", "Random Numbers", "r");
44 fillRandom(randomNumbers, rndm, nNumbers);
45 randomNumbers.Write();
46 ofile.Close();
47
48 // We now go MP! ------------------------------------------------------------
49
50 // We define our work item
51 auto workItem = [](UInt_t workerID) {
52 // One generator, file and ntuple per worker
53 TRandom3 workerRndm(workerID); // Change the seed
54 TFile ofile(Form("mtbb101_multiCore_%u.root", workerID), "RECREATE");
55 TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
56 fillRandom(workerRandomNumbers, workerRndm, workSize);
57 workerRandomNumbers.Write();
58 return 0;
59 };
60
61 // Create the pool of workers
62 ROOT::TThreadExecutor pool(nThreads);
63
64 // Fill the pool with work
65 pool.Map(workItem, ROOT::TSeqI(nThreads));
66
67 return 0;
68}
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
#define gROOT
Definition TROOT.h:407
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
A pseudo container class which is a generator of indices.
Definition TSeq.hxx:67
This class provides a simple interface to execute the same task multiple times in parallel threads,...
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Random number generator class based on M.
Definition TRandom3.h:27
const Int_t n
Definition legend1.C:16
Double_t Gaus(Double_t x, Double_t mean, Double_t sigma)
Gauss.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
TSeq< int > TSeqI
Definition TSeq.hxx:203