def putHistogram(self, channelName, datasetName, systematicName, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param datasetName the name of the dataset # @param systematicName the name of the systematic variation # @param var the variable # @param cut the cut # @param histogram the histogram object if not self._open(): return None path, histogramName = self._buildPath(channelName, datasetName, systematicName) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self.file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) if 'resopara' in histogramName or 'resoperp' in histogramName: if '_low' in histogramName: histogramName.replace('_low', '') elif '_high' in histogramName: return histogram.SetTitle(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def putHistogram(self, dataset, systematicVariation, var, cut, histogram): ## update or put a new histogram into store based on how it was created # @param dataset Dataset object # @param systematicVariation SystematicVariation object # @param var Variable object # @param cut Cut object # @param histogram ROOT::TH1 object #if not self._open( dataset, systematicVariation, var, cut ): # return None if not self._open(dataset, systematicVariation, var, cut, 'update'): self.logger.warning( 'putHistogram(): unable to store histogram, file not open') return None path, histogramName = self._buildPath(dataset, systematicVariation, var, cut) self.logger.debug('putHistogram(): storing histogram "%s/%s"' % (path, histogramName)) self._file.cd() from ROOT import TObject, gDirectory for directory in path.split('/'): if not gDirectory.GetDirectory(directory): gDirectory.mkdir(directory) gDirectory.cd(directory) histogram = histogram.Clone(histogramName) histogram.Write(histogramName, TObject.kOverwrite)
def saveToFile(h, fileName, directory, name): f = TFile(fileName, 'UPDATE') path = '/' ## Create the directory tree for dir in directory.split('/'): path += dir + '/' if not gDirectory.Get(dir): gDirectory.mkdir(dir) gDirectory.cd(dir) ## Now the tree is written, move to the dir f.cd(path) hc = h.Clone() hc.Write(name, TObject.kOverwrite) f.Close()
def LoopOverKeys(currentDir, testFile, outputFile): testFile.cd(currentDir) for key in gDirectory.GetListOfKeys(): # Loop over directories if re.match ('TH1', key.GetClassName()): #found a 1D histogram MakeOnePlot(currentDir, key.GetName()) if (key.GetClassName() == "TDirectoryFile"): if arguments.verbose: print "Looping over directory: ", key.GetName() if currentDir == "": histDir = key.GetName() else: histDir = currentDir + "/" + key.GetName() outputFile.cd() gDirectory.mkdir(histDir) outputFile.cd(histDir) LoopOverKeys(histDir, testFile, outputFile)
def LoopAndScale(dir,Weight): """goes through all histos and reweights them""" nextkey=TIter(dir.GetListOfKeys()) key=nextkey() #================================================= #Iterate over the histos in the file #ListOfNewH=[] while(str(key).find('nil') == -1): #GET THE KEY obj=dir.Get(key.GetName() ) #print 'the current directory is ',gDirectory.pwd() #rint 'currently at object',obj.GetName() #raw_input() # #IF a histo # # if obj.IsA().InheritsFrom("TH1"): newhist=obj.Clone(key.GetName()) SetOwnership(newhist,False) newhist.Scale(Weight) elif obj.IsA().InheritsFrom("TDirectory"): newdir=gDirectory.mkdir(obj.GetName()) newdir.cd() LoopAndScale(obj,Weight) # # #set it to return back to where it was gDirectory.GetMotherDir().cd() else: print "Unknown object" print "It is called ",key.GetName() # #dir.cd() key=nextkey()
for key in inputFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue outputFile.cd() rootDirectory = key.GetName() outputFile.mkdir(rootDirectory) inputFile.cd(rootDirectory) for key2 in gDirectory.GetListOfKeys(): if (key2.GetClassName() != "TDirectoryFile"): continue current_channel_name = key2.GetName() if current_channel_name in channel_map: input_channels.append(current_channel_name) outputFile.cd(rootDirectory) gDirectory.mkdir(channel_map[current_channel_name]) #do the thing for cutflow histograms inputFile.cd(rootDirectory) for key in gDirectory.GetListOfKeys( ): # loop over histograms in the current directory if not re.match(r"TH[123]", key.GetClassName()): continue histogramName = key.GetName() for channel in input_channels: if histogramName.find(channel) is -1: continue Histogram = inputFile.Get(rootDirectory + "/" + histogramName).Clone() Histogram.SetDirectory(0)
def CreateSystFile(Args, TDirectoryNames, Objects, Variables, NominalSamples, SystSamples, DataSamples, EventVar): logging.info("---------------------------------------------------") logging.info("Beginning file creation needed for plotting") logging.info("---------------------------------------------------") logging.info('Input path is: ' + str(Args.InputPath)) # Open up the output file OutFile = tfile(Args.OutputFile,"UPDATE") logging.info("Outputting everything to: " + str(Args.OutputFile)) AllInputDirectories = NominalSamples + SystSamples + DataSamples AllInputDirsClone = list(AllInputDirectories) for TDirectoryName in TDirectoryNames: # Using a certain TDirectory, calculate all the systematics logging.info("Calculating systematic uncertainties for the directory " + TDirectoryName) OutFile.mkdir(TDirectoryName) for Sample in AllInputDirectories: # Take a samples directory, get the nominal file from that directory and compare it for each systematic logging.info("Working in directory: " + str(Sample)) OutFile.cd(TDirectoryName) gDirectory.mkdir(Sample) base = Args.InputPath + Sample + "/" # Get the nominal file if "data" in Sample: nominal_file = Sample+"_data_combination.root" elif Sample == "FTAG2_ttbar_PhPy8_hdamp3mtop": nominal_file = Sample+"_weight_mc_rad_UP_combination.root" else: nominal_file = Sample+"_nominal_combination.root" NomFile = tfile(base+nominal_file) if NomFile == None: continue logging.debug("Successfully opened nominal file: " + nominal_file) # Get a list of the systematic files if not "data" in Sample and Sample != "FTAG2_ttbar_PhPy8_hdamp3mtop": SystFiles = [f for f in os.listdir(base) if "FTAG2_" in f] SystFiles.remove(nominal_file) # if Sample == "FTAG2_ttbar_PhPy8": # bad_file = "FTAG2_ttbar_PhPy8_weight_mc_shower_np_131_combination.root" # SystFiles.remove(bad_file) else: SystFiles = [] CompletedMethod = CalculateSystematics(NomFile, SystFiles, TDirectoryName, base, OutFile, Sample, Objects, Variables, EventVar) if not CompletedMethod: logging.info("Removing sample with missing histograms "+str(Sample)) AllInputDirsClone.remove(Sample) NomFile.Close() OutFile.cd(TDirectoryName) OutFile.Write('', ROOT.TObject.kOverwrite) logging.info("-----------------------------------------") logging.info("Now calculating each total uncertainty ...") logging.info("-----------------------------------------") for TDirectoryName in TDirectoryNames: logging.info("Creating syst band for the TDirectoryName: " + TDirectoryName) for Sample in AllInputDirsClone: if "data" in Sample: continue for Obj in Objects: logging.info("And object " + Obj) for Var in Variables: logging.info("and variable " + Var) if Sample in SystSamples: logging.debug("Calculating for a systematic sample and not tree based syst ... ") CalculateSampleUncertainty(OutFile, TDirectoryName, Obj, Var, Sample, EventVar, True, Args.InputPath) else: logging.debug("Calcating a combined tree based systematic uncertainty") CalculateSampleUncertainty(OutFile, TDirectoryName, Obj, Var, Sample, EventVar, False) OutFile.Close()
sys.path.append('python') ifile = sys.argv[1] cfgfile = sys.argv[2] ofile = sys.argv[3] iFILE = rt.TFile(ifile) oFILE = rt.TFile(ofile, "RECREATE") cfg = imp.load_source('final', cfgfile) for _cfg in cfg.cfg: hist = _cfg.getHist(iFILE) if not gDirectory.GetDirectory(_cfg.collection): gDirectory.mkdir(_cfg.collection + "/" + _cfg.ID) gDirectory.cd(_cfg.collection + "/" + _cfg.ID) else: gDirectory.cd(_cfg.collection) if not gDirectory.GetDirectory(_cfg.ID): gDirectory.mkdir(_cfg.ID) gDirectory.cd(_cfg.ID) for h in hist: h.Write() #CANVAS.Print(_cfg.collection + "_" + _cfg.ID + "_" + h.GetName() + ".pdf") gDirectory.cd("/") oFILE.Close()
if len(processed_datasets) is 0: sys.exit("No datasets have been processed") #### open first input file and re-make its directory structure in the output file testFile = TFile(bkgdSrc_dir + "/" + processed_datasets[0] + ".root") testFile.cd() for key in testFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue if not key.GetName () in bkgd_sources[bkgd]['channel_map']: continue rootDirectory = key.GetName() print "Found channel directory: ", key.GetName() for targetChannel in bkgd_sources[bkgd]['channel_map'][key.GetName()]: outputFile.cd() gDirectory.mkdir(targetChannel) if arguments.verbose: print "Debug: mkdir ", targetChannel channels.append(key.GetName()) if arguments.verbose: print "Debug: added channel: ", key.GetName() testFile.cd(key.GetName()) for key2 in gDirectory.GetListOfKeys(): if arguments.verbose: print "Debug: found key2: ", key2.GetName(), ", classname = ", key2.GetClassName() if (key2.GetClassName() != "TDirectoryFile"): continue for targetChannel in bkgd_sources[bkgd]['channel_map'][rootDirectory]: # outputFile.cd(key.GetName()) outputFile.cd()
def analyse_scenario(file_baseline, file_selection, ROOT_file, scenario_names, memory_cache, int_lumi): """Analyse a specific scenario. We receive in input two json files, @file_baseline and @file_selection, and perform a standard set of plot of information extracted from @file_selection versus information extracted from @file_baseline. """ gDirectory.cd('/') folder = '%s_vs_%s' % (scenario_names[int(re.match('json_(\d+).*', os.path.splitext(file_baseline)[0]).group(1))], scenario_names[int(re.match('json_(\d+).*', os.path.splitext(file_selection)[0]).group(1))]) gDirectory.mkdir(folder) gDirectory.cd(folder) full_file = open(file_baseline, 'r') full_file_content = [''.join(l) for l in full_file.readlines()] full_object = cjson.decode(full_file_content[0]) selection_file = open(file_selection, 'r') selection_file_content = [''.join(l) for l in selection_file.readlines()] selection_object = cjson.decode(selection_file_content[0]) sum_inv_tot_w = 0 w_eff_rec_tot_over_del_tot = 0 w_eff_rec_tot_over_del_tot_numerator = 0 sum_inv_selection_w = 0 w_eff_rec_selection_over_rec_tot = 0 w_eff_rec_selection_over_rec_tot_numerator = 0 all_del_selection_lumi = 0 all_rec_selection_lumi = 0 run_list = [] list_del_tot_lumi = [] list_del_lumi = [] list_eff_rec_tot_over_del_tot = [] list_w_eff_rec_tot_over_del_tot = [] list_eff_rec_selection_over_rec_tot = [] list_w_eff_rec_selection_over_rec_tot = [] run_count = 0 print "\n%s vs %s" % (file_baseline, file_selection) for run in sorted(full_object.keys()): run_count += 1 if run_count%25 == 0: sys.stdout.write('.') sys.stdout.flush() run_list.append(int(run)) (del_tot_lumi, rec_tot_lumi) = get_luminosity_cached_from_memory(run, full_object[run], memory_cache, certified=True) (eff_rec_tot_over_del_tot, w_eff_rec_tot_over_del_tot, w_mean_numerator, sum_w) = compute_efficiencies(del_tot_lumi, rec_tot_lumi, rec_tot_lumi, w_eff_rec_tot_over_del_tot_numerator, sum_inv_tot_w) w_eff_rec_tot_over_del_tot_numerator = w_mean_numerator sum_inv_tot_w = sum_w list_eff_rec_tot_over_del_tot.append(eff_rec_tot_over_del_tot) list_w_eff_rec_tot_over_del_tot.append(w_eff_rec_tot_over_del_tot) list_del_tot_lumi.append(int_lumi[run][0]) list_del_lumi.append(del_tot_lumi) if run in selection_object.keys(): (del_selection_lumi, rec_selection_lumi) = get_luminosity_cached_from_memory(run, selection_object[run], memory_cache, certified=True) else: (del_selection_lumi, rec_selection_lumi) = (0., 0.) all_del_selection_lumi += del_selection_lumi all_rec_selection_lumi += rec_selection_lumi (eff_rec_selection_over_rec_tot, w_eff_rec_selection_over_rec_tot, w_mean_numerator, sum_w) = compute_efficiencies(rec_tot_lumi, rec_selection_lumi, rec_tot_lumi, w_eff_rec_selection_over_rec_tot_numerator, sum_inv_selection_w) w_eff_rec_selection_over_rec_tot_numerator = w_mean_numerator sum_inv_selection_w = sum_w list_eff_rec_selection_over_rec_tot.append(eff_rec_selection_over_rec_tot) list_w_eff_rec_selection_over_rec_tot.append(w_eff_rec_selection_over_rec_tot) x_axis_list = [(run_list, 'Vs_Run'), (list_del_tot_lumi, 'Vs_IntLumi'), (list_del_lumi, 'Vs_Lumi')] for x_type in x_axis_list: gDirectory.cd('/%s' % folder) x_folder = '%s' % x_type[1] gDirectory.mkdir('%s' % x_folder) gDirectory.cd('%s' % x_folder) ROOTsave(x_type[0], [{'values': list_eff_rec_tot_over_del_tot, 'y_label': 'rec_tot_vs_del_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_eff_rec_selection_over_rec_tot, 'y_label': 'rec_selection_vs_rec_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_w_eff_rec_tot_over_del_tot, 'y_label': 'running_wm_rec_tot_vs_del_tot', 'y_range': [0., 1.05], 'y_projection': True}, {'values': list_w_eff_rec_selection_over_rec_tot, 'y_label': 'running_wm_rec_selection_vs_rec_tot', 'y_range': [0., 1.05], 'y_projection': True}, ])
#### open first input file and re-make its directory structure in the output file testFile = TFile(processed_datasets[0]+".root") testFile.cd() for key in testFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue outputFile.cd() outputFile.mkdir(key.GetName()) rootDirectory = key.GetName() testFile.cd(key.GetName()) for key2 in gDirectory.GetListOfKeys(): if (key2.GetClassName() != "TDirectoryFile"): continue outputFile.cd(key.GetName()) gDirectory.mkdir(key2.GetName()) channels.append(key2.GetName()) for channel in channels: # loop over final states, which each have their own directory if channel != "Isolated": continue testFile.cd(rootDirectory+"/"+channel) histograms = [] for key in gDirectory.GetListOfKeys(): if (key.GetClassName() == "TH2D" or key.GetClassName() == "TH1D"): histograms.append(key.GetName())
if template: myfile = TFile("%s%s.root" % (plotDirectory, myfilename), "update") # i have to get the nominal histogram from root file first and get the integration value if systematics == '': myDatahist = rebinnedData.Clone("nominal") mydataDir = "%s/data_obs/" % channel if myfile.GetDirectory(mydataDir): gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() else: gDirectory.mkdir(mydataDir) gDirectory.cd(mydataDir) gDirectory.Delete("*;*") myDatahist.Write() # create directory only if it does not exist ### ele channel for iprocess in template_category.keys(): myfile.cd() mydir = "%s/%s/" % (channel, iprocess) #print "%s/%s/"%(channel,iprocess) if systematics == '': myhist = rebinnedHist[iprocess].Clone("nominal") else: myhist = rebinnedHist[iprocess].Clone("%s%s" %
else: channelDirectory = first_input['channel'] + "Plotter" testFile.cd(channelDirectory) if arguments.savePDFs: try: shutil.rmtree ("comparison_histograms_pdfs") except OSError: pass os.mkdir ("comparison_histograms_pdfs") if arguments.generic: outputFile.cd() gDirectory.mkdir(channelDirectory) LoopOverKeys(channelDirectory, testFile, outputFile) else: for key in gDirectory.GetListOfKeys(): # Loop over directories in same way as in makePlots.py if re.match ('TH1', key.GetClassName()): #found a 1D histogram MakeOnePlot("", key.GetName()) if (key.GetClassName() != "TDirectoryFile"): continue if arguments.verbose: print "Checking key: ", key.GetName() histogramDirectory = key.GetName() outputFile.cd() gDirectory.mkdir(histogramDirectory) outputFile.cd(key.GetName())
for key in testFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue outputFile.cd() outputFile.mkdir(key.GetName()) rootDirectory = key.GetName() testFile.cd(key.GetName()) for key2 in gDirectory.GetListOfKeys(): if (key2.GetClassName() != "TDirectoryFile"): continue if key2.GetName() in qcd_from_data['channel_map']: channels.append(key2.GetName()) for targetChannel in qcd_from_data['channel_map'][key2.GetName()]: outputFile.cd(key.GetName()) gDirectory.mkdir(targetChannel) def applySF(Histogram, sf, sfError): xLimit = Histogram.GetNbinsX() + 2 yLimit = Histogram.GetNbinsY() + 2 zLimit = Histogram.GetNbinsZ() + 2 if yLimit == 3: yLimit = 1 if zLimit == 3: zLimit = 1 for x in range(0, xLimit): for y in range(0, yLimit): for z in range(0, zLimit): bin = Histogram.GetBin(x, y, z) content = Histogram.GetBinContent(bin)
if histo.GetNbinsX() > 0: histo = histo.Rebin(len(binning)-1,histo.GetName(),binning) histo2 = copy.deepcopy(histo) for bin in range(histo2.GetNbinsX()+1) : if 'QCD' in histo2.GetName() or 'ZTT' in histo2.GetName() and not 'tau' in histo2.GetName(): if histo2.GetBinContent(bin)<0.001: histo2.SetBinContent(bin,0.001) histo2.SetBinError(bin,1.) hists.append(histo2) gDirectory.cd('..') grandeFile.Close() fileFinale = TFile.Open('htt_tt.inputs-sm-8TeV-rebinned.root','recreate') gDirectory.mkdir('tauTau_boost') gDirectory.cd('tauTau_boost') for h in histosB : h.Write() gDirectory.cd('..') gDirectory.mkdir('tauTau_vbf') gDirectory.cd('tauTau_vbf') for h in histosV : h.Write() fileFinale.Close() os.system('cp htt_tt.inputs-sm-8TeV.root htt_tt.inputs-sm-8TeV-notRebinned.root') os.system('cp htt_tt.inputs-sm-8TeV-rebinned.root htt_tt.inputs-sm-8TeV.root')
def fhadd(self, prefix="", force=False, verbose=False, slow=True): """ taken from https://root.cern.ch/phpBB3/viewtopic.php?t=14881 This function will merge objects from a list of root files and write them to a target root file. The target file is newly created and must not exist, or if -f ("force") is given, must not be one of the source files. IMPORTANT: It is required that all files have the same content! Fast but memory hungry alternative to ROOT's hadd. Arguments: target -- name of the target root file sources -- list of source root files classname -- restrict merging to objects inheriting from classname force -- overwrite target file if exists """ target = prefix + self.Name + ".root" sources = [j.Output for j in self.Jobs] TH1.AddDirectory(False) # check if target file exists and exit if it does and not in force mode if not force and os.path.exists(target): raise RuntimeError("target file %s exists" % target) # open the target file print "fhadd Target file:", target outfile = TFile(target, "RECREATE") # open the seed file - contents is looked up from here seedfilename = sources[0] print "fhadd Source file 1", seedfilename seedfile = TFile(seedfilename) # get contents of seed file print "looping over seed file" contents = self.loop(seedfile) print "done %d objects are ready to be merged" % len(contents) if (verbose): for c in contents: print c # open remaining files otherfiles = [] for n, f in enumerate(sources[1:]): print "fhadd Source file %d: %s" % (n + 2, f) otherfiles.append(TFile(f)) # loop over contents and merge objects from other files to seed file objects for n, (path, hname) in enumerate(contents): print "fhadd Target object: %s" % os.path.join(path, hname) obj_path = os.path.join(path, hname) obj_ = seedfile.Get(obj_path[1:]) outfile.cd('/') # create target directory structure for d in path.split('/')[1:]: directory = gDirectory.GetDirectory(d) if not directory: gDirectory.mkdir(d).cd() else: gDirectory.cd(d) obj = None if obj_.InheritsFrom("TTree"): obj = obj_.CloneTree() else: obj = obj_.Clone() # merge objects l = TList() for o in [of.Get(obj_path[1:]) for of in otherfiles]: l.Add(o) obj.Merge(l) # delete objects if in slow mode if slow: print "Deleting %d object(s)", l.GetEntries() l.Delete() # write object to target obj.Write(obj.GetName(), TObject.kOverwrite) print "Writing and closing file" # let ROOT forget about open files - prevents deletion of TKeys for f in [outfile, seedfile] + otherfiles: gROOT.GetListOfFiles().Remove(f) outfile.Write() outfile.Close() for f in [seedfile] + otherfiles: f.Close()
#### open first input file and re-make its directory structure in the output file testFile = TFile(processed_datasets[0] + ".root") testFile.cd() for key in testFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue outputFile.cd() outputFile.mkdir(key.GetName()) rootDirectory = key.GetName() testFile.cd(key.GetName()) for key2 in gDirectory.GetListOfKeys(): if (key2.GetClassName() != "TDirectoryFile"): continue outputFile.cd(key.GetName()) gDirectory.mkdir(key2.GetName()) channels.append(key2.GetName()) for channel in channels: # loop over final states, which each have their own directory if channel != "Isolated": continue testFile.cd(rootDirectory + "/" + channel) histograms = [] for key in gDirectory.GetListOfKeys(): if (key.GetClassName() == "TH2D" or key.GetClassName() == "TH1D"): histograms.append(key.GetName()) if histogramNames: histograms = histogramNames
m = TH1F() m1 = TH1F() m2 = TH1F() m = myfile.Get("HLT/ResultMon/HLTResultEF;1") m1 = myfile.Get("HLT/ResultMon/HLTResultL2;1") m2 = myfile.Get("HLT/ResultMon/L1Events;1") mynewfile = TFile('histo_new.root','recreate') mynewfile.mkdir('HLT') mynewfile.cd('HLT') gDirectory.pwd() gDirectory.mkdir('EgammaMon') mynewfile.cd('HLT/EgammaMon') gDirectory.pwd() gDirectory.mkdir('L1') mynewfile.cd('HLT/EgammaMon/L1') h.Write() h1.Write() h2.Write() h3.Write() h4.Write() h5.Write() gDirectory.cd('../') gDirectory.pwd() gDirectory.mkdir('L2Calo')
for key in inputFile.GetListOfKeys(): if (key.GetClassName() != "TDirectoryFile"): continue outputFile.cd() rootDirectory = key.GetName() outputFile.mkdir(rootDirectory) inputFile.cd(rootDirectory) for key2 in gDirectory.GetListOfKeys(): if (key2.GetClassName() != "TDirectoryFile"): continue current_channel_name = key2.GetName() if current_channel_name in channel_map: input_channels.append(current_channel_name) outputFile.cd(rootDirectory) gDirectory.mkdir(channel_map[current_channel_name]) #do the thing for cutflow histograms inputFile.cd(rootDirectory) for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory if not re.match (r"TH[123]", key.GetClassName()): continue histogramName = key.GetName() for channel in input_channels: if histogramName.find(channel) is -1: continue Histogram = inputFile.Get(rootDirectory+"/"+histogramName).Clone() Histogram.SetDirectory(0)
def fhadd(self, force=False, verbose=False, slow=True): """ taken from https://root.cern.ch/phpBB3/viewtopic.php?t=14881 This function will merge objects from a list of root files and write them to a target root file. The target file is newly created and must not exist, or if -f ("force") is given, must not be one of the source files. IMPORTANT: It is required that all files have the same content! Fast but memory hungry alternative to ROOT's hadd. Arguments: target -- name of the target root file sources -- list of source root files classname -- restrict merging to objects inheriting from classname force -- overwrite target file if exists """ target = self.Name + ".root" sources = self.Jobs TH1.AddDirectory(False) # check if target file exists and exit if it does and not in force mode if not force and os.path.exists(target): raise RuntimeError("target file %s exists" % target) # open the target file print "fhadd Target file:", target outfile = TFile(target, "RECREATE") # open the seed file - contents is looked up from here seedfilename = sources[0] print "fhadd Source file 1", seedfilename seedfile = TFile(seedfilename) # get contents of seed file print "looping over seed file" contents = self.loop(seedfile) print "done %d objects are ready to be merged" % len(contents) if( verbose ): for c in contents: print c # open remaining files otherfiles = [] for n, f in enumerate(sources[1:]): #print "fhadd Source file %d: %s" % (n+2, f) otherfiles.append(TFile(f)) cut = "" #(CMS_hgg_mass > 100 && CMS_hgg_mass < 180) && (diphoMVA > -0.4) && (n_loose_ele == 1 || n_LooseMu25 == 1) && (MET_pt > 30)" # loop over contents and merge objects from other files to seed file objects for n, (path, hname) in enumerate(contents): #print "fhadd Target object: %s" % os.path.join(path, hname) obj_path = os.path.join(path, hname) obj_ = seedfile.Get(obj_path[1:]) outfile.cd('/') # create target directory structure for d in path.split('/')[1:]: directory = gDirectory.GetDirectory(d) if not directory: gDirectory.mkdir(d).cd() else: gDirectory.cd(d) obj = None IsTree = False #if "sigma" in obj_.GetName(): # continue if obj_.InheritsFrom("TTree"): #obj = obj_.CloneTree() obj = obj_.CopyTree( cut ) IsTree = True else: continue obj = obj_.Clone() # merge objects l = TList() for o in [of.Get(obj_path[1:]) for of in otherfiles]: if IsTree : l.Add( o.CopyTree( cut ) ) else : l.Add(o) obj.Merge(l) # delete objects if in slow mode if slow: #print "Deleting %d object(s)", l.GetEntries() l.Delete() # write object to target obj.Write(obj.GetName(), TObject.kOverwrite) print "Writing and closing file" # let ROOT forget about open files - prevents deletion of TKeys for f in [outfile, seedfile]+otherfiles: gROOT.GetListOfFiles().Remove(f); outfile.Write() outfile.Close() for f in [seedfile]+otherfiles: f.Close() print "fhadd completed"