Logo ROOT   6.07/09
Reference Guide
mt101_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 threads 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 /// NOTE: this code can be executed in a macro, ACLiC'ed or not, but not yet at
9 /// the command line prompt.
10 ///
11 /// \macro_code
12 ///
13 /// \author Danilo Piparo
14 /// \date January 2016
15 
16 // Some useful constants and functions
17 
18 // Total amount of numbers
19 const UInt_t nNumbers = 20000000U;
20 
21 // The number of workers
22 const UInt_t nWorkers = 4U;
23 
24 // We split the work in equal parts
25 const auto workSize = nNumbers / nWorkers;
26 
27 // A simple function to fill ntuples randomly
28 void fillRandom (TNtuple & ntuple, TRandom3 & rndm, UInt_t n)
29 {
30  for (auto i : ROOT::TSeqI(n)) ntuple.Fill(rndm.Gaus());
31 }
32 
33 Int_t mt101_fillNtuples()
34 {
35 
36  // No nuisance for batch execution
37  gROOT->SetBatch();
38 
39  // Perform the operation sequentially ---------------------------------------
40 
41  // Create a random generator and and Ntuple to hold the numbers
42  TRandom3 rndm(1);
43  TFile ofile("mp101_singleCore.root", "RECREATE");
44  TNtuple randomNumbers("singleCore", "Random Numbers", "r");
45  fillRandom(randomNumbers, rndm, nNumbers);
46  randomNumbers.Write();
47  ofile.Close();
48 
49  // We now go MT! ------------------------------------------------------------
50 
51  // The first, fundamental operation to be performed in order to make ROOT
52  // thread-aware.
54 
55  // We define our work item
56  auto workItem = [](UInt_t workerID) {
57  // One generator, file and ntuple per worker
58  TRandom3 workerRndm(workerID); // Change the seed
59  TFile ofile(Form("mt101_multiCore_%u.root", workerID), "RECREATE");
60  TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
61  fillRandom(workerRandomNumbers, workerRndm, workSize);
62  workerRandomNumbers.Write();
63  return 0;
64  };
65 
66  // Create the collection which will hold the threads, our "pool"
67  std::vector<std::thread> workers;
68 
69  // Fill the "pool" with workers
70  for (auto workerID : ROOT::TSeqI(nWorkers)) {
71  workers.emplace_back(workItem, workerID);
72  }
73 
74  // Now join them
75  for (auto && worker : workers) worker.join();
76 
77  return 0;
78 
79 }
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