# Multiplying histograms

Hello All,

I am running ROOT 6.06/02.

I would like to multiply two (h1, and h2) histograms bins by bins, and assign the value to the third (h3).
and the no of bins in h1 and h2 are not necessarily the same. So the no of bins in h3 is the smaller of bins(h1,h2).

What is the easiest way to do this?

I looked at this FAQ:
root.cern.ch/root/htmldoc/guide … ultiplying

but there is a problem running this:

## [code]a > root

| Welcome to ROOT 6.06/02 http://root.cern.ch |
| © 1995-2014, The ROOT Team |
| Built for linuxx8664gcc |
| From heads/master@v6-07-02-437-gb06340c, Mar 02 2016, 19:01:57 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

root [0] TH1F h1 = new TH1F(“h1”,“h1”,10,0,10);
root [1] h1->FillRandom(“gaus”,30);
root [2] TH1F h3 = 8
h1;
ROOT_prompt_2:1:12: error: invalid operands to binary expression (‘int’ and 'TH1F ')
TH1F h3 = 8
h1;
~^~~
root [3] [/code]

Thanks.

Hi,

you can do it with

``````TH1F h1("h1", "h1", 100, -3, 3);
h1.FillRandom("gaus",30);
h2=8*h1;``````

Thanks. But how do you do it with pointers?

I think it is a requirement that both histograms (which one wants to multiply) must have the same number of bins.

You can do it as follows:

``````TH1F *h1 = new TH1F("h1", "h1", 100, -3, 3);
h1->FillRandom("gaus",30);
TH1F *h2 = new TH1F();
*h2 = 8*(*h1)``````

Hi,

Or use hist->Scale(8) - that’s faster, operator* creates a temporary copy of the whole histogram.

Cheers, Axel.

I am facing a similar problem, i want to multiply a 2d histogram to a specific number say ‘8’ . for that i am using the following,

histo_newMC[n]->Scale(8);
but no matter i write this command or not, i get the same plots , i can’t figure out why?. please let me know if anybody knows what the problem is

I wonder, is there is any way to multiply each bin content without scaling. I would like to use Multiply(TH1F, number) inside the script, but it didn’t work out.

Cheers,
Nep

Please consider opening new topics instead of resurrecting old ones

multiply each bin content without scaling

Which part of scaling are you trying to avoid? In the end, scaling is multiplication of the bin content…

Axel.