Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
mp_fillNtuples.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
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 nWorkers = 4U;
22
23// We split the work in equal parts
24const auto workSize = nNumbers / nWorkers;
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 mp_fillNtuples()
34{
35
36 // No nuisance for batch execution
37 gROOT->SetBatch();
38
39 //---------------------------------------
40 // Perform the operation sequentially
41
42 // Create a random generator and and Ntuple to hold the numbers
43 TRandom3 rndm(1);
44 TFile ofile("mp101_singleCore.root", "RECREATE");
45 TNtuple randomNumbers("singleCore", "Random Numbers", "r");
46 fillRandom(randomNumbers, rndm, nNumbers);
47 randomNumbers.Write();
48 ofile.Close();
49
50 //---------------------------------------
51 // We now go MP!
52
53 // We define our work item
54 auto workItem = [](UInt_t workerID) {
55 // One generator, file and ntuple per worker
56 TRandom3 workerRndm(workerID); // Change the seed
57 TFile ofile(Form("mp101_multiCore_%u.root", workerID), "RECREATE");
58 TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
59 fillRandom(workerRandomNumbers, workerRndm, workSize);
60 workerRandomNumbers.Write();
61 return 0;
62 };
63
64 // Create the pool of workers
65 ROOT::TProcessExecutor workers(nWorkers);
66
67 // Fill the pool with work
68 workers.Map(workItem, ROOT::TSeqI(nWorkers));
69
70 return 0;
71}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
#define gROOT
Definition TROOT.h:417
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
This class provides a simple interface to execute the same task multiple times in parallel,...
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
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
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
TSeq< int > TSeqI
Definition TSeq.hxx:203