Problems with THStack

Hi,
I’m trying to plot two histograms in the same pad. Here I have the basic example:

void TEST(){
	
	TCanvas* c1 = new TCanvas("Name", "Title", 600, 600);
	
	THStack *hstack = new THStack("Name", "Title");
	
	TH1F* th1f1 = new TH1F("Name", "Title", 50, 0, TMath::Pi()/2.);
	TH1F* th1f2 = new TH1F("Name1", "Title2", 50, 0, TMath::Pi()/2);
	
	// statistical errors
	//th1f1->Sumw2();
	//th1f2->Sumw2();
	// normalize
	//th1f1->Scale(1./th1f1->GetEntries());
	//th1f2->Scale(1./th1f2->GetEntries());
	
	th1f1->SetFillColor(kBlue);
	th1f2->SetFillColor(kRed);
	
	th1f1->FillRandom("gaus",20000);
	th1f2->FillRandom("gaus",20000);
	
	hstack-> Add(th1f1);
	hstack-> Add(th1f2);
	
	hstack->Draw("nostackb");
	
}


I would like to normalize the histogram and to draw errors. If I try to add errors, uncommenting th1f1->Sumw2() and th1f2->Sumw2():

Histogram is not filled properly.

Now if I comment again that part and uncomment the one related to normalization th1f1->Scale(1./th1f1->GetEntries()) and th1f2->Scale(1./th1f2->GetEntries()) I get:


Even better.


Please read tips for efficient and successful posting and posting code

_ROOT Version:ROOT 6.24/06
Platform: Debian, Windows Subsystem for Linux
Compiler: Not Provided


Hi,

Did you try to normalize histograms after filling?

Regards,
Sergey

void TEST(){
	
	TCanvas* c1 = new TCanvas("Name", "Title", 600, 600);
	
	THStack *hstack = new THStack("Name", "Title");
	
	TH1F* th1f1 = new TH1F("Name", "Title", 50, 0, TMath::Pi()/2.);
	TH1F* th1f2 = new TH1F("Name1", "Title2", 50, 0, TMath::Pi()/2);
	
	// statistical errors
	//th1f1->Sumw2();
	//th1f2->Sumw2();
	
	th1f1->SetFillColor(kBlue);
	th1f2->SetFillColor(kRed);
	
	th1f1->FillRandom("gaus",20000);
	th1f2->FillRandom("gaus",20000);
	
	// normalize
	th1f1->Scale(1./th1f1->GetEntries());
	th1f2->Scale(1./th1f2->GetEntries());
	
	hstack-> Add(th1f1);
	hstack-> Add(th1f2);
	
	
	hstack->Draw("nostackb");
	
}

Gives the same aberrant graph.

hstack->Draw("hist nostackb"); hstack->Draw("same ex0 nostackb");

void TEST(){
	
	TCanvas* c1 = new TCanvas("Name", "Title", 600, 600);
	
	THStack *hstack = new THStack("Name", "Title");
	
	TH1F* th1f1 = new TH1F("Name", "Title", 50, 0, TMath::Pi()/2.);
	TH1F* th1f2 = new TH1F("Name1", "Title2", 50, 0, TMath::Pi()/2);
	
	// statistical errors
	//th1f1->Sumw2();
	//th1f2->Sumw2();
	
	th1f1->SetFillColor(kBlue);
	th1f2->SetFillColor(kRed);
	
	th1f1->FillRandom("gaus",20000);
	th1f2->FillRandom("gaus",20000);
	
	// normalize
	th1f1->Scale(1./th1f1->GetEntries());
	th1f2->Scale(1./th1f2->GetEntries());
	
	hstack-> Add(th1f1);
	hstack-> Add(th1f2);
	
	
	hstack->Draw("hist nostackb");
	hstack->Draw("same ex0 nostackb");
	
}

That is not what I want. I want hstack->Draw("nostackb"); style but I want it normalized and if possible with errors.

There seems something wrong with your setup (ROOT?, X11?). Try another machine.

I get the same result with Windows Subsystem for Linux:

   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/06                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 02 2021, 14:20:23                 |
  | From tags/v6-24-06@v6-24-06                                      |
  | With                                                             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

And from Windows installation, version 5:

  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.34/38     12 March 2018   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.34/38 (v5-34-38@v5-34-38, Mar 12 2018, 15:49:39 on win32)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.

In ROOT 5, if you intend to use the errors of the histogram, you must make sure that TH1::Sumw2 is “effective”, before TH1::Scale is executed.

For the “NOSTACKB option to work, you need ROOT 6.

I don’t know what happend but @Wile_E_Coyote solution works now… This is another question related. Now I add a legend:

	TLegend* legend = new TLegend(0.7, 0.75, 0.9, 0.9);
	legend->AddEntry(th1f1, "CP-even");
	legend->AddEntry(th1f2, "CP-odd");
	legend->Draw();

I would like to remove the horizontal line that appears in the coloured boxes in the legend, it is annoying (and I do not know what they do there).

Check out the example and explanation:
https://root.cern/doc/master/classTLegend.html

TLegend::AddEntry

Default option for AddEntry is lpf use on `fas you want to see on the filled area attributes.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.