Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mt_fillNtuples.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
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///
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 mt_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("mt101_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)
76 worker.join();
77
78 return 0;
79}
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