# Shift bins under modulus

**URL:** <https://root-forum.cern.ch/t/shift-bins-under-modulus/44477>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [April 20, 2021, 2:02am UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477 "2021-04-20T02:02:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orionyeung001](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orionyeung001/32/25183_2.png) [@orionyeung001](https://root-forum.cern.ch/u/orionyeung001)\
**Post date:** [April 20, 2021, 2:02am UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/1 "2021-04-20T02:02:19Z")

</div>

Is there a way to shift bins modulo some float? If I have some data on bins over [0,5] with features at 1 and 3.5, can I shift the bin centers by +2 to move those same features to 2 and 0.5?

Some motivation since I might be tackling this problem in a bad way. I have some TH1F’s which all have three peaks some with nicer statistics than others. While the peak that happens closest to 0 isn’t the same physical event for all the histograms, the temporal spacing and order of the events in the histogram are. That is, I can guarantee species A is followed by B followed by C followed by A followed… etc. but I don’t know a priori what species corresponds to the peak that has time closest to zero. I want to use TSpectrum’s peak detection, `Search` but I don’t want medium sized peaks over the boundary to be marked near 0 and the max bin since that double counts.

Since the time differences are known, I figure if I get two correct peaks and a third is a boundary repeat, I can shift to not chop peaks and then reidentify the peaks.

_ROOT Version:_ 6.20/04  
_Platform:_ Ubuntu 20.04  
_Compiler:_ linuxx8664gcc

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 20, 2021, 8:04am UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/2 "2021-04-20T08:04:14Z")

</div>

May be it would be enough to change the X axis limits with [SetLimits](https://root.cern/doc/master/classTAxis.html#aa56ec858687dad0b58ca0cf939e7c79b) ?

```auto
h->GetXaxis()->SetLimits(..)

```

---

<div class="post-metadata">

**Author:** ![orionyeung001](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orionyeung001/32/25183_2.png) [@orionyeung001](https://root-forum.cern.ch/u/orionyeung001)\
**Post date:** [April 21, 2021, 9:14pm UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/3 "2021-04-21T21:14:00Z")

</div>

But will that take data that’s chopped off the left and put it on the right? I will likely manually rebin with something like copying the data and shifting with `original[i] = copy[(i+index_shift)%nbins]` I just wondered about a built-in, as periodic data seems like it would be analyzed enough to have tools for it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 22, 2021, 7:03am UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/4 "2021-04-22T07:03:30Z")

</div>

I do not think there is a built-in way to do it. May be @moneta can confirm.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 22, 2021, 8:41am UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/5 "2021-04-22T08:41:24Z")

</div>

Yes we do not have such function. If it is just shifting the axis, keeping the same bins, you can do as @couet suggested.  
If not you would need to create a new histograms, loop on the bins and copying the content according to your specific procedure using `SetBinContent`

---

<div class="post-metadata">

**Author:** ![orionyeung001](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/orionyeung001/32/25183_2.png) [@orionyeung001](https://root-forum.cern.ch/u/orionyeung001)\
**Post date:** [April 22, 2021, 12:36pm UTC](https://root-forum.cern.ch/t/shift-bins-under-modulus/44477/6 "2021-04-22T12:36:56Z")

</div>

Thanks for the reply! I’ll be going with `SetBinContent`
