def __init__(self, *paths): self.filenames = [] for path in paths: self.filenames.extend(glob.glob(path)) self.files = [] for filename in self.filenames: self.files.append(io.open(filename, 'read')) # Sum them together. super(FileView, self).__init__(**self.files)
def tree_to_table(tree): """Convert a ROOT TTree to an astropy Table. Parameters ---------- Returns ------- table : astropy.table.Table """ from rootpy import asrootpy tree = asrootpy(tree) array = tree.to_array() from rootpy.io import open from rootpy.root2array import tree_to_recarray print('Reading %s' % infile) file = open(infile) tree_name = 'TableAllMu_WithoutNan' # 'ParTree_Postselect' print('Getting %s' % tree_name) tree = file.get(tree_name, ignore_unsupported=True) print('Converting tree to recarray') array = tree_to_recarray(tree) print('Converting recarray to atpy.Table') table = recarray_to_table(array) print('Removing empty columns:') empty_columns = [] for col in table.columns: #if table['col'].min() == table['col'].max() if (table['col'] == 0).all(): empty_columns.append(col) print(empty_columns) table.remove_columns(empty_columns) for name in array.dtype.names: # FITS can't save these types. data = array[name] if data.dtype == np.dtype('uint64'): data = data.copy().astype('int64') table.add_column(name, data) return table
def tree_to_table(tree, tree_name): """Convert a ROOT TTree to an astropy Table. Parameters ---------- tree : ROOT.TTree ROOT TTree Returns ------- table : `~astropy.table.Table` ROOT tree data as an astropy table. """ from rootpy import asrootpy from rootpy.io import open from rootpy.root2array import tree_to_recarray tree = asrootpy(tree) array = tree.to_array() file = open(infile) tree_name = 'TableAllMu_WithoutNan' # 'ParTree_Postselect' tree = file.get(tree_name, ignore_unsupported=True) array = tree_to_recarray(tree) table = recarray_to_table(array) # Remove empty columns empty_columns = [] for col in table.columns: #if table['col'].min() == table['col'].max() if (table['col'] == 0).all(): empty_columns.append(col) print(empty_columns) table.remove_columns(empty_columns) for name in array.dtype.names: # FITS can't save these types. data = array[name] if data.dtype == np.dtype('uint64'): data = data.copy().astype('int64') table.add_column(name, data) return table
#!/usr/bin/env python from rootpy.tree import Tree from rootpy.io import open from rootpy.root2array import tree_to_recarray_py, \ tree_to_recarray, \ tree_to_ndarray import cProfile import time with open('test.root') as f: tree = f.test branches = ["a_x", "a_y", "a_z"] tree.SetWeight(.5) print "Using pure Python method..." cProfile.run('arr1 = tree_to_recarray_py(tree, branches=branches,' 'include_weight=True)') print "time without profiler overhead:" t1 = time.time() arr1 = tree_to_recarray_py(tree, branches=branches, include_weight=True) t2 = time.time() print "%f seconds" % (t2 - t1) print '=' * 40
if args.verbose: logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) else: logging.basicConfig(level=logging.INFO, stream=sys.stderr) ################################# ###main part ############ ################################# from rootpy.plotting import views import rootpy.io as io import ROOT ROOT.gROOT.SetBatch(True) # Build view of input histograms log.info("Merging input files") input_files = [io.open(x) for x in args.input] input_view = views.SumView(*input_files) numerator = input_view.Get(args.num) denominator = input_view.Get(args.den) #Checks if rebinning is needed if (args.rebinX and args.rebinX > 1) or (args.rebinY and args.rebinY > 1): binx = args.rebinX if args.rebinX else 1 biny = args.rebinY if args.rebinY else 1 numerator.Rebin2D(binx, biny) denominator.Rebin2D(binx, biny) eff_map = numerator.Clone("efficiency_map") eff_map_statUp = numerator.Clone("efficiency_map_statUp") eff_map_statDown = numerator.Clone("efficiency_map_statDown") worse_rel_err = numerator.Clone("worse_rel_err")
parser = argparse.ArgumentParser() parser.add_argument("rootfile", help="ROOT file to embed label in") parser.add_argument("labels", metavar='label', nargs="+", help="Label(s) to embed. Format: key=value") args = parser.parse_args() for label in args.labels: if '=' not in label: print "Can't parse %s, you need to specify labels as key=val pars" \ % label sys.exit(1) # Don't import this until we know we need it from rootpy import io from ROOT import TText file = io.open(args.rootfile, 'UPDATE') file.cd() keep = [] for label in args.labels: key, val = tuple(label.split('=')) the_label = TText(0, 0, val) the_label.SetName(key) the_label.Write() keep.append(the_label) file.Write()
signal.SetLineColor(ROOT.EColor.kRed) signal.SetFillStyle(0) wz.SetTitle('WZ') zz.SetTitle('ZZ') fakes.SetTitle("Non-prompt") legend.AddEntry(signal, signal.GetTitle(), "l") legend.AddEntry(zz, 'ZZ', 'lf') legend.AddEntry(wz, 'WZ', 'lf') legend.AddEntry(fakes, 'Non-prompt', "lf") # Make M_ellell for x_title, filenamebase, units in [ ("#Delta R_{min}", 'histo_drmin_afterallothercuts', None), ("|m_{l^{+}l^{-}}-m_{Z}| (GeV)", 'histo_masszmin', 'GeV')]: mass_cut_file = io.open(filenamebase+'.root') hZZ = mass_cut_file.Get("histo2"); hWZ = mass_cut_file.Get("histo3"); hZJets = mass_cut_file.Get("histo4"); hData = mass_cut_file.Get("histo5"); hHWW = mass_cut_file.Get("histos"); hHWW = hHWW*5.0 hZZ.decorate(zz) hWZ.decorate(wz) hZJets.decorate(fakes) hData.decorate(data) hHWW.decorate(signal)
help='Plot fit error band') args = parser.parse_args(args[1:]) if args.verbose: logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) else: logging.basicConfig(level=logging.INFO, stream=sys.stderr) from rootpy.plotting import views import rootpy.io as io from FinalStateAnalysis.Utilities.rootbindings import ROOT # Build view of input histograms log.info("Merging input files") input_view = views.SumView(*[io.open(x) for x in args.input]) if args.rebin and args.rebin > 1: binning = None # if ',' in args.rebin: # print args.rebin.split(',') if args.rebin: binning = eval(args.rebin) if isinstance(binning, float) or isinstance(binning, int): newbinning = int(binning) elif isinstance(binning, list) and isinstance( binning[0], float) or isinstance(binning[0], int): newbinning = tuple(float(x) for x in binning) elif isinstance(binning, list) and isinstance(binning[0], list): # print args.rebin #print binning[0]
#!/usr/bin/env python """ ============================== ROOT.TFile made easy by rootpy ============================== This example demonstrates how basic file operations are made easier in rootpy. """ print __doc__ import shutil from rootpy.io import open, DoesNotExist from rootpy.plotting import Hist, Hist2D from rootpy.utils import asrootpy shutil.copyfile('temp.root', 'data.root') f = open('data.root') print f.a print f.a.b try: print f.a.b.c.d.e.f except DoesNotExist, e: print e for thing in f.walk(): print thing f.close() # supports with statements
def add_cms_blurb(sqrts, intlumi, preliminary=True, blurb=''): latex = ROOT.TLatex() latex.SetNDC() latex.SetTextSize(0.04) latex.SetTextAlign(31) latex.SetTextAlign(11) label_text = "CMS" if preliminary: label_text += " Preliminary" label_text += " %s TeV" % sqrts label_text += " L=%sfb^{-1}" % (intlumi) label_text += " " + blurb return latex.DrawLatex(0.18, 0.97, label_text) vh_7TeV = io.open('VHTT/cmb/common/vhtt.input_7TeV.root') vh_8TeV = io.open('VHTT/cmb/common/vhtt.input_8TeV.root') vh_7TeV_pf = io.open('../HiggsAnalysis/HiggsToTauTau/test/root_postfit/vhtt.input_7TeV.root') vh_8TeV_pf = io.open('../HiggsAnalysis/HiggsToTauTau/test/root_postfit/vhtt.input_8TeV.root') # We only care about 7 + 8 TeV for pf_suffix, (vh7file, vh8file) in [('', (vh_7TeV, vh_8TeV)), ('_postfit', (vh_7TeV_pf, vh_8TeV_pf))]: vh_7and8 = views.SumView(vh7file, vh8file) for period, vh_combined in [('7+8', vh_7and8), ('7', vh7file), ('8', vh8file)]: # Now combine sub-channels llt_combined = views.SumView(*[ views.SubdirectoryView(vh_combined, x) for x in ['emt', 'mmt', ]]) zh_combined = views.SumView(*[
Print out signal efficiencies. ''' import logging import math from rootpy import io from uncertainties import ufloat import sys import ROOT ROOT.gSystem.AddIncludePath('-IHCSaW/Higgs_CS_and_Width/include') ROOT.gROOT.ProcessLine('.L HCSaW/Higgs_CS_and_Width/src/HiggsCSandWidth.cc++') results_file = io.open('vhtt_shapes.root') logging.basicConfig(stream=sys.stderr,level=logging.INFO) log = logging.getLogger("my_efficiencies") log.setLevel(logging.DEBUG) from get_stat_errors import get_stat_error def get_vhtt_yield(mass): log.warning("Get VHTT yield %s", mass) mmt_yield = results_file.Get('mmt_mumu_final_count/VH%i' % mass).Integral() emt_yield = results_file.Get('emt_emu_final_count/VH%i' % mass).Integral() _, (mmt_passed, mmt_total) = get_stat_error('VH%i' % mass, mmt_yield) _, (emt_passed, emt_total) = get_stat_error('VH%i' % mass, emt_yield)
def data_views(files, lumifiles, styles, forceLumi=-1): ''' Builds views of files. [files] gives an iterator of .root files with histograms to build. [lumifiles] gives the correspond list of .lumisum files which contain the effective integrated luminosity of the samples. The lumi to normalize to is taken as the sum of the data file int. lumis. ''' files = list(files) log.info("Creating views from %i files", len(files)) # Map sample_name => root file histo_files = dict((extract_sample(x), io.open(x)) for x in files) # Map sample_name => lumi file lumi_files = dict((extract_sample(x), read_lumi(x)) for x in lumifiles) # Identify data files datafiles = set([name for name in histo_files.keys() if 'data' in name]) log.info("Found the following data samples:") log.info(" ".join(datafiles)) datalumi = 0 for x in datafiles: if x not in lumi_files: raise KeyError( "Can't find a lumi file for %s - I have these ones: " % x + repr(lumi_files.keys())) datalumi += lumi_files[x] log.warning("-> total int. lumi = %0.0fpb-1", datalumi) if forceLumi > 0: datalumi = forceLumi log.warning("-> forcing lumi to = %0.0fpb-1", datalumi) # Figure out the dataset for each file, and the int lumi. # Key = dataset name # Value = {intlumi, rootpy file, weight, weighted view} output = {} has_data = False for sample in histo_files.keys(): raw_file = histo_files[sample] intlumi = lumi_files[sample] weight = 1 if intlumi: weight = datalumi / intlumi if 'data' in sample: has_data = True weight = 1 log.warning( "Building sample: %s => int lumi: %0.f pb-1. Weight => %0.2E", sample, intlumi, weight) view = views.ScaleView(raw_file, weight) unweighted_view = raw_file # Find the longest (i.e. most specific) matching style pattern style = get_best_style(sample, styles) if style: log.info("Found style for %s - applying Style View", sample) # Set style and title # title = the name of the sample, rootpy Legend uses this. nicename = copy.copy(style['name']) log.debug("sample name %s", nicename) style_dict_no_name = dict( [i for i in style.iteritems() if i[0] != 'name']) view = views.TitleView(views.StyleView(view, **style_dict_no_name), nicename) unweighted_view = views.TitleView( views.StyleView(unweighted_view, **style_dict_no_name), nicename) else: log.warning("No matching style found for %s", sample) output[sample] = { 'intlumi': intlumi, 'file': raw_file, 'weight': weight, 'view': view, 'unweighted_view': unweighted_view } if not has_data: return output # Merge the data into just 'data' log.info("Merging data together") output['data'] = { 'intlumi': datalumi, 'weight': 1, 'view': views.SumView(*[output[x]['view'] for x in datafiles]), 'unweighted_view': views.SumView(*[output[x]['unweighted_view'] for x in datafiles]), } return output
def __init__(self, outputFile, pause=False): self.data = {} self.file = open(outputFile, 'recreate') self.tree = Tree("t", model=Event) self.epoch = datetime.datetime(1995, 1, 1) #jan 1 1995 is ROOT epoch
# Build data and MC views mc_stack = views.StackView(*[get_view(x) for x in args.mclist]) data = rebin_view(the_views['data']['view'], args.rebin) canvas = plotting.Canvas(name='adsf', title='asdf') keep = [] def save(name): canvas.SetLogy(False) canvas.SaveAs(os.path.join(outputdir, name) + '.png') canvas.SetLogy(True) canvas.SaveAs(os.path.join(outputdir, name) + '_log.png') keep = [] # Walk the histogram layout for path, subdirs, histos in io.open(files[0]).walk(class_pattern='TH1F'): for histo in histos: full_path = os.path.join(path, histo) clean_path = full_path.replace('/', '-') mc_hist = mc_stack.Get(full_path) data_hist = data.Get(full_path) mc_hist.Draw() data_hist.Draw('same') mc_hist.SetMaximum(2 * mc_hist.GetMaximum()) # Make sure we can see everything #if data_hist.GetMaximum() > mc_hist.GetHistogram().GetMaximum(): #mc_hist.SetMaximum(2*data_hist.GetMaximum()) keep.append((mc_hist, data_hist)) save(clean_path)
#!/usr/bin/env python from rootpy.tree import Tree, TreeModel from rootpy.io import open from rootpy.types import * from random import gauss f = open("test.root", "recreate") # define the model class Event(TreeModel): x = FloatCol() y = FloatCol() z = FloatCol() i = IntCol() tree = Tree("test", model=Event) # fill the tree 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() # write tree in CSV format
#!/usr/bin/env python from rootpy.tree import Tree from rootpy.io import open from time import time from ROOT import TTreeCache import sys for cached in (False, True): try: f = open("test.root") except IOError: sys.exit("test.root does not exist. Please run tree_write.py first.") tree = f.test if cached: TTreeCache.SetLearnEntries(1) tree.SetCacheSize(10000000) tree.use_cache(cached) start_time = time() for event in tree: event.x end_time = time() print "%.2fsec to read one branch" % (end_time - start_time) start_time = time() for event in tree: event.x event.y
if __name__ == "__main__": parser = argparse.ArgumentParser( "Print the integrals of histograms in a ROOT file. " "The under- and over-flow bins are included") parser.add_argument('file', help="ROOT file") parser.add_argument('--json', action='store_true', help="Write output in JSON format") args = parser.parse_args() # Import after, so ROOT can't mess with sys.argv import rootpy.io as io file = io.open(args.file) results = {} for path, dirs, histonames in file.walk(class_pattern='TH1*'): for histoname in histonames: full_path = os.path.join(path, histoname) histo = file.Get(full_path) int, err = get_integral(histo) results[full_path] = (int, err) if not args.json: for full_path, (int, err) in results.iteritems(): sys.stdout.write(" : ".join( [full_path, "%0.5g" % int, "%0.5g" % err]))
import rootpy.io as io import sys sys.path.append(".") from analysis_cfg import cfg print "Run me from test/plotting" file = io.open("../uw_old_emt_emu_vh.root") tree = file.emt.final.Ntuple print tree events = [ # (166163, 21535313), # (166380, 1570830562), # (167284, 482525686), # (167898, 1936669536), # (171156, 408963773), # (171315, 134426527), # (176841, 121308752), # (177074, 136387485), # (178854, 76038953), # (178920, 71077537), # (180241, 731156083), (172411, 237, 290309172), (166408, 485, 618658267), (166380, 1023, 1114594455), (172949, 1103, 1547599611), (177074, 496, 772810697), (177074, 159, 205460982),
h = Hist(10, 0, 1) def pyroot(tree): tree.Draw("a_x", "", "goff", hist=h) v1 = tree.GetV1() return set(v1[n] for n in xrange(tree.GetEntries())) def rootpy(tree): return np.unique(tree_to_ndarray(tree, branches=["a_x"])) with open("test.root") as f: tree = f.test print "Trees has %i entries" % tree.GetEntries() print print "Using the ROOT/PyROOT way..." cProfile.run("pyroot(tree)") print "time without profiler overhead:" t1 = time.time() a = pyroot(tree) t2 = time.time() print "%f seconds" % (t2 - t1) print print "=" * 40 print print "Using compiled C extension and numpy..."
import os import sys import rootpy.io as io import rootpy.plotting.views as views import rootpy.plotting as plotting import ROOT from FinalStateAnalysis.PlotTools.RebinView import RebinView if __name__ == "__main__": if len(sys.argv) < 2: print "Usage: WVsQCDComp.py file1 [file2] [file3]..." if not os.path.exists("fake_study_plots"): os.makedirs("fake_study_plots") data_files = [io.open(x) for x in sys.argv[1:]] data = views.SumView(*data_files) qcd_view = views.SubdirectoryView(data, "qcd") wjets_view = views.SubdirectoryView(data, "wjets") ROOT.gStyle.SetPaintTextFormat("0.1f") #canvas = plotting.Canvas(name='adsf', title='asdf') #canvas.SetLogy(False) #qcd_num_eta = RebinView(qcd_view, 2).Get("pt10/pfidiso02/muonAbsEta") #qcd_denom_eta = RebinView(qcd_view, 2).Get("pt10/muonAbsEta") #qcd_eta_eff = ROOT.TGraphAsymmErrors(qcd_num_eta, qcd_denom_eta) #qcd_eta_eff.Draw('ape') #canvas.SaveAs("fake_study_plots/qcd_eta_eff.png")
help='y-axis maximum') args = parser.parse_args(args[1:]) if args.verbose: logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) else: logging.basicConfig(level=logging.INFO, stream=sys.stderr) from rootpy.plotting import views import rootpy.io as io import ROOT canvas = ROOT.TCanvas("asdf", "asdf", 800, 600) workspaces = [io.open(x).Get("fit_efficiency") for x in args.workspaces] x = workspaces[0].var('x') frame = x.frame(ROOT.RooFit.Title("Efficiency")) # Always Delete the frame, otherwise we get a segfault @atexit.register def cleanup_frame(): if frame: frame.Delete() ecolor = ROOT.EColor colors = [ecolor.kAzure, ecolor.kViolet, ecolor.kOrange]
#!/usr/bin/env python from rootpy.tree import Tree, TreeModel from rootpy.io import open from rootpy.types import * from random import gauss f = open("test.root", "recreate") # define the model class Event(TreeModel): x = FloatCol() y = FloatCol() z = FloatCol() i = IntCol() tree = Tree("test", model=Event) # fill the tree 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() # write tree in CSV format tree.csv()
def add_cms_blurb(sqrts, intlumi, preliminary=True, blurb=''): latex = ROOT.TLatex() latex.SetNDC(); latex.SetTextSize(0.04); latex.SetTextAlign(31); latex.SetTextAlign(11); label_text = "CMS" if preliminary: label_text += " Preliminary" label_text += " %s TeV" % sqrts label_text += " L=%sfb^{-1}" % (intlumi) label_text += " " + blurb return latex.DrawLatex(0.18,0.97, label_text); vh_7TeV = io.open('NEW-LIMITS/cmb/common/vhtt.input_7TeV.root') vh_8TeV = io.open('NEW-LIMITS/cmb/common/vhtt.input_8TeV.root') # We only care about 7 + 8 TeV vh_combined = views.SumView(vh_7TeV, vh_8TeV) # Now combine sub-channels llt_combined = views.SumView(*[ views.SubdirectoryView(vh_combined, x) for x in ['emt', 'mmt', ]]) # We have to get the shape versions of these from a separate root file. zh_combined = views.SumView(*[ views.SubdirectoryView(io.open('zh_with_shapes.root'), x) for x in [ 'eeem_zh', 'eeet_zh', 'eemt_zh', 'eett_zh', 'mmme_zh', 'mmet_zh', 'mmmt_zh', 'mmtt_zh', ]])
#!/usr/bin/env python ''' Get fake systematic. ''' from rootpy import io import rootpy.plotting.views as views import sys the_filename = sys.argv[1] base_dirs = sys.argv[2].split(',') syst_name = sys.argv[3] the_file = io.open(the_filename, 'READ') nom_view = views.SumView( *[views.SubdirectoryView(the_file, dirname) for dirname in base_dirs]) qcd_view = views.SumView(*[ views.SubdirectoryView(the_file, dirname + '_q') for dirname in base_dirs ]) nom = nom_view.Get('fakes').Integral() qcd = qcd_view.Get('fakes').Integral() print "%s fakes %s %0.2f" % (','.join(base_dirs), syst_name, 1 + abs(nom - qcd) / nom)
log.info("Importing configuration") cfg = __import__(args.cfg.replace('.py', '')) log.info("Building views") mu_fr_views = data_views(files, cfg.data_sample) # Get view of double muon data data = mu_fr_views[cfg.data_sample]['view'] # Get the different regions where we measure the stuff log.info("Getting regions from cfg") regions = cfg.make_regions(data) log.info("Loading workspace") fit_models_file = io.open(args.fit_models, 'READ') # fit_modles is a RooWorkspace fit_models = fit_models_file.fit_models x = fit_models.var('x') cut = fit_models.cat('cut') # Wrap the views of the histograms to translate to RooDataHists def roodatahistizer(hist): ''' Turn a hist into a RooDataHist ''' return ROOT.RooDataHist(hist.GetName(), hist.GetTitle(), ROOT.RooArgList(x), hist) # Now a Get() will return a RooDataHist for type in regions.keys(): # Rebin the histograms. Make this smarter later
from rootpy.io import open from rootpy.tree import Tree, TreeModel from rootpy.types import FloatCol, IntCol import random class Model(TreeModel): x = FloatCol(default=-1.) y = FloatCol() i = IntCol() f = open('test.root', 'recreate') tree = Tree(name='test', model=Model) for i in xrange(100): tree.x = random.gauss(4, 3) tree.y = random.gauss(2, 3) tree.i = i tree.fill() tree.write() f.close()
def __init__(self, outputFile, pause=False): self.data = {} self.file = open(outputFile,'recreate') self.tree = Tree("t", model=Event) self.epoch = datetime.datetime(1995, 1, 1) #jan 1 1995 is ROOT epoch
file_groups[(kind, method)] = [] file_groups[(kind, method)].append(tfile_path) for info, paths in file_groups.iteritems(): store = {} kind, method = info channel = paths[0].split('/')[-3] store['kind'] = kind store['method'] = method store['channel'] = channel store['limits'] ={} for path in paths: print path mass = path.split('/')[-2] store['limits'][mass]={} tfile = io.open(path) limit_tree = tfile.Get('limit') limit_map = tree_to_quantile_map(limit_tree) if kind == 'expected': store['limits'][mass]['+2sigma'] = limit_map['0.975'] store['limits'][mass]['+1sigma'] = limit_map['0.840'] store['limits'][mass]['median'] = limit_map['0.500'] store['limits'][mass]['-1sigma'] = limit_map['0.160'] store['limits'][mass]['-2sigma'] = limit_map['0.025'] else: store['limits'][mass]['median'] = limit_map['-1.000'] tfile.Close() outfilename = '%s_%s_%s_limit.json' % (channel, method, kind) with open( os.path.join(input_dir,outfilename),'w') as outfile: outfile.write(prettyjson.dumps(store))
#!/usr/bin/env python from rootpy.io import open, DoesNotExist from rootpy.plotting import Hist, Hist2D from rootpy.utils import asrootpy f = open('data.root') print f.a print f.a.b try: print f.a.b.c.d.e.f except DoesNotExist, e: print e for thing in f.walk(): print thing f.close() # supports with statements with open('data.root', 'update') as f: print f # write some histograms h1 = Hist(100, 0, 100, name='h1', type='I') # variable bin widths h2 = Hist2D((0, 3, 5, 20, 50), (10, 20, 30, 40, 1000), name='h2') h1.Write()
def get_view(sample_pattern): for sample, sample_info in the_views.iteritems(): if fnmatch.fnmatch(sample, sample_pattern): return rebin_view(sample_info['view']) raise KeyError("I can't find a view that matches %s, I have: %s" % ( sample_pattern, " ".join(the_views.keys()))) wz_view = get_view('WZ*') zz_view = get_view('ZZ*') data = rebin_view(the_views['data']['view']) corrected_view = int_view( SubtractionView(data, wz_view, zz_view, restrict_positive=True)) output = io.open(args.outputfile, 'RECREATE') output.cd() corr_numerator = corrected_view.Get(args.numerator) corr_denominator = corrected_view.Get(args.denom) log.info("Corrected: %0.2f/%0.2f = %0.1f%%", corr_numerator.Integral(), corr_denominator.Integral(), 100*corr_numerator.Integral()/corr_denominator.Integral() if corr_denominator.Integral() else 0 ) uncorr_numerator = data.Get(args.numerator) uncorr_denominator = data.Get(args.denom)
content = ret.GetBinContent(i+1) / bin1 content = content if content <= 1 else 0. ret.SetBinContent(i+1, content) return ret jobid = os.environ['jobid'] channels = ['mmt', 'emt', 'eet'] public = os.environ['pub'] chan_hists = {} for ch, color in zip(channels, ['darkgreen','blue','red']): view = views.TitleView( views.StyleView( views.SumView( # io.open('results/%s/WHAnalyze%s/VH_120_HWW.root' % (jobid, ch.upper()) ), io.open('results/%s/WHAnalyze%s/VH_H2Tau_M-120.root' % (jobid, ch.upper()) ) ), drawstyle = 'hist TEXT00', linecolor = color, linewidth = 2, fillstyle = 'hollow', legendstyle = 'l', ), ch ) chan_hists[ch] = view.Get('ss/CUT_FLOW') canvas = plotting.Canvas(name='adsf', title='asdf') canvas.SetGridx(True) canvas.SetGridy(True) canvas.SetLogy(True)
for sample, sample_info in the_views.iteritems(): if fnmatch.fnmatch(sample, sample_pattern): return rebin_view(sample_info['view']) raise KeyError("I can't find a view that matches %s, I have: %s" % ( sample_pattern, " ".join(the_views.keys()))) #from pdb import set_trace; set_trace() wz_view = get_view('WZ*') zz_view = get_view('ZZ*') data = rebin_view(the_views['data']['view']) corrected_view = int_view( SubtractionView(data, wz_view, zz_view, restrict_positive=True)) log.debug('creating output file') output = io.open(args.outputfile, 'RECREATE') output.cd() log.debug('getting from corrected view') corr_numerator = corrected_view.Get(args.numerator) corr_denominator = corrected_view.Get(args.denom) log.info("Corrected: %0.2f/%0.2f = %0.1f%%", corr_numerator.Integral(), corr_denominator.Integral(), 100*corr_numerator.Integral()/corr_denominator.Integral() if corr_denominator.Integral() else 0 ) uncorr_numerator = data.Get(args.numerator) uncorr_denominator = data.Get(args.denom)
#!/usr/bin/env python """ Get fake systematic. """ from rootpy import io import rootpy.plotting.views as views import sys the_filename = sys.argv[1] base_dirs = sys.argv[2].split(",") syst_name = sys.argv[3] the_file = io.open(the_filename, "READ") nom_view = views.SumView(*[views.SubdirectoryView(the_file, dirname) for dirname in base_dirs]) qcd_view = views.SumView(*[views.SubdirectoryView(the_file, dirname + "_q") for dirname in base_dirs]) nom = nom_view.Get("fakes").Integral() qcd = qcd_view.Get("fakes").Integral() print "%s fakes %s %0.2f" % (",".join(base_dirs), syst_name, 1 + abs(nom - qcd) / nom)
import sys if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('path', help='Path to histogram') parser.add_argument('files', nargs='+', help='Input root files') parser.add_argument('--above', default=None, type=float, help='Cut above') parser.add_argument('--below', default=None, type=float, help='Cut below') args = parser.parse_args() from rootpy.io import open from FinalStateAnalysis.StatTools.efficiencies import efficiency # Get histogram for a_file in args.files: file = open(a_file) if not file: sys.stderr.write("Can't open file: %s\n" % args.file) sys.exit(2) histo = file.Get(args.path) if not histo: sys.stderr.write("Can't get histogram: %s\n" % args.path) sys.exit(3) total = 0 below_below = 0 below_above = 0 if histo.GetEntries(): total = histo.GetIntegral()[histo.GetNbinsX() + 1]
postfix = '_TEST_' ROOT.gStyle.SetPaintTextFormat('.2g') jobid = os.environ['jobid'] channels = ['eet', 'emt', 'mmt'] public = os.environ['pub'] chan_hists = {} for ch, color in zip(channels, ['darkgreen', 'blue', 'red']): view = views.TitleView( views.StyleView( views.SumView( # io.open('results/%s/WHAnalyze%s/VH_120_HWW.root' % (jobid, ch.upper()) ), io.open('results/%s/WHAnalyze%s/VH_H2Tau_M-120.root' % (jobid, ch.upper()))), drawstyle='hist TEXT00', linecolor=color, linewidth=2, fillstyle='hollow', legendstyle='l', ), ch) chan_hists[ch] = view.Get('ss/CUT_FLOW') chan_hists[ch].SetLabelSize(0.035) canvas = plotting.Canvas(name='adsf', title='asdf') canvas.SetGridx(True) canvas.SetGridy(True) canvas.SetLogy(True) legend = plotting.Legend(len(channels),
data.SetLineWidth(2) legend.AddEntry(data, "Observed", "lp") signal.SetLineStyle(1) signal.SetLineWidth(3) signal.SetTitle("m_{H}=120 (#times 5)") signal.SetLineColor(ROOT.EColor.kRed) signal.SetFillStyle(0) wz.SetTitle('WZ') zz.SetTitle('ZZ') fakes.SetTitle("Non-prompt") legend.AddEntry(signal, signal.GetTitle(), "l") #legend.AddEntry(wz, 'WZ', 'lf') legend.AddEntry(zz, 'ZZ', 'lf') legend.AddEntry(fakes, 'Non-prompt', "lf") zz_file = io.open('file_ZZ.root') signal_file = io.open('file_Signal.root') data_file = io.open('file_Data.root') fake_file = io.open('file_Fake.root') hZZ = zz_file.Get('zz') hZJets = fake_file.Get('fake') hData = data_file.Get('data') hSignal = signal_file.Get('signal') hHWW = hSignal canvas.cd() hHWW = hHWW*5.0 #print hZZ, hZJets
parser.add_option("--zz_err", type="float", default=0.40, help="WZ normalization error") parser.add_option("--triboson_err", type="float", default=1.00, help="Error on triboson cross section") parser.add_option("--mu_fake_err", type="float", default=0.3, help="Error on muon fake rate") def quad(*xs): return math.sqrt(sum(x*x for x in xs)) if __name__ == "__main__": (options, args) = parser.parse_args() file = io.open(options.file, 'read') emt_folder = file.Get('emt_emu_final_count') mmt_folder = file.Get('mmt_mumu_final_count') results = {} mass = options.mass results['mass'] = mass results['emt_data'] = emt_folder.Get("data_obs").Integral() results['emt_wz'] = emt_folder.Get("WZ").Integral() results['emt_zz'] = emt_folder.Get("ZZ").Integral() results['emt_triboson'] = emt_folder.Get("tribosons").Integral() results['emt_fakes'] = emt_folder.Get("fakes").Integral() results['emt_fakes_err'] = emt_folder.Get("fakes").GetBinError(1)
action='store_true', help='Plot fit error band') args = parser.parse_args(args[1:]) if args.verbose: logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) else: logging.basicConfig(level=logging.INFO, stream=sys.stderr) from rootpy.plotting import views import rootpy.io as io from FinalStateAnalysis.Utilities.rootbindings import ROOT # Build view of input histograms log.info("Merging input files") input_view = views.SumView(*[io.open(x) for x in args.input]) if args.rebin and args.rebin > 1: binning = None # if ',' in args.rebin: # print args.rebin.split(',') if args.rebin: binning = eval(args.rebin) if isinstance(binning,float) or isinstance(binning,int) : newbinning = int(binning) elif isinstance(binning,list) and isinstance(binning[0],float) or isinstance(binning[0],int) : newbinning= tuple(float(x) for x in binning) elif isinstance(binning,list) and isinstance(binning[0],list): # print args.rebin #print binning[0] binningx= tuple(float(x) for x in binning[0])
file_groups[(kind, method)] = [] file_groups[(kind, method)].append(tfile_path) for info, paths in file_groups.iteritems(): store = {} kind, method = info channel = paths[0].split('/')[-3] store['kind'] = kind store['method'] = method store['channel'] = channel store['limits'] = {} for path in paths: print path mass = path.split('/')[-2] store['limits'][mass] = {} tfile = io.open(path) limit_tree = tfile.Get('limit') limit_map = tree_to_quantile_map(limit_tree) if kind == 'expected': store['limits'][mass]['+2sigma'] = limit_map['0.975'] store['limits'][mass]['+1sigma'] = limit_map['0.840'] store['limits'][mass]['median'] = limit_map['0.500'] store['limits'][mass]['-1sigma'] = limit_map['0.160'] store['limits'][mass]['-2sigma'] = limit_map['0.025'] else: store['limits'][mass]['median'] = limit_map['-1.000'] tfile.Close() outfilename = '%s_%s_%s_limit.json' % (channel, method, kind) with open(os.path.join(input_dir, outfilename), 'w') as outfile: outfile.write(prettyjson.dumps(store))
data.SetLineWidth(2) legend.AddEntry(data, "Observed", "lp") signal.SetLineStyle(1) signal.SetLineWidth(3) signal.SetTitle("(5#times) m_{H}=120 GeV") signal.SetLineColor(ROOT.EColor.kRed) signal.SetFillStyle(0) wz.SetTitle('WZ') zz.SetTitle('ZZ') fakes.SetTitle("Non-prompt") legend.AddEntry(signal, signal.GetTitle(), "l") legend.AddEntry(wz, 'WZ', 'lf') legend.AddEntry(zz, 'ZZ', 'lf') legend.AddEntry(fakes, 'Non-prompt', "lf") whtt_file = io.open('../vhtt_shapes.root') def get_total(histo): return whtt_file.Get('mmt_mumu_final_MuTauMass/%s' % histo) + \ whtt_file.Get('emt_emu_final_SubleadingMass/%s' % histo) hWZ = get_total('WZ') hZZ = get_total('ZZ') hZJets = get_total('fakes') hData = get_total('data_obs') hSignal = get_total('VH120') + get_total('VH120WW') hHWW = hSignal canvas.cd() hHWW = hHWW*5.0
#!/usr/bin/env python ''' Examples of using the TreeChain class, a replacement for TChain ''' from random import gauss from rootpy.io import open from rootpy.tree import Tree, TreeChain # Make two files, each with a Tree called "test" print "Creating test tree in chaintest1.root" f = open("chaintest1.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() # Make a histogram of x when y > 1 hist1 = tree.Draw('x', 'y > 1', min=-10, max=10, bins=100).Clone() hist1.SetName('hist1') hist1.SetDirectory(0) # memory resident print "The first tree has %f entries where y > 1" % hist1.Integral()
def data_views(files, lumifiles): ''' Builds views of files. [files] gives an iterator of .root files with histograms to build. [lumifiles] gives the correspond list of .lumisum files which contain the effective integrated luminosity of the samples. The lumi to normalize to is taken as the sum of the data file int. lumis. ''' files = list(files) log.info("Creating views from %i files", len(files)) # Map sample_name => root file histo_files = dict((extract_sample(x), io.open(x)) for x in files) # Map sample_name => lumi file lumi_files = dict((extract_sample(x), read_lumi(x)) for x in lumifiles) # Identify data files datafiles = set([name for name in histo_files.keys() if 'data' in name]) log.info("Found the following data samples:") log.info(" ".join(datafiles)) datalumi = 0 for x in datafiles: if x not in lumi_files: raise KeyError( "Can't find a lumi file for %s - I have these ones: " % x + repr(lumi_files.keys())) datalumi += lumi_files[x] log.info("-> total int. lumi = %0.0fpb-1", datalumi) # Figure out the dataset for each file, and the int lumi. # Key = dataset name # Value = {intlumi, rootpy file, weight, weighted view} output = {} for sample in histo_files.keys(): raw_file = histo_files[sample] intlumi = lumi_files[sample] log.info("Building sample: %s => int lumi: %0.f pb-1", sample, intlumi) weight = 1 if intlumi: weight = datalumi/intlumi if 'data' in sample: weight = 1 log.debug("Weight: %0.2f", weight) view = views.ScaleView(raw_file, weight) unweighted_view = raw_file # Find the longest (i.e. most specific) matching style pattern best_pattern = '' for pattern, style_dict in data_styles.iteritems(): log.debug("Checking pattern: %s against %s", pattern, sample) if fnmatch.fnmatch(sample, pattern): log.debug("-> it matches!") if len(pattern) > len(best_pattern): best_pattern = pattern log.info("Found new best style for %s: %s", sample, pattern) if best_pattern: style_dict = data_styles[best_pattern] log.info("Found style for %s - applying Style View", sample) # Set style and title # title = the name of the sample, rootpy Legend uses this. nicename = copy.copy(style_dict['name']) view = views.TitleView( views.StyleView(view, **style_dict), nicename ) unweighted_view = views.TitleView( views.StyleView(unweighted_view, **style_dict), nicename ) output[sample] = { 'intlumi': intlumi, 'file' : raw_file, 'weight' : weight, 'view' : view, 'unweighted_view' : unweighted_view } # Merge the data into just 'data' log.info("Merging data together") output['data'] = { 'intlumi' : datalumi, 'weight' : 1, 'view' : views.SumView(*[output[x]['view'] for x in datafiles]), 'unweighted_view' : views.SumView(*[output[x]['unweighted_view'] for x in datafiles]), } return output
return False var.setVal(value) log.info("Setting var %s to %f", name, value) return True if __name__ == "__main__": parser.add_argument('input', help='Input file w/ fit results RooWorkspace') parser.add_argument('output', help='Output directory to store plots in') args = parser.parse_args(args[1:]) log.info("Opening input file %s", args.input) file = io.open(args.input, 'r') # Always close the file before exiting, otherwise RooPlot will segfault atexit.register(file.Close) log.info("Getting workspace") ws = file.Get("fit_results") x = ws.var('x') cut = ws.cat('cut') # Put everything in python form data = {} fake_rate_datas = ws.allData() # Plot the results of each one for datum in fake_rate_datas: data[datum.GetName()] = datum
============================================= The TreeChain class, a replacement for TChain ============================================= This example demonstrates how to use the TreeChain class, a more Python-friendly TChain replacement. """ print __doc__ from random import gauss from rootpy.io import open from rootpy.tree import Tree, TreeChain # Make two files, each with a Tree called "test" print "Creating test tree in chaintest1.root" f = open("chaintest1.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() # Make a histogram of x when y > 1
# Build data and MC views mc_stack = views.StackView(*[get_view(x) for x in args.mclist]) data = rebin_view(the_views['data']['view'], args.rebin) canvas = plotting.Canvas(name='adsf', title='asdf') keep = [] def save(name): canvas.SetLogy(False) canvas.SaveAs(os.path.join(outputdir, name) + '.png') canvas.SetLogy(True) canvas.SaveAs(os.path.join(outputdir, name) + '_log.png') keep = [] # Walk the histogram layout for path, subdirs, histos in io.open(files[0]).walk(class_pattern='TH1F'): for histo in histos: full_path = os.path.join(path, histo) clean_path = full_path.replace('/', '-') mc_hist = mc_stack.Get(full_path) data_hist = data.Get(full_path) mc_hist.Draw() data_hist.Draw('same') mc_hist.SetMaximum(2*mc_hist.GetMaximum()) # Make sure we can see everything #if data_hist.GetMaximum() > mc_hist.GetHistogram().GetMaximum(): #mc_hist.SetMaximum(2*data_hist.GetMaximum()) keep.append( (mc_hist, data_hist) ) save(clean_path)
for _ in range(nentries): new.Fill(center) return new def int_view(x): return views.FunctorView(x, round_to_ints) def get_view(sample_pattern): for sample, sample_info in the_views.iteritems(): if fnmatch.fnmatch(sample, sample_pattern): return rebin_view(sample_info["view"]) raise KeyError("I can't find a view that matches %s, I have: %s" % (sample_pattern, " ".join(the_views.keys()))) data = rebin_view(the_views["data"]["view"]) output = io.open(args.outputfile, "RECREATE") output.cd() log.info("numerator = %s", args.numerator) uncorr_numerator = data.Get(args.numerator) uncorr_denominator = data.Get(args.denom) uncorr_numerator.SetName("numerator_uncorr") uncorr_denominator.SetName("denominator_uncorr") uncorr_numerator.Write() uncorr_denominator.Write() log.info( "Uncorrected: %0.2f/%0.2f = %0.1f%%", uncorr_numerator.Integral(),
with open(args.meta) as meta_file: meta_info = json.load(meta_file) log.info("Importing configuration") cfg = __import__(args.cfg.replace('.py', '')) log.info("Building views") data_views = data_views.data_views( args.files, cfg.data_sample) # We just need to figure out the directory structure from any old file layout_filename = data_views.values()[0]['subsamples'].values()[0]['filename'] log.info("Getting file layout from %s", layout_filename) keys_to_plot = [] with io.open(layout_filename, 'r') as layout_file: for path, subdirs, histos in layout_file.walk(class_pattern='TH1F'): # Skip folders w/o histograms if not histos: continue for histo in histos: key = [ x for x in path.split('/') if x != 'all' and x != 'pass' ] + [ histo ] keys_to_plot.append(tuple(key)) for key in keys_to_plot: path = os.path.join(*key) sys.stdout.write("Fake rates for %s\n" % path) for sample, sample_info in data_views.iteritems(): num_view = views.PathModifierView(
help='pattern of shape to be excluded',dest='excluded') parser.add_option('-n','--nuisance', metavar='pattern', type=str, default = '*CMS_*', help='pattern of nuisances to be excluded',dest='nuisances') parser.add_option('-x','--x-title', metavar='name', type=str, default = '', help='x axis title',dest='xtitle') parser.add_option('-y','--y-title', metavar='name', type=str, default = '', help='y axis title',dest='ytitle') parser.add_option('-d','--differential', type=int, default = 0, help='makes a differential plot',dest='differential') (options,arguments) = parser.parse_args() tfile_name = arguments.pop(0) categories = arguments tfile = io.open(tfile_name) logging.info("Opened file %s" % tfile_name) #get a directory and look into that keys = [i.GetName() for i in tfile.Get(categories[0]).GetListOfKeys() if not fnmatch(i.GetName(), options.nuisances)] if options.excluded: keys = [ i for i in keys if not fnmatch(i, options.excluded)] data = [i for i in keys if i.startswith('data')][0] keys = [i for i in keys if not i.startswith('data')] logging.debug("These shapes will be plotted: %s" % keys.__repr__()) input_view = views.SumView( *[ views.SubdirectoryView( tfile, category ) for category in categories] )
''' Stupid tests of the th1fmorph tool ''' from FinalStateAnalysis.StatTools.morph import morph from rootpy.io import open, DoesNotExist file = open('$fsa/VHiggs/test/plotting/wh_shapes.root') hist1 = file.get('mmt_mumu_final_140_MuTauMass/VH140') hist2 = file.get('mmt_mumu_final_120_MuTauMass/VH120') hist130true = file.get('mmt_mumu_final_130_MuTauMass/VH130') print '140', hist1.Integral(), hist1.GetMean() print '130 true', hist130true.Integral(), hist130true.GetMean() print '120', hist2.Integral(), hist2.GetMean() # Try to morph to 130 m130 = morph('130', '130', 130, hist1, 140, hist2, 120) print m130.Integral(), m130.GetMean()