Re-binning counts in a histogram (Python)

38 Views Asked by At

I am working with two data sets (tof_n22 and tof_n30) in Python and I need to re-bin the data and plot it as another histogram. The two datasets are neutron time-of-flight data and I am trying to re-bin this data according to measured neutron energy. I came across a randomization method in which you update each event inside the histogram by the bin width * a random number between 0 and 1. I do this N number of times and then I plot the new histogram. Is that the correct way to re-bin data using randomization? I have attached the sample code below:

bin_width22 = (max(tof_n22) - min(tof_n22))/num_bins #calculate bin widths in datasets
bin_width30 = (max(tof_n30) - min(tof_n30))/num_bins

N = 100
# creating copies of the two datasets
tof_n22_new = tof_n22.copy()
tof_n30_new = tof_n30.copy()


for i in range(0,N):

    # generate uniform random number arrays
    rnd_22 = np.random.uniform(0,1,len(tof_n22))
    rnd_30 = np.random.uniform(0,1,len(tof_n30))

    # update the copy datasets by bin width*random number
    tof_n22_new += bin_width22*rnd_22
    tof_n30_new += bin_width30*rnd_30
    
    # add the old values to the updated values
    tof_n22_new += tof_n22  
    tof_n30_new += tof_n30

# average of all the runs
tof_n22_avg = tof_n22_new/N
tof_n30_avg = tof_n30_new/N
0

There are 0 best solutions below