Logo ROOT  
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 /// \date January 2016
11 /// \author Danilo Piparo
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  if (inputChain.GetNtrees() <= 0) {
24  Printf(" No files in the TChain: did you run mp101_fillNtuples.C before?");
25  return 1;
26  }
27  TH1F outHisto("outHisto", "Random Numbers", 128, -4, 4);
28  inputChain.Draw("r >> outHisto");
29  outHisto.Fit("gaus");
30 
31  //---------------------------------------
32  // We now go MP!
33  // TProcessExecutor offers an interface to directly process trees and chains without
34  // the need for the user to go through the low level implementation of a
35  // map-reduce.
36 
37  // We adapt our parallelisation to the number of input files
38  const auto nFiles = inputChain.GetListOfFiles()->GetEntries();
39 
40  // This is the function invoked during the processing of the trees.
41  auto workItem = [](TTreeReader &reader) {
42  TTreeReaderValue<Float_t> randomRV(reader, "r");
43  auto partialHisto = new TH1F("outHistoMP", "Random Numbers", 128, -4, 4);
44  while (reader.Next()) {
45  partialHisto->Fill(*randomRV);
46  }
47  return partialHisto;
48  };
49 
50  // Create the pool of processes
51  ROOT::TTreeProcessorMP workers(nFiles);
52 
53  // Process the TChain
54  auto sumHistogram = workers.Process(inputChain, workItem, "multiCore");
55  sumHistogram->Fit("gaus", 0);
56 
57  return 0;
58 }
TTreeReaderValue< Float_t >
Int_t
int Int_t
Definition: RtypesCore.h:45
ROOT::TTreeProcessorMP
This class provides an interface to process a TTree dataset in parallel with multi-process technology...
Definition: TTreeProcessorMP.hxx:38
TTreeReader
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:44
Printf
void Printf(const char *fmt,...)
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
TChain
A chain is a collection of files containing TTree objects.
Definition: TChain.h:33
gROOT
#define gROOT
Definition: TROOT.h:406