def main():
    '''This is the entry point to execution.'''
    print 'Welcome to parameterized_keys_pdf_fit_example!'
    global data, model, x, fitresult
    data = getdata()
    data.SetName('zeeDataYong')
    ## Reduce data for debugging
    data = data.reduce(roo.EventRange(0, 1000))
    x = data.get()[variable]
    initialize_fit_parameters(data, x)
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    model = ParameterizedKeysPdf('model',
                                 'model',
                                 x,
                                 mode,
                                 effsigma,
                                 data,
                                 rho=1,
                                 forcerange=True)

    fitresult = model.fitTo(data, roo.SumW2Error(True), roo.NumCPU(8),
                            roo.Strategy(2), roo.Save())
    set_default_integrator_precision(*old_precision)
    make_plot(x, data, model)
    print '\n==  Fitted parameters =='
    mode.Print()
    effsigma.Print()
    save_result(fitresult)
    print '\nExiting parameterized_keys_pdf_fit_example with success.'
示例#2
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to test_pkeys - test of the mode and effective sigma'
    print 'extraction for a generic distribution using ParametrizedKeysPdf.'
    
    w = ROOT.RooWorkspace('w', 'w')
    
    truepdf = w.factory('Gaussian::truepdf(x[-5,5],m[0],s[1])')
    data = truepdf.generate(ROOT.RooArgSet(w.var('x')), 10000)
    canvases.next('truepdf')
    plot = w.var('x').frame()
    data.plotOn(plot)
    truepdf.plotOn(plot)
    plot.Draw()
    
    toypdf = ParameterizedKeysPdf('toypdf', 'toypdf', w.var('x'), 
                                  w.factory('mtoy[0,-5,5]'), 
                                  w.factory('stoy[1,0.1,5]'), data, rho=3)
    toypdf.shape.plotOn(plot, roo.LineColor(ROOT.kRed))
    toypdf.fitTo(data)
    toypdf.plotOn(plot, roo.LineColor(ROOT.kGreen))
    plot.Draw()
        
    
    canvases.update()
    print 'Exiting test_pkeys with success!'
示例#3
0
    def __init__(self, data, rho=1.5, printlevel=-1, option='monolithic'):
        if data.get().getSize() != 1:
            raise RuntimeError, 'Data must contain just one variable!'
        
        self.w = ROOT.RooWorkspace('KeysResponseFitter',
                                   'KeysResponseFitter Workspace')
        self.w.Import(data)
        self.data = self.w.data(data.GetName())
        self.data.SetName('data')      

        self.x = self.w.var(data.get().first().GetName())
        self.x.SetName('x')
        
        self.rho = rho        
        self.printlevel = printlevel

        ## Define mode and effsigma.
        self.mode = self.w.factory('mode[0, -50, 50]')
        self.effsigma = self.w.factory('effsigma[1, 0.01, 50]')
        for x in 'mode effsigma'.split():
            getattr(self, x).setUnit(self.x.getUnit())

        self.option = option
        
        if option == 'monolithic':
            self.model = ParameterizedKeysPdf('model', 'model', self.x,
                                              self.mode,
                                              self.effsigma, self.data, rho=rho,
                                              forcerange=True)
        elif 'split' in option:
            self.initsplit()
        else:
            raise RuntimeError, 'Unknown option: %s' % option
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to r9_correction_fitter!'
    global data, model, x, fitresults
    data = getdata()
    ## Reduce data for debugging
    for source, dataset in data.items():
        # data[source] = dataset.reduce(roo.EventRange(0, 1000))
        pass
    x = data['data'].get()[variable.GetName()]
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, data['z'],
                                 rho=0.6, forcerange=True)
    #initialize_fit_parameters(data['data'], x)
    for var, varname in zip([mode, effsigma], ['mode', 'width']):
        var.setVal(getattr(model, 'shape' + varname + 'var').getVal())
    make_plot(x, data['z'], model.shape, 'MC_Shape')
    fitresults = {}
    for source in 'z data'.split():
        dataset = data[source]
        name = {'z': 'MC', 'data': 'Data'}[source]
        dataset.SetName(name)
        if source == 'data':
            # mode.setConstant(True)
            pass
        fitresult = model.fitTo(dataset, roo.SumW2Error(True),
                                roo.NumCPU(8), roo.Strategy(2), 
                                roo.Save(), roo.Range('fit'))
        fitresults[name] = fitresult
        make_plot(x, dataset, model, name)
        save_result(fitresult, name)

    print_report(fitresults)
    set_default_integrator_precision(*old_precision)
    print '\nExiting r9_correction_fitter with success.'
示例#5
0
 def initsplit(self):
     '''
     Splits the dataset in two and builds a model for each half.
     '''
     global arg
     self.resampler = Resampler(self.data)
     self.subdata = []
     self.submodel = []
     if not self.option.replace('split', ''):
         self.nsplit = 2
     else:
         self.nsplit = int(self.option.replace('split', ''))
     self.part = ROOT.RooCategory('part', 'part')
     self.model = ROOT.RooSimultaneous('model', 'model', self.part)
     for i in range(self.nsplit):
         cat = str(i)
         name = 'subdata%d' % i
         self.part.defineType(cat)
         self.subdata.append(self.resampler.prescale(self.nsplit, [i], name, name))
         name = 'submodel%d' % i
         self.submodel.append(
             ParameterizedKeysPdf(
                 name, name, self.x, self.mode, self.effsigma,
                 self.subdata[i], rho=self.rho, forcerange=True
             )
         )
         self.model.addPdf(self.submodel[i], cat)
     # Now shuffle the subdata and the categories
     args = [roo.Index(self.part)]
     cats = [str(i) for i in range(self.nsplit)]
     cats.reverse()
     for cat, subdata in zip(cats, self.subdata):
         args.append(roo.Import(cat, subdata))
     self.data = ROOT.RooDataSet('data', 'data', ROOT.RooArgSet(self.x),
                                 *args)
def main():
    '''This is the entry point to execution.'''
    global data, model, x, fitresult
    data = getdata()
    x = data['raw'].get()[variable]
    if fit_range:
        x.setRange('fit', *fit_range)
    if plot_range:
        x.setRange('plot', *plot_range)
    initialize_fit_parameters(data['raw'], x)
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    ## Reduce data for model building
    data_kde_training = data['raw']
    if data['raw'].numEntries() > kde_training_max_events:
        resampler = Resampler(data['raw'])
        data['raw_reduced'] = resampler.bootstrap(
            name = data['raw'].GetName() + '_boot',
            title = data['raw'].GetTitle() + ' Bootstrap Replica',
            size = kde_training_max_events,
            seed = 12345,
            )
        data_kde_training = data['raw_reduced']    
    
    model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, 
                                 data_kde_training, rho=0.3, forcerange=True)
                
    
    fitresult = {}
    for name, dataset in data.items():
        dataset.SetName(name + 'Data')
        ## Reduce data for debugging
        # dataset = dataset.reduce(roo.EventRange(0, 10000))
        mode.setVal(model.shapemodevar.getVal())
        effsigma.setVal(model.shapewidthvar.getVal())
        fitresult[name] = model.fitTo(dataset, roo.SumW2Error(True),
                                      roo.NumCPU(8), roo.Strategy(2),
                                      roo.Save(), roo.Range("fit"))
        make_plot(x, dataset, model)
                            
        print '\n==  Fitted parameters =='
        mode.Print()
        effsigma.Print()
        save_result(fitresult[name])
    set_default_integrator_precision(*old_precision)
    x_corr = get_correction(x, fitresult['raw'].floatParsFinal(),
                            fitresult['target'].floatParsFinal())
    make_comparison_plot(data['raw'], data['target'], x_corr)
示例#7
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to test_pkeys - test of the mode and effective sigma'
    print 'extraction for a generic distribution using ParametrizedKeysPdf.'

    w = ROOT.RooWorkspace('w', 'w')

    truepdf = w.factory('Gaussian::truepdf(x[-5,5],m[0],s[1])')
    data = truepdf.generate(ROOT.RooArgSet(w.var('x')), 10000)
    canvases.next('truepdf')
    plot = w.var('x').frame()
    data.plotOn(plot)
    truepdf.plotOn(plot)
    plot.Draw()

    toypdf = ParameterizedKeysPdf('toypdf',
                                  'toypdf',
                                  w.var('x'),
                                  w.factory('mtoy[0,-5,5]'),
                                  w.factory('stoy[1,0.1,5]'),
                                  data,
                                  rho=3)
    toypdf.shape.plotOn(plot, roo.LineColor(ROOT.kRed))
    toypdf.fitTo(data)
    toypdf.plotOn(plot, roo.LineColor(ROOT.kGreen))
    plot.Draw()

    canvases.update()
    print 'Exiting test_pkeys with success!'
示例#8
0
 def run(self):
     ## Set the range of deltaE to cover all the date plus a small margin.
     mi = self.modal_interval
     mi.setFraction(1.)
     self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
     self.model = ParameterizedKeysPdf(self.name + '_model',
                                       self.name + '_model',
                                       self.deltaE,
                                       self.mode,
                                       self.effsigma,
                                       self.train_data,
                                       rho=2)
     mit = self.modal_interval_training
     mit.setFraction(0.99)
     fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
     self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8),
                                        roo.Save(), fitrange)
     self.fit_result.SetName(self.name + '_fit_result')
     self.make_log_plot()
     self.make_zoom_plot()
     self.make_fixed_range_log_plot()
     self.make_fixed_range_zoom_plot()
     canvases.update()
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to parameterized_keys_pdf_fit_example!'
    global data, model, x, fitresult
    data = getdata()
    data.SetName('zeeDataYong')
    ## Reduce data for debugging
    data = data.reduce(roo.EventRange(0, 1000))
    x = data.get()[variable]
    initialize_fit_parameters(data, x)
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, data,
                                 rho=1, forcerange=True)
                                 
    fitresult = model.fitTo(data, roo.SumW2Error(True), roo.NumCPU(8), 
                            roo.Strategy(2), roo.Save())
    set_default_integrator_precision(*old_precision)
    make_plot(x, data, model)
    print '\n==  Fitted parameters =='
    mode.Print()
    effsigma.Print()
    save_result(fitresult)
    print '\nExiting parameterized_keys_pdf_fit_example with success.'
示例#10
0
def main():
    '''This is the entry point to execution.'''
    print 'Welcome to r9_scaling_fitter!'
    global data, model, x, fitresults
    init()
    data = getdata()
    ## Reduce data for debugging
    for source, dataset in data.items():
        # data[source] = dataset.reduce(roo.EventRange(0, 1000))
        pass
    x = variable
    old_precision = set_default_integrator_precision(1e-8, 1e-8)
    model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, data['z'],
                                 rho=0.7, forcerange=True)
    fix_model_parameters(model)
    # make_plot(x, data['z'], model.shape, name + 'MC_Shape', '')
    fitresults = {}
    for source in 'z data'.split():
        dataset = data[source]
        label = {'z': 'MC', 'data': 'Data'}[source]
        dataset.SetName(label)
        if source == 'data':
            # mode.setConstant(True)
            pass
        fitresult = model.fitTo(dataset, roo.SumW2Error(True),
                                roo.NumCPU(8), roo.Strategy(2), 
                                roo.Save(), roo.Range('fit'))
        fitresults[label] = fitresult
        make_plot(x, dataset, model, '_'.join([name,label]), '')
        decorate_plot(labels)
        # save_result(fitresult, label)

    canvases.update()
    print_report(fitresults)
    set_default_integrator_precision(*old_precision)
    print '\nExiting r9_scaling_fitter with success.'
    return(get_scaling(fitresults))
示例#11
0
 def run(self):
     ## Set the range of deltaE to cover all the date plus a small margin.
     mi = self.modal_interval
     mi.setFraction(1.0)
     self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
     self.model = ParameterizedKeysPdf(
         self.name + "_model", self.name + "_model", self.deltaE, self.mode, self.effsigma, self.train_data, rho=2
     )
     mit = self.modal_interval_training
     mit.setFraction(0.99)
     fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
     self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8), roo.Save(), fitrange)
     self.fit_result.SetName(self.name + "_fit_result")
     self.make_log_plot()
     self.make_zoom_plot()
     self.make_fixed_range_log_plot()
     self.make_fixed_range_zoom_plot()
     canvases.update()
示例#12
0
class ModeAndEffSigmaFitter:
    def __init__(self,
                 name='data_cat0',
                 debuglevel=0,
                 numentries=-1,
                 fitmode='odd-even'):
        ## Cofniguration data
        self.name = name

        ## 0: production, 1: debugging
        self.debuglevel = debuglevel
        self.numentries = numentries
        self.fitmode = fitmode

        self.numentries_train_max = 100000

        for tok in self.name.split('_'):
            if 'pho' in tok:
                self.emtype = 'pho'
            if 'ele' in tok:
                self.emtype = 'ele'
            if 'cat' in tok:
                self.cat = tok
            if 'data' in tok:
                self.src = 'data'
            elif 'mc' in tok:
                self.src = 'mc'

        ## Decoration
        self.labels = [{'mc': 'Simulation', 'data': 'Data'}[self.src]]
        self.labels.extend({
            'cat0': ['Barrel', 'R_{9} > 0.94'],
            'cat1': ['Barrel', 'R_{9} < 0.94'],
            'cat2': ['Endcaps', 'R_{9} > 0.94'],
            'cat3': ['Endcaps', 'R_{9} < 0.94'],
            'calcat0': ['Central Barrel', 'R_{9} > 0.94'],
            'calcat1': ['Central Barrel', 'R_{9} < 0.94'],
            'calcat2': ['Forward Barrel', 'R_{9} > 0.94'],
            'calcat3': ['Forward Barrel', 'R_{9} < 0.94'],
            'calcat4': ['Outer Endcaps', 'R_{9} > 0.94'],
            'calcat5': ['Outer Endcaps', 'R_{9} < 0.94'],
            'calcat6': ['Inner Endcaps', 'R_{9} > 0.94'],
            'calcat7': ['Inner Endcaps', 'R_{9} < 0.94'],
        }[self.cat])
        ## self.color = {
        ##     'cat0': ROOT.kRed,
        ##     'cat1': ROOT.kGreen,
        ##     'cat2': ROOT.kBlue,
        ##     'cat3': ROOT.kBlack,
        ##     'calcat0': ROOT.kRed,
        ##     'calcat1': ROOT.kRed - 3,
        ##     'calcat2': ROOT.kOrange - 2,
        ##     'calcat3': ROOT.kSpring + 5,
        ##     'calcat4': ROOT.kGreen,
        ##     'calcat5': ROOT.kAzure - 9,
        ##     'calcat6': ROOT.kBlue,
        ##     'calcat7': ROOT.kBlack
        ##     }[self.cat]
        self.color = ROOT.kBlack
        self.color_model = ROOT.kBlue
        self.color_data = ROOT.kBlack
        ## self.marker_style = {'data': 20, 'mc': 21}[self.src]
        self.marker_style = 20
        self.plots = []
        self.canvases = []
        self.fixed_range_zoom = (-8, 8)
        self.fixed_range_log = (-25, 25)
        self.init_workspace()
        self.get_data()

    ## End of __init__(self, ..).

    ##--------------------------------------------------------------------------
    def init_workspace(self):
        self.w = w = ROOT.RooWorkspace('w')

        ## Define the observable.
        self.deltaE = w.factory('deltaE[0, -50, 50]')

        # Define the model parameters.
        self.mode = w.factory('m[0, -50, 50]')
        self.effsigma = w.factory('effsigma[1, 0.1, 25]')
        self.effsigma.SetName('#sigma_{eff}')

        for x in [self.deltaE, self.mode, self.effsigma]:
            x.setUnit('%')

    ## End of init_workspace().

    ##--------------------------------------------------------------------------
    def get_data(self):
        'Gets the RooDataSet with deltaE data.'
        chain = ROOT.TChain('Analysis')
        datapath = '/raid2/veverka/yyTrees/tworeg'

        if self.emtype == 'pho':
            self.filenames = '''
testSelection.v3.PhotonRun2011AandB30Nov2011v1AOD.preselcut3.sel0.n1cut0.smear0.phtcorr219.phtid1.merged.root

testSelection.v3.GluGluToHToGG_M-140_7TeV-powheg-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.TTH_HToGG_M-140_7TeV-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.VBF_HToGG_M-140_7TeV-powheg-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.WH_ZH_HToGG_M-140_7TeV-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root
'''.split()
        elif self.emtype == 'ele':
            self.filenames = '''
testSelectionZeev1.v3.DoubleElectronRun2011A30Nov2011v1AOD.etcut25.corr216.eleid1.datapu0.mcpu0.r*.scale1.root

testSelectionZeev1.v3.DoubleElectronRun2011B30Nov2011v1AOD.etcut25.corr216.eleid1.datapu0.mcpu0.r*.scale1.root
  
testSelectionZeev1.v3.DYJetsToLL_TuneZ2_M50_7TeVmadgraphtauolaFall11PU_S6_START42_V14Bv1AODSIM.etcut25.corr216.eleid1.datapu6.mcpu1.r*.scale0.root
'''.split()
        else:
            raise RuntimeError, "Illegal emtype: `%s'!" % str(self.emtype)

        for f in self.filenames:
            chain.Add(os.path.join(datapath, f))

        ## Selection
        if self.emtype == 'pho':
            cuts = ['100 <= mpair & mpair <= 180']
        elif self.emtype == 'ele':
            cuts = ['80 <= mpair & mpair <= 100']
        else:
            raise RuntimeError, "Illegal emtype: `%s'!" % str(self.emtype)

        cuts.append({
            'mc': 'runNumber == 1',
            'data': 'runNumber >  1'
        }[self.src])
        cuts.extend({
            'cat0': ['scr9 >  0.94', 'fabs(sceta) <  1.48'],
            'cat1': ['scr9 <= 0.94', 'fabs(sceta) <  1.48'],
            'cat2': ['scr9 >  0.94', 'fabs(sceta) >= 1.48'],
            'cat3': ['scr9 <= 0.94', 'fabs(sceta) >= 1.48'],
            'calcat0': ['scr9 >  0.94', 'fabs(sceta) <  1'],
            'calcat1': ['scr9 <  0.94', 'fabs(sceta) <  1'],
            'calcat2':
            ['scr9 >  0.94', '1 < fabs(sceta) & fabs(sceta) <  1.48'],
            'calcat3':
            ['scr9 <  0.94', '1 < fabs(sceta) & fabs(sceta) <  1.48'],
            'calcat4':
            ['scr9 >  0.94', '1.48 < fabs(sceta) & fabs(sceta) <  2'],
            'calcat5':
            ['scr9 <  0.94', '1.48 < fabs(sceta) & fabs(sceta) <  2'],
            'calcat6': ['scr9 >  0.94', '2 < fabs(sceta) & fabs(sceta) < 2.5'],
            'calcat7': ['scr9 <  0.94', '2 < fabs(sceta) & fabs(sceta) < 2.5'],
        }[self.cat])

        if self.numentries > 0:
            cuts.append('Entry$ < %d' % self.numentries)

        self.deltaE.SetTitle('200*(scen_bendavid - scen_yangyong)/'
                             '    (scen_bendavid + scen_yangyong)')
        self.data = dataset.get(tree=chain,
                                variable=self.deltaE,
                                cuts=cuts[:],
                                name=self.name + '_data')
        self.data_half_odd = dataset.get(tree=chain,
                                         variable=self.deltaE,
                                         cuts=cuts[:] + ['Entry$ % 2 == 0'],
                                         name=self.name + '_data_half_odd')
        self.data_half_even = dataset.get(tree=chain,
                                          variable=self.deltaE,
                                          cuts=cuts[:] + ['Entry$ % 2 == 1'],
                                          name=self.name + '_data_half_even')
        if self.debuglevel > 0:
            reduced_range = roo.EventRange(0, 5000)
            self.data = self.data.reduce(reduced_range)
            self.data_half_odd = self.data_half_odd.reduce(reduced_range)
            self.data_half_even = self.data_half_even.reduce(reduced_range)

        nentries = self.data.tree().Draw('deltaE', '', 'goff')
        self.modal_interval = ModalInterval(nentries,
                                            self.data.tree().GetV1(), 1.)
        if self.fitmode == 'odd-even':
            self.train_data = self.data_half_odd
            self.fit_data = self.data_half_even
        elif self.fitmode == 'event-odd':
            self.train_data = self.data_half_even
            self.fit_data = self.data_half_odd
        elif self.fitmode == 'full-full':
            self.train_data = self.data
            self.fit_data = self.data
        else:
            raise RuntimeError, "Fit mode `%s' not supported!" % self.fitmode

        ## Make sure that the trainining dataset isn't too large
        if self.train_data.numEntries() > self.numentries_train_max:
            prescale = (
                self.train_data.numEntries() / self.numentries_train_max + 1)
            self.deltaE.SetTitle('deltaE')
            self.train_data = dataset.get(
                tree=self.train_data.tree(),
                variable=self.deltaE,
                cuts=['Entry$ %% %d == 0' % prescale],
                name=self.name + '_train_data')
        nentries = self.train_data.tree().Draw('deltaE', '', 'goff')
        self.modal_interval_training = ModalInterval(
            nentries,
            self.train_data.tree().GetV1(), 0.99)

        ## Set a nice title for the x-axis of plots
        if self.emtype == 'pho':
            self.deltaE.SetTitle('Photon #DeltaE_{two regr.}/E')
        elif self.emtype == 'ele':
            self.deltaE.SetTitle('Electron #DeltaE_{two regr.}/E')
        else:
            raise RuntimeError, "Unsupported emtype `%s'!" % self.emtype

    ## End of get_data().

    ##--------------------------------------------------------------------------
    def get_fit_based_range(self, nsigma=5):
        return (self.mode.getVal() - nsigma * self.effsigma.getVal(),
                self.mode.getVal() + nsigma * self.effsigma.getVal())

    ## End of get_fit_based_range().

    ##--------------------------------------------------------------------------
    def make_log_plot(self):
        c1 = canvases.next(self.name + '_log_autorange')
        c1.SetGrid()
        c1.SetLogy()
        self.canvases.append(c1)

        ## Use the ModalInterval class to display the shortest range containing
        ## 100% of all the entries.
        mi = self.modal_interval
        mi.setFraction(1)

        fullrange = (mi.lowerBound(), mi.upperBound())
        plot = self.deltaE.frame(roo.Range(*fullrange))
        plot.SetTitle(', '.join(self.labels))
        self.data.plotOn(plot, roo.MarkerColor(self.color_data),
                         roo.MarkerStyle(self.marker_style),
                         roo.LineColor(self.color_data))
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        if fullrange[0] + fullrange[1] > 0:
            layout = (0.6, 0.9, 0.87)
        else:
            layout = (0.2, 0.5, 0.87)
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                           roo.Layout(*layout))
        ## Fine tune the y-axis range so that we see all the events.
        plot.SetMinimum(0.5)
        ## Add a larger top margin to the y-axis range
        plot.SetMaximum(pow(plot.GetMaximum(), 1.1))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_log_plot().

    ##--------------------------------------------------------------------------
    def make_fixed_range_log_plot(self):
        c1 = canvases.next(self.name + '_log_fixedrange')
        c1.SetGrid()
        c1.SetLogy()
        self.canvases.append(c1)

        ## Use the ModalInterval class to display the shortest range containing
        ## 100% of all the entries.
        mi = self.modal_interval
        mi.setFraction(1)

        fullrange = (mi.lowerBound(), mi.upperBound())
        plot = self.deltaE.frame(roo.Range(*self.fixed_range_log))
        plot.SetTitle(', '.join(self.labels))
        self.fit_data.plotOn(plot, roo.MarkerColor(self.color_data),
                             roo.MarkerStyle(self.marker_style),
                             roo.LineColor(self.color_data))
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        if fullrange[0] + fullrange[1] > 0:
            layout = (0.6, 0.9, 0.87)
        else:
            layout = (0.2, 0.5, 0.87)
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                           roo.Layout(*layout))
        ## Fine tune the y-axis range so that we see all the events.
        plot.SetMinimum(0.5)
        ## Add a larger top margin to the y-axis range
        plot.SetMaximum(pow(plot.GetMaximum(), 1.1))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_fixed_range_log_plot().

    ##--------------------------------------------------------------------------
    def make_zoom_plot(self):
        c1 = canvases.next(self.name + '_lin_autorange')
        c1.SetGrid()
        self.canvases.append(c1)
        plot = self.deltaE.frame(roo.Range(*self.get_fit_based_range()))
        plot.SetTitle(', '.join(self.labels))
        self.fit_data.plotOn(plot, roo.MarkerColor(self.color_data),
                             roo.MarkerStyle(self.marker_style),
                             roo.LineColor(self.color_data))
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                           roo.Layout(0.2, 0.52, 0.87))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_zoom_plot().

    ##--------------------------------------------------------------------------
    def make_fixed_range_zoom_plot(self):
        c1 = canvases.next(self.name + '_lin_fixedrange')
        c1.SetGrid()
        self.canvases.append(c1)
        plot = self.deltaE.frame(roo.Range(*self.fixed_range_zoom))
        plot.SetTitle(', '.join(self.labels))
        self.fit_data.plotOn(plot, roo.MarkerColor(self.color_data),
                             roo.MarkerStyle(self.marker_style),
                             roo.LineColor(self.color_data))
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)),
                           roo.Layout(0.2, 0.52, 0.87))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_zoom_plot().

    ##--------------------------------------------------------------------------
    def run(self):
        ## Set the range of deltaE to cover all the date plus a small margin.
        mi = self.modal_interval
        mi.setFraction(1.)
        self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
        self.model = ParameterizedKeysPdf(self.name + '_model',
                                          self.name + '_model',
                                          self.deltaE,
                                          self.mode,
                                          self.effsigma,
                                          self.train_data,
                                          rho=2)
        mit = self.modal_interval_training
        mit.setFraction(0.99)
        fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
        self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8),
                                           roo.Save(), fitrange)
        self.fit_result.SetName(self.name + '_fit_result')
        self.make_log_plot()
        self.make_zoom_plot()
        self.make_fixed_range_log_plot()
        self.make_fixed_range_zoom_plot()
        canvases.update()
示例#13
0
class KeysResponseFitter:
    #__________________________________________________________________________
    def __init__(self, data, rho=1.5, printlevel=-1, option='monolithic'):
        if data.get().getSize() != 1:
            raise RuntimeError, 'Data must contain just one variable!'
        
        self.w = ROOT.RooWorkspace('KeysResponseFitter',
                                   'KeysResponseFitter Workspace')
        self.w.Import(data)
        self.data = self.w.data(data.GetName())
        self.data.SetName('data')      

        self.x = self.w.var(data.get().first().GetName())
        self.x.SetName('x')
        
        self.rho = rho        
        self.printlevel = printlevel

        ## Define mode and effsigma.
        self.mode = self.w.factory('mode[0, -50, 50]')
        self.effsigma = self.w.factory('effsigma[1, 0.01, 50]')
        for x in 'mode effsigma'.split():
            getattr(self, x).setUnit(self.x.getUnit())

        self.option = option
        
        if option == 'monolithic':
            self.model = ParameterizedKeysPdf('model', 'model', self.x,
                                              self.mode,
                                              self.effsigma, self.data, rho=rho,
                                              forcerange=True)
        elif 'split' in option:
            self.initsplit()
        else:
            raise RuntimeError, 'Unknown option: %s' % option
            
        # self.dofit()
    ## end of __init__
    
    #__________________________________________________________________________
    def initsplit(self):
        '''
        Splits the dataset in two and builds a model for each half.
        '''
        global arg
        self.resampler = Resampler(self.data)
        self.subdata = []
        self.submodel = []
        if not self.option.replace('split', ''):
            self.nsplit = 2
        else:
            self.nsplit = int(self.option.replace('split', ''))
        self.part = ROOT.RooCategory('part', 'part')
        self.model = ROOT.RooSimultaneous('model', 'model', self.part)
        for i in range(self.nsplit):
            cat = str(i)
            name = 'subdata%d' % i
            self.part.defineType(cat)
            self.subdata.append(self.resampler.prescale(self.nsplit, [i], name, name))
            name = 'submodel%d' % i
            self.submodel.append(
                ParameterizedKeysPdf(
                    name, name, self.x, self.mode, self.effsigma,
                    self.subdata[i], rho=self.rho, forcerange=True
                )
            )
            self.model.addPdf(self.submodel[i], cat)
        # Now shuffle the subdata and the categories
        args = [roo.Index(self.part)]
        cats = [str(i) for i in range(self.nsplit)]
        cats.reverse()
        for cat, subdata in zip(cats, self.subdata):
            args.append(roo.Import(cat, subdata))
        self.data = ROOT.RooDataSet('data', 'data', ROOT.RooArgSet(self.x),
                                    *args)
    ## end of initsplit()
        
    #__________________________________________________________________________
    def dofit(self):
        self.model.fitTo(self.data, roo.PrintLevel(1), roo.Verbose(False))
    ## end of doFit

    #__________________________________________________________________________
    def makeplot(self):
        self.canvas = canvases.next()
        self.plot = self.x.frame()
        self.data.plotOn(self.plot)
        self.model.plotOn(self.plot)
        self.model.paramOn(self.plot)
        self.plot.Draw()
        self.canvas.Update()
示例#14
0
class ModeAndEffSigmaFitter:
    def __init__(self, name="data_cat0", debuglevel=0, numentries=-1, fitmode="odd-even"):
        ## Cofniguration data
        self.name = name

        ## 0: production, 1: debugging
        self.debuglevel = debuglevel
        self.numentries = numentries
        self.fitmode = fitmode

        self.numentries_train_max = 100000

        for tok in self.name.split("_"):
            if "pho" in tok:
                self.emtype = "pho"
            if "ele" in tok:
                self.emtype = "ele"
            if "cat" in tok:
                self.cat = tok
            if "data" in tok:
                self.src = "data"
            elif "mc" in tok:
                self.src = "mc"

        ## Decoration
        self.labels = [{"mc": "Simulation", "data": "Data"}[self.src]]
        self.labels.extend(
            {
                "cat0": ["Barrel", "R_{9} > 0.94"],
                "cat1": ["Barrel", "R_{9} < 0.94"],
                "cat2": ["Endcaps", "R_{9} > 0.94"],
                "cat3": ["Endcaps", "R_{9} < 0.94"],
                "calcat0": ["Central Barrel", "R_{9} > 0.94"],
                "calcat1": ["Central Barrel", "R_{9} < 0.94"],
                "calcat2": ["Forward Barrel", "R_{9} > 0.94"],
                "calcat3": ["Forward Barrel", "R_{9} < 0.94"],
                "calcat4": ["Outer Endcaps", "R_{9} > 0.94"],
                "calcat5": ["Outer Endcaps", "R_{9} < 0.94"],
                "calcat6": ["Inner Endcaps", "R_{9} > 0.94"],
                "calcat7": ["Inner Endcaps", "R_{9} < 0.94"],
            }[self.cat]
        )
        ## self.color = {
        ##     'cat0': ROOT.kRed,
        ##     'cat1': ROOT.kGreen,
        ##     'cat2': ROOT.kBlue,
        ##     'cat3': ROOT.kBlack,
        ##     'calcat0': ROOT.kRed,
        ##     'calcat1': ROOT.kRed - 3,
        ##     'calcat2': ROOT.kOrange - 2,
        ##     'calcat3': ROOT.kSpring + 5,
        ##     'calcat4': ROOT.kGreen,
        ##     'calcat5': ROOT.kAzure - 9,
        ##     'calcat6': ROOT.kBlue,
        ##     'calcat7': ROOT.kBlack
        ##     }[self.cat]
        self.color = ROOT.kBlack
        self.color_model = ROOT.kBlue
        self.color_data = ROOT.kBlack
        ## self.marker_style = {'data': 20, 'mc': 21}[self.src]
        self.marker_style = 20
        self.plots = []
        self.canvases = []
        self.fixed_range_zoom = (-8, 8)
        self.fixed_range_log = (-25, 25)
        self.init_workspace()
        self.get_data()

    ## End of __init__(self, ..).

    ##--------------------------------------------------------------------------
    def init_workspace(self):
        self.w = w = ROOT.RooWorkspace("w")

        ## Define the observable.
        self.deltaE = w.factory("deltaE[0, -50, 50]")

        # Define the model parameters.
        self.mode = w.factory("m[0, -50, 50]")
        self.effsigma = w.factory("effsigma[1, 0.1, 25]")
        self.effsigma.SetName("#sigma_{eff}")

        for x in [self.deltaE, self.mode, self.effsigma]:
            x.setUnit("%")

    ## End of init_workspace().

    ##--------------------------------------------------------------------------
    def get_data(self):
        "Gets the RooDataSet with deltaE data."
        chain = ROOT.TChain("Analysis")
        datapath = "/raid2/veverka/yyTrees/tworeg"

        if self.emtype == "pho":
            self.filenames = """
testSelection.v3.PhotonRun2011AandB30Nov2011v1AOD.preselcut3.sel0.n1cut0.smear0.phtcorr219.phtid1.merged.root

testSelection.v3.GluGluToHToGG_M-140_7TeV-powheg-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.TTH_HToGG_M-140_7TeV-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.VBF_HToGG_M-140_7TeV-powheg-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root

testSelection.v3.WH_ZH_HToGG_M-140_7TeV-pythia6Fall11-PU_S6_START42_V14B-v1AODSIM.preselcut3.sel0.n1cut0.smear3.phtcorr219.phtid1.r1.root
""".split()
        elif self.emtype == "ele":
            self.filenames = """
testSelectionZeev1.v3.DoubleElectronRun2011A30Nov2011v1AOD.etcut25.corr216.eleid1.datapu0.mcpu0.r*.scale1.root

testSelectionZeev1.v3.DoubleElectronRun2011B30Nov2011v1AOD.etcut25.corr216.eleid1.datapu0.mcpu0.r*.scale1.root
  
testSelectionZeev1.v3.DYJetsToLL_TuneZ2_M50_7TeVmadgraphtauolaFall11PU_S6_START42_V14Bv1AODSIM.etcut25.corr216.eleid1.datapu6.mcpu1.r*.scale0.root
""".split()
        else:
            raise RuntimeError, "Illegal emtype: `%s'!" % str(self.emtype)

        for f in self.filenames:
            chain.Add(os.path.join(datapath, f))

        ## Selection
        if self.emtype == "pho":
            cuts = ["100 <= mpair & mpair <= 180"]
        elif self.emtype == "ele":
            cuts = ["80 <= mpair & mpair <= 100"]
        else:
            raise RuntimeError, "Illegal emtype: `%s'!" % str(self.emtype)

        cuts.append({"mc": "runNumber == 1", "data": "runNumber >  1"}[self.src])
        cuts.extend(
            {
                "cat0": ["scr9 >  0.94", "fabs(sceta) <  1.48"],
                "cat1": ["scr9 <= 0.94", "fabs(sceta) <  1.48"],
                "cat2": ["scr9 >  0.94", "fabs(sceta) >= 1.48"],
                "cat3": ["scr9 <= 0.94", "fabs(sceta) >= 1.48"],
                "calcat0": ["scr9 >  0.94", "fabs(sceta) <  1"],
                "calcat1": ["scr9 <  0.94", "fabs(sceta) <  1"],
                "calcat2": ["scr9 >  0.94", "1 < fabs(sceta) & fabs(sceta) <  1.48"],
                "calcat3": ["scr9 <  0.94", "1 < fabs(sceta) & fabs(sceta) <  1.48"],
                "calcat4": ["scr9 >  0.94", "1.48 < fabs(sceta) & fabs(sceta) <  2"],
                "calcat5": ["scr9 <  0.94", "1.48 < fabs(sceta) & fabs(sceta) <  2"],
                "calcat6": ["scr9 >  0.94", "2 < fabs(sceta) & fabs(sceta) < 2.5"],
                "calcat7": ["scr9 <  0.94", "2 < fabs(sceta) & fabs(sceta) < 2.5"],
            }[self.cat]
        )

        if self.numentries > 0:
            cuts.append("Entry$ < %d" % self.numentries)

        self.deltaE.SetTitle("200*(scen_bendavid - scen_yangyong)/" "    (scen_bendavid + scen_yangyong)")
        self.data = dataset.get(tree=chain, variable=self.deltaE, cuts=cuts[:], name=self.name + "_data")
        self.data_half_odd = dataset.get(
            tree=chain, variable=self.deltaE, cuts=cuts[:] + ["Entry$ % 2 == 0"], name=self.name + "_data_half_odd"
        )
        self.data_half_even = dataset.get(
            tree=chain, variable=self.deltaE, cuts=cuts[:] + ["Entry$ % 2 == 1"], name=self.name + "_data_half_even"
        )
        if self.debuglevel > 0:
            reduced_range = roo.EventRange(0, 5000)
            self.data = self.data.reduce(reduced_range)
            self.data_half_odd = self.data_half_odd.reduce(reduced_range)
            self.data_half_even = self.data_half_even.reduce(reduced_range)

        nentries = self.data.tree().Draw("deltaE", "", "goff")
        self.modal_interval = ModalInterval(nentries, self.data.tree().GetV1(), 1.0)
        if self.fitmode == "odd-even":
            self.train_data = self.data_half_odd
            self.fit_data = self.data_half_even
        elif self.fitmode == "event-odd":
            self.train_data = self.data_half_even
            self.fit_data = self.data_half_odd
        elif self.fitmode == "full-full":
            self.train_data = self.data
            self.fit_data = self.data
        else:
            raise RuntimeError, "Fit mode `%s' not supported!" % self.fitmode

        ## Make sure that the trainining dataset isn't too large
        if self.train_data.numEntries() > self.numentries_train_max:
            prescale = self.train_data.numEntries() / self.numentries_train_max + 1
            self.deltaE.SetTitle("deltaE")
            self.train_data = dataset.get(
                tree=self.train_data.tree(),
                variable=self.deltaE,
                cuts=["Entry$ %% %d == 0" % prescale],
                name=self.name + "_train_data",
            )
        nentries = self.train_data.tree().Draw("deltaE", "", "goff")
        self.modal_interval_training = ModalInterval(nentries, self.train_data.tree().GetV1(), 0.99)

        ## Set a nice title for the x-axis of plots
        if self.emtype == "pho":
            self.deltaE.SetTitle("Photon #DeltaE_{two regr.}/E")
        elif self.emtype == "ele":
            self.deltaE.SetTitle("Electron #DeltaE_{two regr.}/E")
        else:
            raise RuntimeError, "Unsupported emtype `%s'!" % self.emtype

    ## End of get_data().

    ##--------------------------------------------------------------------------
    def get_fit_based_range(self, nsigma=5):
        return (
            self.mode.getVal() - nsigma * self.effsigma.getVal(),
            self.mode.getVal() + nsigma * self.effsigma.getVal(),
        )

    ## End of get_fit_based_range().

    ##--------------------------------------------------------------------------
    def make_log_plot(self):
        c1 = canvases.next(self.name + "_log_autorange")
        c1.SetGrid()
        c1.SetLogy()
        self.canvases.append(c1)

        ## Use the ModalInterval class to display the shortest range containing
        ## 100% of all the entries.
        mi = self.modal_interval
        mi.setFraction(1)

        fullrange = (mi.lowerBound(), mi.upperBound())
        plot = self.deltaE.frame(roo.Range(*fullrange))
        plot.SetTitle(", ".join(self.labels))
        self.data.plotOn(
            plot, roo.MarkerColor(self.color_data), roo.MarkerStyle(self.marker_style), roo.LineColor(self.color_data)
        )
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        if fullrange[0] + fullrange[1] > 0:
            layout = (0.6, 0.9, 0.87)
        else:
            layout = (0.2, 0.5, 0.87)
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)), roo.Layout(*layout))
        ## Fine tune the y-axis range so that we see all the events.
        plot.SetMinimum(0.5)
        ## Add a larger top margin to the y-axis range
        plot.SetMaximum(pow(plot.GetMaximum(), 1.1))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_log_plot().

    ##--------------------------------------------------------------------------
    def make_fixed_range_log_plot(self):
        c1 = canvases.next(self.name + "_log_fixedrange")
        c1.SetGrid()
        c1.SetLogy()
        self.canvases.append(c1)

        ## Use the ModalInterval class to display the shortest range containing
        ## 100% of all the entries.
        mi = self.modal_interval
        mi.setFraction(1)

        fullrange = (mi.lowerBound(), mi.upperBound())
        plot = self.deltaE.frame(roo.Range(*self.fixed_range_log))
        plot.SetTitle(", ".join(self.labels))
        self.fit_data.plotOn(
            plot, roo.MarkerColor(self.color_data), roo.MarkerStyle(self.marker_style), roo.LineColor(self.color_data)
        )
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        if fullrange[0] + fullrange[1] > 0:
            layout = (0.6, 0.9, 0.87)
        else:
            layout = (0.2, 0.5, 0.87)
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)), roo.Layout(*layout))
        ## Fine tune the y-axis range so that we see all the events.
        plot.SetMinimum(0.5)
        ## Add a larger top margin to the y-axis range
        plot.SetMaximum(pow(plot.GetMaximum(), 1.1))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_fixed_range_log_plot().

    ##--------------------------------------------------------------------------
    def make_zoom_plot(self):
        c1 = canvases.next(self.name + "_lin_autorange")
        c1.SetGrid()
        self.canvases.append(c1)
        plot = self.deltaE.frame(roo.Range(*self.get_fit_based_range()))
        plot.SetTitle(", ".join(self.labels))
        self.fit_data.plotOn(
            plot, roo.MarkerColor(self.color_data), roo.MarkerStyle(self.marker_style), roo.LineColor(self.color_data)
        )
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)), roo.Layout(0.2, 0.52, 0.87))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_zoom_plot().

    ##--------------------------------------------------------------------------
    def make_fixed_range_zoom_plot(self):
        c1 = canvases.next(self.name + "_lin_fixedrange")
        c1.SetGrid()
        self.canvases.append(c1)
        plot = self.deltaE.frame(roo.Range(*self.fixed_range_zoom))
        plot.SetTitle(", ".join(self.labels))
        self.fit_data.plotOn(
            plot, roo.MarkerColor(self.color_data), roo.MarkerStyle(self.marker_style), roo.LineColor(self.color_data)
        )
        self.model.plotOn(plot, roo.LineColor(self.color_model))
        self.model.paramOn(plot, roo.Format("NEU", roo.AutoPrecision(2)), roo.Layout(0.2, 0.52, 0.87))
        plot.Draw()
        self.plots.append(plot)

    ## End of make_zoom_plot().

    ##--------------------------------------------------------------------------
    def run(self):
        ## Set the range of deltaE to cover all the date plus a small margin.
        mi = self.modal_interval
        mi.setFraction(1.0)
        self.deltaE.setRange(mi.lowerBound() - 1, mi.upperBound() + 1)
        self.model = ParameterizedKeysPdf(
            self.name + "_model", self.name + "_model", self.deltaE, self.mode, self.effsigma, self.train_data, rho=2
        )
        mit = self.modal_interval_training
        mit.setFraction(0.99)
        fitrange = roo.Range(mit.lowerBound(), mit.upperBound())
        self.fit_result = self.model.fitTo(self.fit_data, roo.NumCPU(8), roo.Save(), fitrange)
        self.fit_result.SetName(self.name + "_fit_result")
        self.make_log_plot()
        self.make_zoom_plot()
        self.make_fixed_range_log_plot()
        self.make_fixed_range_zoom_plot()
        canvases.update()