ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mp001_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 
11 Int_t mp001_fillHistos(UInt_t nWorkers = 4)
12 {
13 
14  // Total amount of numbers
15  const UInt_t nNumbers = 20000000U;
16 
17  // We define our work item
18  auto workItem = [nNumbers](UInt_t workerID) {
19  // One generator, file and ntuple per worker
20  TRandom3 workerRndm(workerID); // Change the seed
21  TFile f(Form("myFile_%u.root", workerID), "RECREATE");
22  TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
23  for (UInt_t i = 0; i < nNumbers; ++i) {
24  h.Fill(workerRndm.Gaus());
25  }
26  h.Write();
27  return 0;
28  };
29 
30  // Create the pool of workers
31  TProcPool workers(nWorkers);
32 
33  // Fill the pool with work
34  std::forward_list<UInt_t> workerIDs(nWorkers);
35  std::iota(std::begin(workerIDs), std::end(workerIDs), 0);
36  workers.Map(workItem, workerIDs);
37 
38  return 0;
39 
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:823
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
Random number generator class based on M.
Definition: TRandom3.h:29
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:45
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
TFile * f
This class provides a simple interface to execute the same task multiple times in parallel...
Definition: TProcPool.h:38
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)