def get_histograms_from_trees( trees = [], branch = 'var', weightBranch = 'EventWeight', selection = '1', files = {}, verbose = False, nBins = 40, xMin = 0, xMax = 100, ignoreUnderflow = True, ): histograms = {} nHistograms = 0 # Setup selection and weight string for ttree draw weightAndSelection = '( %s ) * ( %s )' % ( weightBranch, selection ) for sample, input_file in files.iteritems(): histograms[sample] = {} for tree in trees: tempTree = tree if 'data' in sample and ( 'Up' in tempTree or 'Down' in tempTree ) : tempTree = tempTree.replace('_'+tempTree.split('_')[-1],'') chain = None; if isinstance( input_file, list ): for f in input_file: chain.Add(f) else: chain = TreeChain(tempTree, [input_file]); weightAndSelection = '( %s ) * ( %s )' % ( weightBranch, selection ) root_histogram = Hist( nBins, xMin, xMax, type='D') chain.Draw(branch, weightAndSelection, hist = root_histogram) if not is_valid_histogram( root_histogram, tree, input_file): return # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it if ignoreUnderflow: root_histogram.SetBinContent(0, 0) root_histogram.SetBinError(0,0) gcd() nHistograms += 1 histograms[sample][tree] = root_histogram.Clone() return histograms
tree = Tree("test") tree.create_branches(branches) for i in range(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.) tree.i = i tree.fill() tree.write() # Make a histogram of the second tree hist2 = Hist(100, -10, 10, name='hist2') tree.Draw('x', 'y > 1', hist=hist2) hist2.SetDirectory(0) # memory resident print("The second tree has {0:f} entries where y > 1".format(hist2.Integral())) f.close() combined_hist = hist1 + hist2 print("Building TreeChain") chain = TreeChain('test', ['chaintest2.root', 'chaintest1.root']) # Make the equivalent of the combined_hist combined_hist_chain = Hist(100, -10, 10, name='combined') chain.Draw('x', 'y > 1', hist=combined_hist_chain) residual = combined_hist_chain - combined_hist print("The combined histogram (separately) minus " "the combined from the chain has {0:f} entries".format( residual.Integral()))
print "Creating test tree in chaintest2.root" f = open("chaintest2.root", "recreate") tree = Tree("test") tree.create_branches([('x', 'F'), ('y', 'F'), ('z', 'F'), ('i', 'I')]) for i in xrange(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.) tree.i = i tree.fill() tree.write() # Make a histogram of the second tree hist2 = tree.Draw('x', 'y > 1', min=-10, max=10, bins=100).Clone() hist2.SetName('hist2') hist2.SetDirectory(0) # memory resident print "The second tree has %f entries where y > 1" % hist2.Integral() f.close() combined_hist = hist1 + hist2 print "Building TreeChain" chain = TreeChain('test', ['chaintest2.root', 'chaintest1.root']) # Make the equivalent of the combined_hist combined_hist_chain = chain.Draw('x', 'y > 1', min=-10, max=10, bins=100) residual = combined_hist_chain - combined_hist print "The combined histogram (separately) minus the combined from the chain "\ "has %f entries" % residual.Integral()
'SingleTop_tree.root', 'TTJets_PowhegPythia8_tree.root', 'VJets_tree.root', 'QCD_Muon_tree.root', 'QCD_Electron_tree.root' ] mc_files = glob.glob(base + 'TTJets_PowhegPythia8_tree.root') data_files_el = glob.glob(base + 'data_electron_tree.root') data_files_mu = glob.glob(base + 'data_muon_tree.root') pu_el_mc_reco = Hist(60, 0.5, 60.5, name='pu_el_mc_reco') pu_mu_mc_reco = Hist(60, 0.5, 60.5, name='pu_mu_mc_reco') pu_el_reco = Hist(60, 0.5, 60.5, name='pu_el_reco') pu_mu_reco = Hist(60, 0.5, 60.5, name='pu_mu_reco') el_mc_chain = TreeChain( "TTbar_plus_X_analysis/EPlusJets/Ref selection/Pileup/Pileup", mc_files) el_mc_chain.Draw('NVertices', 'EventWeight * LeptonEfficiencyCorrection', hist=pu_el_mc_reco) mu_mc_chain = TreeChain( "TTbar_plus_X_analysis/MuPlusJets/Ref selection/Pileup/Pileup", mc_files) mu_mc_chain.Draw('NVertices', 'EventWeight * LeptonEfficiencyCorrection', hist=pu_mu_mc_reco) el_chain = TreeChain( "TTbar_plus_X_analysis/EPlusJets/Ref selection/Pileup/Pileup", data_files_el) el_chain.Draw('NVertices', hist=pu_el_reco) mu_chain = TreeChain( "TTbar_plus_X_analysis/MuPlusJets/Ref selection/Pileup/Pileup", data_files_mu) mu_chain.Draw('NVertices', hist=pu_mu_reco)