Skip to content

Instantly share code, notes, and snippets.

@exaos
Created January 11, 2013 03:59
Show Gist options
  • Save exaos/4507846 to your computer and use it in GitHub Desktop.
Save exaos/4507846 to your computer and use it in GitHub Desktop.
A example of using ROOT::TSpectrum in Python. (http://root.cern.ch/)
#!/usr/bin/env python
'''
'''
from ROOT import TH1F, TH1, TSpectrum, TFile
from pprint import pformat
def get_gpeaks(h,lrange=[1000,3800],sigma=2,opt="",thres=0.05,niter=20):
s = TSpectrum(niter)
h.GetXaxis().SetRange(lrange[0],lrange[1])
s.Search(h,sigma,opt,thres)
bufX, bufY = s.GetPositionX(), s.GetPositionY()
pos = []
for i in range(s.GetNPeaks()):
pos.append([bufX[i], bufY[i]])
pos.sort()
return pos
def get_all_he(f):
f.Get("histos").ls()
hs = []
for i in range(16):
hs.append( f.Get("RADC0_%02d"%i) )
return hs
def get_all_peaks_info(f):
histos = get_all_he(f)
pchs = []
for i in [0,1,2,3,12,13,14,15]:
info = '"' + histos[i].GetName() + '":'
info += pformat(get_gpeaks(histos[i]))
pchs.append(info)
return pchs
def info_write2json(fr, fj):
fdat = TFile(fr)
fout = open(fj,"w")
pinfo = get_all_peaks_info(fdat)
fout.write("{\n")
for i in pinfo[:-1]: fout.write(i+",\n")
fout.write(pinfo[-1])
fout.write("\n}\n")
fout.close()
if __name__=="__main__":
print "Usage: getgpeaks.py <file.root>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment