TH1F arithmetic, sum, etc

Hi,
It would be super nice to be able to write things like

``````import ROOT as r

h1 = r.TH1F("h1", "h1", 100, 0, 1)
h2 = r.TH1F("h2", "h2", 100, 0, 2)

h3 = h1 + h2
h4 = h1*5.
h4 *= 0.2
h3 += h4
h5 = sum([h1, h2])``````

Can these sorts of things be made possible in a future ROOT version? Are they possible in extant versions with a bit of hackery?
Jon

Hi,

This kind of thing works for me, at least in ipython.

Elliott

Hi,

some of them work in as far as there are global overloads available, as well as a few special cases such as *= with a float that have been mapped explicitly (in the case of *= to the Scale() function). Not all of these have C++ equivalents available, though. You can still roll your own by overloading the various numeric member functions on THn.

Cheers,
Wim

Thanks for the replies, Wim, Elliott. What version do these work in? I’m using 5.26, and I get

``````>>> import ROOT as r
>>> h1 = r.TH1F("h1", "h1", 100, 0, 1)
****************************
* Welcome to ROOT v5.26/00 *
****************************

/brew/users/jsw/data151
>>> h1 *= 2.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *=: 'TH1F' and 'float'``````

Perhaps I simply need to move to 5.28 to make this work?
Regards,
Jon

Jon,

yes, or redefine imul on TH1F (or its base; in the .cxx code it’s added to TH1) to map to Scale().

Cheers,
Wim

[code]>>> def imul( self, scale ):
… self.Scale( scale )
… return self

import ROOT as r
r.TH1F.imul = imul
h1 = r.TH1F(“h1”, “h1”, 100, 0, 1)
h1 *= 2.0
[/code]