Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 }