Histogram Questions

Hi All,

I have two questions involving histograms:

  1. If I have two root files (say file1.root and file2.root) filled with the exact same histograms (same trees and histograms within them. The exact bin counts of the histograms is different.), is it possible to combine the root files themselves and add each corresponding histogram? Is there any limit to how big the histograms within .root files can be?

  2. Are there any simple ways to sum the bins in a certain x-range?

Many thanks in advance. Your help is much appreciated!

use $ROOTSYS/bin/hadd to merge files containing mergeable objects like histograms and trees.
hadd -f result.root file1.root file2.root … fileN.root


Thanks Rene!


Hello, I have about thirty root files, in which there exist several subdirectories, and in the last subdirectory, there are about 1000 histograms. All of the root files have the same structure, except for the “first” subdirectory, which is the run number. So the structure is as below:

run number - sub dir 1 - sub dir 2 - … - sub dir 6 or 7 / histogram files…

What I would like to do is this: I would like to merge each histogram file (there is one histogram for each eta phi channel) among all runs. For example, say eta 5 phi 7 has an energy mean of 800 for run 1. This energy mean is 820 for run 2… and so on until the last run. This “merging” is to be done in such a way that I want to be able to extract the mean of each histogram, and plot run number vs. energy mean, for each channel.I tried hadd -f result.root root1.root root2.root etc., but it is not what I want.

Thank you in advance.


Somehow, you are asking us to repair your design mistake ::slight_smile:
Why did you create a directory per run if you have only one run in the top directory? It would have been much simpler to include the run number in the file name. In this case the automatic tools like hadd will have worked immediatly.
On the same design issue, did you consider using a Tree instead of your hierarchy of directories?
Anyhow, there is certainly a solution to your problem. Most of us are in India for a conference and we will look to your problem once we are back early March.


Thank you for the reply. The point is that I did not design the root files! :slight_smile: The root files were created by DQM,


I could only try saving a copy of the python file located there:

(after “installing” DQM); and modifying it. But it does not work, I could not do it.

Is there a way of changing the structure of a root file after it is created?
Also, if I am able to use “hadd” somehow, does it retain previous information, so will I be able to plot run number vs. energy mean for each channel, as I mentioned in my previous post?

[quote]Also, if I am able to use “hadd” somehow, does it retain previous information, so will I be able to plot run number vs. energy mean for each channel,[/quote]When it is useable, hadd does preserve information ; however it can not be used in your case since you need to not only merge histogram but also displace them based on some information (essentially) encoded in the directory name (i.e. there is no way for a generic tool to know what to do).

What you will need to do is write a small script (possibly inspired from the code in $ROOTSYS/main/src/hadd.cxx or $ROOTSYS/tutorials/io/hadd.C) which will retrieve the histogram from the file as you need them and accumulate the ‘matching’ histogram together.

[quote]Is there a way of changing the structure of a root file after it is created?[/quote]Usually the simplest (and most space efficient) is to read the data from the old file and write a new file with the correct structure.


Dear Philippe,

First of all, thank you very much for the effort. I really appreciate it. I wrote a small code to take the output from the root files to a text file. The rest was easy. So the problem is solved. Thank you all for your kind answers to my problem.

Melih Ozbek