Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mt001_fillHistos.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_multicore
3/// \notebook
4/// Fill histograms in parallel and write them on file.
5/// The simplest meaningful possible example which shows ROOT thread awareness.
6///
7/// \macro_code
8///
9/// \date January 2016
10/// \author Danilo Piparo
11
12// Total amount of numbers
13const UInt_t nNumbers = 20000000U;
14
15// The number of workers
16const UInt_t nWorkers = 4U;
17
18Int_t mt001_fillHistos()
19{
20
21 // The first, fundamental operation to be performed in order to make ROOT
22 // thread-aware.
24
25 // We define our work item
26 auto workItem = [](UInt_t workerID) {
27 // One generator, file and ntuple per worker
28 TRandom3 workerRndm(workerID); // Change the seed
29 TFile f(Form("myFile_mt001_%u.root", workerID), "RECREATE");
30 TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
31 for (UInt_t i = 0; i < nNumbers; ++i) {
32 h.Fill(workerRndm.Gaus());
33 }
34 h.Write();
35 };
36
37 // Create the collection which will hold the threads, our "pool"
38 std::vector<std::thread> workers;
39
40 // Fill the "pool" with workers
41 for (auto workerID : ROOT::TSeqI(nWorkers)) {
42 workers.emplace_back(workItem, workerID);
43 }
44
45 // Now join them
46 for (auto &&worker : workers)
47 worker.join();
48
49 return 0;
50}
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
char * Form(const char *fmt,...)
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
Random number generator class based on M.
Definition TRandom3.h:27
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition TROOT.cxx:493
TSeq< int > TSeqI
Definition TSeq.hxx:201