Combining two or more histograms

I would like to combine two or more histograms and display the sum in one histogram, in order to get more entries. I’m not sure how I would go about doing this. Thanks for your help.

Not sure to understand your request. I suggest to have a look at class THStack and tutorials at $ROOTSYS/tutorils/hist/hstack.C


Ok. Here’s a better explanation: To combine or add two histograms you use the hs.Add(h3) command.

My question is this: If you have two or more different histograms with the same name (let’s say h1) in different folders (his1, his2, his2, etc.) how would you use the Add command to combine the different histograms (the h1’s)? Thanks for your help.

Anyone? Thanks.


the object name in the file (or directory) does not necessarily represent the histogram’s name in C++. You can reuse the same C++ variable and assign it a different object read from file, e.g.

TH1 *h = 0; file->GetObject("dir1/hist", h); DoSomething(h); file->GetObject("dir2/hist", h); DoSomething(h);

In your case:

TH1 *h = 0; file->GetObject("dir1/hist", h); TH1 *hadd = 0; file->GetObject("dir2/hist", hadd); h->Add(hadd); file->GetObject("dir3/hist", hadd); h->Add(hadd);

Cheers, Axel.

Thanks for your reply. When you say file->GetObject("dir1/hist", h); do you mean to enter in the directory and histogram (in bold) as histos->GetObject(“his1/h1”, h)?