def Getter(view, histoname, title=None): def doot(x): #print "doot", histoname return histoname if title is not None: return views.FunctorView(views.TitleView(views.PathModifierView(view, doot), title), set_line_width) else: return views.FunctorView(views.PathModifierView(view, doot), set_line_width)
def rebin_view(x, rebin): ''' Make a view which rebins histograms ''' if rebin > 1: rebinner = lambda x: x.Rebin(rebin) output = views.FunctorView(x, rebinner) return output else: return x
def int_view(x): return views.FunctorView(x, round_to_ints)
parser.add_argument('pd', help='Data primary dataset') parser.add_argument('output', help='Data primary dataset') parser.add_argument('files', metavar='file', nargs='+', help='Histogram files') args = parser.parse_args(args[1:]) meta_info = None log.info("Opening meta file: %s", args.meta) with open(args.meta) as meta_file: meta_info = json.load(meta_file) log.info("Building views") data_views = data_views.data_views(args.files, args.pd) data_view = views.FunctorView(data_views[args.pd]['view'], lambda x: x.Rebin(2)) mc_samples_to_stack = ['Zjets', 'QCDMu', 'Wjets', 'ttjets'] # Build the MC stack mc_view = views.StackView(*[ views.FunctorView(data_views[x]['view'], lambda x: x.Rebin(2)) for x in mc_samples_to_stack ], sort=True) # 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) canvas = Canvas()
def write_shapes(self, variable, rebin, outdir, qcd_fraction=0., #[1., 0., -1.], show_charge_fakes=False, project=None, project_axis=None, different_fakes=False): ''' Write final shapes for [variable] into a TDirectory [outputdir] ''' show_charge_fakes = show_charge_fakes if 'show_charge_fakes' not in self.defaults else self.defaults['show_charge_fakes'] sig_view = self.make_signal_views(unblinded=(not self.blind), qcd_weight_fraction=qcd_fraction, rebin=rebin, project=project, project_axis=project_axis) different_fakes_views = self.make_additional_fakes_view( unblinded=(not self.blind), rebin=rebin, project=project, project_axis=project_axis) outdir.cd() wz_weight = self.get_view('WZJetsTo3LNu*ZToTauTau*', 'weight') zz_weight = self.get_view('ZZJetsTo4L*', 'weight') print "wz_weight: %s" % wz_weight print "zz_weight: %s" % zz_weight wz = views.FunctorView( views.SumView(sig_view['wz'], sig_view['wz_3l']), make_empty_bin_remover(wz_weight)).Get(variable) zz = views.FunctorView( sig_view['zz'], make_empty_bin_remover(zz_weight)).Get(variable) obs = sig_view['data'].Get(variable) fakes = sig_view['fakes'].Get(variable) if not different_fakes else different_fakes_views['fakes'].Get(variable) fakes_down = different_fakes_views['fakes'].Get(variable) fakes_up = PositiveView( views.SumView( views.ScaleView(sig_view['fakes'], 2.), views.ScaleView(different_fakes_views['fakes'], -1.) ) ).Get(variable) wz.SetName('wz') zz.SetName('zz') obs.SetName('data_obs') fakes.SetName('fakes') fakes_down.SetName('fakes_CMS_vhtt_%s_fakeshape_%sTeVDown' % (outdir.GetName(), self.sqrts)) fakes_up.SetName('fakes_CMS_vhtt_%s_fakeshape_%sTeVUp' % (outdir.GetName(), self.sqrts)) #for mass in [110, 115, 120, 125, 130, 135, 140]: #set_trace() for mass in range(90, 165, 5): try: vh = None if mass == 90 and self.sqrts == 8: vh = views.ScaleView(sig_view['vh100'], 1.3719).Get(variable) elif mass == 95 and self.sqrts == 8: vh = views.ScaleView(sig_view['vh100'], 1.1717).Get(variable) else: vh = sig_view['vh%i' % mass].Get(variable) vh.SetName('WH%i' % mass) vh.SetLineColor(0) vh.Write() except KeyError: #logging.warning('No sample found matching VH_*%i' % mass) continue if mass % 10 == 0 and mass < 150: # Only have 10 GeV steps for WW if 'vh%i_hww' % mass in sig_view: ww = sig_view['vh%i_hww' % mass].Get(variable) ww.SetName('WH_hww%i' % mass) ww.Write() wz.Write() zz.Write() obs.Write() fakes.Write() fakes_down.Write() fakes_up.Write() #charge_fakes_CMS_vhtt_emt_chargeFlip_8TeVUpx if show_charge_fakes: logging.info('adding charge fakes shape errors') charge_fakes = sig_view['charge_fakes']['central'].Get(variable) charge_fakes_sys_up = sig_view['charge_fakes']['sys_up' ].Get(variable) #shift='up') charge_fakes_sys_down = charge_fakes+charge_fakes - charge_fakes_sys_up charge_fakes.SetName('charge_fakes') charge_fakes_sys_up.SetName('charge_fakes_CMS_vhtt_%s_chargeFlip_%sTeVUp' % (self.channel.lower(), self.sqrts)) charge_fakes_sys_down.SetName('charge_fakes_CMS_vhtt_%s_chargeFlip_%sTeVDown' % (self.channel.lower(), self.sqrts)) charge_fakes.Write() charge_fakes_sys_up.Write() charge_fakes_sys_down.Write()
# 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 regions[type] = views.FunctorView(regions[type], lambda x: x.Rebin(5)) # Make views of the numerator and denominator. # For RooFit we have to split into Pass & Fail # So subtract the numerator from the denominator num_view = regions[type] all_view = views.PathModifierView(regions[type], denominator_path_mangler) negative_num_view = views.ScaleView(num_view, -1) fail_view = views.SumView(all_view, negative_num_view) # Now make RooDataHistViews of the numerator & denominator regions[type] = ( views.FunctorView(num_view, roodatahistizer), views.FunctorView(fail_view, roodatahistizer), )
parser.add_argument('output', help='Output directory') parser.add_argument('files', metavar='file', nargs='+', help = 'Histogram files') args = parser.parse_args(args[1:]) meta_info = None log.info("Opening meta file: %s", args.meta) with open(args.meta) as meta_file: meta_info = json.load(meta_file) log.info("Building views") data_views = data_views.data_views( args.files, 'data_DoubleMu', ) data_view = views.FunctorView(data_views['data_DoubleMu']['view'], lambda x: x.Rebin(1)) mc_samples_to_stack = ['Zjets', 'QCDMu', 'Wjets', 'ttjets'] # Build the MC stack mc_view = views.StackView(*[ views.FunctorView(data_views[x]['view'], lambda x: x.Rebin(1)) for x in mc_samples_to_stack], sort=True) log.debug("Zjets unweighted entries: %f", data_views['Zjets']['unweighted_view'].Get( 'plots/entry_count').Integral() ) # We just need to figure out the directory structure from any old file layout_filename = data_views.values()[0]['subsamples'].values()[0]['filename']
def postive_view(x): ''' Make a view where all bins > 0 ''' return views.FunctorView(x, all_bins_positive)