def GetHistogram(inp, out, nameOptionList): # clear the new dir dictionary (necessary if GetHistogram is to be used more than once) SetObjectDir("", "", None, True) # took me 30 mins to find this f***ing bug... changeCount = 0 inpFile = TFile(inp) ################### check for shift/expert/etc flags and construct the approprate list of histograms to get #################### nameList = [] metaDict = getMetadataDict(inpFile) for option in nameOptionList: optionList = option.split( ":") # optionList[0] = histogram name, optionList[1] = flag if len(optionList) == 1: if not optionList[0] in nameList: nameList.append(optionList[0]) else: # we have a flag if (not optionList[0].endswith("/")) and ( not optionList[0] == "" ): # flags go only with directories (i.e. name ends with "/") print >> sys.stderr, "Warning: Directory names should always end with \"/\" OR flag defined for histogram." if optionList[0] == "": # we have to get all flagged histograms for key in metaDict.keys(): for event in metaDict[key]: if event.LevelOfDetail == optionList[1]: histName = key + event.Name if not histName in nameList: changeCount += 1 nameList.append(histName) if changeCount == 0: print >> sys.stderr, "Warning: No histogram flagged", optionList[ 1], "found." else: # we have to get flagged histograms only from the specified directory try: for event in metaDict[optionList[0]]: if event.LevelOfDetail == optionList[1]: histName = optionList[0] + event.Name if not histName in nameList: changeCount += 1 nameList.append(histName) if changeCount == 0: print >> sys.stderr, "Warning: No histogram flagged", optionList[ 1], "found in", optionList[0], "." except KeyError: print >> sys.stderr, "Warning:", optionList[ 0], "directory/metadata object does not exist." ################################################################################################################################## outFile = TFile(out, "RECREATE") changeCount = 0 for path in nameList: objList = GetObjectList(inpFile, path) for obj in objList: # print "Setting path for '" + obj.GetName() + "' to '" + path + "'" changeCount = changeCount + 1 SetObjectDir(outFile, path, obj) outFile.Write() outFile.Flush() inpFile.Close() outFile.Close() if changeCount == 0: os.remove(out) return False else: return True