I have a 1D histogram. I want to stretched it along xaxis. How do I do that?
What do you mean by “stretch”?
Is the histogram already created and filled?
Maybe you just want to “scale” the x-axis?
In any case, if you want to increase the number of bins, you will need to create a completely new histogram.
Yes I have a histogram created and Filled. Rebining will just combine bins in to less number. Stretching is just like splitting into more number of bins. I want to increase x value of histogram by a constant factor.
{
TH1F *h_narrow = new TH1F("h_narrow", "h_narrow", 200, -1., 1.);
h_narrow->FillRandom("gaus");
// make sure that the new histogram has "compatible" bins' widths
TH1F *h_wide = new TH1F("h_wide", "h_wide", 1000, -5., 5.);
h_wide->Sumw2(kTRUE); // ensure that the errors are properly "transfered"
h_wide->Add(h_narrow);
// h_wide->ResetStats(); // may be needed sometimes
h_wide->Draw();
}
# include "TGraph.h"
#include "TCanvas.h"
Double_t ScaleX(Double_t x)
{
Double_t v;
v=x*1.5;
return v;
}
void ScaleAxis(TAxis *a)
{
if (!a) return;
if (a->GetXbins()->GetSize())
{
TArrayD X(*(a->GetXbins()));
for (int i = 0; i<X.GetSize(); i++) X[i] = ScaleX(X[i]);
a->Set((X.GetSize()-1),X.GetArray());
}
}
void ScaleXaxis(TH1D *h)
{
if (!h) return;
ScaleAxis(h->GetXaxis());
return;
}
void graph()
{
TH1F *h1 = new TH1F("h1","h1",100,-5,5);
TH1F *h2 = new TH1F("h2","h2",100,-5,5);
h1->FillRandom("gaus");
h2->FillRandom("gaus");
TCanvas *c =new TCanvas("c","c",1500,1500);
c->Divide(2,1);
c->cd(1);
h1->Draw();
h2->Draw("same");
c->cd(2);
ScaleXaxis(h1);
h1->ResetStats();
h1->Draw();
I think this above code should stretch my histogram. I have defined all necessary functions and stretching parameter.
Can you look why my this code is not working?
This code is supposed to draw original histogram in pad 1 and stretched one in pad 2.
Thank You