Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mt_fillHistos.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_analysis_parallel
3/// \notebook
4/// Fill histograms in parallel and write them on file
5/// with a multithreaded approach using std::thread.
6/// This is the simplest meaningful example which shows
7/// ROOT thread awareness.
8///
9/// \macro_code
10///
11/// \date January 2016
12/// \author Danilo Piparo
13
14// Total amount of numbers
15const UInt_t nNumbers = 20000000U;
16
17// The number of workers
18const UInt_t nWorkers = 4U;
19
21{
22
23 // The first, fundamental operation to be performed in order to make ROOT
24 // thread-aware.
26
27 // We define our work item
28 auto workItem = [](UInt_t workerID) {
29 // One generator, file and ntuple per worker
30 TRandom3 workerRndm(workerID); // Change the seed
31 TFile f(Form("myFile_mt001_%u.root", workerID), "RECREATE");
32 TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
33 for (UInt_t i = 0; i < nNumbers; ++i) {
34 h.Fill(workerRndm.Gaus());
35 }
36 h.Write();
37 };
38
39 // Create the collection which will hold the threads, our "pool"
40 std::vector<std::thread> workers;
41
42 // Fill the "pool" with workers
43 for (auto workerID : ROOT::TSeqI(nWorkers)) {
44 workers.emplace_back(workItem, workerID);
45 }
46
47 // Now join them
48 for (auto &&worker : workers)
49 worker.join();
50
51 return 0;
52}
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
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()
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