def run_test ( test_data ):
    ''' Used the test_data to fit the number of events for each process
    '''
    global config
    data_scale = 1.2
    fit_data_collection = FitDataCollection()
    for fit_variable, fit_input in test_data.iteritems():
        # create the histograms
        mc_histograms = {}
        for sample, h_input in fit_input.iteritems():
            mc_histograms[sample] = value_tuplelist_to_hist( h_input['distribution'],
                                                             fit_variable_bin_edges[fit_variable] )
        real_data = sum( mc_histograms[sample] for sample in mc_histograms.keys() )
        # scale data so that the fit does not start in the minimum
        real_data.Scale( data_scale )
        fit_data = FitData( real_data, mc_histograms, fit_boundaries = config.fit_boundaries[fit_variable] )
        fit_data_collection.add( fit_data, fit_variable )
    # do fit
    fitter = Minuit( fit_data_collection )
    fitter.fit()
    fit_results = fitter.results
    # calculate chi2 for each sample
    chi2_results = {}
    for sample in fit_results.keys():
        true_normalisation = fit_input[sample]['normalisation'] * data_scale
#         fit_result, fit_error = fit_results[sample]
#         chi2 = pow( true_normalisation - fit_result, 2 ) / pow( fit_error, 2 )
        fit_result, _ = fit_results[sample]
        chi2 = pow( true_normalisation - fit_result, 2 )
        chi2_results[sample] = chi2
    
    return chi2_results
    def simultaneous_fit(self, histograms):
        from tools.Fitting import FitData, FitDataCollection, Minuit
        print('not in production yet')
        fitter = None
        fit_data_collection = FitDataCollection()
        for fit_variable in self.fit_variables:
            mc_histograms = {
                'TTJet': histograms['TTJet'],
                'SingleTop': histograms['SingleTop'],
                'V+Jets': histograms['V+Jets'],
                'QCD': histograms['QCD'],
            }
            h_data = histograms['data']
            fit_data = FitData(h_data, mc_histograms,
                               fit_boundaries=self.config.fit_boundaries[fit_variable])
            fit_data_collection.add(fit_data, name=fit_variable)
        fitter = Minuit(fit_data_collection)
        fitter.fit()
        fit_results = fitter.readResults()

        normalisation = fit_data_collection.mc_normalisation(
            self.fit_variables[0])
        normalisation_errors = fit_data_collection.mc_normalisation_errors(
            self.fit_variables[0])
        print normalisation, normalisation_errors
    def simultaneous_fit(self, histograms):
        from tools.Fitting import FitData, FitDataCollection, Minuit
        print('not in production yet')
        fitter = None
        fit_data_collection = FitDataCollection()
        for fit_variable in self.fit_variables:
            mc_histograms = {
                'TTJet': histograms['TTJet'],
                'SingleTop': histograms['SingleTop'],
                'V+Jets': histograms['V+Jets'],
                'QCD': histograms['QCD'],
            }
            h_data = histograms['data']
            fit_data = FitData(
                h_data,
                mc_histograms,
                fit_boundaries=self.config.fit_boundaries[fit_variable])
            fit_data_collection.add(fit_data, name=fit_variable)
        fitter = Minuit(fit_data_collection)
        fitter.fit()
        fit_results = fitter.readResults()

        normalisation = fit_data_collection.mc_normalisation(
            self.fit_variables[0])
        normalisation_errors = fit_data_collection.mc_normalisation_errors(
            self.fit_variables[0])
        print normalisation, normalisation_errors
def run_test(test_data):
    ''' Used the test_data to fit the number of events for each process
    '''
    global config
    data_scale = 1.2
    fit_data_collection = FitDataCollection()
    for fit_variable, fit_input in test_data.iteritems():
        # create the histograms
        mc_histograms = {}
        for sample, h_input in fit_input.iteritems():
            mc_histograms[sample] = value_tuplelist_to_hist(
                h_input['distribution'], fit_variable_bin_edges[fit_variable])
        real_data = sum(mc_histograms[sample]
                        for sample in mc_histograms.keys())
        # scale data so that the fit does not start in the minimum
        real_data.Scale(data_scale)
        fit_data = FitData(real_data,
                           mc_histograms,
                           fit_boundaries=config.fit_boundaries[fit_variable])
        fit_data_collection.add(fit_data, fit_variable)
    # do fit
    fitter = Minuit(fit_data_collection)
    fitter.fit()
    fit_results = fitter.results
    # calculate chi2 for each sample
    chi2_results = {}
    for sample in fit_results.keys():
        true_normalisation = fit_input[sample]['normalisation'] * data_scale
        #         fit_result, fit_error = fit_results[sample]
        #         chi2 = pow( true_normalisation - fit_result, 2 ) / pow( fit_error, 2 )
        fit_result, _ = fit_results[sample]
        chi2 = pow(true_normalisation - fit_result, 2)
        chi2_results[sample] = chi2

    return chi2_results
h_t4.Draw('SAME HIST')

templates = {
             }
if useT1: templates['t1'] = h_t1
if useT2: templates['t2'] = h_t2
if useT3: templates['t3'] = h_t3
if useT4: templates['t4'] = h_t4

fit_data = FitData( h_data, templates, fit_boundaries = ( 0, h_data.nbins() ) )

fit_collection = FitDataCollection()
fit_collection.add( fit_data )

minuit_fitter = Minuit( fit_collection, method = 'logLikelihood', verbose = True )
minuit_fitter.fit()

results = minuit_fitter.readResults()

c.cd(2)
ymax = h_data.GetBinContent( h_data.GetMaximumBin() ) * 1.1
h_data.GetYaxis().SetRangeUser(0,ymax)
h_data.Draw('PE')
leg = Legend(nTemplates+2)
leg.AddEntry( h_data, style='LEP')
h_tSumAfter=0

print '----> Target \t Fit Result'
if useT1:
    h_t1After = h_t1.Clone()
    h_t1After.Scale( results['t1'][0] / h_t1.Integral() )
Example #6
0
h_t3.Draw('SAME HIST')
h_t4.Draw('SAME HIST')

templates = {}
if useT1: templates['t1'] = h_t1
if useT2: templates['t2'] = h_t2
if useT3: templates['t3'] = h_t3
if useT4: templates['t4'] = h_t4

fit_data = FitData(h_data, templates, fit_boundaries=(0, h_data.nbins()))

fit_collection = FitDataCollection()
fit_collection.add(fit_data)

minuit_fitter = Minuit(fit_collection, method='logLikelihood', verbose=True)
minuit_fitter.fit()

results = minuit_fitter.readResults()

c.cd(2)
ymax = h_data.GetBinContent(h_data.GetMaximumBin()) * 1.1
h_data.GetYaxis().SetRangeUser(0, ymax)
h_data.Draw('PE')
leg = Legend(nTemplates + 2)
leg.AddEntry(h_data, style='LEP')
h_tSumAfter = 0

print '----> Target \t Fit Result'
if useT1:
    h_t1After = h_t1.Clone()
    h_t1After.Scale(results['t1'][0] / h_t1.Integral())
Example #7
0
class Test(unittest.TestCase):
    def setUp(self):

        # create histograms
        h_bkg1_1 = Hist(100, 40, 200, title='Background')
        h_signal_1 = h_bkg1_1.Clone(title='Signal')
        h_data_1 = h_bkg1_1.Clone(title='Data')
        h_bkg1_2 = h_bkg1_1.Clone(title='Background')
        h_signal_2 = h_bkg1_1.Clone(title='Signal')
        h_data_2 = h_bkg1_1.Clone(title='Data')

        # fill the histograms with our distributions
        map(h_bkg1_1.Fill, x1)
        map(h_signal_1.Fill, x2)
        map(h_data_1.Fill, x1_obs)
        map(h_data_1.Fill, x2_obs)

        map(h_bkg1_2.Fill, x3)
        map(h_signal_2.Fill, x4)
        map(h_data_2.Fill, x3_obs)
        map(h_data_2.Fill, x4_obs)

        h_data_1.Scale(data_scale)
        h_data_2.Scale(data_scale)

        histograms_1 = {'signal': h_signal_1, 'bkg1': h_bkg1_1}

        histograms_2 = {'signal': h_signal_2, 'bkg1': h_bkg1_2}

        fit_data_1 = FitData(h_data_1, histograms_1, fit_boundaries=(40, 200))
        fit_data_2 = FitData(h_data_2, histograms_2, fit_boundaries=(40, 200))

        single_fit_collection = FitDataCollection()
        single_fit_collection.add(fit_data_1)

        collection_1 = FitDataCollection()
        collection_1.add(fit_data_1, 'var1')
        collection_1.add(fit_data_2, 'var2')

        collection_2 = FitDataCollection()
        collection_2.add(fit_data_1, 'var1')
        collection_2.add(fit_data_2, 'var2')
        collection_2.set_normalisation_constraints({'bkg1': 0.5})

        collection_3 = FitDataCollection()
        collection_3.add(fit_data_1, 'var1')
        collection_3.add(fit_data_2, 'var2')
        collection_3.set_normalisation_constraints({'bkg1': 0.001})

        self.minuit_fitter = Minuit(single_fit_collection)
        self.minuit_fitter.fit()

        self.simultaneous_fit = Minuit(collection_1)
        self.simultaneous_fit.fit()

        self.simultaneous_fit_with_constraints = Minuit(collection_2)
        self.simultaneous_fit_with_constraints.fit()

        self.simultaneous_fit_with_bad_constraints = Minuit(collection_3)
        self.simultaneous_fit_with_bad_constraints.fit()

    def tearDown(self):
        pass

    def test_normalisation(self):
        normalisation = self.minuit_fitter.normalisation
        self.assertAlmostEqual(normalisation["data"],
                               N_data,
                               delta=sqrt(N_data))
        self.assertAlmostEqual(normalisation["bkg1"],
                               N_bkg1,
                               delta=sqrt(N_bkg1))
        self.assertAlmostEqual(normalisation["signal"],
                               N_signal,
                               delta=sqrt(N_signal))

    def test_result(self):
        results = self.minuit_fitter.readResults()
        self.assertAlmostEqual(N_signal_obs * data_scale,
                               results['signal'][0],
                               delta=2 * results['signal'][1])
        self.assertAlmostEqual(N_bkg1_obs * data_scale,
                               results['bkg1'][0],
                               delta=2 * results['bkg1'][1])

    def test_result_simultaneous(self):
        results = self.simultaneous_fit.readResults()
        self.assertAlmostEqual(N_signal_obs * data_scale,
                               results['signal'][0],
                               delta=2 * results['signal'][1])
        self.assertAlmostEqual(N_bkg1_obs * data_scale,
                               results['bkg1'][0],
                               delta=2 * results['bkg1'][1])

    def test_result_simultaneous_with_constraints(self):
        results = self.simultaneous_fit_with_constraints.readResults()
        self.assertAlmostEqual(N_signal_obs * data_scale,
                               results['signal'][0],
                               delta=2 * results['signal'][1])
        self.assertAlmostEqual(N_bkg1_obs * data_scale,
                               results['bkg1'][0],
                               delta=2 * results['bkg1'][1])

    def test_result_simultaneous_with_bad_constraints(self):
        results = self.simultaneous_fit_with_bad_constraints.readResults()
        self.assertNotAlmostEqual(N_signal_obs * data_scale,
                                  results['signal'][0],
                                  delta=results['signal'][1])
        self.assertNotAlmostEqual(N_bkg1_obs * data_scale,
                                  results['bkg1'][0],
                                  delta=results['bkg1'][1])

    def test_relative_error(self):
        results = self.minuit_fitter.readResults()
        self.assertLess(results['signal'][1] / results['signal'][0], 0.1)
        self.assertLess(results['bkg1'][1] / results['bkg1'][0], 0.1)
class Test( unittest.TestCase ):


    def setUp( self ):
        

        # create histograms
        h_bkg1_1 = Hist( 100, 40, 200, title = 'Background' )
        h_signal_1 = h_bkg1_1.Clone( title = 'Signal' )
        h_data_1 = h_bkg1_1.Clone( title = 'Data' )
        h_bkg1_2 = h_bkg1_1.Clone( title = 'Background' )
        h_signal_2 = h_bkg1_1.Clone( title = 'Signal' )
        h_data_2 = h_bkg1_1.Clone( title = 'Data' )
    
        # fill the histograms with our distributions
        map( h_bkg1_1.Fill, x1 )
        map( h_signal_1.Fill, x2 )
        map( h_data_1.Fill, x1_obs )
        map( h_data_1.Fill, x2_obs )
        
        map( h_bkg1_2.Fill, x3 )
        map( h_signal_2.Fill, x4 )
        map( h_data_2.Fill, x3_obs )
        map( h_data_2.Fill, x4_obs )
        
        h_data_1.Scale( data_scale )
        h_data_2.Scale( data_scale )
        
        histograms_1 = {'signal': h_signal_1,
                        'bkg1': h_bkg1_1}
        
        histograms_2 = {'signal': h_signal_2,
                        'bkg1': h_bkg1_2}
        
        fit_data_1 = FitData( h_data_1, histograms_1, fit_boundaries = ( 40, 200 ) )
        fit_data_2 = FitData( h_data_2, histograms_2, fit_boundaries = ( 40, 200 ) )
        
        single_fit_collection = FitDataCollection()
        single_fit_collection.add( fit_data_1 )
        
        collection_1 = FitDataCollection()
        collection_1.add( fit_data_1, 'var1' )
        collection_1.add( fit_data_2, 'var2' )
        
        collection_2 = FitDataCollection()
        collection_2.add( fit_data_1, 'var1' )
        collection_2.add( fit_data_2, 'var2' )
        collection_2.set_normalisation_constraints( {'bkg1':0.5} )
        
        collection_3 = FitDataCollection()
        collection_3.add( fit_data_1, 'var1' )
        collection_3.add( fit_data_2, 'var2' )
        collection_3.set_normalisation_constraints( {'bkg1':0.001} )
        
        self.minuit_fitter = Minuit( single_fit_collection )
        self.minuit_fitter.fit()
        
        self.simultaneous_fit = Minuit( collection_1 )
        self.simultaneous_fit.fit()
        
        
        self.simultaneous_fit_with_constraints = Minuit( collection_2 )
        self.simultaneous_fit_with_constraints.fit()
        
        self.simultaneous_fit_with_bad_constraints = Minuit( collection_3 )
        self.simultaneous_fit_with_bad_constraints.fit()


    def tearDown( self ):
        pass

    def test_normalisation( self ):
        normalisation = self.minuit_fitter.normalisation
        self.assertAlmostEqual( normalisation["data"], N_data, delta = sqrt( N_data ) )
        self.assertAlmostEqual( normalisation["bkg1"], N_bkg1, delta = sqrt( N_bkg1 ) )
        self.assertAlmostEqual( normalisation["signal"], N_signal, delta = sqrt( N_signal ) )
        
    def test_result( self ):
        results = self.minuit_fitter.readResults()
        self.assertAlmostEqual( N_signal_obs * data_scale, results['signal'][0], delta = 2 * results['signal'][1] )
        self.assertAlmostEqual( N_bkg1_obs * data_scale, results['bkg1'][0], delta = 2 * results['bkg1'][1] )
        
    def test_result_simultaneous( self ):
        results = self.simultaneous_fit.readResults()
        self.assertAlmostEqual( N_signal_obs * data_scale, results['signal'][0], delta = 2 * results['signal'][1] )
        self.assertAlmostEqual( N_bkg1_obs * data_scale, results['bkg1'][0], delta = 2 * results['bkg1'][1] )
        
    def test_result_simultaneous_with_constraints( self ):
        results = self.simultaneous_fit_with_constraints.readResults()
        self.assertAlmostEqual( N_signal_obs * data_scale, results['signal'][0], delta = 2 * results['signal'][1] )
        self.assertAlmostEqual( N_bkg1_obs * data_scale, results['bkg1'][0], delta = 2 * results['bkg1'][1] )
        
    def test_result_simultaneous_with_bad_constraints( self ):
        results = self.simultaneous_fit_with_bad_constraints.readResults()
        self.assertNotAlmostEqual( N_signal_obs * data_scale, results['signal'][0], delta = results['signal'][1] )
        self.assertNotAlmostEqual( N_bkg1_obs * data_scale, results['bkg1'][0], delta = results['bkg1'][1] )
        
    def test_relative_error( self ):
        results = self.minuit_fitter.readResults()
        self.assertLess( results['signal'][1] / results['signal'][0], 0.1 )
        self.assertLess( results['bkg1'][1] / results['bkg1'][0], 0.1 )