def calculate(self, histo): import os langauPath = os.path.join(os.path.dirname(__file__), 'langau.c') import ROOT ROOT.gSystem.CompileMacro(langauPath, "k-") ROOT.gSystem.Load(langauPath) from ROOT import langaufun from ROOT import TF1 fit = TF1("langau", langaufun, self.range[0], self.range[1], 4) if (histo.GetEntries() < 150): histo.Rebin(2) fit.SetParameters(*(self.parameters)) if (histo.GetBinCenter(histo.GetMaximumBin()) > self.range[0]): fit.SetParameter(1, histo.GetBinCenter(histo.GetMaximumBin())) fit.SetParameter(2, histo.Integral()) histo.Fit(fit, "QOR") histo.Fit(fit, "QOR") histo.Fit(fit, "QOR") control = 0 while control < 5: if (fit.GetParameter(0) < self.controlVal or fit.GetParameter(1) < self.range[0]): print "########### REFIT #######" fit.SetParameters(*(self.parameters)) if (histo.GetBinCenter(histo.GetMaximumBin()) > self.range[0]): fit.SetParameter(1, histo.GetBinCenter(histo.GetMaximumBin())) fit.SetParameter(2, histo.Integral() * (5 + control) / 5) fit.SetParameter(4, self.parameters[3] * (control + 1)) histo.Fit(fit, "QO", "", self.range[0] - 2, self.range[1]) histo.Fit(fit, "QO", "", self.range[0] - 2, self.range[1]) histo.Fit(fit, "QO", "", self.range[0] - 2, self.range[1]) control = control + 1 else: print "##### GOOD #####" control = 5 result = (fit.GetMaximumX(), fit.GetParError(self.desired)) del fit return result
def singleGaus(hist, units, color, xLeft, yLow, same, title): #print "InSingle gaus" can = TCanvas(title, title) can.cd() hist.Draw() min = -0.5 #hist.GetMinimum() max = 0.5 #hist.GetMaximum() print "Fitting from", min, "to", max FZ01 = TF1("FZ01", "gaus", min, max) FZ01.SetLineColor(color) FZ01.SetLineWidth(1) hist.Fit("FZ01", "ORQ", "same") FZ01.Draw("same") hist.Draw("same") # Draw the after mean and sigma text = "#mu=" text += str(int(1000 * FZ01.GetParameter(1))) #text += str(round(FZ01.GetParameter(1),4)) text += " " + units + ", #sigma=" text += str(int(1000 * FZ01.GetParameter(2))) #text += str(round(FZ01.GetParameter(2),4)) text += " " + units m_l = TLatex() m_l.SetTextSize(0.04) m_l.SetTextAlign(12) m_l.SetNDC() m_l.DrawLatex(xLeft, yLow, text) if writeOut: can.SaveAs(title + ".png") return m_l, can, FZ01, hist
def __init__(self, config_name, file_type): self.Dir = dirname(dirname(realpath(__file__))) self.Config = load_config(join(self.Dir, 'config', config_name)) self.DataDir = self.Config.get('MAIN', 'data directory') self.RunNumber = self.load_run_number() self.FileName = '{}_{:03d}.{}'.format( self.Config.get('MAIN', 'filename'), self.RunNumber, file_type) self.NPlanes = self.Config.getint('MAIN', 'number of planes') self.WBC = self.Config.getint('CHIP', 'wbc') self.NCols = self.Config.getint('CHIP', 'columns') self.NRows = self.Config.getint('CHIP', 'rows') self.Trim = self.Config.get('CHIP', 'trim') if self.Config.has_option( 'CHIP', 'trim') else '' self.NEvents = 0 # Pulse Height Calibrations self.Fit = TF1('ErFit', '[3] * (TMath::Erf((x - [0]) / [1]) + [2])', -500, 255 * 7) self.Parameters = self.load_calibration_fitpars() self.PBar = PBar()
def __init__(self, pars=list(), norm=True): self.name = "Wiedenhoever" self.id = self.name + "_" + hex(id(self)) # originally, [4] was not the first coefficient, this version seems to work better # "( N * d * (E- b + c *exp(- d *E))^- a )" # [0] is fixed self.TF1 = TF1( self.id, "([0]*[4]*pow(x-[2]+[3]*exp(-[4]*x), -[1]))", 0, 0) _Efficiency.__init__(self, num_pars=5, pars=pars, norm=norm) # List of derivatives self._dEff_dP = [None, None, None, None, None] self._dEff_dP[0] = lambda E, fPars: self.norm * \ self.value(E) / fPars[0] # dEff/da self._dEff_dP[1] = lambda E, fPars: self.norm * (-self.value(E)) * math.log( E - fPars[2] + fPars[3] * math.exp(-fPars[4] * E)) # dEff/db self._dEff_dP[2] = lambda E, fPars: self.norm * self.value(E) * fPars[1] / ( E - fPars[2] + fPars[3] * math.exp(-fPars[4] * E)) # dEff/dc self._dEff_dP[3] = lambda E, fPars: self.norm * (-self.value(E)) * fPars[1] / ( E - fPars[2] + fPars[3] * math.exp(-fPars[4] * E)) * math.exp(-fPars[4] * E) # dEff/dd self._dEff_dP[4] = lambda E, fPars: self.norm * self.value(E) * (1 / fPars[4] + fPars[1] / ( E - fPars[2] + fPars[3] * math.exp(-fPars[4] * E)) * fPars[3] * math.exp(-fPars[4] * E) * E) # dEff/de
def FindMinMaxFromFit(MaxListInd, Z, By, fROOT=None): "Return the max B and Z locations from a fit" # Where is the calculated max By from the fit MaxBy = [] MaxByZ = [] # Calculate the max based on pol2 fit maximum. Assumption is to use max +-NFitWidth NFitWidth = 1 for i in MaxListInd: x = [] y = [] for j in range(-NFitWidth, NFitWidth + 1): x.append(Z[i + j]) if By[i] >= 0: y.append(By[i + j]) else: y.append(-By[i + j]) g = TGraph(NFitWidth * 2 + 1, array('d', x), array('d', y)) g.SetName('gFit' + str(i)) FitFunction = TF1("FitFunction_" + str(i), "pol2", -4, 4) g.Fit(FitFunction, 'q') # If the root file exists write the graphs if (fROOT): g.Write() MaxByZ.append(FitFunction.GetMaximumX()) if By[i] >= 0: MaxBy.append(FitFunction.GetMaximum()) else: MaxBy.append(-FitFunction.GetMaximum()) return [MaxByZ, MaxBy]
def fitScanData(treeFile): gROOT.SetBatch(True) gStyle.SetOptStat(0) inF = TFile(treeFile) scanHistos = {} scanFits = {} for vfat in range(0, 24): scanHistos[vfat] = {} scanFits[vfat] = {} for ch in range(0, 128): scanHistos[vfat][ch] = TH1D('scurve_%i_%i_h' % (vfat, ch), 'scurve_%i_%i_h' % (vfat, ch), 254, 0.5, 254.5) for event in inF.scurveTree: scanHistos[event.vfatN][event.vfatCH].Fill(event.vcal, event.Nhits) fitTF1 = TF1('myERF', '500*TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1]))+500', 1, 253) for vfat in range(0, 24): print 'fitting vfat %i' % vfat for ch in range(0, 128): fitStatus = 1 fitN = 0 while (fitStatus): fitTF1.SetParameter(0, 5 * fitN) fitTF1.SetParameter(1, 2.0) fitResult = scanHistos[vfat][ch].Fit('myERF', 'S') fitStatus = fitResult.Status() scanFits[vfat][ch] = fitTF1.GetParameter(0) fitN += 1 return scanFits