def get_old_kfac(tag): if tag.startswith('w'): f = uproot.open(bucoffea_path('data/sf/theory/merged_kfactors_wjets.root')) elif tag.startswith('dy'): f = uproot.open(bucoffea_path('data/sf/theory/merged_kfactors_wjets.root')) elif tag.startswith('gjets'): f = uproot.open(bucoffea_path('data/sf/theory/merged_kfactors_gjets.root')) return f['kfactor_monojet_qcd']
def evaluator_from_config(cfg): """Initiates the SF evaluator and populates it with the right values :param cfg: Configuration :type cfg: DynaConf object :return: Ready-to-use SF evaluator :rtype: coffea.lookup_tools.evaluator """ ext = extractor() for sfname, definition in cfg.SF.items(): if not 'file' in definition: continue fpath = bucoffea_path(definition['file']) if fpath.endswith(".root"): ext.add_weight_sets( [f"{sfname} {definition['histogram']} {fpath}"]) ext.add_weight_sets( [f"{sfname}_error {definition['histogram']}_error {fpath}"]) else: continue ext.finalize() evaluator = ext.make_evaluator() return evaluator
def load_xs(): """Function to read per-sample cross sections from file. :return: Mapping dataset -> cross-section :rtype: dict """ xsfile = bucoffea_path('data/datasets/xs/xs.yml') with open(xsfile, 'r') as f: xs_yml = yaml.load(f, Loader=yaml.FullLoader) # See the documentation in data/datasets/xs/README.md # for more information on how the XS is chosen. xs = {} loading_priority = ['nnnlo', 'nnlo', 'nlo', 'lo', 'gen'] for dataset, xs_dict in xs_yml.items(): if 'use' in xs_dict: key_to_use = xs_dict['use'] else: for key in loading_priority: if key in xs_dict: key_to_use = key break xs[dataset] = xs_dict[key_to_use] # Data sets that only exist as extensions # cause problems later on, so we duplicate the XS # for the base process. tmp = {} for k in xs.keys(): base = re.sub('_ext(\d+)', '', k) if base not in xs.keys(): tmp[base] = xs[k] xs.update(tmp) return xs
def btag_weights(bjets, cfg): # Evaluate weight variations weight_variations = {} # Only calculate for DeepCSV if cfg.BTAG.ALGO != "deepcsv": weight_variations["central"] = bjets.pt.ones_like() weight_variations["up"] = bjets.pt.ones_like() weight_variations["down"] = bjets.pt.ones_like() return weight_variations # Heavy lifting done by coffea implementation bsf = BTagScaleFactor( filename=bucoffea_path(cfg.SF.DEEPCSV.FILE), workingpoint=cfg.BTAG.WP.upper(), methods='comb,comb,incl' # Comb for b and c flavors, incl for light ) for variation in ["central","up","down"]: weights = bsf.eval( systematic=variation, flavor=bjets.hadflav, abseta=bjets.abseta, pt=bjets.pt) # Cap the weights just in case weights[np.abs(weights)>5] = 1 weight_variations[variation] = weights return weight_variations
def pack_repo(path_to_gridpack): '''Creates a gridpack containing the bucoffea repo''' if os.path.exists(path_to_gridpack): raise RuntimeError(f"Gridpack file already exists. Will not overwrite {path_to_gridpack}.") tar = tarfile.open(path_to_gridpack,'w') files = get_repo_files() for f in files: tar.add( name=f, arcname=f.replace(os.path.abspath(bucoffea_path('..')),'bucoffea'), exclude=lambda x: ('tgz' in x or 'submission' in x) ) tar.close() return
def get_repo_files(): '''Returns a list of tracked files in the bucoffea repo''' import git repo = git.Repo(bucoffea_path('..')) to_iterate = [repo.tree()] to_add = [] while len(to_iterate): for item in to_iterate.pop(): if item.type == 'tree': to_iterate.append(item) elif item.type == 'blob': to_add.append(item.abspath) return to_add
def main(): create_tdr_style() r.gROOT.SetBatch(r.kTRUE) fname=bucoffea_path('data/sf/pileup/pileup.root') f = r.TFile(fname) for year in [2017, 2018]: c,t1,t2=setup_canvas(want_ratio=True) hdata = f.Get(f'data{year}_nominal') hmc = f.Get(f'mc{year}') hmc.SetLineWidth(4) hmc.SetFillStyle(4001) hmc.SetFillColorAlpha(r.kAzure,0.25) hdata.SetMarkerStyle(20) hdata.SetMarkerSize(0.7) ratio = hdata.Clone("ratio") ratio.Divide(hmc) s = r.THStack() s.Add(hmc) s.Add(hdata,'PE') t1.cd() s.Draw('nostack') s.SetMaximum(.05) s.GetXaxis().SetRangeUser(0,80) leg = r.TLegend(0.6,0.7,0.9,0.9) leg.SetHeader(f'{year}') leg.AddEntry(hmc,f"MC, #mu = {hmc.GetMean():.1f}") leg.AddEntry(hdata,f"Data #mu = {hdata.GetMean():.1f}") leg.Draw() t2.cd() ratio.Draw('PE') apply_style_to_axis(s,is_ratio=False) apply_style_to_axis(ratio,is_ratio=True,ymin=0.,ymax=2.) ratio.SetMarkerStyle(20) s.GetXaxis().SetRangeUser(0,80) ratio.GetXaxis().SetRangeUser(0,80) ratio.GetXaxis().SetTitle("True number of PU events") c.SaveAs(f"pu_weights_{year}.pdf")
def monojet_evaluator(cfg): """Initiates the SF evaluator and populates it with the right values :param cfg: Configuration :type cfg: DynaConf object :return: Ready-to-use SF evaluator :rtype: coffea.lookup_tools.evaluator """ ext = extractor() for sfname, definition in cfg.SF.items(): fpath = bucoffea_path(definition['file']) ext.add_weight_sets([f"{sfname} {definition['histogram']} {fpath}"]) ext.finalize() evaluator = ext.make_evaluator() return evaluator
def get_old_kfac(tag): '''Given the dataset tag, get the nominal 2D VBF k-factors.''' f = uproot.open(bucoffea_path('data/sf/theory/2017_gen_v_pt_qcd_sf.root')) return f[f'2d_{tag}_vbf'].values