# Binning of histograms and accessing spectrum manipulation functions

**URL:** <https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552>\
**Category:** Newbie\
**Created:** [December 29, 2024, 7:10am UTC](https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552 "2024-12-29T07:10:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![skumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/skumar/32/38231_2.png) [@skumar](https://root-forum.cern.ch/u/skumar)\
**Post date:** [December 29, 2024, 7:10am UTC](https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552/1 "2024-12-29T07:10:33Z")

</div>

Hello everyone!

1. I am completely new to ROOT and data analysis. I have data for histogram in an ASCII file (Energy vs Counts). There are 4096 bins and the width of each bin is 0.5 keV. I want to write this data in a 1D histogram in pyROOT. Can someone please assist, as I am confused about setting the bin width in ROOT.
2. Additionally, I want to access some spectrum manipulation functions (smoothing, deconvolution, peak finding etc.) maybe in TSpectrum class or something else, in pyROOT. Can you please assist me regarding how to set parameters properly for these functions and how to implement them properly in pyROOT? I have gone through the documentation, but I am not sure.  
Thanks a lot in advance

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 29, 2024, 9:46am UTC](https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552/2 "2024-12-29T09:46:42Z")

</div>

For 1, you probably want this constructor:

```auto
h = ROOT.TH1D('h', 'some title', nbins, xlow, xhigh)

```

see the [Documentation](https://root.cern/doc/master/classTH1D.html#a4e0536d7ab2c883c26b2e4ea5b540334) for the meaning of each parameter, and to see other constructors (e.g., you could also define the bins as arrays or vectors, which of course means you have to define the bins one by one). If you want a given bin width for the above constructor, you have to adjust `nbins, xlow, xhigh` accordingly, given `binw = (xhigh-xlow)/nbins`.

---

<div class="post-metadata">

**Author:** ![skumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/skumar/32/38231_2.png) [@skumar](https://root-forum.cern.ch/u/skumar)\
**Post date:** [December 29, 2024, 9:49am UTC](https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552/3 "2024-12-29T09:49:51Z")

</div>

Thank you for your reply. If possible, can you explain a bit more in detail about what you mean by defining the bins as arrays?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 29, 2024, 9:56am UTC](https://root-forum.cern.ch/t/binning-of-histograms-and-accessing-spectrum-manipulation-functions/62552/4 "2024-12-29T09:56:21Z")

</div>

> [@Numpy arrays in variable binned histogram with pyroot](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070):
>
> Hello, I am using root-6.08.02 to make a variable binned histogram with PyROOT. I find that this works: from array import array m = [400,450,500,550,600,650,700,750,800,850,900,950,1000,1150,1500] h = ROOT.TH1D("h",";m [GeV];Events/ 50 GeV;;",len(m)-1,array('d',m)) but this doesn’t import numpy as np m = [400,450,500,550,600,650,700,750,800,850,900,950,1000,1150,1500] h = ROOT.TH1D("h",";m [GeV];Events/ 50 GeV;;",len(m)-1,np.asarray(m)) I error I get is Error in \< TGaxis::PaintAxis \>: len…

> [@Problem constructing TH2F](https://root-forum.cern.ch/t/problem-constructing-th2f/8895/6):
>
> (Number of bins = array\_dimension) gives: root [0] Double\_t b\_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6}; root [1] TH2F\*h=new TH2F("a","a",10,0,1,9,b\_f) Error in \<TAxis::TAxis::Set\>: bins must be in increasing order root [2] (Number of bins = array\_dimension-1) gives: root [0] Double\_t b\_f[9]={0.,0.1,0.16,0.2,0.25,0.3,0.35,0.4,0.6}; root [1] TH2F\*h=new TH2F("a","a",10,0,1,8,b\_f) root [2] That’s logic, the array defines the bins’ boundaries.
