Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13Int_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}
int Int_t
Definition RtypesCore.h:45
#define gROOT
Definition TROOT.h:405
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2481
This class provides an interface to process a TTree dataset in parallel with multi-process technology...
A chain is a collection of files containing TTree objects.
Definition TChain.h:33
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:577
An interface for reading values stored in ROOT columnar datasets.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44