TAxis::SetRange
or the mouse (clicking on the axis).
TH1::GetMaximumBin
if you don't want to loop at the bins yourself.
For zooming an histogram you can use TAxis::SetRange(firstbin,last bin)
or TAxis::SetRangeUser(x1,x2)
. You can also do it with the GUI by selecting the axis range with the mouse.
#include "TH1.h" #include "TRandom.h" #include "TPad.h" #include <iostream> void exerciseHistogram1() { TH1D * h1 = new TH1D("h1","flat+gaus histogram",200,0,10); for (int i = 0; i < 10000; ++i) { h1->Fill( gRandom->Uniform(0,10) ); } for (int i = 0; i < 1000; ++i) { h1->Fill( gRandom->Gaus(5,0.2) ); } h1->Draw(); double ibinMax = h1->GetMaximumBin(); std::cout << "Bin with maximum is " << ibinMax << " at x = " << h1->GetBinCenter(ibinMax) << std::endl; std::cout << "Maximum Content is " << h1->GetBinContent(ibinMax) << " +/- " << h1->GetBinError(ibinMax) << std::endl; TAxis * axis = h1->GetXaxis(); axis->SetRange( axis->FindBin(4.0001), axis->FindBin(5.9999) ); // add or subtract a small eps to avoid being at the edge of the bin // axis->SetRangeUser(4.,6. ); // alternativly you can use SetRangeUser gPad->Update(); }
TH1::GetBinContent
on the original histogram and TH1::SetBinContent
on the new histogram. How many bins has the new histogram ?
TAxis:FindBin
for this. Then you create the sub-histogram using as lower value for the axis, the lower edge of the bin corresponding to 4, and, as upper value, the upper edge of the bin corresponding to 6. Afterwards, by looping on the bins, you can copy the bin content from the original histogram into the new one. It is better to use a slightly value larger than 4 (e.g. 4.0001) and a value a little bit smaller than 6 (e.g. 5.999) to avoid the bin edges.
#include "TH1.h" #include "TRandom.h" #include "TPad.h" #include <iostream> void exerciseHistogram1b() { TH1D * h1 = new TH1D("h1","flat+gaus histogram",200,0,10); for (int i = 0; i < 10000; ++i) { h1->Fill( gRandom->Uniform(0,10) ); } for (int i = 0; i < 1000; ++i) { h1->Fill( gRandom->Gaus(5,0.2) ); } h1->Draw(); double ibinMax = h1->GetMaximumBin(); std::cout << "Bin with maximum is " << ibinMax << " at x = " << h1->GetBinCenter(ibinMax) << std::endl; std::cout << "Maximum Content is " << h1->GetBinContent(ibinMax) << " +/- " << h1->GetBinError(ibinMax) << std::endl; TAxis * axis = h1->GetXaxis(); int ifirst = axis->FindBin(4.001); int ilast = axis->FindBin(5.9999); int nbins = ilast - ifirst; TH1D * h2 = new TH1D("h2","flat+gaus zoomed histogram",nbins, axis->GetBinLowEdge(ifirst), axis->GetBinUpEdge(ilast)); std::cout << " Number of bins of zoomed histogram is " << nbins << std::endl; for (int i1 = ifirst; i1 <= ilast; ++i1) { int i2 = i1 - ifirst + 1; //std::cout << " setting content for bin " << i2 << " from " << i1 << " c = " << h1->GetBinContent(i1) << std::endl; h2->SetBinContent( i2, h1->GetBinContent(i1) ); } h2->Draw(); gPad->Update(); // to check if we did not screw-up, we recompute maximum position double ibinMax2 = h2->GetMaximumBin(); std::cout << "Bin with maximum is " << ibinMax2 << " at x = " << h2->GetBinCenter(ibinMax2) << std::endl; }
TH1::Add
TH1::Scale
.
TH1::Add
h1->Draw("E")
). Do the error make sense ? If not, how can you get the correct bin errors ?
TH1::Add
to add the two histogram.
TH1::Scale(10000/ 100000)
to re-normalise the histogram.
TH1::Add
to subtract the histogram, but with a second coefficient equal to -1. (TH1::Add(h1,h2,1,-1)
).
TH1::Sumw2=
before doing the operations on the histograms (i.e. before scaling and before subtracting them)
#include "TH1.h" #include "TRandom.h" #include "TCanvas.h" #include "TLine.h" #include <iostream> void exerciseHistogram2() { TH1D * h1 = new TH1D("h1","flat histogram",100,0,10); for (int i = 0; i < 10000; ++i) { h1->Fill( gRandom->Uniform(0,10) ); } TH1D * h2 = new TH1D("h2","gaus histogram",100,0,10); for (int i = 0; i < 1000; ++i) { h2->Fill( gRandom->Gaus(5,1) ); } TH1D * h3 = new TH1D("h3","flat+gaus histogram",100,0,10); h3->Add(h1,h2); h3->Draw(); TH1D * h4 = new TH1D("h4","second flat histogram",100,0,10); for (int i = 0; i < 100000; ++i) { h4->Fill( gRandom->Uniform(0,10) ); } // renormalize histogram //!! VERY IMPORTANT - NEED TO CALL Sumw2() to get right ERRORS!!! h4->Sumw2(); h3->Sumw2(); h4->Scale(0.1); // plot in a new Canvas new TCanvas("c2","c2"); TH1D * h5 = new TH1D("h5","(flat+gaus) histogram - flat histogram",100,0,10); h5->Add(h3,h4,1.,-1.); h5->Draw("E"); TLine * line = new TLine(0,0,10,0); line->Draw(); }
TH1::Rebin
with ngroup=4
.
new TCanvas("c2","c2"); TH1 * h6 = h5->Rebin(4,"h6"); h6->SetTitle("Rebinned histogram"); h6->Draw();
x = u
and y=w+0.5*u
for filling the histogram.
Plot the histogram using color boxes (See documentation in THistPainter class) or choose what-ever option you prefer
After having filled the histogram compute the correlation using TH1::GetCorrelationFactor
.
#include "TH2.h" #include "TProfile.h" #include "TRandom.h" #include "TCanvas.h" #include <iostream> void exerciseHistogram3() { TH2D * h2d = new TH2D("h2d","2d histogram",40,-5,5, 40, -5, 5); for (int i = 0; i < 100000; ++i) { double u = gRandom->Gaus(0,1); double w = gRandom->Gaus(0,1); double x = u; double y = w + 0.5 * u; h2d->Fill( x,y ); } h2d->Draw("COLZ"); std::cout << "correlation factor " << h2d->GetCorrelationFactor() << std::endl; }
TH1::ProjectionX
and for making the profile call TH1::ProfileX
For dividing the canvas call TCanvas::Divide(1,2)
and navigate in the pad contained in the canvas by calling TCanvas::cd(pad_number)
.
TH1 * hx = h2d->ProjectionX(); TH1 * px = h2d->ProfileX(); TCanvas * c2 = new TCanvas("c2","c2"); // divide in 2 pad in x and one in y c2->Divide(2,1); c2->cd(1); hx->Draw(); c2->cd(2); px->Draw();