Example #1
0
        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)
Example #2
0
def correct_for_contrib_in_fakes(x, fudge_factor=1.0):
    ''' Make a view of MC which corrects for the contribution in the fakes '''
    fakes_view = views.PathModifierView(x, get_fakes)
    invert_view = views.ScaleView(fakes_view, -1)
    output = views.SumView(x, invert_view)
    # Fudge factor from Zmumu (from H2TAu inclusive)
    fudge = views.ScaleView(output, fudge_factor)
    return fudge
    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(sample_info['unweighted_view'],
                                              make_path_mangler('pass'))
            all_view = views.PathModifierView(sample_info['unweighted_view'],
                                              make_path_mangler('all'))
            num_entries = num_view.Get(path).Integral()
            all_entries = all_view.Get(path).Integral()

            eff, eff_down, eff_up = 0, 0, 0
            if all_entries:
                eff, eff_down, eff_up = efficiency(num_entries, all_entries)
                eff, eff_down, eff_up = (eff * 100, 100 * eff_down,
                                         100 * eff_up)

            sys.stdout.write(
                "%s: %i/%i = %0.2f%% +%0.2f -%0.2f\n" %
                (sample, num_entries, all_entries, eff, eff_up, eff_down))
def get_ss(x):
    return x.replace('os/', 'ss/')


print foldernames
mymapper = {
    "os/": "ss/",
    "os/0/": "ss/0/",
    "os/1": "ss/1",
    "os/2": "ss/2",
    "os/3": "ss/3"
}
QCD = views.TitleView(
    views.StyleView(
        SubtractionView(views.PathModifierView(plotter.data, get_ss),
                        views.PathModifierView(TT, get_ss),
                        views.PathModifierView(singleT, get_ss),
                        views.PathModifierView(SMH, get_ss),
                        views.PathModifierView(DYTT, get_ss),
                        views.PathModifierView(DYLL, get_ss),
                        views.PathModifierView(Wplus, get_ss),
                        views.PathModifierView(EWKDiboson, get_ss)),
        **data_styles['QCD*']), 'QCD')

plotter.views['QCD'] = {'view': QCD}
plotter.mc_samples.extend(['QCD'])

for foldername in foldernames:
    if foldername.startswith("ss") and bool(
            'QCD' in plotter.mc_samples) == True:
Example #5
0
    def make_obj3_fail_cr_views(self, qcd_correction=False,
                                qcd_weight_fraction=0, tau_charge='tau_os'):
        ''' Make views when obj3 fails, estimating the bkg in obj1 pass using
            f1p2f3 '''
        other_tau_sign = 'tau_os' if tau_charge == 'tau_ss' else 'tau_ss'

        all_wz_ztt_view = self.get_view('WZJetsTo3LNu*ZToTauTau*')
        wz_view = views.SubdirectoryView(
            all_wz_ztt_view,
            'ss/%s/p1p2f3/' % tau_charge
        )

        tomatch = 'WZJetsTo3LNu' if self.sqrts == 7 else 'WZJetsTo3LNu_pythia'
        all_wz_3l_view = self.get_view(tomatch)
        wz_view_3l = views.SubdirectoryView(
            all_wz_3l_view,
            'ss/%s/p1p2f3/' % tau_charge
        )

        all_wz_view = views.SumView(all_wz_ztt_view, all_wz_3l_view)

        zz_view = views.SubdirectoryView(
            self.get_view('ZZJetsTo4L*'),
            'ss/%s/p1p2f3/' % tau_charge
        )
        all_data_view = self.get_view('data')
        data_view = views.SubdirectoryView(all_data_view, 'ss/%s/p1p2f3/' % tau_charge)

        def make_fakes(qcd_fraction):
            def make_fakes_view(weight_type, scale):
                scaled_bare_data = views.ScaleView(all_data_view, scale)
                scaled_wz_data   = views.ScaleView(all_wz_view,   scale)
                scaled_data      = SubtractionView(scaled_bare_data, scaled_wz_data, restrict_positive=True) 
                # View of weighted obj1-fails data
                obj1_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/f1p2f3/%s1' % (tau_charge, weight_type))
                # View of weighted obj2-fails data
                obj2_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/p1f2f3/%s2' % (tau_charge, weight_type))
                # View of weighted obj1&2-fails data
                obj12_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/f1f2f3/%s12' % (tau_charge, weight_type))

                # Give the individual object views nice colors
                obj1_view = views.TitleView(
                    views.StyleView(obj1_view, **remove_name_entry(data_styles['TT*'])),
                    'Reducible bkg. 1')
                obj2_view = views.TitleView(
                    views.StyleView(obj2_view, **remove_name_entry(data_styles['QCD*'])),
                    'Reducible bkg. 2')
                obj12_view = views.TitleView(
                    views.StyleView(obj12_view, **remove_name_entry(data_styles['WW*'])),
                    'Reducible bkg. 12')

                subtract_obj12_view = views.ScaleView(obj12_view, -1)
                return obj1_view, obj2_view, obj12_view, subtract_obj12_view

            qcd1, qcd2, qcd12, negqcd12 = make_fakes_view('q', qcd_fraction)
            wjet1, wjet2, wjet12, negwjet12 = make_fakes_view(
                'w', 1 - qcd_fraction)

            obj1_view  = views.SumView(qcd1, wjet1)
            obj2_view  = views.SumView(qcd2, wjet2)
            obj12_view = views.SumView(qcd12, wjet12)
            subtract_obj12_view = views.SumView(negqcd12, negwjet12)

            # Corrected fake view
            fakes_view = views.SumView(obj1_view, obj2_view, subtract_obj12_view)
            fakes_view = views.TitleView(
                views.StyleView(fakes_view, **remove_name_entry(data_styles['Zjets*'])), 'Reducible bkg.')
            return obj1_view, obj2_view, obj12_view, fakes_view

        obj1_view, obj2_view, obj12_view, fakes_view = make_fakes(qcd_weight_fraction)
        fakes_view_05 = make_fakes(0.5)[-1]
        fakes_view_0  = make_fakes(0)[-1]
        fakes_view_1  = make_fakes(1)[-1]

        style_dict_no_name = remove_name_entry(data_styles['TT*'])
        charge_fakes = views.TitleView(  #FIXME
            views.StyleView(
                views.SumView(
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c1' % other_tau_sign),
                        create_mapper(self.obj1_charge_mapper)
                        ),
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c2' % tau_charge),
                        create_mapper(self.obj2_charge_mapper)
                        ),
                    ),
                **style_dict_no_name),
            'Charge mis-id')
        
        charge_fakes_sysup = views.TitleView(
                 views.StyleView(
                     views.SumView(
                         views.PathModifierView(
                             views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c1_sysup'  % other_tau_sign),
                             create_mapper(self.obj1_charge_mapper)
                         ),
                         views.PathModifierView(
                             views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c2_sysup' % tau_charge),
                             create_mapper(self.obj2_charge_mapper)
                         ),
                     ),
                 **style_dict_no_name),
            'Charge mis-id')

        #charge_fakes = MedianView(highv=charge_fakes_sysup, centv=charge_fakes)
                            
        output = {
            'wz': wz_view,
            'wz_3l': wz_view_3l,
            'zz': zz_view,
            'data': data_view,
            'obj1': obj1_view,
            'obj2': obj2_view,
            'obj12': obj12_view,
            'fakes': fakes_view,
            'weighted_fakes' : {
                0.  : fakes_view_0,
                0.5 : fakes_view_05,
                1.  : fakes_view_1,
            },
            'charge_fakes': {
                'central' : charge_fakes,
                'sys_up'  : charge_fakes_sysup,
                }
        }

        #Add signal @ 120, just mo make bkg fitting easier
        mass = 120
        vh_view = views.SubdirectoryView(
            self.get_view('VH_*%i' % mass),
            'ss/tau_os/p1p2f3/'
        )
        output['vh%i' % mass] = vh_view
        try:
            ww_view = views.SubdirectoryView(
                self.get_view('VH_%i_HWW*' % mass),
                'ss/tau_os/p1p2f3/'
            )
        except KeyError:
            #logging.warning('No sample found matching VH_%i_HWW*' % mass)
            ww_view = None
        output['vh%i_hww' % mass] = ww_view
        output['signal%i' % mass] = views.SumView(ww_view, vh_view) if ww_view else vh_view

        return output
Example #6
0
    def make_signal_views(self, unblinded=False, qcd_weight_fraction=0, #MARK
                          rebin=1, project=None, project_axis=None, tau_charge='tau_os' ):
        ''' Make signal views with FR background estimation '''
        other_tau_sign = 'tau_os' if tau_charge == 'tau_ss' else 'tau_ss'

        def preprocess(view):
            ret = view
            if project and project_axis:
                ret = ProjectionView(ret, project_axis, project)
            return RebinView( ret, rebin )

        all_wz_view_tautau = preprocess( self.get_view('WZJetsTo3LNu*ZToTauTau*') )
        wz_view_tautau = views.SubdirectoryView(
                all_wz_view_tautau,
                'ss/%s/p1p2p3/' % tau_charge
        )

        tomatch = 'WZJetsTo3LNu' if self.sqrts == 7 else 'WZJetsTo3LNu_pythia'
        all_wz_view_3l = preprocess( self.get_view(tomatch) )
        wz_view_3l = views.SubdirectoryView(
                all_wz_view_3l,
                'ss/%s/p1p2p3/' % tau_charge
        )
        all_wz_view = views.SumView(all_wz_view_tautau, all_wz_view_3l)

        zz_view = preprocess(
            views.SubdirectoryView(
                self.get_view('ZZJetsTo4L*'),
                'ss/%s/p1p2p3/' % tau_charge
            )
        )

        all_data_view = self.get_view('data')
        #if unblinded:
        #    all_data_view = self.get_view('data', 'unblinded_view')
        all_data_view = preprocess(all_data_view)
            
        data_view = views.SubdirectoryView(all_data_view, 'ss/%s/p1p2p3/' % tau_charge)

        def make_fakes(qcd_fraction):
            def make_fakes_view(weight_type, scale):
                scaled_bare_data = views.ScaleView(all_data_view, scale)
                scaled_wz_data   = views.ScaleView(all_wz_view, scale)
                scaled_data      = SubtractionView(scaled_bare_data, scaled_wz_data, restrict_positive=True)

                # View of weighted obj1-fails data
                obj1_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/f1p2p3/%s1' % (tau_charge, weight_type))
                # View of weighted obj2-fails data
                obj2_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/p1f2p3/%s2' % (tau_charge, weight_type))
                # View of weighted obj1&2-fails data
                obj12_view = views.SubdirectoryView(
                    scaled_data, 'ss/%s/f1f2p3/%s12' % (tau_charge, weight_type))

                # Give the individual object views nice colors
                obj1_view = views.TitleView(
                    views.StyleView(obj1_view, **remove_name_entry(data_styles['TT*'])),
                    'Reducible bkg. 1')
                obj2_view = views.TitleView(
                    views.StyleView(obj2_view, **remove_name_entry(data_styles['QCD*'])),
                    'Reducible bkg. 2')
                obj12_view = views.TitleView(
                    views.StyleView(obj12_view, **remove_name_entry(data_styles['WW*'])),
                    'Reducible bkg. 12')

                return obj1_view, obj2_view, obj12_view

            qcd1, qcd2, qcd12 = make_fakes_view('q', qcd_fraction)
            wjet1, wjet2, wjet12 = make_fakes_view(
                'w', 1 - qcd_fraction)

            obj1_view = views.SumView(qcd1, wjet1)
            obj2_view = views.SumView(qcd2, wjet2)
            obj12_view = views.SumView(qcd12, wjet12)

            # Corrected fake view
            fakes_view = views.MultiFunctorView(fake_rate_estimate, obj1_view, obj2_view, obj12_view)
            fakes_view = views.TitleView(
                views.StyleView(fakes_view, **remove_name_entry(data_styles['Zjets*'])), 'Reducible bkg.')
            return obj1_view, obj2_view, obj12_view, fakes_view

        obj1_view, obj2_view, obj12_view, fakes_view = make_fakes(qcd_weight_fraction)
        fakes_view_05 = make_fakes(0.5)[-1]
        fakes_view_0  = make_fakes(0)[-1]
        fakes_view_1  = make_fakes(1)[-1]
 
        charge_fakes = views.TitleView( 
            views.StyleView(
                views.SumView(
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c1' % other_tau_sign), #FIXME: needs to be fixed for charge 3 region
                        create_mapper(self.obj1_charge_mapper)
                        ),
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c2' % tau_charge),
                        create_mapper(self.obj2_charge_mapper)
                        ),
                    ),
                **remove_name_entry(data_styles['TT*'])),
            'Charge mis-id')

        charge_fakes_sysup = views.TitleView( 
            views.StyleView(
                views.SumView(
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c1_sysup' % other_tau_sign),
                        create_mapper(self.obj1_charge_mapper)
                        ),
                    views.PathModifierView(
                        views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c2_sysup' % tau_charge),
                        create_mapper(self.obj2_charge_mapper)
                        ),
                    ),
                **remove_name_entry(data_styles['TT*'])),
            'Charge mis-id')

        #charge_fakes = MedianView(highv=charge_fakes_sysup, centv=charge_fakes)

        output = {
            'wz': wz_view_tautau,
            'wz_3l': wz_view_3l,
            'zz': zz_view,
            'data': data_view,
            'obj1': obj1_view,
            'obj2': obj2_view,
            'obj12': obj12_view,
            'fakes': fakes_view,
            'weighted_fakes' : {
                0.  : fakes_view_0,
                0.5 : fakes_view_05,
                1.  : fakes_view_1,
            },
            'charge_fakes': {
                'central' : charge_fakes,
                'sys_up'  : charge_fakes_sysup,
                }
        }

        # Add signal
        data_total_lumi = self.views['data']['intlumi']
        for mass in range(90, 165, 5):
            try:
                vh_base_name =  'VH_%s' % mass if self.sqrts == 7 else 'VH_H2Tau_M-%s' % mass
                vh_base_name =  'VHtautau_lepdecay_%s' % mass \
                                if 'VHtautau_lepdecay_%s' % mass in self.views else \
                                vh_base_name
                #print 'using %s' %  vh_base_name
                vh_base_view =  self.views[vh_base_name]['view'] 

                vh_view = views.SubdirectoryView(
                    vh_base_view, #self.get_view('VH_*%i' % mass),
                    'ss/%s/p1p2p3/' % tau_charge 
                )
                output['vh%i' % mass] = preprocess(vh_view)
            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
                try:
                    ww_view = views.SubdirectoryView(
                        self.get_view('VH_%i_HWW*' % mass),
                        'ss/%s/p1p2p3/' % tau_charge
                    )
                    output['vh%i_hww' % mass] = preprocess(ww_view)
                except KeyError:
                    #logging.warning('No sample found matching VH_%i_HWW*' % mass)
                    continue
                #output['signal%i' % mass] = views.SumView(ww_view, vh_view) if ww_view else vh_view

        return output
Example #7
0
for x in samples:
    files.extend(glob.glob('results/%s/ControlEM/%s.root' % (jobid, x)))
    lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x)))

plotter = Plotter(files, lumifiles, output_dir)

# Define how we estimate QCD - just take SS data.
import rootpy.plotting.views as views


def get_ss(x):
    return x.replace('em/', 'em/ss/')

mc_view = views.SumView(
    *[views.PathModifierView(plotter.get_view(x), get_ss) for x in [
        'WZJetsTo3LNu*',
        'ZZJetsTo4L*',
        'WW*',
        'WplusJets_madgraph',
        'TTplusJets_madgraph',
        'Zjets_M50',
    ]]
)

#mc_inverted = views.ScaleView(mc_view, -1)
mc_inverted = views.ScaleView(mc_view, -1)

sqrts = 7 if '7TeV' in jobid else 8

qcd_view = views.StyleView(
    # 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),
        )

    log.info("Making output workspace")
    ws = ROOT.RooWorkspace("fit_results")

    def ws_import(*args):
        getattr(ws, 'import')(*args)