Logo ROOT   6.12/07
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 #include "TH1D.h"
11 #include "TH2D.h"
12 #include "TH3D.h"
13 #include "TList.h"
14 #include "TRandom3.h"
15 #include "TDirectory.h"
16 #include "TROOT.h"
17 #include "TCanvas.h"
18 #include "TString.h"
19 #include "TStyle.h"
20 #include "ROOT/TSeq.hxx"
21 #include "ROOT/TThreadedObject.hxx"
22 
23 #include <thread>
24 #include <iostream>
25 
26 // The number of workers
27 const UInt_t nWorkers = 8U;
28 
29 // Reference boundaries
30 const Double_t xmiref = -1.;
31 const Double_t xmaref = 7.;
32 
33 Int_t mt304_fillHistos(UInt_t nNumbers = 1001)
34 {
35 
36  // The first, fundamental operation to be performed in order to make ROOT
37  // thread-aware.
39 
40  // Histograms to be filled in parallel
41  ROOT::TThreadedObject<TH1D> h1d("h1d", "1D test histogram", 64, 0., -1.);
42  ROOT::TThreadedObject<TH1D> h1dr("h1dr", "1D test histogram w/ ref boundaries", 64, xmiref, xmaref);
43 
44  // We define our work item
45  auto workItem = [&](UInt_t workerID) {
46  // One generator, file and ntuple per worker
47  TRandom3 workerRndm(workerID); // Change the seed
48 
49  auto wh1d = h1d.Get();
50  wh1d->SetBit(TH1::kAutoBinPTwo);
51  auto wh1dr = h1dr.Get();
52 
53  Double_t x;
54  for (UInt_t i = 0; i < nNumbers; ++i) {
55  x = workerRndm.Gaus(3.);
56  wh1d->Fill(x);
57  wh1dr->Fill(x);
58  }
59  };
60 
61  // Create the collection which will hold the threads, our "pool"
62  std::vector<std::thread> workers;
63 
64  // Fill the "pool" with workers
65  for (auto workerID : ROOT::TSeqI(nWorkers)) {
66  workers.emplace_back(workItem, workerID);
67  }
68 
69  // Now join them
70  for (auto &&worker : workers)
71  worker.join();
72 
73  // Merge
74  auto fh1d = h1d.Merge();
75  auto fh1dr = h1dr.Merge();
76 
77  // Make the canvas
78  TCanvas *c = new TCanvas("c", "c", 800, 800);
79  c->Divide(1, 2);
80 
81  gStyle->SetOptStat(111110);
82  c->cd(1);
83  fh1d->DrawCopy();
84  c->cd(2);
85  fh1dr->DrawCopy();
86 
87  c->Update();
88 
89  gROOTMutex = 0;
90 
91  return 0;
92 }
Random number generator class based on M.
Definition: TRandom3.h:27
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
A wrapper to make object instances thread private, lazily.
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
Double_t x[n]
Definition: legend1.C:17
Use Power(2)-based algorithm for autobinning.
Definition: TH1.h:169
unsigned int UInt_t
Definition: RtypesCore.h:42
The Canvas class.
Definition: TCanvas.h:31
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:528
double Double_t
Definition: RtypesCore.h:55
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1153
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:1266
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248