Ejemplo n.º 1
0
 def test_decombine_result_multiple_backgrounds(self):
     N_signal = 100
     N_background_1 = 20
     N_background_2 = 40
     N_total = N_signal + N_background_1 + N_background_2
     # ratio of bkg_1 to other samples
     ratio_signal_bkg_1 = (N_signal + N_background_2)/N_background_1
     # ratio of bkg_2 to signal
     ratio_signal_bkg_2 = N_signal/N_background_2
     
     N_total_prime = N_total * 2
     N_signal_plus_bkg_2_prime, N_background_1_prime = decombine_result((N_total_prime, 0), ratio_signal_bkg_1)
     N_signal_prime, N_background_2_prime = decombine_result(N_signal_plus_bkg_2_prime, ratio_signal_bkg_2)
     
     self.assertEqual(N_signal_prime[0], N_signal * 2)
     self.assertEqual(N_background_1_prime[0], N_background_1 * 2)
     self.assertEqual(N_background_2_prime[0], N_background_2 * 2)
    def test_decombine_result_multiple_backgrounds(self):
        N_signal = 100
        N_background_1 = 20
        N_background_2 = 40
        N_total = N_signal + N_background_1 + N_background_2
        # ratio of bkg_1 to other samples
        ratio_signal_bkg_1 = (N_signal + N_background_2) / N_background_1
        # ratio of bkg_2 to signal
        ratio_signal_bkg_2 = N_signal / N_background_2

        N_total_prime = N_total * 2
        N_signal_plus_bkg_2_prime, N_background_1_prime = decombine_result(
            (N_total_prime, 0), ratio_signal_bkg_1)
        N_signal_prime, N_background_2_prime = decombine_result(
            N_signal_plus_bkg_2_prime, ratio_signal_bkg_2)

        self.assertEqual(N_signal_prime[0], N_signal * 2)
        self.assertEqual(N_background_1_prime[0], N_background_1 * 2)
        self.assertEqual(N_background_2_prime[0], N_background_2 * 2)
Ejemplo n.º 3
0
 def test_decombine_result_background_free(self):
     N_signal = 100
     N_background = 0
     N_total = N_signal
     ratio_signal_bkg = 0
     
     N_total_prime = N_total * 2
     N_signal_prime, N_background_prime = decombine_result((N_total_prime, 0), ratio_signal_bkg)
     
     self.assertEqual(N_signal_prime[0], N_signal * 2)
     self.assertEqual(N_background_prime[0], N_background * 2)
    def test_decombine_result_background_free(self):
        N_signal = 100
        N_background = 0
        N_total = N_signal
        ratio_signal_bkg = 0

        N_total_prime = N_total * 2
        N_signal_prime, N_background_prime = decombine_result(
            (N_total_prime, 0), ratio_signal_bkg)

        self.assertEqual(N_signal_prime[0], N_signal * 2)
        self.assertEqual(N_background_prime[0], N_background * 2)
def get_fitted_normalisation_from_ROOT( channel, input_files, variable, met_type, b_tag_bin, scale_factors = None ):
    '''
    Retrieves the number of ttbar events from fits to one or more distribution
    (fit_variables) for each bin in the variable.
    ''' 
    global use_fitter, measurement_config, verbose, fit_variables, options
    # results and initial values are the same across different fit variables
    # templates are not
    results = {}
    initial_values = {}
    templates = {fit_variable: {} for fit_variable in fit_variables}

    for variable_bin in variable_bins_ROOT[variable]:
        fitter = None
        fit_data_collection = FitDataCollection()
        
        for fit_variable in fit_variables:
            
            histograms = get_histograms( channel,
                                        input_files,
                                        variable = variable,
                                        met_type = met_type,
                                        variable_bin = variable_bin,
                                        b_tag_bin = b_tag_bin,
                                        rebin = measurement_config.rebin[fit_variable],
                                        fit_variable = fit_variable,
                                        scale_factors = scale_factors,
                                        )
            # create data sets
            h_fit_variable_signal = None
            mc_histograms = None
            if options.make_combined_signal:
                if measurement_config.include_higgs:
                    h_fit_variable_signal = histograms['TTJet'] + histograms['SingleTop'] + histograms['Higgs']
                else:
                    h_fit_variable_signal = histograms['TTJet'] + histograms['SingleTop']
                mc_histograms = {
                                'signal' : h_fit_variable_signal,
                                'V+Jets': histograms['V+Jets'],
                                'QCD': histograms['QCD'],
                            }
            else:
                mc_histograms = {
                                'TTJet': histograms['TTJet'],
                                'SingleTop': histograms['SingleTop'],
                                'V+Jets': histograms['V+Jets'],
                                'QCD': histograms['QCD'],
                            }
            h_data = histograms['data']
            if options.closure_test:
                ct_type = options.closure_test_type
                ct_norm = closure_tests[ct_type]
                h_data = histograms['TTJet'] * ct_norm['TTJet'] + histograms['SingleTop'] * ct_norm['SingleTop'] + histograms['V+Jets'] * ct_norm['V+Jets'] + histograms['QCD'] * ct_norm['QCD'] 
            fit_data = FitData( h_data,
                            mc_histograms,
                            fit_boundaries = measurement_config.fit_boundaries[fit_variable] )
            fit_data_collection.add( fit_data, name = fit_variable )
        if options.enable_constraints:
            fit_data_collection.set_normalisation_constraints( {'QCD': 2.0, 'V+Jets': 0.5} )

        if use_fitter == 'RooFit':
            fitter = RooFitFit( fit_data_collection )
        elif use_fitter == 'Minuit':
            fitter = Minuit( fit_data_collection, verbose = verbose )
        else:  # not recognised
            sys.stderr.write( 'Do not recognise fitter "%s". Using default (Minuit).\n' % fitter )
            fitter = Minuit ( fit_data_collection )

        if verbose:
            print "FITTING: " + channel + '_' + variable + '_' + variable_bin + '_' + met_type + '_' + b_tag_bin

        fitter.fit()
        fit_results = fitter.readResults()
        
        normalisation = fit_data_collection.mc_normalisation( fit_variables[0] )
        normalisation_errors = fit_data_collection.mc_normalisation_errors( fit_variables[0] )
        if options.make_combined_signal:
            N_ttbar_before_fit = histograms['TTJet'].Integral()
            N_SingleTop_before_fit = histograms['SingleTop'].Integral()
            N_ttbar_error_before_fit = sum(histograms['TTJet'].yerravg())
            N_SingleTop_error_before_fit = sum(histograms['SingleTop'].yerravg())
            N_Higgs_before_fit = 0
            N_Higgs_error_before_fit = 0
            if measurement_config.include_higgs:
                N_Higgs_before_fit = histograms['Higgs'].Integral()
                N_Higgs_error_before_fit = sum(histograms['Higgs'].yerravg())
     
            if (N_SingleTop_before_fit != 0):
                TTJet_SingleTop_ratio = (N_ttbar_before_fit + N_Higgs_before_fit) / N_SingleTop_before_fit
            else:
                print 'Bin ', variable_bin, ': ttbar/singleTop ratio undefined for %s channel! Setting to 0.' % channel
                TTJet_SingleTop_ratio = 0
     
            N_ttbar_all, N_SingleTop = decombine_result(fit_results['signal'], TTJet_SingleTop_ratio)
            if (N_Higgs_before_fit != 0):
                TTJet_Higgs_ratio = N_ttbar_before_fit/ N_Higgs_before_fit
            else:
                TTJet_Higgs_ratio = 0
     
            
            N_ttbar, N_Higgs = decombine_result(N_ttbar_all, TTJet_Higgs_ratio)
    
            fit_results['TTJet'] = N_ttbar
            fit_results['SingleTop'] = N_SingleTop
            fit_results['Higgs'] = N_Higgs
    
            normalisation['TTJet'] = N_ttbar_before_fit
            normalisation['SingleTop'] = N_SingleTop_before_fit
            normalisation['Higgs'] = N_Higgs_before_fit
            normalisation_errors['TTJet'] = N_ttbar_error_before_fit
            normalisation_errors['SingleTop'] = N_SingleTop_error_before_fit
            normalisation_errors['Higgs'] = N_Higgs_error_before_fit

        if results == {}:  # empty
            initial_values['data'] = [( normalisation['data'], normalisation_errors['data'] )]
            for fit_variable in fit_variables:
                templates[fit_variable]['data'] = [fit_data_collection.vectors( fit_variable )['data']]
            for sample in fit_results.keys():
                results[sample] = [fit_results[sample]]
                initial_values[sample] = [( normalisation[sample], normalisation_errors[sample] )]
                if sample in ['TTJet', 'SingleTop', 'Higgs'] and options.make_combined_signal:
                    continue
                for fit_variable in fit_variables:
                    templates[fit_variable][sample] = [fit_data_collection.vectors( fit_variable )[sample]]
        else:
            initial_values['data'].append( [normalisation['data'], normalisation_errors['data']] )
            for fit_variable in fit_variables:
                templates[fit_variable]['data'].append( fit_data_collection.vectors( fit_variable )['data'] )
            for sample in fit_results.keys():
                results[sample].append( fit_results[sample] )
                initial_values[sample].append( [normalisation[sample], normalisation_errors[sample]] )
                if sample in ['TTJet', 'SingleTop', 'Higgs'] and options.make_combined_signal:
                    continue
                for fit_variable in fit_variables:
                    templates[fit_variable][sample].append( fit_data_collection.vectors( fit_variable )[sample] )

#     print "results = ", results
    return results, initial_values, templates
def get_fitted_normalisation_from_ROOT(channel, input_files, variable, met_type, b_tag_bin):
    results = {}
    initial_values = {}
    templates = {}

    for variable_bin in variable_bins_ROOT[variable]:
        histograms = get_histograms(channel,
                                    input_files,
                                    variable=variable,
                                    met_type=met_type,
                                    variable_bin=variable_bin,
                                    b_tag_bin=b_tag_bin,
                                    rebin=measurement_config.rebin
                                    )
        # prepare histograms
        # normalise histograms
        
        # create signal histograms
        h_eta_signal = histograms['TTJet'] + histograms['SingleTop']
        fitter = TMinuitFit(histograms={
                                      'data':histograms['data'],
                                      'signal':h_eta_signal,
#                                      'background':histograms['V+Jets']+histograms['QCD']
                                      'V+Jets':histograms['V+Jets'],
                                      'QCD':histograms['QCD']
                                      })
        
        fitter.set_fit_constraints({'QCD': 2.0, 'V+Jets': 0.5})
        fitter.fit()
        fit_results = fitter.readResults()
        normalisation = fitter.normalisation
        normalisation_errors = fitter.normalisation_errors
        
        N_ttbar_before_fit = histograms['TTJet'].Integral()
        N_SingleTop_before_fit = histograms['SingleTop'].Integral()
        N_ttbar_error_before_fit = sum(histograms['TTJet'].errors())
        N_SingleTop_error_before_fit = sum(histograms['SingleTop'].errors())

        if (N_SingleTop_before_fit != 0):
            TTJet_SingleTop_ratio = N_ttbar_before_fit / N_SingleTop_before_fit
        else:
            print 'Bin ', variable_bin, ': ttbar/singleTop ratio undefined for %s channel! Setting to 0.' % channel
            TTJet_SingleTop_ratio = 0

        N_ttbar, N_SingleTop = decombine_result(fit_results['signal'], TTJet_SingleTop_ratio)
        
        
        fit_results['TTJet'] = N_ttbar
        fit_results['SingleTop'] = N_SingleTop
        normalisation['TTJet'] = N_ttbar_before_fit
        normalisation['SingleTop'] = N_SingleTop_before_fit
        normalisation_errors['TTJet'] = N_ttbar_error_before_fit
        normalisation_errors['SingleTop'] = N_SingleTop_error_before_fit
        
        if results == {}:  # empty
            initial_values['data'] = [(normalisation['data'], normalisation_errors['data'])]
            templates['data'] = [fitter.vectors['data']]
            for sample in fit_results.keys():
                results[sample] = [fit_results[sample]]
                initial_values[sample] = [(normalisation[sample], normalisation_errors[sample])]
                if not sample == 'TTJet' and not sample == 'SingleTop':
                    templates[sample] = [fitter.vectors[sample]]
        else:
            initial_values['data'].append([normalisation['data'], normalisation_errors['data']])
            templates['data'].append(fitter.vectors['data'])
            for sample in fit_results.keys():
                results[sample].append(fit_results[sample])
                initial_values[sample].append([normalisation[sample], normalisation_errors[sample]])
                if not sample == 'TTJet' and not sample == 'SingleTop':
                    templates[sample].append(fitter.vectors[sample])
        
    return results, initial_values, templates
def get_fitted_normalisation_from_ROOT(input_files, met_type, b_tag_bin):
    global met_bins_ROOT
    electron_results = {}
    electron_initial_values = {}
    muon_results = {}
    muon_initial_values = {}
    for met_bin in met_bins_ROOT:
        electron_histograms, muon_histograms = get_histograms(input_files={
                                  'TTJet': TTJet_file,
                                  'SingleTop': SingleTop_file,
                                  'V+Jets':VJets_file,
                                  'data_electron': data_file_electron,
                                  'data_muon': data_file_muon
                                  },
                   met_type=met_type,
                   met_bin=met_bin,
                   b_tag_bin=b_tag_bin,
                   rebin=20
                   )
        # prepare histograms
        # normalise histograms
        # TODO
        
        # store pre-fit information
        
        # create signal histograms
        h_electron_eta_signal = electron_histograms['TTJet'] + electron_histograms['SingleTop']
        h_muon_eta_signal = muon_histograms['TTJet'] + muon_histograms['SingleTop']
        fitter_electron = TMinuitFit(histograms={
                                      'data':electron_histograms['data_electron'],
                                      'signal':h_electron_eta_signal,
                                      'V+Jets':electron_histograms['V+Jets'],
                                      'QCD':electron_histograms['QCD']
                                      })
        fitter_muon = TMinuitFit(histograms={
                                      'data':muon_histograms['data_muon'],
                                      'signal':h_muon_eta_signal,
                                      'V+Jets':muon_histograms['V+Jets'],
                                      'QCD':muon_histograms['QCD']
                                      })
        
        fitter_electron.fit()
        fit_results_electron = fitter_electron.readResults()
        normalisation_electron = fitter_electron.normalisation
        
        N_ttbar_before_fit_electron = electron_histograms['TTJet'].Integral()
        N_SingleTop_before_fit_electron = electron_histograms['SingleTop'].Integral()
        
        TTJet_SingleTop_ratio_electron = N_ttbar_before_fit_electron / N_SingleTop_before_fit_electron
        N_ttbar_electron, N_SingleTop_electron = decombine_result(fit_results_electron['signal'], TTJet_SingleTop_ratio_electron)
        
        
        fit_results_electron['TTJet'] = N_ttbar_electron
        fit_results_electron['SingleTop'] = N_SingleTop_electron
        normalisation_electron['TTJet'] = N_ttbar_before_fit_electron
        normalisation_electron['SingleTop'] = N_SingleTop_before_fit_electron
        # this needs to
        if electron_results == {}:  # empty
            for sample in fit_results_electron.keys():
                electron_results[sample] = [fit_results_electron[sample]]
                electron_initial_values[sample] = [normalisation_electron[sample]]
        else:
            for sample in fit_results_electron.keys():
                electron_results[sample].append(fit_results_electron[sample])
                electron_initial_values[sample].append(normalisation_electron[sample])
        
        fitter_muon.fit()
        fit_results_muon = fitter_muon.readResults()
        normalisation_muon = fitter_muon.normalisation
        
        N_ttbar_before_fit_muon = muon_histograms['TTJet'].Integral()
        N_SingleTop_before_fit_muon = muon_histograms['SingleTop'].Integral()
        TTJet_SingleTop_ratio_muon = N_ttbar_before_fit_muon / N_SingleTop_before_fit_muon
        N_ttbar_muon, N_SingleTop_muon = decombine_result(fit_results_muon['signal'], TTJet_SingleTop_ratio_muon)
        
        fit_results_muon['TTJet'] = N_ttbar_muon
        fit_results_muon['SingleTop'] = N_SingleTop_muon
        normalisation_muon['TTJet'] = N_ttbar_before_fit_muon
        normalisation_muon['SingleTop'] = N_SingleTop_before_fit_muon
        
        if muon_results == {}:  # empty
            for sample in fit_results_muon.keys():
                muon_results[sample] = [fit_results_muon[sample]]
                muon_initial_values[sample] = [normalisation_muon[sample]]
        else:
            for sample in fit_results_muon.keys():
                muon_results[sample].append(fit_results_muon[sample])
                muon_initial_values[sample].append(normalisation_muon[sample])
        
    return electron_results, muon_results, electron_initial_values, muon_initial_values
Ejemplo n.º 8
0
def get_fitted_normalisation_from_ROOT(channel, input_files, variable, met_type, b_tag_bin):
    results = {}
    initial_values = {}
    templates = {}

    for variable_bin in variable_bins_ROOT[variable]:
        histograms = get_histograms(channel,
                                    input_files,
                                    variable=variable,
                                    met_type=met_type,
                                    variable_bin=variable_bin,
                                    b_tag_bin=b_tag_bin,
                                    rebin=measurement_config.rebin
                                    )
        # create signal histograms
        h_eta_signal = histograms['TTJet'] + histograms['SingleTop']
        
        N_ttbar_before_fit = histograms['TTJet'].Integral()
        N_SingleTop_before_fit = histograms['SingleTop'].Integral()
        N_vjets_before_fit = histograms['V+Jets'].Integral()
        N_qcd_before_fit = histograms['QCD'].Integral()
        N_signal_before_fit = N_ttbar_before_fit + N_SingleTop_before_fit
        
        N_ttbar_error_before_fit = sum(histograms['TTJet'].errors())
        N_SingleTop_error_before_fit = sum(histograms['SingleTop'].errors())
        N_vjets_error_before_fit = sum(histograms['V+Jets'].errors())
        N_QCD_error_before_fit = sum(histograms['QCD'].errors())
        
        if (N_SingleTop_before_fit != 0):
            TTJet_SingleTop_ratio = N_ttbar_before_fit / N_SingleTop_before_fit
        else:
            print 'Bin ', variable_bin, ': ttbar/singleTop ratio undefined for %s channel! Setting to 0.' % channel
            TTJet_SingleTop_ratio = 0
            
        
        leptonAbsEta = RooRealVar("leptonAbsEta", "leptonAbsEta", 0., 2.4)
        # this has to move to tools/Fitting.py
        vars = RooArgList()
        vars.add(leptonAbsEta)
        vars_set = RooArgSet()
        vars_set.add(leptonAbsEta)
        n_event_obs = histograms['data'].Integral()
        
        lowerBound = 0. 
        upperBound = n_event_obs + 10 * sqrt(n_event_obs)
        n_init = n_event_obs / 2.
        
        data = RooDataHist("data", "dataset with leptonAbsEta", vars, histograms['data'])
        rh_vj = RooDataHist("rh_vj", "vj", vars, histograms['V+Jets'])
        rh_qcd = RooDataHist("rh_qcd", "qcd", vars, histograms['QCD'])
        rh_signal = RooDataHist("rh_signal", "signal", vars, h_eta_signal)
        
        pdf_vj = RooHistPdf ("pdf_vj", "V+Jets pdf", vars_set, rh_vj, 0)
        pdf_qcd = RooHistPdf("pdf_qcd", "QCD pdf ", vars_set, rh_qcd, 0)
        pdf_signal = RooHistPdf("pdf_signal", "single top pdf", vars_set, rh_signal, 0)
        
        # RooRealVar(const char *name, const char *title, Double_t value, Double_t minValue, Double_t maxValue, const char *unit) :
        nSignal = RooRealVar("nSignal", "number of single top + ttbar events", N_signal_before_fit, lowerBound, upperBound, "event")
        nvj = RooRealVar  ("nvj", "number of V+Jets bgnd events", N_vjets_before_fit, lowerBound, upperBound, "event")
        nqcd = RooRealVar("nqcd", "number of QCD bgnd events", N_QCD_error_before_fit, lowerBound, upperBound, "event")
        
        model = RooAddPdf("model", "sig+vj+qcd",
                          RooArgList(pdf_signal, pdf_vj, pdf_qcd),
                          RooArgList(nSignal, nvj, nqcd)
                          )
        vj_constraint = RooGaussian("nvj_constraint", "nvj_constraint", nvj, RooFit.RooConst(N_vjets_before_fit), RooFit.RooConst(0.5 * N_vjets_before_fit))
        qcd_constraint = RooGaussian("nqcd_constraint", "nqcd_constraint", nqcd, RooFit.RooConst(N_qcd_before_fit), RooFit.RooConst(2 * N_qcd_before_fit))  
        model_with_constraints = RooProdPdf("model_with_constraints", "model with gaussian constraints",
                                            RooArgSet(model, vj_constraint, qcd_constraint), RooLinkedList())
        model_with_constraints.fitTo(data, RooFit.Minimizer("Minuit2", "Migrad"))  #WARNING: number of cores changes the results!!!
#         nll = model.createNLL(data, RooFit.NumCPU(2))
#         RooMinuit(nll).migrad()
#         frame1 = nSignal.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nSignal")) 
#         nll.plotOn(frame1, RooFit.ShiftToZero())
#         frame2 = nvj.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nvj"))
#         nll.plotOn(frame2, RooFit.ShiftToZero())
#         frame3 = nqcd.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nqcd"))
#         nll.plotOn(frame3, RooFit.ShiftToZero())  
#         
#         pll_nSignal = nll.createProfile(nSignal)
#         pll_nSignal.plotOn(frame1, RooFit.LineColor(2)) 
#         frame1.SetMinimum(0)
#         frame1.SetMaximum(3)
#         
#         pll_nvj = nll.createProfile(nvj)
#         pll_nvj.plotOn(frame2, RooFit.LineColor(2)) 
#         frame2.SetMinimum(0)
#         frame2.SetMaximum(3)
#         
#         pll_nqcd = nll.createProfile(nqcd)
#         pll_nqcd.plotOn(frame3, RooFit.LineColor(2)) 
#         frame3.SetMinimum(0)
#         frame3.SetMaximum(3)
#         c = TCanvas("profilell","profilell",1200, 400)
#         c.Divide(3)
#         c.cd(1)
#         frame1.Draw()
#         c.cd(2)
#         frame2.Draw()
#         c.cd(3)
#         frame3.Draw()
#         c.SaveAs('profileLL.png')
#         model.fitTo(data, RooFit.Minimizer("Minuit2", "Migrad"), RooFit.NumCPU(1))#WARNING: number of cores changes the results!!!
        fit_results = {}
        fit_results['signal'] = (nSignal.getVal(), nSignal.getError())
        fit_results['QCD'] = ufloat(nqcd.getVal(), nqcd.getError())
        fit_results['V+Jets'] = ufloat(nvj.getVal(), nvj.getError())
        
        N_ttbar, N_SingleTop = decombine_result(fit_results['signal'], TTJet_SingleTop_ratio)
        fit_results['signal'] = ufloat(nSignal.getVal(), nSignal.getError())
        fit_results['TTJet'] = ufloat(N_ttbar)
        fit_results['SingleTop'] = ufloat(N_SingleTop)
        
        if results == {}:  # empty
            for sample in fit_results.keys():
                results[sample] = [fit_results[sample]]
        else:
            for sample in fit_results.keys():
                results[sample].append(fit_results[sample])
        
    return results, None, None