Logo ROOT   6.14/05
Reference Guide
mtbb001_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 /// This example expresses the parallelism of the mt001_fillHistos.C tutorial
6 /// with multiprocessing techniques.
7 ///
8 /// \macro_code
9 ///
10 /// \date January 2016
11 /// \author Danilo Piparo
12 
13 // Total amount of numbers
14 const UInt_t nNumbers = 20000000U;
15 
16 // The number of workers
17 const UInt_t nThreads = 4U;
18 
19 Int_t mtbb001_fillHistos()
20 {
22  // We define our work item
23  auto workItem = [](UInt_t workerID) {
24  // One generator, file and ntuple per worker
25  TRandom3 workerRndm(workerID); // Change the seed
26  TFile f(Form("myFile_%u.root", workerID), "RECREATE");
27  TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
28  for (UInt_t i = 0; i < nNumbers; ++i) {
29  h.Fill(workerRndm.Gaus());
30  }
31  h.Write();
32  return 0;
33  };
34 
35  // Create the pool of threads
36  ROOT::TThreadExecutor pool(nThreads);
37 
38  // Fill the pool with work
39  pool.Map(workItem, ROOT::TSeqI(nThreads));
40  return 0;
41 }
Random number generator class based on M.
Definition: TRandom3.h:27
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
This class provides a simple interface to execute the same task multiple times in parallel...
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
#define h(i)
Definition: RSha256.hxx:106
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:546
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66