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_t mt_readNtuplesFillHistosAndFit()
{
TChain inputChain(
"multiCore");
inputChain.Add("mt101_multiCore_*.root");
TH1F outHisto(
"outHisto",
"Random Numbers", 128, -4, 4);
inputChain.Draw("r >> outHisto");
outHisto.Fit("gaus");
const auto nFiles = inputChain.GetListOfFiles()->GetEntries();
std::vector<TH1F> histograms;
histograms.reserve(nFiles);
for (auto workerID : workerIDs) {
histograms.emplace_back(
TH1F(
Form(
"outHisto_%u", workerID),
"Random Numbers", 128, -4, 4));
}
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);
histo.
Fill(ntuple->GetArgs()[0]);
}
};
TH1F sumHistogram(
"SumHisto",
"Random Numbers", 128, -4, 4);
std::vector<std::thread> workers;
for (auto workerID : workerIDs) {
workers.emplace_back(workItem, workerID);
}
for (auto &&worker : workers)
worker.join();
std::for_each(std::begin(histograms), std::end(histograms),
[&sumHistogram](
const TH1F &
h) { sumHistogram.Add(&
h); });
sumHistogram.Fit("gaus", 0);
return 0;
}
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
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
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
A chain is a collection of files containing TTree objects.
1-D histogram with a float per channel (see TH1 documentation)
A simple TTree restricted to a list of float variables only.
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
- Date
- January 2016
- Author
- Danilo Piparo
Definition in file mt_readNtuplesFillHistosAndFit.C.