How to draw a "SUM" of all THStack histograms

VIVE L’AMOUR!
I’d like to draw a “SUM” of all THStack histograms. How?
A pitiful case, am I not?
Pepe Le Pew.

1 Like

Hi,

Something like this (taking tutorials/hist/hstack.C as reference):

TList *histos = hs->GetHists(); TH1F *sum = new TH1F("sum","sum of histograms",100,-4,4); TIter next(histos); TH1F *hist; while ((hist =(TH1F*)next())) { cout << "Adding " << hist->GetName() << endl; sum->Add(hist); } TCanvas *c3 = new TCanvas("c3"); sum->Draw();
Cheers, Bertrand.

VIVE L’AMOUR!
many thanks for your reply. :slight_smile:
Of course I know I can sum them up manually (and that’s what I do now). :wink:
The purpose of my post was to trigger a discussion among ROOT developers … maybe you could add a new “special option” to the THStack “Draw” method (and possibly also to TMultiGraph), called “SUM”. 8-[
I am stupid. No?
Pepe Le Pew.

I do not understand your question.
By default THStack::Draw draw draws the sum of all histograms in the stack.

Rene

VIVE L’AMOUR!
many thanks for your reply. :smiley:
The default behavior of the THStack::Draw is to draw ALL histograms in a THStack, every “next” on the shoulders of the “previous” one. :unamused:
In this sense, the very last drawn histogram is the “sum”, but you get ALL of them drawn in your canvas.
The idea of the “SUM” option would be to “add” them first (with proper “bin contents”, “errors”, “weights” sums) and draw the “proper sum” ONLY. :bulb:
I am stupid. No?
Pepe Le Pew.

If you have a THStack *hs, simply do
hs->GetStack()->Last()->Draw();

The GetStack function returns a TObjArray* of TH1*
where the TH1 at index i is the sum of histograms 0->i.
TObjArray::Last() returns the last TH1 in the list, hence the sum of all TH1.

Rene

WOW :open_mouth:

[quote=“brun”]If you have a THStack *hs, simply do
hs->GetStack()->Last()->Draw();[/quote]
Are the partial “errors” / “weights” / “weights2” summed up properly, too :question:

yes, they are.

Rene

1 Like

VIVE L’AMOUR! \:D/
GREAT, THANKS =D> :mrgreen:
One question remains, though … why was I doing all the summing myself :question: ](*,)
I am stupid. No?
Pepe Le Pew.
P.S. Could you, please, add this information to the THStack::Draw method description (and to the THistPainter description), where you describe the “NOSTACK” option. O:)