# Is there an efficient way for filling TH1 histogram with numpy arrays in 2024?

**URL:** <https://root-forum.cern.ch/t/is-there-an-efficient-way-for-filling-th1-histogram-with-numpy-arrays-in-2024/57874>\
**Category:** Newbie\
**Created:** [January 17, 2024, 11:07am UTC](https://root-forum.cern.ch/t/is-there-an-efficient-way-for-filling-th1-histogram-with-numpy-arrays-in-2024/57874 "2024-01-17T11:07:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarthchoudhary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sarthchoudhary/32/26105_2.png) [@sarthchoudhary](https://root-forum.cern.ch/u/sarthchoudhary)\
**Post date:** [January 17, 2024, 11:07am UTC](https://root-forum.cern.ch/t/is-there-an-efficient-way-for-filling-th1-histogram-with-numpy-arrays-in-2024/57874/1 "2024-01-17T11:07:37Z")

</div>

Is there a better way to fill a TH1 histogram with numpy array than using a ‘for loop’ to loop through the the array.  
PS: I have seen the answers which suggests to use root\_numpy but the thing is that root\_numpy is not compatible with any version of numpy \>1.19.5, hence, the question.

_ROOT Version:_ 6.24.6  
_Platform:_ Ubuntu 22  
_Compiler:_ Not Provided

* * *

---

<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:** [January 17, 2024, 2:27pm UTC](https://root-forum.cern.ch/t/is-there-an-efficient-way-for-filling-th1-histogram-with-numpy-arrays-in-2024/57874/2 "2024-01-17T14:27:32Z")

</div>

Hi,  
The simples and efficient way to fill now a TH1 histogram with a numpy array (1D) is as in this code example:

```auto
import numpy as np
import ROOT

# Create a numpy array with 1000 gaussian data with mu=0 and sigma=1 
x = np.random.normal(0, 1, 1000)

# Create a ROOT histogram
hist = ROOT.TH1D("hist", "hist", 50, -3, 3)
# fill ROOT histogram with weights=1
hist.FillN(x.size, x, np.ones(x.size))

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![sarthchoudhary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sarthchoudhary/32/26105_2.png) [@sarthchoudhary](https://root-forum.cern.ch/u/sarthchoudhary)\
**Post date:** [January 17, 2024, 5:51pm UTC](https://root-forum.cern.ch/t/is-there-an-efficient-way-for-filling-th1-histogram-with-numpy-arrays-in-2024/57874/3 "2024-01-17T17:51:18Z")

</div>

Thank you. I was able to get the desired outcome with FillN.
