Logo ROOT   6.10/09
Reference Guide
mtbb001_fillHistos.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// Fill histograms in parallel and write them on file.
4 /// This example expresses the parallelism of the mt001_fillHistos.C tutorial
5 /// with multiprocessing techniques.
6 ///
7 /// \macro_code
8 ///
9 /// \author Danilo Piparo
10 /// \date January 2016
11 
12 // Total amount of numbers
13 const UInt_t nNumbers = 20000000U;
14 
15 // The number of workers
16 const UInt_t nThreads = 4U;
17 
18 Int_t mtbb001_fillHistos()
19 {
21  // We define our work item
22  auto workItem = [](UInt_t workerID) {
23  // One generator, file and ntuple per worker
24  TRandom3 workerRndm(workerID); // Change the seed
25  TFile f(Form("myFile_%u.root", workerID), "RECREATE");
26  TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
27  for (UInt_t i = 0; i < nNumbers; ++i) {
28  h.Fill(workerRndm.Gaus());
29  }
30  h.Write();
31  return 0;
32  };
33 
34  // Create the pool of threads
35  ROOT::TThreadExecutor pool(nThreads);
36 
37  // Fill the pool with work
38  pool.Map(workItem, ROOT::TSeqI(nThreads));
39  return 0;
40 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
Random number generator class based on M.
Definition: TRandom3.h:27
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
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,...)
double f(double x)
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:498
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66