# Creating an Histo1D with bins of different size

**URL:** <https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072>\
**Category:** Newbie\
**Tags:** python, hist\
**Created:** [September 30, 2021, 11:14pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072 "2021-09-30T23:14:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michele99](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michele99/32/27318_2.png) [@Michele99](https://root-forum.cern.ch/u/Michele99)\
**Post date:** [September 30, 2021, 11:14pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/1 "2021-09-30T23:14:17Z")

</div>

Hi to everybody, I’ve created an histogram Histo1D from an RDataframe called df, using the following definition:  
histo = df.Histo1D((“Histogram”,“Histogram\_background”, 40, 0., 0.2),‘DNN’,‘weight’)  
The histogram has 40 bins between 0 and 0.2.  
For graphic reasons, I’d like to change this code with the definition of the Histo1D with bin of different size, defining the sizes for every bin. Anyone has some advices?  
I’m currently using python.  
Cheers, Michele

---

<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:** [October 1, 2021, 6:57am UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/2 "2021-10-01T06:57:10Z")

</div>

You can use [this constructor](https://root.cern/doc/master/classTH1D.html#aacca0c25fea994d13f5dfd0f36b4c62d).

---

<div class="post-metadata">

**Author:** ![Michele99](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michele99/32/27318_2.png) [@Michele99](https://root-forum.cern.ch/u/Michele99)\
**Post date:** [October 1, 2021, 2:14pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/3 "2021-10-01T14:14:08Z")

</div>

The definition is the same also for RDataframe.Histo1D? Because I think RDataframe hasn’t the attribute TH1D.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 1, 2021, 2:30pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/4 "2021-10-01T14:30:29Z")

</div>

That first argument to `Histo1D` is of type [TH1DModel](https://root.cern/doc/master/structROOT_1_1RDF_1_1TH1DModel.html), which also supports that construction with a list of bin widths.

I am not 100% sure what’s the simplest way to pass that array of doubles in PyROOT, but if you have any issue we can certainly provide an example.

---

<div class="post-metadata">

**Author:** ![Michele99](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michele99/32/27318_2.png) [@Michele99](https://root-forum.cern.ch/u/Michele99)\
**Post date:** [October 1, 2021, 3:09pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/5 "2021-10-01T15:09:59Z")

</div>

I’m currently defining the array as:  
bins=[0.002,0.003,0.005,0.007,0.008]  
and the Histo1D as:  
histo1 = df.Histo1D((“Histogram1”,“Histogram\_background”, 5, bins),‘DNN’,‘weight’)

The error presented is ‘TypeError: can not resolve method template call for ‘Histo1D’’

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 1, 2021, 3:11pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/6 "2021-10-01T15:11:26Z")

</div>

Alright, example coming up xD

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 1, 2021, 3:30pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/7 "2021-10-01T15:30:38Z")

</div>

So, since the C++ signature wants an array (`double *`), from Python you can pass a numpy array or an array.array, but not a list:

```python
model = ROOT.TH1DModel("h","h",3,np.array([1.,2.,3.,4.]))
ROOT.RDataFrame(10).Histo1D(model, "rdfentry_")

```

I hope this helps!  
Enrico

---

<div class="post-metadata">

**Author:** ![Michele99](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michele99/32/27318_2.png) [@Michele99](https://root-forum.cern.ch/u/Michele99)\
**Post date:** [October 1, 2021, 3:46pm UTC](https://root-forum.cern.ch/t/creating-an-histo1d-with-bins-of-different-size/47072/8 "2021-10-01T15:46:50Z")

</div>

Thanks, I will try!
