def __contains__(self, other): ''' The other multiBin is contained in this multiBin if the intervals of all variables of this multiBin are larger than the corresponding intervals of the other multiBin. If this multiBin is binned in a variable in which the other multiBin is not binned, the other multiBin is not contained in this multiBin. If the other multiBin is binned in a variable in which this multiBin is not binned, the other multiBin is contained in this multiBin. ''' if isinstance(other, dict): other = multiBin(other) if not isinstance(other, multiBin): msg = "Cannot check whether object of type {0} is contained in a multiBin".format( type(other)) printErr(msg) raise ValueError(msg) for key in self.boundaries.keys(): if key in other.boundaries.keys(): if not self.boundaries[key][0] <= other.boundaries[key][0]: return False if not self.boundaries[key][1] >= other.boundaries[key][1]: return False else: return False return True
def __contains__(self, other): ''' The other multiBin is contained in this multiBin if the intervals of all variables of this multiBin are larger than the corresponding intervals of the other multiBin. If this multiBin is binned in a variable in which the other multiBin is not binned, the other multiBin is not contained in this multiBin. If the other multiBin is binned in a variable in which this multiBin is not binned, the other multiBin is contained in this multiBin. ''' if isinstance(other, dict): other = multiBin(other) if not isinstance(other, multiBin): msg = "Cannot check whether object of type {0} is contained in a multiBin".format(type(other)) printErr(msg) raise ValueError(msg) for key in self.boundaries.keys(): if key in other.boundaries.keys(): if not self.boundaries[key][0] <= other.boundaries[key][0]: return False if not self.boundaries[key][1] >= other.boundaries[key][1]: return False else: return False return True
def fromUniqueStr(cls, strIn): boundaries = {} if strIn: variables = strIn.split(',') for variable in variables: if variable.count('=') == 2: name, lower, upper = variable.split("=") boundaries[name] = (float(lower), float(upper)) else: printErr("Cannot get multiBin form string '{0}'".format(strIn)) return multiBin(boundaries)
def fromUniqueStr(cls, strIn): boundaries = {} if strIn: variables = strIn.split(',') for variable in variables: if variable.count('=') == 2: name, lower, upper = variable.split("=") boundaries[name] = (float(lower), float(upper)) else: printErr( "Cannot get multiBin form string '{0}'".format(strIn)) return multiBin(boundaries)
def getBestFitResultFromFile(fitResultFileName, massBinCenter = None, fitResultTreeName = "pwa", fitResultBranchName = "fitResult_v2" ): fitResultFile = ROOT.TFile.Open(fitResultFileName, "READ") if not fitResultFile: printErr("Could not open generated fit result file '" + fitResultFileName + "'.") return None fitResultTree = fitResultFile.Get(fitResultTreeName) if not fitResultTree: printErr("could not find fit result tree '" + fitResultTreeName + "' in file '" + fitResultFileName + "'. Aborting...") return None result = pyRootPwa.core.fitResult() result.setBranchAddress(fitResultTree, fitResultBranchName) # only print the warning about multiple mass-bin centers in one file # once printedMassBinCenterWarning = False bestResult = None for i in xrange(fitResultTree.GetEntries()): fitResultTree.GetEntry(i) # skip fit results that did no converge if not result.converged(): continue if bestResult is None: bestResult = pyRootPwa.core.fitResult(result) elif massBinCenter is None: if bestResult.massBinCenter() != result.massBinCenter() and not printedMassBinCenterWarning: printWarn("fit result file '" + fitResultFileName + "' " + "contains more than one mass bin, return the " + "fit result with the best likelihood.") printedMassBinCenterWarning = True if result.logLikelihood() < bestResult.logLikelihood(): bestResult = pyRootPwa.core.fitResult(result) else: if abs(massBinCenter - result.massBinCenter()) < abs(massBinCenter - bestResult.massBinCenter()): bestResult = pyRootPwa.core.fitResult(result) elif abs(massBinCenter - result.massBinCenter()) == abs(massBinCenter - bestResult.massBinCenter()): if result.logLikelihood() < bestResult.logLikelihood(): bestResult = pyRootPwa.core.fitResult(result) fitResultFile.Close() return bestResult
def getFitResultFromFile(fitResultFileName, fitResultTreeName="pwa", fitResultBranchName="fitResult_v2"): fitResultFile = ROOT.TFile.Open(fitResultFileName, "READ") if not fitResultFile: printErr("Could not open generated fit result file '" + fitResultFileName + "'.") return None fitResultTree = fitResultFile.Get(fitResultTreeName) if not fitResultTree: printErr("could not find fit result tree '" + fitResultTreeName + "' in file '" + fitResultFileName + "'. Aborting...") return None result = pyRootPwa.core.fitResult() result.setBranchAddress(fitResultTree, fitResultBranchName) if fitResultTree.GetEntries() != 1: printErr( "More than one fit result in TTree, somebody should probably implement this properly..." ) fitResultFile.Close() return None fitResultTree.GetEntry(0) if not result.converged(): printErr("Fit not converged for fit result file '" + fitResultFileName + "'.") return None fitResultFile.Close() return result
def getFitResultFromFile(fitResultFileName, fitResultTreeName = "pwa", fitResultBranchName = "fitResult_v2" ): fitResultFile = ROOT.TFile.Open(fitResultFileName, "READ") if not fitResultFile: printErr("Could not open generated fit result file '" + fitResultFileName + "'.") return None fitResultTree = fitResultFile.Get(fitResultTreeName) if not fitResultTree: printErr("could not find fit result tree '" + fitResultTreeName + "' in file '" + fitResultFileName + "'. Aborting...") return None result = pyRootPwa.core.fitResult() result.setBranchAddress(fitResultTree, fitResultBranchName) if fitResultTree.GetEntries() != 1: printErr("More than one fit result in TTree, somebody should probably implement this properly...") fitResultFile.Close() return None fitResultTree.GetEntry(0) if not result.converged(): printErr("Fit not converged for fit result file '" + fitResultFileName + "'.") return None fitResultFile.Close() return result
def binningMapFromArgList(argList): binningMap = {} if argList: for binArg in argList: try: splitUp = binArg.split(";") if len(splitUp)==3: binningMap[splitUp[0]] = (float(splitUp[1]), float(splitUp[2])) printInfo("adding to binning map: " + splitUp[0] + " -> (" + splitUp[1] + "," + splitUp[2] + ")") else: printErr("did not get the right amount of semicolon seperated values for " + splitUp[0] + "-bin.") return False except ValueError: printErr("could not convert binning map boundaries of " + splitUp[0] + "-bin to float. Aborting...") return False return binningMap
def multibinBoundariesFromArgList(argList): multibinBoundaries = {} if argList: for binArg in argList: try: splitUp = binArg.split(";") if len(splitUp)==3: multibinBoundaries[splitUp[0]] = (float(splitUp[1]), float(splitUp[2])) printInfo("adding to multibin boundaries: " + splitUp[0] + " -> (" + splitUp[1] + "," + splitUp[2] + ")") else: printErr("did not get the right amount of semicolon seperated values for " + splitUp[0] + "-bin.") return False except ValueError: printErr("could not convert multibin boundaries of " + splitUp[0] + "-bin to float. Aborting...") return False return multibinBoundaries
def getBestFitResultsFromFile(fitResultFileName, fitResultTreeName="pwa", fitResultBranchName="fitResult_v2"): fitResultFile = ROOT.TFile.Open(fitResultFileName, "READ") if not fitResultFile: printErr("Could not open generated fit result file '" + fitResultFileName + "'.") return None fitResultTree = fitResultFile.Get(fitResultTreeName) if not fitResultTree: printErr("could not find fit result tree '" + fitResultTreeName + "' in file '" + fitResultFileName + "'. Aborting...") return None result = pyRootPwa.core.fitResult() result.setBranchAddress(fitResultTree, fitResultBranchName) bestResults = {} for i in xrange(fitResultTree.GetEntries()): fitResultTree.GetEntry(i) # skip fit results that did no converge if not result.converged(): continue if not result.massBinCenter() in bestResults: bestResults[result.massBinCenter()] = pyRootPwa.core.fitResult( result) else: if result.logLikelihood() < bestResults[ result.massBinCenter()].logLikelihood(): bestResults[result.massBinCenter()] = pyRootPwa.core.fitResult( result) fitResultFile.Close() return bestResults
def getBestFitResultsFromFile(fitResultFileName, fitResultTreeName = "pwa", fitResultBranchName = "fitResult_v2" ): fitResultFile = ROOT.TFile.Open(fitResultFileName, "READ") if not fitResultFile: printErr("Could not open generated fit result file '" + fitResultFileName + "'.") return None fitResultTree = fitResultFile.Get(fitResultTreeName) if not fitResultTree: printErr("could not find fit result tree '" + fitResultTreeName + "' in file '" + fitResultFileName + "'. Aborting...") return None result = pyRootPwa.core.fitResult() result.setBranchAddress(fitResultTree, fitResultBranchName) bestResults = { } for i in xrange(fitResultTree.GetEntries()): fitResultTree.GetEntry(i) # skip fit results that did no converge if not result.converged(): continue if not result.massBinCenter() in bestResults: bestResults[result.massBinCenter()] = pyRootPwa.core.fitResult(result) else: if result.logLikelihood() < bestResults[result.massBinCenter()].logLikelihood(): bestResults[result.massBinCenter()] = pyRootPwa.core.fitResult(result) fitResultFile.Close() return bestResults