Logo ROOT   6.07/09
Reference Guide
mp102_readNtuplesFillHistosAndFit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// \notebook -js
4 /// Read n-tuples in distinct workers, fill histograms, merge them and fit.
5 /// We express parallelism with multiprocessing as it is done with multithreading
6 /// in mt102_readNtuplesFillHistosAndFit.
7 ///
8 /// \macro_code
9 ///
10 /// \author Danilo Piparo
11 /// \date January 2016
12 
13 Int_t mp102_readNtuplesFillHistosAndFit()
14 {
15 
16  // No nuisance for batch execution
17  gROOT->SetBatch();
18 
19  //---------------------------------------
20  // Perform the operation sequentially
21  TChain inputChain("multiCore");
22  inputChain.Add("mp101_multiCore_*.root");
23  TH1F outHisto("outHisto", "Random Numbers", 128, -4, 4);
24  inputChain.Draw("r >> outHisto");
25  outHisto.Fit("gaus");
26 
27  //---------------------------------------
28  // We now go MP!
29  // TProcessExecutor offers an interface to directly process trees and chains without
30  // the need for the user to go through the low level implementation of a
31  // map-reduce.
32 
33  // We adapt our parallelisation to the number of input files
34  const auto nFiles = inputChain.GetListOfFiles()->GetEntries();
35 
36  // This is the function invoked during the processing of the trees.
37  auto workItem = [](TTreeReader & reader) {
38  TTreeReaderValue<Float_t> randomRV(reader, "r");
39  auto partialHisto = new TH1F("outHistoMP", "Random Numbers", 128, -4, 4);
40  while (reader.Next()) {
41  partialHisto->Fill(*randomRV);
42  }
43  return partialHisto;
44  };
45 
46  // Create the pool of processes
47  ROOT::TProcessExecutor workers(nFiles);
48 
49  // Process the TChain
50  auto sumHistogram = workers.ProcTree(inputChain, workItem, "multiCore");
51  sumHistogram->Fit("gaus", 0);
52 
53  return 0;
54 }
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
#define gROOT
Definition: TROOT.h:364
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
A chain is a collection of files containg TTree objects.
Definition: TChain.h:35