Adding two 1d profile histograms

Re: the subject line,

Should be simple, right?
I thought so, but I don’t get the results I expect, and I’m wondering if someone could tell me what I’m doing wrong.

I have attached the root file with the two operands and the result, as well as a “.C” script that reads the two operands, performs the same operation again and puts all three on a canvas.

Thanks for any assistance,
-Phil D.
testaddprofiles.root (11.3 KB)
test.C (956 Bytes)

Hi Phil,

What is the difference between what you see and what you expected?
Note that your 2 profile have very different weight (Operand1 is 34 times ‘larger’ and hence completely dominate the operation). (i.e maybe you were expecting the result of sumtp->Add(tpop1,tpop2,-tpop2->GetEntries()/tpop1->GetEntries(),1.0):wink:

Cheers,
Philippe.

[quote=“pcanal”]Hi Phil,

What is the difference between what you see and what you expected?
Note that your 2 profile have very different weight (Operand1 is 34 times ‘larger’ and hence completely dominate the operation). (i.e maybe you were expecting the result of sumtp->Add(tpop1,tpop2,-tpop2->GetEntries()/tpop1->GetEntries(),1.0):wink:

Cheers,
Philippe.[/quote]

Why yes, I guess that was exactly what I expected, given the documentation!!! (or lack thereof).

Aaagh! This has to be one of the most poorly documented “features” of root, how math is done with TProfile. It says nothing about this in Chapter 3 of the Reference Guide (as of March of this year), section on profile histograms, and it says nothing about this on the page for TProfile on the root website - in fact it is quite misleading when it says that “Add” performs
this = this + c1*h1

I suspect also that an important clue is provided when it says this is like merging two profiles with weights c1 and c2, except then that the process of merging two profiles also is never explained.

But thanks for the guru tip, Philippe, “worlds have opened up for me now”, and I can pass it on to others that I know have given up on doing such a thing…

Oh, and if there is in fact a documentation source where this is plainly discussed that I should add to my toolbelt, please let me know!

Thanks much and regards,
-Phil D.

[quote=“pcanal”]Hi Phil,

What is the difference between what you see and what you expected?
Note that your 2 profile have very different weight (Operand1 is 34 times ‘larger’ and hence completely dominate the operation). (i.e maybe you were expecting the result of sumtp->Add(tpop1,tpop2,-tpop2->GetEntries()/tpop1->GetEntries(),1.0):wink:

Cheers,
Philippe.[/quote]

I stand corrected, this does NOT work!!! I implemented exactly this statement in the attached test.C file and generated the plot now attached, and you see that the “subtraction” yields about half of the expected result per bin.

I looked at the source code for TProfile2D::Add and I think I see why - the weighting is per entries in the bin. There’s no way to specify a single weight to use with “Add” across the whole histogram and make it work. So my take on it is this function as it stands will never be able to give you the linear combination of means across all the bins. One has to create a different type of histogram or TGraph and do it manually, bin by bin.

Of course I would love to be corrected on this point…
Regards,
-Phil D.


Hi,

TProfile::Add is currently implemented like a merge, the scaling by the coefficient c1 and c2 is done both on the sum of the y content and on the number of entries.
I guess what you want is doing a linear combination on the bin content (the plotted profile values). The simplest way to do this is projecting the profile in an histogram by calling TProfile::ProjectionX() and do the additions using the projected histograms.

Best Regards
Lorenzo