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