Storing and adding a large number of histograms

I have a large number (about 20,000) of 1D histograms stored in a directory structure three levels deep. There seems to be a large amount of time required for writing the histograms into the directory of a root file on disk. Also, hadd takes a long time to add the histograms. How can I make the handling of these histograms more efficient? How about putting the histograms in a tree? Or should I be using arrays rather than histograms?

Hi David,

In the CVS development version I have optimized hadd such that it hehaves like nlog(n) instead of nn.

In the example below, you will find a way to create a large number of histograms and write them to a file. This technique is faster than the default option in ROOT that stores the histograms in the THashList of the current directory. In this case, all the time is spent in inserting the new histograms in the THashList. We certainly should improve teh current poor algorithm in the THashList.

Putting the histograms in a Tree is also a good solution, having one Tree entry per histogram. In this case the time to add or read a histogram will be independent of the number of histograms


void h20000() {
TFile *f = new TFile(“h20000.root”,“recreate”);
TH1F *h = new TH1F(“h”,“h”,100,-3,3);
const int nh = 20000;
TH1F hlist[nh];
TRandom r;
for (Int_t i=0;i<nh;i++) {
hlist[i] = (TH1F
for (int j=0;j<nh;j++) hlist[j]->Write();
delete f;