Logo ROOT  
Reference Guide
mt102_readNtuplesFillHistosAndFit.C File Reference

Detailed Description

View in nbviewer Open in SWAN Read n-tuples in distinct workers, fill histograms, merge them and fit.

Knowing that other facilities like TProcessExecutor might be more adequate for this operation, this tutorial complements mc101, reading and merging. We convey another message with this tutorial: the synergy of ROOT and STL algorithms is possible.

(int) 0
Int_t mt102_readNtuplesFillHistosAndFit()
// No nuisance for batch execution
// Perform the operation sequentially ---------------------------------------
TChain inputChain("multiCore");
TH1F outHisto("outHisto", "Random Numbers", 128, -4, 4);
inputChain.Draw("r >> outHisto");
// We now go MT! ------------------------------------------------------------
// The first, fundamental operation to be performed in order to make ROOT
// thread-aware.
// We adapt our parallelisation to the number of input files
const auto nFiles = inputChain.GetListOfFiles()->GetEntries();
// We define the histograms we'll fill
std::vector<TH1F> histograms;
auto workerIDs = ROOT::TSeqI(nFiles);
for (auto workerID : workerIDs) {
histograms.emplace_back(TH1F(Form("outHisto_%u", workerID), "Random Numbers", 128, -4, 4));
// We define our work item
auto workItem = [&histograms](UInt_t workerID) {
TFile f(Form("mt101_multiCore_%u.root", workerID));
auto ntuple = f.Get<TNtuple>("multiCore");
auto &histo = histograms.at(workerID);
for (auto index : ROOT::TSeqL(ntuple->GetEntriesFast())) {
TH1F sumHistogram("SumHisto", "Random Numbers", 128, -4, 4);
// Create the collection which will hold the threads, our "pool"
std::vector<std::thread> workers;
// Spawn workers
// Fill the "pool" with workers
for (auto workerID : workerIDs) {
workers.emplace_back(workItem, workerID);
// Now join them
for (auto &&worker : workers)
// And reduce with a simple lambda
std::for_each(std::begin(histograms), std::end(histograms),
[&sumHistogram](const TH1F &h) { sumHistogram.Add(&h); });
sumHistogram.Fit("gaus", 0);
return 0;
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:45
unsigned int UInt_t
Definition: RtypesCore.h:46
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2452
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:67
A chain is a collection of files containing TTree objects.
Definition: TChain.h:33
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:169
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5612
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:493
TSeq< int > TSeqI
Definition: TSeq.hxx:203
January 2016
Danilo Piparo

Definition in file mt102_readNtuplesFillHistosAndFit.C.