Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mt101_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 threads 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/// NOTE: this code can be executed in a macro, ACLiC'ed or not, but not yet at
10/// the command line prompt.
11///
12/// \macro_code
13///
14/// \date January 2016
15/// \author Danilo Piparo
16
17// Some useful constants and functions
18
19// Total amount of numbers
20const UInt_t nNumbers = 20000000U;
21
22// The number of workers
23const UInt_t nWorkers = 4U;
24
25// We split the work in equal parts
26const auto workSize = nNumbers / nWorkers;
27
28// A simple function to fill ntuples randomly
29void fillRandom(TNtuple &ntuple, TRandom3 &rndm, UInt_t n)
30{
31 for (auto i : ROOT::TSeqI(n))
32 ntuple.Fill(rndm.Gaus());
33}
34
35Int_t mt101_fillNtuples()
36{
37
38 // No nuisance for batch execution
39 gROOT->SetBatch();
40
41 // Perform the operation sequentially ---------------------------------------
42
43 // Create a random generator and and Ntuple to hold the numbers
44 TRandom3 rndm(1);
45 TFile ofile("mt101_singleCore.root", "RECREATE");
46 TNtuple randomNumbers("singleCore", "Random Numbers", "r");
47 fillRandom(randomNumbers, rndm, nNumbers);
48 randomNumbers.Write();
49 ofile.Close();
50
51 // We now go MT! ------------------------------------------------------------
52
53 // The first, fundamental operation to be performed in order to make ROOT
54 // thread-aware.
56
57 // We define our work item
58 auto workItem = [](UInt_t workerID) {
59 // One generator, file and ntuple per worker
60 TRandom3 workerRndm(workerID); // Change the seed
61 TFile ofile(Form("mt101_multiCore_%u.root", workerID), "RECREATE");
62 TNtuple workerRandomNumbers("multiCore", "Random Numbers", "r");
63 fillRandom(workerRandomNumbers, workerRndm, workSize);
64 workerRandomNumbers.Write();
65 return 0;
66 };
67
68 // Create the collection which will hold the threads, our "pool"
69 std::vector<std::thread> workers;
70
71 // Fill the "pool" with workers
72 for (auto workerID : ROOT::TSeqI(nWorkers)) {
73 workers.emplace_back(workItem, workerID);
74 }
75
76 // Now join them
77 for (auto &&worker : workers)
78 worker.join();
79
80 return 0;
81}
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
Definition TROOT.cxx:501
TSeq< int > TSeqI
Definition TSeq.hxx:203