Logo ROOT   6.16/01
Reference Guide
mt304_fillHistos.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_multicore
3/// Fill histograms in parallel with automatic binning.
4/// Illustrates use of power-of-two autobin algorithm
5///
6/// \macro_code
7///
8/// \author Gerardo Ganis
9/// \date November 2017
10
11// The number of workers
12const UInt_t nWorkers = 8U;
13
14// Reference boundaries
15const Double_t xmiref = -1.;
16const Double_t xmaref = 7.;
17
18Int_t mt304_fillHistos(UInt_t nNumbers = 1001)
19{
20
21 // The first, fundamental operation to be performed in order to make ROOT
22 // thread-aware.
24
25 // Histograms to be filled in parallel
26 ROOT::TThreadedObject<TH1D> h1d("h1d", "1D test histogram", 64, 0., -1.);
27 ROOT::TThreadedObject<TH1D> h1dr("h1dr", "1D test histogram w/ ref boundaries", 64, xmiref, xmaref);
28
29 // We define our work item
30 auto workItem = [&](UInt_t workerID) {
31 // One generator, file and ntuple per worker
32 TRandom3 workerRndm(workerID); // Change the seed
33
34 auto wh1d = h1d.Get();
35 wh1d->SetBit(TH1::kAutoBinPTwo);
36 auto wh1dr = h1dr.Get();
37
38 Double_t x;
39 for (UInt_t i = 0; i < nNumbers; ++i) {
40 x = workerRndm.Gaus(3.);
41 wh1d->Fill(x);
42 wh1dr->Fill(x);
43 }
44 };
45
46 // Create the collection which will hold the threads, our "pool"
47 std::vector<std::thread> workers;
48
49 // Fill the "pool" with workers
50 for (auto workerID : ROOT::TSeqI(nWorkers)) {
51 workers.emplace_back(workItem, workerID);
52 }
53
54 // Now join them
55 for (auto &&worker : workers)
56 worker.join();
57
58 // Merge
59 auto fh1d = h1d.Merge();
60 auto fh1dr = h1dr.Merge();
61
62 // Make the canvas
63 auto c = new TCanvas("c", "c", 800, 800);
64 c->Divide(1, 2);
65
66 gStyle->SetOptStat(111110);
67 c->cd(1);
68 fh1d->DrawCopy();
69 c->cd(2);
70 fh1dr->DrawCopy();
71
72 c->Update();
73
74 return 0;
75}
#define c(i)
Definition: RSha256.hxx:101
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
double Double_t
Definition: RtypesCore.h:55
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
A wrapper to make object instances thread private, lazily.
The Canvas class.
Definition: TCanvas.h:31
@ kAutoBinPTwo
Use Power(2)-based algorithm for autobinning.
Definition: TH1.h:169
Random number generator class based on M.
Definition: TRandom3.h:27
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1444
Double_t x[n]
Definition: legend1.C:17
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:545