Read n-tuples in distinct workers, fill histograms, merge them and fit. We express parallelism with multiprocessing as it is done with multithreading in mt102_readNtuplesFillHistosAndFit.
Author: Danilo Piparo
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, April 17, 2024 at 11:15 AM.
No nuisance for batch execution
gROOT->SetBatch();
Perform the operation sequentially
TChain inputChain("multiCore");
inputChain.Add("mp101_multiCore_*.root");
if (inputChain.GetNtrees() <= 0) {
Printf(" No files in the TChain: did you run mp101_fillNtuples.C before?");
return 1;
}
TH1F outHisto("outHisto", "Random Numbers", 128, -4, 4);
inputChain.Draw("r >> outHisto");
outHisto.Fit("gaus");
**************************************** Minimizer is Minuit2 / Migrad Chi2 = 144.198 NDf = 125 Edm = 2.66531e-10 NCalls = 56 Constant = 498648 +/- 136.722 Mean = -5.33321e-05 +/- 0.000223746 Sigma = 1.00005 +/- 0.000158841 (limited)
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
We now go MP! TProcessExecutor offers an interface to directly process trees and chains without the need for the user to go through the low level implementation of a map-reduce.
We adapt our parallelisation to the number of input files
const auto nFiles = inputChain.GetListOfFiles()->GetEntries();
This is the function invoked during the processing of the trees.
auto workItem = [](TTreeReader &reader) {
TTreeReaderValue<Float_t> randomRV(reader, "r");
auto partialHisto = new TH1F("outHistoMP", "Random Numbers", 128, -4, 4);
while (reader.Next()) {
partialHisto->Fill(*randomRV);
}
return partialHisto;
};
Create the pool of processes
ROOT::TTreeProcessorMP workers(nFiles);
Process the TChain
auto sumHistogram = workers.Process(inputChain, workItem, "multiCore");
sumHistogram->Fit("gaus", 0);
return 0;
**************************************** Minimizer is Minuit2 / Migrad Chi2 = 144.198 NDf = 125 Edm = 2.62701e-10 NCalls = 50 Constant = 498648 +/- 136.722 Mean = -5.33322e-05 +/- 0.000223746 Sigma = 1.00005 +/- 0.000158841 (limited)
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()