def drawAllTrainStacks(signal, bkg, data, labelCodes, weightsPerSampleA, weightsPerSampleB, corrWeightsA = DEFAULT, corrWeightsB = DEFAULT, subset = 'TrainA'): """Draw all train stacks for signal and bkg at once. Keyword arguments: signal -- the signal sample bkg -- the background sample labelCodes -- the label codes for the type of sample (W, Z for eg.) weightsPerSample -- the XS weight subset -- extra identifier for the filename (default TrainA) """ if corrWeightsA is DEFAULT: corrWeightsA = hstack(signal.returnTestCorrectionWeights('A'), bkg.returnTestCorrectionWeights('A')) if corrWeightsB is DEFAULT: corrWeightsB = hstack(signal.returnTestCorrectionWeights('B'), bkg.returnTestCorrectionWeights('B')) # store all histograms in output.root for x in xrange (0, len(signal.train)): if x == 0: subset = 'A' weightsPerSample = weightsPerSampleA corrWeights = corrWeightsA else: subset = 'B' weightsPerSample = weightsPerSampleB corrWeights = corrWeightsB f = ropen('outputTrain'+str(subset)+'.root','recreate') c1 = Canvas() c1.cd() allStack = [] legendAllStack = [] # get sigA histograms hist, histDictSigA, testAStack, legendSigStack = createHists(signal.returnTrainSample(subset), labelCodes, 'signal', signal.returnTrainSampleLabels(subset), weightsPerSample[0], signal.returnFoundVariables(), allStack, legendAllStack, corrWeights, str('Train'+subset), True) # get bkgA histograms # how to fix legends???? hist2, histDictBkgA, testAStackBkg, legendBkgStack = createHists(bkg.returnTrainSample(subset), labelCodes, 'bkg', bkg.returnTrainSampleLabels(subset), weightsPerSample[1], bkg.returnFoundVariables(), allStack, legendAllStack, corrWeights, str('Train'+subset), True) for hist2idx in xrange(0,len(hist)): legend = Legend(3) legend.AddEntry(hist[hist2idx],'F') legend.AddEntry(hist2[hist2idx],'F') hist[hist2idx].draw('hist') hist[hist2idx].Write() hist2[hist2idx].draw('histsame') hist2[hist2idx].Write() legend.Draw('same') c1.Write() #c1.SaveAs(signal.returnFoundVariables()[hist2idx]+".png") hist2idx+=1 drawStack(testAStack, legendSigStack, signal.returnFoundVariables(), 'Sig', str('Train'+subset)) # draw histograms drawStack(testAStackBkg, legendBkgStack, bkg.returnFoundVariables(), 'Bkg', str('Train'+subset)) drawStack(allStack, legendAllStack, signal.returnFoundVariables(), 'All', str('Train' + subset)) f.close()
def make_cutflow_table(samples, args): filters = None db = datasets.Database(args.db) # build table cutflow_table = {} data_index = [] used_samples = [] i = 0 for latex_name, text_name, sample in samples: matched_samples = db.search(sample) if not matched_samples: if args.warn_missing: log.warning("sample %s not found" % sample) continue raise datasets.NoMatchingDatasetsFound(sample) total_cutflow = None for ds in matched_samples: log.info(ds.name) for filename in ds.files: log.debug(filename) with ropen(filename) as rfile: cutflow = rfile.cutflow_event cutflow.SetDirectory(0) if filters is None: filters = [cutflow.GetXaxis().GetBinLabel(j + 1) for j in xrange(len(cutflow))] # scale MC by lumi and xsec if ds.datatype != datasets.DATA and ds.datatype != datasets.EMBED and not args.no_weight: lumi = LUMI[args.year] events = cutflow[1].value xsec, kfact, effic = ds.xsec_kfact_effic weight = 1E3 * lumi * xsec * kfact / (effic * events) if args.verbose: print '-' * 30 print ds.name print "xsec: %f [nb]" % xsec print "effic: %f" % effic print "events: %d" % events print "lumi: %f [1/pb]" % lumi print "weight (1E3 * lumi * xsec / (effic * events)): %f" % weight cutflow *= weight if ds.datatype == datasets.DATA: data_index.append(i) if total_cutflow is None: total_cutflow = cutflow else: total_cutflow += cutflow if args.errors: cutflow_table[i] = list(zip(total_cutflow.y(), total_cutflow.yerrh())) else: cutflow_table[i] = list(total_cutflow.y()) used_samples.append((latex_name, text_name, sample)) i += 1 cutflows = [cutflow_table[idx] for idx in xrange(i)] return used_samples, filters, cutflows, data_index
def run(self): # ignore sigterm signal and let parent process take care of this signal.signal(signal.SIGINT, signal.SIG_IGN) ROOT.gROOT.SetBatch() os.nice(self.nice) h = multilogging.QueueHandler(self.logging_queue) # get the top-level logger log_root = logging.getLogger() # clear any existing handlers in the top-level logger log_root.handlers = [] # add the queuehandler log_root.addHandler(h) if not self.gridmode: # direct stdout and stderr to the local logger sys.stdout = multilogging.stdout(log) sys.stderr = multilogging.stderr(log) try: filename = 'student-%s-%s.root' % (self.name, self.uuid) with ropen(os.path.join( os.getcwd(), filename), 'recreate') as self.output: ROOT.gROOT.SetBatch(True) if self.queuemode: log.info("Receiving files from Supervisor's queue") else: log.info( "Received %i files from Supervisor for processing" % len(self.files)) self.output.cd() if self.profile: profile_filename = 'student-%s-%s.profile' % ( self.name, self.uuid) profile.runctx('self.work()', globals=globals(), locals=locals(), filename=profile_filename) self.output_queue.put( (self.uuid, [self.filters, self.output.GetName(), profile_filename])) else: self.work() self.output_queue.put( (self.uuid, [self.filters, self.output.GetName()])) except: print sys.exc_info() traceback.print_tb(sys.exc_info()[2]) self.output_queue.put((self.uuid, None)) self.output_queue.close() self.logging_queue.close()
def run(self): # ignore sigterm signal and let parent process take care of this signal.signal(signal.SIGINT, signal.SIG_IGN) ROOT.gROOT.SetBatch() os.nice(self.nice) h = multilogging.QueueHandler(self.logging_queue) self.logger = logging.getLogger('Student') self.logger.addHandler(h) self.logger.setLevel(logging.DEBUG) if not self.gridmode: sys.stdout = multilogging.stdout(self.logger) sys.stderr = multilogging.stderr(self.logger) try: filename = 'student-%s-%s.root' % (self.name, self.uuid) with ropen(os.path.join(os.getcwd(), filename), 'recreate') as self.output: ROOT.gROOT.SetBatch(True) if self.queuemode: self.logger.info("Receiving files from Supervisor's queue") else: self.logger.info( "Received %i files from Supervisor for processing" % len(self.files)) self.output.cd() if self.profile: profile_filename = 'student-%s-%s.profile' % (self.name, self.uuid) profile.runctx('self.work()', globals=globals(), locals=locals(), filename=profile_filename) self.output_queue.put((self.uuid, [ self.event_filters, self.object_filters, self.output.GetName(), profile_filename ])) else: self.work() self.output_queue.put((self.uuid, [ self.event_filters, self.object_filters, self.output.GetName() ])) except: print sys.exc_info() traceback.print_tb(sys.exc_info()[2]) self.output_queue.put((self.uuid, None)) self.output_queue.close() self.logging_queue.close()
def test_attrs(self): with ropen(self.temp_file_path) as f: tree = f.tree tree.use_cache() tree.define_object('a', 'a_') tree.define_collection('b', 'b_', 'b_n') for event in tree: # test a setattr before a getattr with caching new_a_y = random() event.a_y = new_a_y assert_almost_equal(event.a_y, new_a_y) assert event.a_x == event.a.x assert len(event.b) > 0
def test_attrs(self): with ropen(self.file_paths[0]) as f: tree = f.tree tree.read_branches_on_demand(True) tree.define_object("a", "a_") tree.define_collection("b", "b_", "b_n") for event in tree: # test a setattr before a getattr with caching new_a_y = random() event.a_y = new_a_y assert_almost_equal(event.a_y, new_a_y) assert_equals(event.a_x, event.a.x) assert_equals(len(event.b) > 0, True)
def test_cuts(self): with ropen(self.file_paths[0]) as f: tree = f.tree h1 = Hist(10, -1, 2) h2 = Hist2D(10, -1, 2, 10, -1, 2) h3 = Hist3D(10, -1, 2, 10, -1, 2, 10, -1, 2) tree.draw("a_x", hist=h1) assert_equals(h1.Integral() > 0, True) tree.draw("a_x:a_y", hist=h2) assert_equals(h2.Integral() > 0, True) tree.draw("a_x:a_y:a_z", hist=h3) assert_equals(h3.Integral() > 0, True) h3.Reset() tree.draw("a_x>0:a_y/2:a_z*2", hist=h3) assert_equals(h3.Integral() > 0, True)
def run(self): # ignore sigterm signal and let parent process take care of this signal.signal(signal.SIGINT, signal.SIG_IGN) ROOT.gROOT.SetBatch() os.nice(self.nice) h = multilogging.QueueHandler(self.logging_queue) self.logger = logging.getLogger('Student') self.logger.addHandler(h) self.logger.setLevel(logging.DEBUG) if not self.gridmode: sys.stdout = multilogging.stdout(self.logger) sys.stderr = multilogging.stderr(self.logger) try: filename = 'student-%s-%s.root' % (self.name, self.uuid) with ropen(os.path.join(os.getcwd(), filename), 'recreate') as self.output: ROOT.gROOT.SetBatch(True) if self.queuemode: self.logger.info("Receiving files from Supervisor's queue") else: self.logger.info("Received %i files from Supervisor for processing" % len(self.files)) self.output.cd() if self.profile: profile_filename = 'student-%s-%s.profile' % (self.name, self.uuid) profile.runctx('self.work()', globals=globals(), locals=locals(), filename=profile_filename) self.output_queue.put((self.uuid, [self.event_filters, self.object_filters, self.output.GetName(), profile_filename])) else: self.work() self.output_queue.put((self.uuid, [self.event_filters, self.object_filters, self.output.GetName()])) except: print sys.exc_info() traceback.print_tb(sys.exc_info()[2]) self.output_queue.put((self.uuid, None)) self.output_queue.close() self.logging_queue.close()
def createTransformedBDT(bdt_in, classes, bdt_in_B, classes_B, name_A, name_B, bkg_name): import HistoTransform_ext as ht from numpy import argmax,argmin,ones,save from numpy import transpose,bincount,array from rootpy.interactive import wait from rootpy.plotting import Canvas, Hist, Hist2D, Hist3D, Legend from rootpy.io import root_open as ropen, DoesNotExist from rootpy.plotting import HistStack print 'saving files as npy' save('bdt_output_scores_'+name_A+'.npy', bdt_in) save('bdt_output_classes_'+name_A+'.npy', classes) save('bdt_output_scores_'+name_B+'.npy', bdt_in_B) save('bdt_output_classes_'+name_B+'.npy', classes_B) #need to assign names that follow correct naming convention return "Going to stop here for now until HistoTransform is fixed" names = [ bkg_name+'_2tag2jet_mva', 'WlvH125_2tag2jet_mva'] name_x = [name_A, name_B] for j in xrange(2): bdtx_inFile_name = "bdt_inFile_"+name_x[j]+'_'+str(j)+"of2.root" bdtx_inFile = ropen('bdt_inFile_'+name_x[j]+'_'+str(j)+'of2.root','recreate') bdtxarr = [] for i in xrange(2): bdtxarr.append(Hist (1000,-1,1, name=names[i], title=names[i])) bdtxarr[i].fill_array(bdt_in[classes == i]) bdtxarr[i].Write() bdtx_inFile.close() for count in xrange(2): #try: bdtx_tx = ht.HistoTransform('bdt_inFile_'+name_x[count]+'_'+str(count)+'of2.root', "bdt_outFile_"+name_x[count]+'_'+str(count)+"of2.root") #except: # return "Could not create object" bdtx_tx.transformBkgBDTs = False bdtx_tx.doMergeKFolds = False bdtx_tx.doTransformBeforeMerging = False try: subDir = bdtx_tx.addSubDirectory("") except: return "failed addsubdir" if count == 1: bdtx_tx.transformAlgorithm = 5 try: bdtx_tx.setSignal(subDir, names[0]) except: return "failed setsignal" else: bdtx_tx.transformAlgorithm = 1 try: bdtx_tx.addBackground(subDir, bkg_name)#"bkg"); except: return "Failed addBackground" nFold = 2 maxUncFactor = 1 print 'starting run' try: bdtx_tx.addRegion(subDir, "2tag2jet_mva", 0.05 * maxUncFactor, nFold); except: return "addRegion failed" try: bdtx_tx.run() return "it ran" print 'done run ' + str(count) + ' for ' + name_x[count] except: print 'Error whilst running HistoTransform run() for ' + name_x[count] return "Something Failed in .run()" return "Success!"
#!/usr/bin/env python # this import is required to register the Tree class import rootpy.tree from rootpy.io import root_open as ropen from time import time from ROOT import TTreeCache import sys for cached in (False, True): try: f = ropen("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
float x; double y; }; """, ["Thingy"]) # alternatively you can ROOT.gSystem.Load() your library # define the model class Event(TreeModel): event_number = IntCol() thingy = ObjectCol(C.Thingy) f = ropen("test.root", "recreate") tree = Tree("test", model=Event) # fill the tree for i in xrange(20): tree.event_number = i tree.thingy.i = i tree.thingy.x = gauss(.3, 2.) tree.thingy.y = gauss(13., 42.) tree.fill() tree.write() f.close() # now to read the same tree with ropen("test.root") as f:
from rootpy.io import open as ropen with ropen('file.root') as f: myhist = f.somedirectory.histname # recursively walk through the file for path, dirs, objects in f.walk(): # do something pass
import signals import optfilter import rootpy rootpy.log.basic_config_colorized() from random import gauss from rootpy.io import open as ropen from rootpy.tree import Tree, TreeChain from rootpy.plotting import Hist import numpy as np import random outfilename = 'ampInAmpOutHeatArbitraryNumpyFilter.root' rootFileOut = ropen(outfilename, 'recreate') tree = Tree("t") branches = { 'amp_in':'F', 'amp_out': 'F', 'amp_out_maxtime': 'F' } tree.create_branches(branches) numEvents = 1000 _pulselength = 512 _pulseStartTime = _pulselength/2 _tukey_alpha = 0.3 amps = [-10, -20, -50, -100, -200, -400, -500, -800, -1000, -1400, -1800, -2000] #amps = [-100] #signal simpulse = signals.HeatSignal(length = _pulselength) scaleFactor = -1.0*simpulse().min()
============================== This example demonstrates how basic file operations are made easier in rootpy. """ print __doc__ import os import shutil import rootpy rootpy.log.basic_config_colorized() from rootpy.io import root_open as ropen, DoesNotExist from rootpy.plotting import Hist, Hist2D from rootpy import testdata from rootpy import asrootpy shutil.copyfile(testdata.get_filepath('test_file_2.root'), 'data.root') f = ropen('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
This example demonstrates how to use the TreeChain class, a more Python-friendly TChain replacement. """ print __doc__ import rootpy rootpy.log.basic_config_colorized() from random import gauss from rootpy.io import root_open as ropen from rootpy.tree import Tree, TreeChain from rootpy.plotting import Hist # Make two files, each with a Tree called "test" print "Creating test tree in chaintest1.root" f = ropen("chaintest1.root", "recreate") tree = Tree("test") branches = { 'x': 'F', 'y': 'F', 'z': 'F', 'i': 'I'} tree.create_branches(branches) 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()
from rootpy.io import root_open as ropen from rootpy.plotting import Canvas, Hist, Efficiency, Legend import rootpy rootpy.log.basic_config_colorized() eff_file = ropen("../data/LSB50/uct_tau_efficiency.root") eff_file_low = ropen("../data/LSB50/uct_mc_efficiency_pu35.root") eff_file_high = ropen("../data/LSB50/uct_mc_efficiency_pu50.root") eff_ntuple = eff_file_high["rlxTauEfficiency/Ntuple"] #binning = (25, 20, 70) binning = (range(20, 32, 2) + range(32, 45, 1) + range(45, 55, 2) + range(55, 70, 5), ) l1_pass_vs_pu = Hist(*binning) uct_pass_vs_pu = Hist(*binning) uct_iso_pass_vs_pu = Hist(*binning) total_vs_pu = Hist(*binning) eff_ntuple.Draw("nPVs", "recoPt > 40", hist=total_vs_pu) eff_ntuple.Draw("nPVs", "recoPt > 40 && l1gMatch && max(l1gPt, l1gRegionEt) > 25", hist=uct_pass_vs_pu) eff_ntuple.Draw("nPVs", "recoPt > 40 && l1gMatch && max(l1gPt, l1gRegionEt) > 25" " && l1gJetPt/max(l1gPt, l1gRegionEt) - 1 < 0.5", hist=uct_iso_pass_vs_pu) eff_ntuple.Draw("nPVs", "recoPt > 40 && l1Match && l1Pt > 44",
tchi2posX = 0.07 tchi2posY = 0.75 h2costphiTitle = '%s, W = [%.3f,%.3f), Q^{2} = [%.3f,%.3f)' h2costphiXtitle = '#phi^{*} (radians)' h2costphiYtitle = 'cos(#theta^{*})' #output data oroot = 'out/ana_bd.root' orecords = 'out/ana_bd.dat' keyorder = ['Wbin', 'Q2bin', 'W', 'Wlo', 'Whi', 'Q2', 'Q2lo', 'Q2hi', 'weight3s', 'weightsb', 'fbgstat', 'fbgchi2', 'fsigstat', 'fsigchi2', 'fbgpar0', 'fbgpar1', 'fbgpar2', 'fbgpar3', 'fbgpar4', 'fbgpar5', 'fsigpar0', 'fsigpar1', 'fsigpar2', 'fsigpar3', 'hsigint3s'] # get histograms for simulation, acceptance, and experiment fin_t = ropen(ifacc) fin_e = ropen(ifexp) h4_t, h4_a = fin_t.Get(ihthr), fin_t.Get(ihacc) h6_e = fin_e.Get(ihexp) # open output files fout = ropen(oroot, 'recreate') foutrecs = open(orecords, 'w') # write data header for i, k in enumerate(keyorder): if i > 0: foutrecs.write(',') foutrecs.write(k) foutrecs.write(',xsect,err,hcost\n') # TODO: add cutoff at phase-space edge
Tree models and object collections ================================== This example demonstrates how to define a tree model and collections of objects associated to sets of tree branches. """ print __doc__ from rootpy.tree import Tree, TreeModel from rootpy.io import root_open as ropen from rootpy.types import FloatCol, IntCol from rootpy.math.physics.vector import LorentzVector from rootpy import stl from random import gauss, randint f = ropen("test.root", "recreate") # define the model class Event(TreeModel): # properties of particle "a" a_x = FloatCol() a_y = FloatCol() a_z = FloatCol() # properties of particle "b" b_x = FloatCol() b_y = FloatCol() b_z = FloatCol()
sys.exit(1) channel = sys.argv[-2] saveplot = str2bool(sys.argv[-1]) print sys.argv mydirpath = 'results/2013-May-14-8TeV/FakeRates' mydir = ''.join([mydirpath,channel,'/']) print 'Analysing Fakerates in dir ', mydir files = glob(mydir+'alldata.root') ##outputfiles = glob(mydir+'output.root') ##print outputfiles print files nfile =0 tfile=ropen(files[0]) dir=tfile.Get('qcd') subdirlistQCD = GetContent(dir) #[ i for i in GetContent(dir) if i.GetName().startswith('pt10_') or i.GetName().startswith('pt20_')] total_view = SumView( *[ropen(i) for i in files] ) qcd_view = SubdirectoryView(total_view, 'qcd') if channel=='EE': wjet_view = SubdirectoryView(total_view, 'wjetsNoZmass') else: wjet_view = SubdirectoryView(total_view, 'wjets') for m,n in enumerate (subdirlistQCD): thlistQCD = [] FindTH1( subdirlistQCD[m], '', thlistQCD)
from rootpy.io import root_open as ropen from ..common import nprong, PRONGS, LEVELS import os HERE = os.path.dirname(os.path.abspath(__file__)) P1130_FILE = ropen(os.path.join(HERE, "ParametrizedBDTSelection.root")) def selection(level, prong): prong = nprong(prong) return P1130_FILE.Get("%s_%dp" % (level, prong))
idx1 = self.xmapping[v1] idx2 = self.ymapping[v2] return self.matrix[idx1,idx2].value wps = glob('plots/%s/ctageff/mass_discriminant/%s*' % (jobid, args.algo.lower())) wp_mapping = { i:j for j, i in enumerate(['Loose', 'Medium', 'Tight'])} sfs = ['bottomSF', 'charmSF', 'lightSF'] for wp in wps: wp_name = os.path.basename(wp)[len(args.algo):] wp_idx = wp_mapping[wp_name] shifts = ['%s/MaxLikeFit.root' % wp, '%s/MaxLikeFitStatistic.root' % wp] + glob('%s/sys_breakdown/*.root' % wp) first = True stat_errs = {} correlations = CorrelationMatrix( rpy(ropen('%s/MaxLikeFit.root' % wp).fit_s.correlationHist()) ) for shift in shifts: tfile = ropen(shift) pars = rpy(tfile.fit_s.floatParsFinal()) for sf_idx, sf_name in enumerate(sfs): if sf_name in pars: var = pars[sf_name] err = max(abs(i) for i in var.error) if hasattr(var.error, '__len__') else var.error shift_name = '' sign = +1 if 'sys_breakdown' in shift: base = os.path.basename(shift) shift_name = '_%s' % base.replace('.root', '') sign = -1 if shift_name in correlations and correlations[sf_name, shift_name] < 0 else +1 elif shift.endswith('Statistic.root'):
""" This first section of code only creates an example tree chain. """ class Event(TreeModel): """Event model definition""" x = FloatCol() y = FloatCol() z = FloatCol() i = IntCol() # first create several example trees in separate files fnames = ["test_%d.root" % i for i in xrange(10)] for fname in fnames: with ropen(fname, "recreate") as f: 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() """ This section below takes the example trees and copies it while overwriting a branch with new values.
ft.SetLineColor(r.kGreen+1) ft.SetLineStyle(2) #ft.Draw('same') h.GetListOfFunctions().Add(ft) fu = None if uamp > 0: fu = r.TF1('fu', '[0]*TMath::Exp(-[1]*x)', -1, 1) fu.SetParameters(f.GetParameter(len(f.legs)+2), f.GetParameter(len(f.legs)+3)) fu.SetLineColor(r.kYellow+1) fu.SetLineStyle(2) #fu.Draw('same') return (f, fl, ft, fu, h, status) fin = ropen('top1/ana_bd.root') df = pd.read_csv('top1/ana_bd.dat') def intQ2(wmid=1890): wstr = '_%d_' % wmid hs = [fin.Get(hn) for hn in df.hcost[df.hcost.apply(lambda x: wstr in x)]] dQ2 = df[df.hcost.apply(lambda x: wstr in x)][['Q2lo', 'Q2hi']] dQ2 = dQ2.Q2hi - dQ2.Q2lo # unscale, add, scale for i, h in enumerate(hs): h.Scale(dQ2.iloc[i]) htot = hs[0].Clone(wstr) for i in range(1, len(hs)): htot.Add(hs[i]) for i, h in enumerate(hs): h.Scale(1/dQ2.iloc[i]) htot.Scale(1/dQ2.sum())
import os HERE = os.path.dirname(os.path.abspath(__file__)) CATEGORIES = { '_3': Cut('tau_numberOfVertices<=3'), '3_5': Cut('3<tau_numberOfVertices<=5'), '5_7': Cut('5<tau_numberOfVertices<=7'), '7_': Cut('tau_numberOfVertices>7'), } if __name__ == '__main__': with ropen(os.path.join(HERE, 'bdt_selection.root'), 'recreate') as f: for prong in PRONGS: for cat_str, category in CATEGORIES.items(): for level_name, level in LEVELS.items(): fname = 'sig-bits-%dp-%s-perfB--%d.txt' % ( prong, category.safe(parentheses=False), level) with open(fname) as fin: lines = fin.readlines()[1:] graph = Graph(len(lines), name='%s_%dp_%s' % ( level_name, prong, cat_str)) for i, line in enumerate(lines): pt, bdt = map(float, line.strip().split()) graph[i] = (pt, bdt) graph.Write() else:
import pandas as pd from rootpy.io import root_open as ropen from rootpy.interactive import wait import ROOT as r from functools import reduce r.gSystem.Load('libMathMore.so') fin = ropen('out/ana_bd.root') df = pd.read_csv('out/ana_bd.dat') df1bin = df[(df.Wbin == 15) & (df.Q2bin == 5)] hs = dict(zip(zip(df.Wbin, df.Q2bin), [fin.Get(hn) for hn in df.hcost])) h = hs[(15, 5)] def get_d_legs(legs): lms = legs def d_legs(v, par): retval = reduce(lambda x, y: x + y, [par[i] * r.Math.assoc_legendre( L, M, v[0]) for i, (L, M) in enumerate(lms)]) return retval return d_legs def get_f_legs(legs, par): retf = r.TF1('flegs', get_d_legs(legs), -1, 1, len(legs)) for i in range(0, len(legs)): retf.SetParameter(i, par[i]) return retf
from rootpy.io import root_open as ropen from rootpy.plotting import Canvas, Hist, Efficiency, Legend import rootpy rootpy.log.basic_config_colorized() eff_file = ropen("../data/LSB50/uct_tau_efficiency.root") eff_file_low = ropen("../data/LSB50/uct_mc_efficiency_pu35.root") eff_file_high = ropen("../data/LSB50/uct_mc_efficiency_pu50.root") eff_ntuple = eff_file_high["rlxTauEfficiency/Ntuple"] #binning = (25, 20, 70) binning = (range(20, 32, 2) + range(32, 45, 1) + range(45, 55, 2) + range(55, 70, 5),) l1_pass_vs_pu = Hist(*binning) uct_pass_vs_pu = Hist(*binning) uct_iso_pass_vs_pu = Hist(*binning) total_vs_pu = Hist(*binning) eff_ntuple.Draw("nPVs", "recoPt > 40", hist=total_vs_pu) eff_ntuple.Draw( "nPVs", "recoPt > 40 && l1gMatch && max(l1gPt, l1gRegionEt) > 25", hist=uct_pass_vs_pu) eff_ntuple.Draw( "nPVs", "recoPt > 40 && l1gMatch && max(l1gPt, l1gRegionEt) > 25"
import ROOT from rootpy.io import root_open as ropen from rootpy.tree import Tree f = ropen("test.root", "recreate") print ROOT.gDirectory.GetName() t = Tree() f2 = ropen("test2.root", "recreate") f2.Close() print ROOT.gDirectory.GetName() #f.cd() <== this should not be needed! # the tree should "remember" what file it was created in t.Write() f.Close()
else: raise ValueError("invalid working point: %s" % selection) return passing / total if __name__ == '__main__': from samples import MC_TauID from higgstautau.tauid.p851 import selection, nvtx_to_category, CATEGORIES from higgstautau.tauid.common import LEVELS, PRONGS from higgstautau.tauid import EFFIC_UNCERT_2011 as EFFIC_UNCERT from rootpy.io import open as ropen with ropen('bdt_uncertainty.root', 'recreate') as f: ztautau = MC_TauID(systematics=False) for prong in PRONGS: for cat_str, category in CATEGORIES.items(): def binary_search(target, selection, shift, min_error=0.0001, min_change=0.000001, reverse=False): print "target:", target if reverse: a = 1. b = 0. else: a = 0. b = 1.
print weightsPerSample x1A = vstack((sigTestA, bkgTestA)) y1A = hstack((onesInt(len(sigTestA)), zerosInt(len(bkgTestA)))) y1A = transpose(y1A) from rootpy.interactive import wait from rootpy.plotting import Canvas, Hist, Hist2D, Hist3D, Legend from rootpy.io import root_open as ropen, DoesNotExist from rootpy.plotting import HistStack import ROOT ROOT.gROOT.SetBatch(True) f = ropen('output.root','recreate') c1 = Canvas() c1.cd() histDictSigA = {'W':[],'Z':[],'WW':[],'ZZ':[],'st':[],'ttbar':[],'WZ':[],'WH125':[]} histDictBkgA = {'W':[],'Z':[],'WW':[],'ZZ':[],'st':[],'ttbar':[],'WZ':[],'WH125':[]} coloursForStack = ['blue', 'green', 'red', 'yellow', 'black', 'pink', 'magenta', 'cyan'] colourDict = {'W':0,'Z':1,'WW':2,'ZZ':3,'st':4,'ttbar':5,'WZ':6,'WH125':7} lblcount = 0 sigTestA = transpose(sigTestA) bkgTestA = transpose(bkgTestA)
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 ropen('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..."
if abs(tau.eta) > 1.5: region = 'forward' else: region = 'central' sf, sf_uncert = EFFIC_SF_2012[wp][np][region] else: raise ValueError( 'no efficiency scale factors defined for year %d' % year) return sf, sf_uncert HERE = os.path.dirname(os.path.abspath(__file__)) # uncertainty currently only valid for 2011 MC BDT_UNCERT = {} with ropen(os.path.join(HERE, 'bdt_uncertainty.root')) as f: for level in LEVELS.keys(): BDT_UNCERT[level] = {} for prong in PRONGS: BDT_UNCERT[level][prong] = {} for category in CATEGORIES.keys(): BDT_UNCERT[level][prong][category] = {} for dir in ('high', 'low'): BDT_UNCERT[level][prong][category][dir] = f.Get( '%s_%s_%dp_%s' % ( level, dir, prong, category)).Clone() def uncertainty(score, pt, prong, nvtx): prong = nprong(prong)
sys.exit(1) channel = sys.argv[-2] saveplot = str2bool(sys.argv[-1]) print sys.argv mydirpath = 'results/2013-May-14-8TeV/FakeRates' mydir = ''.join([mydirpath, channel, '/']) print 'Analysing Fakerates in dir ', mydir files = glob(mydir + 'alldata.root') ##outputfiles = glob(mydir+'output.root') ##print outputfiles print files nfile = 0 tfile = ropen(files[0]) dir = tfile.Get('qcd') subdirlistQCD = GetContent( dir ) #[ i for i in GetContent(dir) if i.GetName().startswith('pt10_') or i.GetName().startswith('pt20_')] total_view = SumView(*[ropen(i) for i in files]) qcd_view = SubdirectoryView(total_view, 'qcd') if channel == 'EE': wjet_view = SubdirectoryView(total_view, 'wjetsNoZmass') else: wjet_view = SubdirectoryView(total_view, 'wjets') for m, n in enumerate(subdirlistQCD): thlistQCD = []
from random import gauss """ This first section of code only creates an example tree. """ # define the model class Event(TreeModel): x = FloatCol() y = FloatCol() z = FloatCol() i = IntCol() # first create a tree "test" in a file "test.root" f = ropen("test.root", "recreate") 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() """ This section below takes the example tree and copies it while overwriting a branch with new values.
from numpy import * from rootpy.interactive import wait from rootpy.plotting import Canvas, Hist, Hist2D, Hist3D, Legend from rootpy.io import root_open as ropen, DoesNotExist from rootpy.plotting import HistStack import ROOT import HistoTransform_ext as ht print 'loading files from npy' bdt_in = load('bdt_output_scores_A.npy') classes = load('bdt_output_classes_A.npy') bdt_in_B = load('bdt_output_scores_B.npy') classes_B = load('bdt_output_classes_B.npy') #need to assign names that follow correct naming convention names = [ 'bkg_2tag2jet_mva', 'WlvH125_2tag2jet_mva'] bdtx_inFile_name = "bdt_inFile.root" bdtx_inFile = ropen('bdt_inFile.root','recreate') #for j in xrange(2): #bdtx_inFile_name = "bdt_inFile_"+str(j)+"of2.root" #bdtx_inFile = ropen('bdt_inFile_'+str(j)+'of2.root','recreate') bdtxarr = [] bdtxarr.append([]) bdtxarr.append([]) for i in xrange(2): for j in xrange(2): bdtxarr[i].append(Hist (1000,-1,1, name=str(names[i]+'_' +str(j) + 'of2'), title=str(names[i]+'_' +str(j) + 'of2'))) bdtxarr[i][j].fill_array(bdt_in[classes == i]) bdtxarr[i][j].Write() bdtx_inFile.close()