Re: problem in histogram computations
Rene Brun (Rene.Brun@cern.ch)
Fri, 05 Feb 1999 15:56:08 +0000
M. Sievers wrote:
>
> Hello,
>
> I have tried posting this via news, but it probably has not
> reached you.
>
> I have a problem with arithmetic expressions involving
> histograms, as the name of the histograms that gets
> assigned to is overwritten.
>
> An example:
>
> *******************************************
> * *
> * W E L C O M E to R O O T *
> * *
> * Version 2.20/06 18 December 1998 *
> * *
> * You are welcome to visit our Web site *
> * http://root.cern.ch *
> * *
> *******************************************
>
> CINT/ROOT C/C++ Interpreter version 5.13.83, Dec 2 1998
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> root [0] TH1D one("hone","histo one",10,0,10);
> root [1] TH1D two("htwo","histo two",10,0,10);
> root [2] TH1D three("hthree","histo three",10,0,10);
> root [3] three.Print()
> TH1.Print Name= hthree, Total sum= 0
> root [4] three = one + two;
> root [5] three.Print()
> TH1.Print Name= hone, Total sum= 0
>
> The histogram three, with name hthree, is assigned the sum of
> one and two, and its name is *changed to that of one*.
> The reason seems to be that the TH1::Copy() calls TNamed::Copy().
> It could be argued that '=' should make an exact copy of the
> right side, but I would argue in favor of dropping the
> TNamed::Copy() call from TH1::Copy():
> 1. two objects will carry the same name from now on, if I
> write three to a file, it will overwrite 'hone'.
> 2. The process is ambiguos: one + two != two + one
> 3. Anytime I use an intuitive formula as above, I have
> to store the histogram name in some variable, do the
> computation and set the name again.
>
> It would be great if you could remove the call to TNamed::Copy()
> in future versions.
>
Hi Michael,
Apologies for the delay in answering several mails to roottalk, rootdev
and private lists.
To add histograms using the operator "+", you should not create an
object
before making the operation. In your case above, you can do directly:
The resulting histogram is automatically created in the operation.
Its name by default is the name of the first histogram.
This is simply a convention. You can use SetName immediatly after
to rename it.
You can also create your histogram "three" before the operation and
instead of using the operator "+", use directly one of the functions
TH1::Add.
Rene Brun