def draw(s, variables): xarr1 = read_tree(t_in1, variables + [weight1[s]], cut1[s]) xarr2 = read_tree(t_in2, variables + [weight2[s]], cut2[s]) ret = {} for var in variables: h1 = hbase[var].Clone(s + '_1') h2 = hbase[var].Clone(s + '_2') draw_hist(h1, xarr1, [var], weight1[s]) draw_hist(h2, xarr2, [var], weight2[s]) # h1.SetMaximum(h1.GetMaximum()*2) # h2.SetMaximum(h2.GetMaximum()*2) ret[var] = {1: h1, 2: h2} return ret
def tree_to_arrays(infilepath, treename='inputs'): f = root.TFile(infilepath) t = f.Get(treename) data = {} singletons = r.read_tree(t, branches=singleton_branches) for k in singleton_branches: data[k] = singletons[k] arr = r.read_tree(t, branches=['kinematics']) data['pf'] = np.array([x[0].tolist() for x in arr]) arr = r.read_tree(t, branches=['svs']) data['sv'] = np.array([x[0].tolist() for x in arr]) return data
def draw_all(): f_in = root.TFile.Open('merged.root') t_in = f_in.Get('events') f_out = root.TFile.Open('hists.root', 'RECREATE') weight_strs = [] for idx in xrange(len(weights)): if idx == 0: weight_str = 'normalizedWeight' else: weight_str = 'normalizedWeight*fabs(weights[%i])' % (idx - 1) weight_strs.append(weight_str) n_weights = len(weight_strs) n_per = 50 nominal_arr = None for iw in xrange(0, n_weights, n_per): PInfo(sname, 'Extracting %i -> %i' % (iw, iw + n_per)) xarr = read_tree(t_in, ['genBosonPt'] + weight_strs[iw:iw + n_per]) for idx in xrange(iw, iw + n_per): if idx == n_weights: break h = hbase.Clone() weight_str = weight_strs[idx] weight_name = weights[idx] draw_hist(h, xarr, ['genBosonPt'], weight_str) f_out.WriteTObject(h, 'h_' + weight_name) f_out.Close() f_in.Close()
def convert(tree, branches=['px', 'py', 'pz', 'e', 'ptype'], astype=np.float16): struct_arr = read_tree(tree, branches) arr = np.stack([struct_arr[f].astype(astype) for f in branches], axis=1).transpose((0, 2, 1)) return arr
def tree_to_arrays(infilepath, treename='inputs'): f = root.TFile(infilepath) t = f.Get(treename) data = {} singleton_branches = [ x.GetName() for x in t.GetListOfBranches() if (x.GetName() != 'kinematics') ] singleton_branches = sorted(list(set(singleton_branches))) singletons = r.read_tree(t, branches=singleton_branches) for k in singleton_branches: data[k] = singletons[k] data['singleton_branches'] = np.array(singleton_branches) arr = r.read_tree(t, branches=['kinematics']) data['particles'] = np.array([x[0].tolist() for x in arr]) return data
def run(self, f_out): PInfo('fitting_forest.Process.run', 'Running ' + self.name) branches = sorted(self.all_branches.values()) try: xarr = root_interface.read_tree(tree=self.tree, branches=branches, cut=self.cut) fields = self.variables.keys() + ['nominal'] self.__write_out(f_out, xarr, fields, '') for shift, weight in self.weights.iteritems(): fields = self.variables.keys() + [shift] self.__write_out(f_out, xarr, fields, '_' + shift) except ValueError as e: PError('fitting_forest.Process.run', str(e)) return
def build_hists(cut,weight,observable): h = {} weight = weight%1 cut = tAND(cut,add_cut) weights = { '13TeV' : weight, '8TeV' : weight.replace('sf_tt','sf_tt8TeV'), 'none' : weight.replace('sf_tt','1') } xarr = read_tree(tree,[observable]+weights.values(),cut) for label,weight in weights.iteritems(): h[label] = hbase.Clone(label+'_'+observable) draw_hist(h[label],xarr,[observable],weight) divide_bin_width(h[label]) return h
def draw(s): weight_ = weight cut_ = cut to_plot_ = [x[0] for x in to_plot] for k,v in repl[s].iteritems(): cut_ = cut_.replace(k,v) for i in xrange(len(to_plot_)): to_plot_[i] = to_plot_[i].replace(k,v) cut_ = cut_.replace('dphipfmetUp','dphipfmet') cut_ = cut_.replace('dphipfmetDown','dphipfmet') xarr = read_tree(tree=t_in,branches=to_plot_+[weight_],cut=cut_) ret = {} for name,shifted_name in zip(to_plot,to_plot_): h = hbase[name[0]].Clone(shifted_name) draw_hist(h,xarr,[shifted_name],weight_) ret[name[0]] = h return ret
def draw_all(): f_in = root.TFile.Open('merged.root') t_in = f_in.Get('events') f_out = root.TFile.Open('hists.root', 'RECREATE') weight_strs = [] for idx in xrange(len(weights)): if idx == 0: weight_str = 'normalizedWeight' else: weight_str = 'normalizedWeight*weights[%i]' % (idx - 1) weight_strs.append(weight_str) xarr = read_tree(t_in, ['genBosonPt'] + weight_strs) for idx in xrange(len(weights)): h = hbase.Clone() weight_str = weight_strs[idx] weight_name = weights[idx] draw_hist(h, xarr, ['genBosonPt'], weight_str) f_out.WriteTObject(h, 'h_' + weight_name) f_out.Close() f_in.Close()
e_num = hnum.GetBinError(ib) v_num = hnum.GetBinContent(ib) hratio.SetBinContent(ib,v_num/v_den) hratio.SetBinError(ib,e_num/v_den) return hratio def build_hists(cut,weight,observable): h = {} weight = weight%1 cut = tAND(cut,add_cut) weights = { '13TeV' : weight, '8TeV' : weight.replace('sf_tt','sf_tt8TeV'), 'none' : weight.replace('sf_tt','1') } xarr = read_tree(tree,[observable]+weights.values(),cut) for label,weight in weights.iteritems(): h[label] = hbase.Clone(label+'_'+observable) draw_hist(h[label],xarr,[observable],weight) divide_bin_width(h[label]) return h h_sig = build_hists(sel.cuts['signal'],sel.weights['signal'],'pfmet') h_con = build_hists(sel.cuts['singlemuontop'],sel.weights['singlemuontop'],'pfUWmag') for k in h_sig: h_sig[k].Divide(h_con[k])
def interpolate(): f_interp_path = basedir + '/interpolate/hists_couplings/%s.root' % ( args.mass) f_interp = root.TFile.Open(f_interp_path) PInfo(sname, 'We want to interpolate point %s' % args.mass) if not args.mass_reco: m_V, m_DM = [int(x) for x in args.mass.split('_')] offshell = (2 * m_DM >= m_V) frescos = map( lambda x: x.split('/')[-1].replace('.root', '').replace( 'fittingForest_signal_vector_', ''), glob(basedir + '/signals_3/fittingForest_signal_vector*root')) frescos = map(lambda x: tuple(map(lambda y: int(y), x.split('_'))), frescos) m_V_recos = [x[0] for x in frescos] m_V_reco = min(m_V_recos, key=lambda x: abs(m_V - x)) m_DM_recos = [x[1] for x in frescos if x[0] == m_V_reco] m_DM_recos = [x for x in m_DM_recos if ((2 * x >= m_V) == offshell)] if len(m_DM_recos) == 0: m_DM_recos = [x[1] for x in frescos if x[0] == m_V_reco] m_DM_reco = min(m_DM_recos, key=lambda x: abs(m_DM - x)) args.mass_reco = '%i_%i' % (m_V_reco, m_DM_reco) PInfo(sname, 'We have chosen as the reconstructed point %s' % args.mass_reco) f_reco_path = basedir + '/signals_3/fittingForest_signal_vector_%s.root' % ( args.mass_reco) f_reco = root.TFile.Open(f_reco_path) t_reco = f_reco.Get('%s_signal' % (args.mass_reco)) f_reco_gen_path = basedir + '/interpolate/hists/%s.root' % (args.mass_reco) f_reco_gen = root.TFile.Open(f_reco_gen_path) system('mkdir -p %s/interpolate/interpolated_couplings2test/' % basedir) t_out = t_reco.Clone() metname = 'min(met,999.9999)' branches = [metname] interp2reco = {} for g in couplings_interp: # find the reco g # g_reco = min(couplings_reco, key = lambda x : euclid(parse_coupling_name(g), parse_coupling_name(x))) g_reco = 'nominal' # override PInfo(sname, 'Interpolating %s with %s' % (g, g_reco)) interp2reco[g] = g_reco h_interp = f_interp.Get('h_%s' % g) h_reco_gen = f_reco_gen.Get('h_%s' % g_reco) h_ratio = h_interp.Clone() h_ratio.Divide(h_reco_gen) ba.newBranchName = 'interp_%s' % g ba.AddBranchFromHistogram(t_reco, h_ratio) if g_reco == 'nominal': weightname = 'weight*interp_%s' % g else: weightname = 'weight*%s*interp_%s' % (g_reco.replace( '_nlo', '').replace('rw_', ''), g) branches.append(weightname) for s in systs: branches.append(weightname.replace('weight', s)) xarr = {} xarr['tight'] = read_tree(t_reco, branches, 'top_ecf_bdt>0.45 && met>250') xarr['loose'] = read_tree( t_reco, branches, 'top_ecf_bdt>0.1 && top_ecf_bdt<0.45 && met>250') f_out = root.TFile( '%s/interpolate/interpolated_couplings2test/interp_%s.root' % (basedir, args.mass), 'RECREATE') PInfo( sname, 'Creating %s/interpolate/interpolated_couplings2test/interp_%s.root' % (basedir, args.mass)) for g in couplings_interp: f_out.cd() folder = f_out.mkdir(g) h_templates = {} g_reco = interp2reco[g] if g_reco == 'nominal': weightname = 'weight*interp_%s' % g else: weightname = 'weight*%s*interp_%s' % (g_reco.replace( '_nlo', '').replace('rw_', ''), g) for t in ['tight', 'loose']: h_templates[t] = h_base.Clone() draw_hist(h_templates[t], xarr[t], [metname], weightname) for s in systs: hname = '%s_%s' % (t, s) h_templates[hname] = h_base.Clone() draw_hist(h_templates[hname], xarr[t], [metname], weightname.replace('weight', s)) for k, h in h_templates.iteritems(): folder.WriteTObject(h, 'h_%s' % (k)) f_out.Close()
def draw(tree,weight,cut): h = hbase.Clone() xarr = read_tree(tree,['met',weight],cut) draw_hist(h,xarr,['met'],weight) return h
def get_hist(cut): h = hbase.Clone() xarr = read_tree(events,['pfmetnomu'],cut) draw_hist(h,xarr,['pfmetnomu'],None) return h