Logo ROOT   6.08/07
Reference Guide
mtbb101_fillNtuples.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// Fill n-tuples in distinct workers.
4 /// This tutorial illustrates the basics of how it's possible with ROOT to
5 /// offload heavy operations on multiple processes and how it's possible to write
6 /// simultaneously multiple files. The operation performed in this case is the
7 /// creation of random gaussian numbers.
8 ///
9 /// \macro_code
10 ///
11 /// \author Danilo Piparo
12 /// \date January 2016
13 
14 // Some useful constants and functions
15 
16 // Total amount of numbers
17 const UInt_t nNumbers = 20000000U;
18 
19 // The number of workers
20 const UInt_t nThreads = 4U;
21 
22 // We split the work in equal parts
23 const auto workSize = nNumbers / nThreads;
24 
25 // A simple function to fill ntuples randomly
26 void fillRandom (TNtuple & ntuple, TRandom3 & rndm, UInt_t n)
27 {
28  for (auto i : ROOT::TSeqI(n)) ntuple.Fill(rndm.Gaus());
29 }
30 
31 Int_t mtbb101_fillNtuples()
32 {
34  // No nuisance for batch execution
35  gROOT->SetBatch();
36 
37  // Perform the operation sequentially ---------------------------------------
38 
39  // Create a random generator and and Ntuple to hold the numbers
40  TRandom3 rndm(1);
41  TFile ofile("mp101_singleCore.root", "RECREATE");
42  TNtuple randomNumbers("singleCore", "Random Numbers", "r");
43  fillRandom(randomNumbers, rndm, nNumbers);
44  randomNumbers.Write();
45  ofile.Close();
46 
47  // We now go MP! ------------------------------------------------------------
48 
49  // We define our work item
50  auto workItem = [](UInt_t workerID) {
51  // One generator, file and ntuple per worker
52  TRandom3 workerRndm(workerID); // Change the seed
53  TFile ofile(Form("mp101_multiCore_%u.root", workerID), "RECREATE");
54  TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
55  fillRandom(workerRandomNumbers, workerRndm, workSize);
56  workerRandomNumbers.Write();
57  return 0;
58  };
59 
60  // Create the pool of workers
61  ROOT::TThreadExecutor pool(nThreads);
62 
63  // Fill the pool with work
64  pool.Map(workItem, ROOT::TSeqI(nThreads));
65 
66  return 0;
67 
68 }
Random number generator class based on M.
Definition: TRandom3.h:29
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:235
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
#define gROOT
Definition: TROOT.h:364
int Int_t
Definition: RtypesCore.h:41
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
void fillRandom(std::vector< T > &randomV, const rangeType theRangeType)
Definition: stressVdt.cxx:98
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:498
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:170
const Int_t n
Definition: legend1.C:16