def run_egammaMVACalibMulti(outputfile, inputTree, inputPath, useTMVA, method, particleType, calibrationType, nEvents, branchName, copyBranches='input', shift=0, fudge=False, debug=False, etaBinDef = '', energyBinDef = '', particleTypeVar='', filePattern='', ignoreSpectators=True, interact=False, friend = False, defs=None): """run_egammaMVACalibMulti(outputfile, inputTree, inputPath, useTMVA, method, particleType, calibrationType, nEvents, branchName, copyBranches='input', shift=0, fudge=False, debug=False, etaBinDef = '', energyBinDef = '', particleTypeVar='', filePattern='', ignoreSpectators=True, interact=False, friend = False, defs=None)""" print "Running egammaMVACalibMulti" # Convert all the inputs to lists and make sure all the lists have the same size # (lists that have size 1 are multiplied by N, the maximum size) iter_options = map(make_list, (inputPath, useTMVA, method, calibrationType, shift, defs)) N = max(len(i) for i in iter_options) if any(len(i) not in (1, N) for i in iter_options): raise ValueError("Invalid input: %s" % iter_options) iter_options = (make_list(i[0], N) if len(i) == 1 else i for i in iter_options) loop = zip(*iter_options) # print loop if len(loop) != len(branchName): raise ValueError("Multiple options passed, branchName must have %s values, got %s" % \ (len(loop), len(branchName)) ) if len(set(branchName)) != len(branchName): raise ValueError("Repeated values for branchName: %s" % branchName) import ROOT loadLibs() M = ROOT.egammaMVACalibMulti(particleType, inputTree, copyBranches) for i,j in enumerate(loop): inputPath, useTMVA, method, calibrationType, shift, defs = j if fudge is None: print ROOT.gSystem.WorkingDirectory() params = particleType, not useTMVA, inputPath, method, calibrationType, debug, \ etaBinDef, energyBinDef, particleTypeVar, filePattern, ignoreSpectators if fudge is None: m = ROOT.egammaMVACalib(*params) else: m = ROOT.egammaMVACalibFudge(fudge, *params) setDefinitions(m, defs) M.Add(m, branchName[i]) if shift is not None and shift >= 0: M.GetInstance().setPeakCorrection(shift) if interact: doInteract(**locals()) return fout = ROOT.TFile(outputfile, 'update') mvaTree = M.Run(nEvents) mvaTree.Print() if friend: mvaTree.AddFriend(inputTree) mvaTree.Write('', ROOT.TObject.kOverwrite) fout.Close()
def run_egammaMVACalib(outputfile, inputTree, inputPath='egammaMVACalib/offline/v3', useTMVA='', particleType=0, method='BDTG', calibrationType=1, nEvents=-1, debug=False, printBranches=False, branchName="", copyBranches="input", shift=0, fudge=None, etaBinDef='', energyBinDef='', particleTypeVar= '', filePattern="", ignoreSpectators = True, interact=False, first_event=0, friend=False, defs=None): """run_egammaMVACalib(outputfile, inputTree, inputPath='egammaMVACalib/offline/v3', useTMVA='', particleType=0, method='BDTG', calibrationType=1, nEvents=-1, debug=False, printBranches=False, branchName="", copyBranches="input", shift=0, fudge=None, etaBinDef='', energyBinDef='', particleTypeVar= '', filePattern="", ignoreSpectators = True, interact=False, first_event=0, friend=False, defs=None)""" iter_options = inputPath, useTMVA, method, calibrationType, shift, branchName, defs if any(isinstance(i, (list,tuple)) and len(i) > 1 for i in iter_options): inputPath, useTMVA, method, calibrationType, shift, branchName, defs = iter_options return run_egammaMVACalibMulti(outputfile, inputTree, inputPath, useTMVA, method, particleType, calibrationType, nEvents, branchName, copyBranches, shift, fudge, debug, etaBinDef, energyBinDef, particleTypeVar, filePattern, ignoreSpectators, first_event, friend, defs) # FIXME else: # Make sure we do not have lists inputPath, useTMVA, method, calibrationType, shift, branchName, defs = \ (i[0] if isinstance(i, (list,tuple)) else i for i in iter_options) import ROOT loadLibs() params = particleType, not useTMVA, inputPath, method, calibrationType, debug, \ etaBinDef, energyBinDef, particleTypeVar, filePattern, ignoreSpectators if fudge is None: m = ROOT.egammaMVACalib(*params) else: m = ROOT.egammaMVACalibFudge(fudge, *params) if shift is not None and shift >= 0: m.setPeakCorrection(shift) setDefinitions(m, defs) if printBranches: m.InitTree(inputTree) for i in m.getListOfBranches(): print i.GetName() return elif interact: doInteract(**locals()) return fout = ROOT.TFile(outputfile, 'update') mvaTree = m.getMVAResponseTree(inputTree, nEvents, branchName, copyBranches, first_event) mvaTree.Print() if friend: mvaTree.AddFriend(inputTree) mvaTree.Write('', ROOT.TObject.kOverwrite) fout.Close()