def DivideTGraphErrors(num, denom, name): nPoints = num.GetN() nSkipPoints = 0 # count of when we can't divide newTGraph = TGraphErrors(nPoints) newTGraph.SetName(name) for i in range(nPoints): x = num.GetX()[i] x_err = num.GetEX()[i] y1 = num.GetY()[i] y2 = denom.GetY()[i] y = 0.0 y_err = 0.0 if ((y1 != 0) and (y2 != 0)): # technically could make this work for y1 = 0 y = y1 / y2 y1_err = num.GetEY()[i] y2_err = denom.GetEY()[i] y_err = y * math.sqrt( math.pow(y1_err / y1, 2) + math.pow(y2_err / y2, 2)) newTGraph.SetPoint(i - nSkipPoints, x, y) newTGraph.SetPointError(i - nSkipPoints, x_err, y_err) else: newTGraph.RemovePoint(i - nSkipPoints) nSkipPoints += 1 newTGraph.SetLineColor(num.GetLineColor()) newTGraph.SetMarkerColor(num.GetMarkerColor()) newTGraph.SetMarkerStyle(num.GetMarkerStyle()) return newTGraph
def ApplyBinShiftCorrectionGeneral(self, hist, fit): """ Alternative method for bin shift correction: - Apply user-default model for bin-shift correction - don't multiply by pt @param hist: Input spectrum for the bin shift correction @param fit: Model for the bin-shift correction @return: The bin-shift corrected spectrum as graph """ h = deepcopy(hist) hist.SetName("htemp") result = TGraphErrors(h) for i in range(0, result.GetN()): result.GetEX()[i] = 0. y = 0 #for now 10 iterations fixes for k in range(0, 10): for i in range(1, h.GetNbinsX() + 1): y = fit.Integral(h.GetBinLowEdge(i), h.GetBinUpEdge(i)) / h.GetBinWidth(i) x = self.FindX(y, fit, h.GetBinLowEdge(i), h.GetBinUpEdge(i)) result.GetX()[i - 1] = x # remove points that are 0 while result.GetY()[0] < 1.e-99: result.RemovePoint(0) mybin = 0 for biniter in range(0, result.GetN()): if result.GetY()[biniter] < 1.e-99: mybin = biniter break while result.RemovePoint(mybin) > 0: continue return result
def GetData(file, scale=1., sed=True, title='SED', barUL=True): GetData.Ng += 1 g = TGraphErrors(file) gUL = TGraphErrors() if sed: for i in range(g.GetN()): g.GetY()[i] = pow(g.GetX()[i], 2) * g.GetY()[i] * 1e-6 / scale g.GetEY()[i] = pow(g.GetX()[i], 2) * g.GetEY()[i] * 1e-6 / scale g.GetX()[i] *= 1e-3 idel = 0 nUL = 0 while idel < g.GetN(): if g.GetEY()[idel] < 0: gUL.SetPoint(nUL, g.GetX()[idel], g.GetY()[idel]) if barUL: gUL.SetPointError(nUL, 0, g.GetY()[idel] * 1e-5) nUL += 1 g.RemovePoint(idel) else: idel += 1 if sed: g.SetTitle(title + ";Energy [GeV];E^{2}dN/dE [TeV cm^{-2} s^{-1}]") else: g.SetTitle(title + ";Energy [MeV];dN/dE [cm^{-2} s^{-1} MeV^{-1}]") g.SetLineColor(kRed) g.SetMarkerColor(kRed) g.SetMarkerStyle(20) g.SetName("g%d" % GetData.Ng) gUL.SetLineColor(g.GetLineColor()) gUL.SetName("gUL%d" % GetData.Ng) return g, gUL
def ApplyBinShiftCorrection(self, hist): """ Apply bin-shift correction to the input spectrum using an iterative procedure @param hist: Input spectrum @return: Bin-shift corrected spectrum """ h = deepcopy(hist) h.SetName("htemp") # Bin shift correction performed in model specturm * pt for i in range(1, h.GetNbinsX() + 1): pt = h.GetBinCenter(i) h.SetBinContent(i, h.GetBinContent(i) * pt) h.SetBinError(i, h.GetBinError(i) * pt) result = TGraphErrors(h) for i in range(0, result.GetN()): result.GetEX()[i] = 0. fitfun = TF1("fitfun", "([0]*(1.+x/[1])^(-[2])*x)-[3]", 0.15, 100.0) fitfun.SetParameter(0, 1000) fitfun.SetParameter(1, 1) fitfun.SetParameter(2, 5) fitfun.FixParameter(3, 0) h.Fit(fitfun, "") self.__StableFit(h, fitfun, True) # Iterative approach: # - Use model to get the mean of the function inside the bin # - Get the X where the mean is found # - Use the new coordinate (x,y) for the next iteration of the fit # for now 10 iterations fixed for k in range(1, 11): for i in range(1, h.GetNbinsX() + 1): y = fitfun.Integral(h.GetBinLowEdge(i), h.GetBinUpEdge(i)) / h.GetBinWidth(i) result.GetX()[i - 1] = self.FindX(y, fitfun, h.GetBinLowEdge(i), h.GetBinUpEdge(i)) self.__StableFit(result, fitfun, False) # Undo multiplication with pt for i in range(0, result.GetN()): pt = result.GetX()[i] result.GetY()[i] /= pt result.GetEY()[i] /= pt #remove points that are 0 while result.GetY()[0] < 1.e-99: result.RemovePoint(0) bval = 0 for mybin in range(0, result.GetN() + 1): if result.GetY()[bin] < 1.e-99: bval = mybin break while result.RemovePoint(bval) > 0: continue return result