def recursiveMerge(target, infile, path='', cache={'TOTALLUMI': 0}, cutflow=True): l = infile.GetDirectory(path) keys = l.GetListOfKeys() cycles = {} for entry in range(keys.GetEntries()): name = keys.At(entry).GetName() + ";" + str(keys.At(entry).GetCycle()) if path: cachename = path + "/" + name else: cachename = name obj = l.Get(name) if type(obj) == TDirectoryFile: #print obj, "DIRECTORY" targetpath = keys.At(entry).GetName() if not target.Get(targetpath): target.mkdir(targetpath) recursiveMerge(target, infile, path + "/" + obj.GetName(), cache) elif type(obj) == TTree: # print obj, cachename, "TTree" cyclename, cyclenumber = cachename.split(';') if cyclename in cycles: continue # print cachename, "Used!" cycles[cyclename] = cyclenumber if not cyclename in cache: target.cd(path) cache[cyclename] = obj.CloneTree() else: objcached = cache[cyclename] col = TObjArray() col.Add(obj) objcached.Merge(col) elif issubclass(obj.__class__, TH1): #print obj, "TH1" if not cutflow and keys.At(entry).GetName() == "CutFlow": continue if not cachename in cache: target.cd(path) cache[cachename] = obj.Clone() else: objcached = cache[cachename] col = TObjArray() col.Add(obj) objcached.Merge(col) elif type(obj) == TObjString: #print type(obj), name, "TObjString" if obj: target.cd(path) objnew = TObjString(obj.GetString().Data()) objnew.Write(keys.At(entry).GetName()) cache['TOTALLUMI'] += 1 else: print "UNKNOWN OBJECT", name, "OF TYPE", type(obj)
class PickledStringMerger(DefaultMerger): pyobject_merge_registry = { int: int.__add__, long: long.__add__, set: set.union, list: list.__add__, str: str.__add__, unicode: unicode.__add__, dict: dict_merge, defaultdict: dict_merge, } def unpickle_string(self, string): try: return loads(string.GetName()) except UnpicklingError: raise UnableToMerge def __init__(self, first_object, target_directory): self.merged_object = self.unpickle_string(first_object) t = type(self.merged_object) self.merger_function = self.pyobject_merge_registry.get(t) if not self.merger_function: raise RuntimeError("I don't know how to merge objects of type %r" % t) def merge(self, next_object): do_merge = self.merger_function try: next_object = self.unpickle_string(next_object) except UnpicklingError: # Silently ignore non-python strings return self.merged_object = do_merge(self.merged_object, next_object) #UnpicklingError #new_value = self.merged_object.GetVal() + next_object.GetVal() #self.merged_object.SetVal(new_value) def finish(self, key_name): self.merged_object = TObjString(dumps(self.merged_object)) self.merged_object.Write(key_name)
def copyLumi(inputdir): inputpath = listifyInputFiles(inputdir) lumidir = outputFile.mkdir("Lumi") for d in inputpath: f = TFile.Open(d) try: l = f.GetDirectory("Lumi") keys = l.GetListOfKeys() for entry in range(keys.GetEntries()): objstr = l.Get( keys.At(entry).GetName() + ";" + str(keys.At(entry).GetCycle())) if objstr: lumidir.cd() objnew = TObjString(objstr.GetString().Data()) objnew.Write(keys.At(entry).GetName()) except: pass f.Close() outputFile.cd()
def recursiveMerge(target, infile, path='', cache={'TOTALLUMI': 0}, cutflow=True): l = infile.GetDirectory(path) keys = l.GetListOfKeys() cycles = {} #print("keys in input file: \n\n{0}\n\n".format(keys.ls())) for entry in range(keys.GetEntries()): name = keys.At(entry).GetName() + ";" + str(keys.At(entry).GetCycle()) if path: cachename = path + "/" + name else: cachename = name obj = l.Get(name) if type(obj) == TDirectoryFile: #print("TDirectory obj name: {0}".format(obj.GetName())) targetpath = keys.At(entry).GetName() if not target.Get(targetpath): target.mkdir(targetpath) recursiveMerge(target, infile, path + "/" + obj.GetName(), cache) elif type(obj) == TTree: #print("TTree obj name: {0} - cachename: {1} ".format(obj.GetName(), cachename)) cyclename, cyclenumber = cachename.split(';') if cyclename in cycles: continue #print("cyclename: {0} - cyclenumber: {1}".format(cyclename, cyclenumber)) cycles[cyclename] = cyclenumber if not cyclename in cache: #print("adding cyclename {0} to cache (via TTree::CloneTree())".format(cyclename)) target.cd(path) cache[cyclename] = obj.CloneTree() else: objcached = cache[cyclename] col = TObjArray() col.Add(obj) #print("merging TTree obj to cached object") objcached.Merge(col) elif issubclass(obj.__class__, TH1): #print("TH1 obj name: {0}".format(obj.GetName())) if not cutflow and keys.At(entry).GetName() == "CutFlow": continue if not cachename in cache: target.cd(path) cache[cachename] = obj.Clone() else: objcached = cache[cachename] col = TObjArray() col.Add(obj) objcached.Merge(col) elif type(obj) == TObjString: #print("TObjString obj name: {0}".format(obj.GetName())) if obj: target.cd(path) objnew = TObjString(obj.GetString().Data()) objnew.Write(keys.At(entry).GetName()) cache['TOTALLUMI'] += 1 elif issubclass(obj.__class__, TList): #print("TList obj name: {0}".format(obj.GetName())) if obj: target.cd(path) objnew = TList(obj) objnew.Write(keys.At(entry).GetName()) # not working... else: print "UNKNOWN OBJECT", name, "OF TYPE", type(obj)