Using SplinePolynomial for data smoothing
Code: "spline_smoothing.py". Programming language: Python
DMelt Version 1.4. Last modified: 09/15/2019. License: Free
https://datamelt.org/code/cache/spline_smoothing_2453.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
# This example runs cubic spline, calculates chi2/ndf and stops when chi2/ndf<1
from java.awt import Color
from jhplot import *
from jhplot.stat import Interpolator
import time
nbins,min,max=100,-1000,1000
h1 = H1D("Data",nbins, min, max)
h1.fillGauss(100000, 0, 900) # mean=0, sigma=100
h1.fillGauss(5000, 200, 100) # mean=200, sigma=100
p1=P1D(h1,0,0)
def getResult(k1,p1,rho,nbins,min,max):
s1=k1.interpolateCubicSpline(rho,2)
p2=P1D()
fit=s1.getSplinePolynomials()
h=(max-min)/float(nbins); chi2=0
for i in range(nbins):
z1 = min + i * h;
z2=s1.evaluate(z1)
delta=((p1.getY(i)-z2)*(p1.getY(i)-z2) ) / (p1.getErr(i)*p1.getErr(i))
chi2= chi2+delta
p2.add(z1,z2)
chi2ndf= chi2/float(nbins)
if (chi2ndf<1): return None
p2.setTitle( "CubicSpline ρ="+str(rho)+ " χ^{2}/ndf ="+str(int(chi2ndf)))
p2.setStyle("l")
p2.setColor(Color.red)
return p2
c1 = HPlot("Spline 3rd order"); c1.visible()
c1.setRange(min-100,max+100,400,1500)
k1=Interpolator(p1)
rho=0.0000000001
for i in range(20):
rho=rho*2
p2=getResult(k1,p1,rho,nbins,min,max)
if (p2 != None):
c1.clearData()
c1.draw([p1,p2])
time.sleep(0.3)