acgc99
March 4, 2022, 9:11pm
1
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
linev
March 5, 2022, 8:46am
2
Hi,
Did you try to normalize histograms after filling?
Regards,
Sergey
acgc99
March 5, 2022, 9:07am
3
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");
acgc99
March 5, 2022, 11:44am
5
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.
acgc99
March 5, 2022, 1:31pm
7
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.
acgc99
March 7, 2022, 9:09am
9
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).
couet
March 7, 2022, 11:58am
12
Default option for AddEntry is lpf
use on `fas you want to see on the filled area attributes.
system
Closed
March 21, 2022, 11:59am
13
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.