# Change scale of a TH1F (energy calibration)

**URL:** <https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971>\
**Category:** ROOT\
**Created:** [July 3, 2007, 4:01pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971 "2007-07-03T16:01:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 3, 2007, 4:01pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/1 "2007-07-03T16:01:34Z")

</div>

I’ve a TH1F, its x axis are millivolt measured by a termistor. I’ve a linear calibration:

energy [keV] = q + x[millvolt] \* m

how can change the x scale of my TH1F, I’ve tried so:

```auto
min_kev = q;
max_kev = h->GetNbinsX () * m +q;
h->GetXaxis()->SetLimits(min_kev,max_kev);
```

it changes the scale, but it doesn’t work well. It changes only the scale, not the datas stored! How can I do it?

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 3, 2007, 6:10pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/2 "2007-07-03T18:10:12Z")

</div>

Use TH1::Multiply(TF1 \*f1, double c1)

see: [root.cern.ch/root/htmldoc/TH1.html#TH1:Multiply](http://root.cern.ch/root/htmldoc/TH1.html#TH1:Multiply)

Rene

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 3, 2007, 9:46pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/3 "2007-07-03T21:46:04Z")

</div>

[quote=“brun”]Use TH1::Multiply(TF1 \*f1, double c1)  
[/quote]

I’ve to rescale the x axis, not y. Although this, I don’t understand how TH1F::Multiply woks:

```auto
TF1 *f = new TF1("f","x[0]*2.",0,100);
TH1F *h = new TH1F("h","h",100,0,100);
h->SetBinContent(20,10);
h->SetBinContent(30,20);
h->Draw(); //I've 2 bin filled equally
h->Multiply(f);
h->Draw();
```

(20,10) go to (20,390)  
(30,10) go to (30,1180)

I hope there is an explanation

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 4, 2007, 6:50am UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/4 "2007-07-04T06:50:57Z")

</div>

OK now I understand your question. This is possible with some approximation as in principle to make a clean operation you would need to start from teh original unbinned x values. The approximation could be the following

` TH1F *h; //your original histogram
   int nbins = h->GetXaxis()->GetNbins();
   TH1F *hnew = new TH1F("hnew","title",nbins,xminnew,xmaxnew);
   for (int i=1;i<=nbins;i++0 {
      double y = h->getBinContent(i);
      double x = h->GetXaxis()->GetBinCenter(i);
      double xnew = alpha*x + beta; //your transformation
      hnew->Fill(xnew,y);
   }
`  
You may want to look also to TH1::Rebin (case xbins != 0) and see the procedure to recmpute errors in case the original hist has errors.

Rene

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [July 5, 2007, 6:20pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/5 "2007-07-05T18:20:58Z")

</div>

[quote=“brun”]OK now I understand your question. This is possible with some approximation as in principle to make a clean operation you would need to start from teh original unbinned x values. The approximation could be the following

` TH1F *h; //your original histogram
   int nbins = h->GetXaxis()->GetNbins();
   TH1F *hnew = new TH1F("hnew","title",nbins,xminnew,xmaxnew);
   for (int i=1;i<=nbins;i++0 {
      double y = h->getBinContent(i);
      double x = h->GetXaxis()->GetBinCenter(i);
      double xnew = alpha*x + beta; //your transformation
      hnew->Fill(xnew,y);
   }
`  
You may want to look also to TH1::Rebin (case xbins != 0) and see the procedure to recmpute errors in case the original hist has errors.

Rene[/quote]

it’s exactly the same procedure that I’m using. The problem is when alpha \> 1 (a dilatation), for example with alpha=2 half bins are empty, so I must do a rebinning.

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 6, 2007, 1:12pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/6 "2007-07-06T13:12:21Z")

</div>

Yes this procedure is not going to create energy from vacuum.  
It is up to you to figure out the right number of bins in the new histogram to avoid either empty bins or binning effects.

Rene

---

<div class="post-metadata">

**Author:** ![HEPatUTStudent](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@HEPatUTStudent](https://root-forum.cern.ch/u/HEPatUTStudent)\
**Post date:** [July 23, 2007, 2:39pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/7 "2007-07-23T14:39:31Z")

</div>

Hi,

I used this trick in a similar situation:

const int nbins = h1-\>GetXaxis()-\>GetNbins();  
double new\_bins[nbins+1];  
for(int i=0; i \< nbins; i++){  
new\_bins[i] = transform\_function( h1-\> GetBinLowEdge(i+1) );  
}

h1-\>Set(nbins, new\_bins);

This will of course result in uneven binwidths if your transoformation is not linear, but does not require rebinning, etc.

Cheers!

---

<div class="post-metadata">

**Author:** ![kara](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kara](https://root-forum.cern.ch/u/kara)\
**Post date:** [May 28, 2013, 5:31pm UTC](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/8 "2013-05-28T17:31:51Z")

</div>

Just a small correction since I found it useful…

const int nbins = h1-\>GetXaxis()-\>GetNbins();  
double new\_bins[nbins+1];  
for(int i=0; i \<= nbins; i++){  
new\_bins[i] = transform\_function( h1-\> GetBinLowEdge(i+1) );  
}

h1-\>SetBins(nbins, new\_bins);
