unfortunately using
p = array.array( ‘d’, [1,2] )
OR IN MY CASE
p = array.array( ‘d’, [0] *300 )
did not work for me, since I can’t hyperlink my code I will copy and paste my code here
import sys
import ROOT
import uproot4
import math
from tqdm import tqdm
import numpy as np
import array
print("Beginning...")
#Defining usage
if len(sys.argv) != 3:
print (" USAGE : %s <input file> <run identifier>"%( sys.argv[0]))
sys.exit(1)
#Function designed for saving histograms
def savehist(hist, histname):
"""At the end of the function, there are no more references to `file`.
The `TFile` object gets deleted, which in turn saves and closes
the ROOT file."""
myfile.WriteObject(hist, histname)
#Opening .root file to save histograms
myfile = ROOT.TFile.Open("../droc_epical/plots/"+sys.argv[2]+"_histograms.root", "RECREATE")
class CLA_buffer:
def __init__(self,data: int):
# self._data = bytes(data,encoding="utf-8")
self._data = bytes(data)
self._view=None
def __buffer__(self,flags=None) -> memoryview:
self._view = memoryview(self._data)
return self._view
class CLA_MultiGauss:
def __call__(self, t, para):
height = para[0]
mean = para[1]
sig = para[2]
x = t[0]
results = height * ROOT.TMath.Gaus(x,mean,sig)
# tmp = -1.0 * ((x - mean)*(x - mean))/(2.0 * (sig * sig))
return results
# return height * math.exp(tmp)
class CLA_HistoFit(object):
def __init__(self,histogram,histo_identifier):
c0 = ROOT.TCanvas()
c1 = ROOT.TSpectrum(20)
self.CLA_HistoFit = histogram
self.CLA_HistoFit = histo_identifier
c0.Update()
# The code snippet you provided is performing background subtraction on a histogram using the
# TSpectrum class in ROOT. Here is a breakdown of the steps:
histogram_BG = c1.Background(histogram,14,"smooting3")
histogram_SUBT_BG = histogram.Clone()
histogram_SUBT_BG.Sumw2()
histogram_SUBT_BG.Add(histogram_BG, -1)
c1.Search(histogram_SUBT_BG,1,"",0.005)
# c1.Search(histogram,1,"",0.005)
x_peak, y_peak = c1.GetPositionX(), c1.GetPositionY()
nPeaks = 0
pos = []
par = array.array('d',[0]*3000)
# par = np.zeros(3000)
# multi = [0] * c1.GetNPeaks()
multi = 0
sigma = 0.005
par[0] = 0
par[1] = 0
for j in range(c1.GetNPeaks()):
par[3*nPeaks +2] = y_peak[j] #height
par[3*nPeaks +3] = x_peak[j] #mean
par[3*nPeaks +4] = sigma
nPeaks += 1
print("Number of peaks found for CH" + histo_identifier + ": " + str(nPeaks) )
for i in range(c1.GetNPeaks()):
pos.append([x_peak[i],y_peak[i], sigma])
pos.sort()
# for i, entry in enumerate(pos):
# multi[i] = ROOT.TF1("MULTIFIT"+str(i), CLA_MultiGauss(), (entry[0] - 20), (entry[0] + 20),3)
# multi[i].SetParameters(entry[1],entry[0],entry[2])
# multi[i].FixParameter(3, multi[i].GetParameter(3))
# multi[i].SetNpx(1000)
# histogram.Fit(multi[i],"QMR+")
multi = ROOT.TF1("MULTIFIT", CLA_MultiGauss(), 0, 4096 ,3 * nPeaks + 2)
multi.SetParameters(*par)
for j in range(c1.GetNPeaks()):
multi.FixParameter(3 * j + 3, multi.GetParameter(3 * j + 3))
multi.SetNpx(1000)
histogram.Fit(multi,"QMR+")
histogram.Draw()
c0.Print("../droc_epical/plots/"+sys.argv[2]+"_CH"+ histo_identifier +".png")
savehist(histogram,"histHGFIT_CH"+str(i))
myfile.Write()
c0.Clear()
class CLA_GetData():
def __init__(self):
file = uproot4.open(sys.argv[1])
channelref = file["tree/channel"].array(library="np")
HGref = file["tree/HG"].array(library="np")
hist_init = [0] * len(channelref[0])
name = [0] *len(channelref[0])
print("Number of Channels: " + str(len(channelref[0])) )
for j, val in enumerate(channelref):
for k in tqdm(range(len(val)) ): #Progression Bar ON
# for k in range( len(val) ): # Progression Bar OFF
hist_init[k] = ROOT.TH1F("Channel" + str(k), "HG Channel"+ str(k), 300, 0, 1500)
name[k] = str(val[k])
for i, entry in enumerate(HGref):
hist_init[k].Fill(entry[k])
CLA_HistoFit(hist_init[k],name[k])
break
CLA_GetData()
print("Done!")
To be clear I am making 3000 “slots” for the possibility of having x amount of peaks