从函数内部调用多个函数

时间:2019-05-26 18:51:47

标签: python pyroot

我正在尝试从一个函数中调用一个函数,而前者则调用了更多函数,等等。

import ROOT as root
import sys, math

class SFs():
    def __init__(self):
        self.get_EfficiencyData = None
        self.get_EfficiencyMC = None
        self.get_ScaleFactor = None
        self.eff_dataH = None
        self.eff_mcH = None
        self.get_ScaleFactor = None
        self.get_EfficiencyMC = None

    def ScaleFactor(self,inputRootFile):
        self.eff_dataH = root.std.map("string", root.TGraphAsymmErrors)()
        self.eff_mcH = root.std.map("string", root.TGraphAsymmErrors)()
        EtaBins=["Lt0p9", "0p9to1p2","1p2to2p1","Gt2p1"]

        fileIn = root.TFile(inputRootFile,"read")
        HistoBaseName = "Label"
        etaBinsH = fileIn.Get("Bins")
        # etaLabel, GraphName
        nEtaBins = int(etaBinsH.GetNbinsX())
        for iBin in range (0, nEtaBins):
            etaLabel = EtaBins[iBin]
            GraphName = HistoBaseName+etaLabel+"_Data"
        print GraphName,etaLabel

        self.eff_dataH[etaLabel]=fileIn.Get(str(GraphName))
        self.eff_mcH[etaLabel]=fileIn.Get("histo")

        print self.eff_mcH[etaLabel].GetXaxis().GetNbins()
        print self.eff_mcH[etaLabel].GetX()[5]
        self.get_ScaleFactor(46.8,2.0)


    def get_ScaleFactor(self,pt, eta):

        efficiency_mc = get_EfficiencyMC(pt, eta)
        if  efficiency_mc != 0.:
            SF = 1/float(efficiency_mc)
        else:
            SF=1.

        return SF


    def get_EfficiencyMC(self,pt, eta):

        label = FindEtaLabel(eta,"mc")
        # label= "Lt0p9"
        binNumber = etaBinsH.GetXaxis().FindFixBin(eta)
        label = etaBinsH.GetXaxis().GetBinLabel(binNumber)
        ptbin = FindPtBin(eff_mcH, label, pt)
        Eta = math.fabs(eta)
        print "eff_mcH ==================",eff_mcH,binNumber,label,ptbin
        # ptbin=10
        if ptbin == -99: eff =1
        else: eff= eff_mcH[label].GetY()[ptbin-1]

        if eff > 1.: eff = -1
        if eff < 0: eff = 0.
        print "inside eff_mc",eff
        return eff

    sf = SFs()
    sf.ScaleFactor("Muon_IsoMu27.root")

我想调用get_ScaleFactor(),依次调用get_EfficiencyMC()等,但是一旦我尝试调用前者,就会收到TypeError:'NoneType'对象不可调用

1 个答案:

答案 0 :(得分:0)

在您的类初始化中定义:

self.get_EfficiencyMC =无

,然后定义一个函数(即类方法) def get_EfficiencyMC(self,pt,eta):

在创建类的实例时,将在部分类方法的阴影下执行init部分。只需将其从init中删除,或更改方法名称即可。