# Aligning two histograms by shifting bins

**URL:** <https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845>\
**Category:** Newbie\
**Tags:** root\
**Created:** [January 15, 2024, 11:31am UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845 "2024-01-15T11:31:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [January 15, 2024, 11:31am UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/1 "2024-01-15T11:31:27Z")

</div>

Hello users,

I have two histograms that are shifted by some factor

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/4/44f60a1c6479c94fdd5fd5de81808568f760b66e.png)

Is it possible to shift the bins of one wrt to the other to align them?

So far, I am having the centroids of the peaks for both the histograms obtained by fitting. The ultimate goal is to align these centroids such that the peaks overlap.

I am a new user with minimal experience in these operations, so any suggestion will be precious.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [January 15, 2024, 12:18pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/2 "2024-01-15T12:18:22Z")

</div>

It is possible in principle, see [Can we shift histogram for several channels? - #2 by Pepe\_Le\_Pew](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2)

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [January 15, 2024, 12:40pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/3 "2024-01-15T12:40:59Z")

</div>

Hi @yus ,

Thanks a lot for pointing to the valuable thread.

> [@Can we shift histogram for several channels?](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2):
>
> BTW. If you need to “scale” the contents of the histogram (i.e. not any of its axes), you can use:  
> [`TH1::Scale (Double_t c1 = 1, Option_t *option = "")`](https://root.cern/root/html534/TH1.html#TH1:Scale)  
> [`TH1::Multiply (TF1 *f1, Double_t c1 = 1)`](https://root.cern/root/html534/TH1.html#TH1:Multiply)  
> [`TH1::Divide (TF1 *f1, Double_t c1 = 1)`](https://root.cern/root/html534/TH1.html#TH1:Divide)

I think I should scale the contents, however from the documentation it seems function `TH1::Scale` only changes the contents by a **multiplicative factor** `this = this*c1`

Is it possible to change the contents by `this = this*c1 + c2`  
c2 is a constant offset

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [January 15, 2024, 12:55pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/4 "2024-01-15T12:55:13Z")

</div>

I think you have to change the X-axis values, not the Y-axis ones. `TH1::Scale` changes the Y-axis values.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [January 15, 2024, 1:10pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/5 "2024-01-15T13:10:02Z")

</div>

Yes, you are correct.  
I think I should use the

```auto
ScaleXaxis(hist1, ScaleX);

```

However, doing so gives an error.

```auto
ScaleXaxis(his1, Scale)

```

```auto
ROOT_prompt_396:1:18: error: cannot initialize an array element of type 'void *' with an rvalue of type 'Double_t (*)(Double_t)' (aka 'double (*)(double)')

```

The function scale

```auto
#include "TAxis.h"
#include "TH1.h"
#include "TArrayD.h"

Double_t Scale(Double_t x)
{
  Double_t v;
  v = 10 * x + 100; // "linear scaling" function example
  return v;
}

```

Can you please suggest what I am messing up here ?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [January 15, 2024, 1:16pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/6 "2024-01-15T13:16:40Z")

</div>

Can you show your full the code? You should have three functions defined (`ScaleX`, `ScaleAxis`, `ScaleXaxis`), and call the latter in your own code.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [January 15, 2024, 1:42pm UTC](https://root-forum.cern.ch/t/aligning-two-histograms-by-shifting-bins/57845/7 "2024-01-15T13:42:30Z")

</div>

> [@yus](#):
>
> You should have three functions

Ya, my bad I did not copy the whole code. Now it seems to work.

Thanks a lot for your help and time.
