Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mt001_fillHistos.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Fill histograms in parallel and write them on file.

The simplest meaningful possible example which shows ROOT thread awareness.

// Total amount of numbers
const UInt_t nNumbers = 20000000U;
// The number of workers
const UInt_t nWorkers = 4U;
Int_t mt001_fillHistos()
{
// The first, fundamental operation to be performed in order to make ROOT
// thread-aware.
// We define our work item
auto workItem = [](UInt_t workerID) {
// One generator, file and ntuple per worker
TRandom3 workerRndm(workerID); // Change the seed
TFile f(Form("myFile_mt001_%u.root", workerID), "RECREATE");
TH1F h(Form("myHisto_%u", workerID), "The Histogram", 64, -4, 4);
for (UInt_t i = 0; i < nNumbers; ++i) {
h.Fill(workerRndm.Gaus());
}
h.Write();
};
// Create the collection which will hold the threads, our "pool"
std::vector<std::thread> workers;
// Fill the "pool" with workers
for (auto workerID : ROOT::TSeqI(nWorkers)) {
workers.emplace_back(workItem, workerID);
}
// Now join them
for (auto &&worker : workers)
worker.join();
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
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
Random number generator class based on M.
Definition TRandom3.h:27
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
Definition TROOT.cxx:501
TSeq< int > TSeqI
Definition TSeq.hxx:203
Date
January 2016
Author
Danilo Piparo

Definition in file mt001_fillHistos.C.