Loading [MathJax]/jax/output/HTML-CSS/config.js
Logo ROOT   6.12/07
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
imt101_parTreeProcessing.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// \notebook
4 /// Illustrate the usage of the TTreeProcessorMT::Process method.
5 /// Such method provides an implicit parallelisation of the reading and processing of a TTree.
6 /// In particular, when invoking Process, the user provides a function that iterates on a subrange
7 /// of the tree via a TTreeReader. Multiple tasks will be spawned, one for each sub-range, so that
8 /// the processing of the tree is parallelised. Since two invocations of the user function can
9 /// potentially run in parallel, the function code must be thread safe.
10 /// The example also introduces a new class, ROOT::TThreadedObject, which makes objects
11 /// thread private. With the help of this class, histograms can be filled safely inside the
12 /// user function and then merged at the end to get the final result.
13 ///
14 /// \macro_code
15 ///
16 /// \date 26/09/2016
17 /// \author Enric Tejedor
18 
19 
20 int imt101_parTreeProcessing()
21 {
22  // First enable implicit multi-threading globally, so that the implicit parallelisation is on.
23  // The parameter of the call specifies the number of threads to use.
24  int nthreads = 4;
25  ROOT::EnableImplicitMT(nthreads);
26 
27  // Create one TThreadedObject per histogram to fill during the processing of the tree
28  ROOT::TThreadedObject<TH1F> ptHist("pt_dist","p_{T} Distribution;p_{T};dN/p_{T}dp_{T}", 100, 0, 5);
29  ROOT::TThreadedObject<TH1F> pzHist("pz_dist","p_{Z} Distribution;p_{Z};dN/dp_{Z}", 100, 0, 5);
30  ROOT::TThreadedObject<TH2F> pxpyHist("px_py","p_{X} vs p_{Y} Distribution;p_{X};p_{Y}", 100, -5., 5., 100, -5., 5.);
31 
32  // Create a TTreeProcessorMT: specify the file and the tree in it
33  ROOT::TTreeProcessorMT tp("http://root.cern.ch/files/tp_process_imt.root",
34  "events");
35 
36  // Define the function that will process a subrange of the tree.
37  // The function must receive only one parameter, a TTreeReader,
38  // and it must be thread safe. To enforce the latter requirement,
39  // TThreadedObject histograms will be used.
40  auto myFunction = [&](TTreeReader &myReader) {
41  TTreeReaderValue<std::vector<ROOT::Math::PxPyPzEVector>> tracksRV(myReader, "tracks");
42 
43  // For performance reasons, a copy of the pointer associated to this thread on the
44  // stack is used
45  auto myPtHist = ptHist.Get();
46  auto myPzHist = pzHist.Get();
47  auto myPxPyHist = pxpyHist.Get();
48 
49  while (myReader.Next()) {
50  auto tracks = *tracksRV;
51  for (auto&& track : tracks) {
52  myPtHist->Fill(track.Pt(), 1./track.Pt());
53  myPxPyHist->Fill(track.Px(), track.Py());
54 
55  myPzHist->Fill(track.Pz());
56  }
57  }
58  };
59 
60  // Launch the parallel processing of the tree
61  tp.Process(myFunction);
62 
63  // Use the TThreadedObject::Merge method to merge the thread private histograms
64  // into the final result
65  auto ptHistMerged = ptHist.Merge();
66  auto pzHistMerged = pzHist.Merge();
67  auto pxpyHistMerged = pxpyHist.Merge();
68 
69  return 0;
70 }
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:43
A wrapper to make object instances thread private, lazily.
Extracts data from a TTree.
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT&#39;s implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition: TROOT.cxx:555
A class to process the entries of a TTree in parallel.