# Rescaling the x axis of a histogram in ROOT6.08

**URL:** <https://root-forum.cern.ch/t/rescaling-the-x-axis-of-a-histogram-in-root6-08/35452>\
**Category:** ROOT\
**Created:** [July 31, 2019, 8:01pm UTC](https://root-forum.cern.ch/t/rescaling-the-x-axis-of-a-histogram-in-root6-08/35452 "2019-07-31T20:01:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spencer/32/39846_2.png) [@spencer](https://root-forum.cern.ch/u/spencer)\
**Post date:** [July 31, 2019, 8:01pm UTC](https://root-forum.cern.ch/t/rescaling-the-x-axis-of-a-histogram-in-root6-08/35452/1 "2019-07-31T20:01:02Z")

</div>

Hi,

I would like to rescale the x axis of a histogram (essentially to convert a histogram with units GeV^2 to MeV^2, by rescaling the x axis by 10^6). The code posted at the end of

> [@Change scale of a TH1F (energy calibration)](https://root-forum.cern.ch/t/change-scale-of-a-th1f-energy-calibration/4971/7):
>
> 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!

by Kara looks like it should do exactly what I want:

int nbins = tdataall-\>GetXaxis()-\>GetNbins();  
cout\<\<" nbins="\<\<nbins\<\<endl;  
float new\_bins[nbins+1];

for(int i=0; i \<= nbins; i++){  
double old = tdataall-\>GetBinLowEdge(i+1);  
new\_bins[i]=1.E6\*old;  
}  
tdataall-\>SetBins(nbins,new\_bins);

However, it does not seem to work in ROOT6.06./08, producing multiple errors. To isolate things, if I just use the array definition, I get the following error:

Processing all.C…  
/Users/sklein/twopifit/./all.C:476:8: error: variable length array declaration not allowed at file scope  
float new\_bins[nbins+1];

If I replace this line with:

float new\_bins[61]; - not what I want, but OK for debugging purposes, then I get more errors:

Processing all.C…  
/Users/sklein/twopifit/./all.C:483:11: error: no matching member function for call to ‘SetBins’  
tdataall-\>SetBins(nbins,new\_bins);

```auto
/opt/local/libexec/root6/include/root/TH1.h:365:21: note: candidate function not viable: no known conversion from
      'float [61]' to 'const Double_t *' (aka 'const double *') for 2nd argument
   virtual void SetBins(Int_t nx, const Double_t *xBins);
                    ^
/opt/local/libexec/root6/include/root/TH1.h:364:21: note: candidate function not viable: requires 3 arguments, but 2 were
      provided
   virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax);
                    ^
/opt/local/libexec/root6/include/root/TH1.h:367:21: note: candidate function not viable: requires 4 arguments, but 2 were
      provided
   virtual void SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins);
                    ^
/opt/local/libexec/root6/include/root/TH1.h:366:21: note: candidate function not viable: requires 6 arguments, but 2 were
      provided
   virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax);
                    ^
/opt/local/libexec/root6/include/root/TH1.h:370:21: note: candidate function not viable: requires 6 arguments, but 2 were
      provided
   virtual void SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t * yBins, Int_t nz,
                    ^
/opt/local/libexec/root6/include/root/TH1.h:368:21: note: candidate function not viable: requires 9 arguments, but 2 were
      provided
   virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax,

Is there a more up-to-date example for how to do this?

Thanks.

Spencer
```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 31, 2019, 8:17pm UTC](https://root-forum.cern.ch/t/rescaling-the-x-axis-of-a-histogram-in-root6-08/35452/2 "2019-07-31T20:17:42Z")

</div>

> [@Can we shift histogram for several channels?](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2):
>
> I’m not sure if this is fully kosher, but suit yourself : #include "TAxis.h" #include "TH1.h" #include "TArrayD.h" Double\_t ScaleX(Double\_t x) { Double\_t v; v = 10 \* x + 100; // "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 (…

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 14, 2019, 8:17pm UTC](https://root-forum.cern.ch/t/rescaling-the-x-axis-of-a-histogram-in-root6-08/35452/3 "2019-08-14T20:17:47Z")

</div>

Closed on August 14, 2019, 8:17pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
