<p>Assuming that the three arrays <code>BH</code>, <code>MTW</code> and <code>z</code> are numpy arrays, you can do the following:</p>
<pre><code># First determine the length of each merger tree
MTlength = np.diff(z) # length of each MT

# The length of the last MT is missing, let's add it
MTlength = np.append(MTlength, len(BH)-z[-1])

# Now let's build an array with len(BH) elements that for each black hole
# contains the corresponding MT weight.
BHW = np.repeat(MTW, MTlength)

# Define the array of mass bins, this is an example that goes from
# 10 to 10^10 in 30 logspaced bins
mbins = np.logspace(1,10,30)

# Build the mass function
MF, _ = np.histogram(BH, bins=mbins, weights=BHW)

# Divide by the size of each bin
MF = MF/np.diff(mbins)
</code></pre>

CC BY-SA 4.0
2022-08-22T07:53:30.437Z
2022-08-22T07:53:30.437Z
Prallax
<p>I'm running a set of simulations to study the evolution of Super Massive black holes.</p>
<p>In principle, I have the black hole masses and other  property called merger tree weight in Mpc<sup>-3</sup> unit which is different for each merger tree created. If I want to construct the black hole mass function I should read  the black holes masses, and the mergerTreeWeight but I don't know how to implement the merger tree weight exactly in the code.</p>
<p>Also, I have the indexes where every merger tree start and end. I know  it  is useful because if  the mass function  consists of a set of bins in mass. Let's say that each bin ranges from
<span class="math-container">$\log(M)-\mathrm{d}(\log M)/2$</span> to <span class="math-container">$\log(M)+\mathrm{d}(\log M)/2$</span>. To compute the mass function in a bin, I'd select all black holes with mass in that range. Then, just sum the mergerTreeWeight of those galaxies, and divide by the bin width, <span class="math-container">$\mathrm{d}(\log M)$</span>.</p>
<p>My problem is related to how to implement this process in a python code.</p>
<p><em>EDIT</em></p>
<p>I think it should be understood considering three variables such as BH = blackHoleMass , MTW= MergerTreeWeight and z= Mergertreeindex. The &quot;z&quot; variable give the index where every merger tree (with different weight) start <code>BH = [1e9, 4e7, 7e5,2e3 ] MTW= [1.2e-5, 4.6e-4,1.4e-6]  z=[0,2,3] </code>.   BH[:3]  is assigned to the first merger tree, which has a MergerTreeWeight equals to MTW[0], BH[3] is assigned to the second MergerTreeWeight, MTW[1] and BH[4] is assigned to the third MergerTreeWeight.</p>

CC BY-SA 4.0
2022-08-21T06:45:09.183Z
Let me understand: you have some files, each file contains two columns, one is the black hole mass and the other is its merger tree weigth. Is that correct?
Prallax
CC BY-SA 4.0
2022-08-21T14:37:59.717Z
In principle, all is in a HDF5 file. I can read the file and the variables stored with h5py but my problem is about how to implement the algorithm in python to construct the mass function. You'd suppose I have three arrays such as BH = blackHoleMass , MTW= MergerTreeWeight and  z= Mergertreeindex.  The "z" variable give the index where every merger tree (with different weight) start.
Matías Liempi
<data-analysis><supermassive-black-hole><python>
How do I create well a weighted black hole mass function?
Matías Liempi
# How do I create well a weighted black hole mass function?

(Asked by: username_0 on 2022-08-21)

I'm running a set of simulations to study the evolution of Super Massive black holes.

In principle, I have the black hole masses and other property called merger tree weight in Mpc-3 unit which is different for each merger tree created. If I want to construct the black hole mass function I should read the black holes masses, and the mergerTreeWeight but I don't know how to implement the merger tree weight exactly in the code.

Also, I have the indexes where every merger tree start and end. I know it is useful because if the mass function consists of a set of bins in mass. Let's say that each bin ranges from
$\log(M)-\mathrm{d}(\log M)/2$ to $\log(M)+\mathrm{d}(\log M)/2$. To compute the mass function in a bin, I'd select all black holes with mass in that range. Then, just sum the mergerTreeWeight of those galaxies, and divide by the bin width, $\mathrm{d}(\log M)$.

My problem is related to how to implement this process in a python code.

*EDIT*

I think it should be understood considering three variables such as BH = blackHoleMass , MTW= MergerTreeWeight and z= Mergertreeindex. The "z" variable give the index where every merger tree (with different weight) start `BH = [1e9, 4e7, 7e5,2e3 ] MTW= [1.2e-5, 4.6e-4,1.4e-6] z=[0,2,3]` . BH[:3] is assigned to the first merger tree, which has a MergerTreeWeight equals to MTW[0], BH[3] is assigned to the second MergerTreeWeight, MTW[1] and BH[4] is assigned to the third MergerTreeWeight.

**Tags:** data-analysis, supermassive-black-hole, python
> **Comments:**
> **username_1**: Let me understand: you have some files, each file contains two columns, one is the black hole mass and the other is its merger tree weigth. Is that correct?  
> **username_0**: In principle, all is in a HDF5 file. I can read the file and the variables stored with h5py but my problem is about how to implement the algorithm in python to construct the mass function. You'd suppose I have three arrays such as BH = blackHoleMass , MTW= MergerTreeWeight and  z= Mergertreeindex.  The "z" variable give the index where every merger tree (with different weight) start.  

---

## 1 answers

### Answer by username_1 on 2022-08-22. Score: 1 | Accepted answer

Assuming that the three arrays BH, MTW and z are numpy arrays, you can do the following:
# First determine the length of each merger tree
MTlength = np.diff(z) # length of each MT

# The length of the last MT is missing, let's add it
MTlength = np.append(MTlength, len(BH)-z[-1])

# Now let's build an array with len(BH) elements that for each black hole
# contains the corresponding MT weight.
BHW = np.repeat(MTW, MTlength)

# Define the array of mass bins, this is an example that goes from
# 10 to 10^10 in 30 logspaced bins
mbins = np.logspace(1,10,30)

# Build the mass function
MF, _ = np.histogram(BH, bins=mbins, weights=BHW)

# Divide by the size of each bin
MF = MF/np.diff(mbins)


---

