# Scale the Xaxis

Dear Rootrs,
I am trying to read a histogram and scale the x-axis, then plot both the scaled and unscaled one

``````Double_t ScaleX(Double_t x)

{

Double_t v;

v = 1000 * x + 0; // "linear scaling" function example

**return** v;

}

Double_t ScaleY(Double_t y)

{

Double_t v;

v = 20 * y + 200; // "linear scaling" function example

**return** v;

}

Double_t ScaleZ(Double_t z)

{

Double_t v;

v = 30 * z + 300; // "linear scaling" function example

**return** v;

}

**void** ScaleAxis(TAxis *a, Double_t (*Scale)(Double_t))

{

**if** (!a) **return** ; // just a precaution

**if** (a->GetXbins()->GetSize())

{

// an axis with variable bins

// note: bins must remain in increasing order, hence the "Scale"

// function must be strictly (monotonically) increasing

TArrayD X(*(a->GetXbins()));

**for** (Int_t i = 0; i < X.GetSize(); i++) X[i] = Scale(X[i]);

a->Set((X.GetSize() - 1), X.GetArray()); // new Xbins

}

**else**

{

// an axis with fix bins

// note: we modify Xmin and Xmax only, hence the "Scale" function

// must be linear (and Xmax must remain greater than Xmin)

a->Set( a->GetNbins(),

Scale(a->GetXmin()), // new Xmin

Scale(a->GetXmax()) ); // new Xmax

}

**return** ;

}

**void** ScaleXaxis(TH1 *h, Double_t (*Scale)(Double_t))

{

**if** (!h) **return** ; // just a precaution

ScaleAxis(h->GetXaxis(), Scale);

**return** ;

}

**void** ScaleYaxis(TH1 *h, Double_t (*Scale)(Double_t))

{

**if** (!h) **return** ; // just a precaution

ScaleAxis(h->GetYaxis(), Scale);

**return** ;

}

**void** ScaleZaxis(TH1 *h, Double_t (*Scale)(Double_t))

{

**if** (!h) **return** ; // just a precaution

ScaleAxis(h->GetZaxis(), Scale);

**return** ;

}
**void** fitting() {
TCanvas *c = new TCanvas("c","C");
c->Divide(1,2);

sprintf (fname,"Ba_Bi.txt");
output1.open(fname);

//.......Read the root file and creat canvase.....................................................
TH1F * h0 = new TH1F("h0","h0",4096, 0,4096);
TH1F * h1 = new TH1F("h1","h1",4096, 0,4096);
TH1F * h2 = new TH1F("h2","h2",4096, 0,4096);
TH1F * h2_calibrated = new TH1F("h2_calibrated","h2_calibrated",409600, 0,409600);

TFile *myfile = new TFile("Ba_Bi.root", "read");
h0        =(TH1F*)myfile->Get("Ba_Bi_0");
h1        =(TH1F*)myfile->Get("Ba_Bi_1");
h2        =(TH1F*)myfile->Get("Ba_Bi_2");
h2_calibrated        =(TH1F*)myfile->Get("Ba_Bi_2");

ScaleXaxis(h2_calibrated, ScaleX);

c->cd(1);
h2->Draw();

c->cd(2);
h2_calibrated->Draw();
h2_calibrated->ResetStats();
}
``````

This code should read the histogram 2 twice and calibrate one of them, the final plot should be two histograms one uncalibrated (h2) and one calibrated (h2_calibrated).
but what I got is that both histograms are calibrated calibration.pdf (28.0 KB)

cheers

``````TH1F *h2; myfile->GetObject("Ba_Bi_2", h2);