Example #1
0
def calculate_resolutions(variable,
                          bin_edges=[],
                          channel='combined',
                          res_to_plot=False):
    '''
    Calculate the resolutions in the bins using the residual method
    '''
    f = File('unfolding/13TeV/unfolding_TTJets_13TeV.root')
    fineBinEdges_path = '{}_{}/responseVis_without_fakes'.format(
        variable, channel)
    fineBinEdges_hist2d = f.Get(fineBinEdges_path).Clone()
    fineBinEdges_hist1d = asrootpy(fineBinEdges_hist2d.ProjectionX())
    fineBinEdges = list(fineBinEdges_hist1d.xedges())
    nFineBins = len(fineBinEdges) - 1

    tmp_residual_path = '{}_{}/residuals/Residuals_Bin_'.format(
        variable, channel)
    # Absolute lepton eta can have multiple fine bins at the same precision as wide bins. Only taking first.
    if variable == 'abs_lepton_eta':
        fineBinEdges = [round(entry, 2) for entry in fineBinEdges]

    # For N Bin edges find resolutions of bins
    resolutions = []
    for i in range(len(bin_edges) - 1):
        list_of_fine_bins = []
        # Find fine bin edges in wide bins
        for j, fine_bin_edge in enumerate(fineBinEdges):
            if fine_bin_edge >= bin_edges[i] and fine_bin_edge < bin_edges[
                    i + 1] and j < nFineBins:
                list_of_fine_bins.append(j + 1)

        # Sum the residuals of the fine bins
        for fine_bin in list_of_fine_bins:
            if fine_bin == list_of_fine_bins[0]:
                fineBin_histRes = f.Get(tmp_residual_path +
                                        str(fine_bin)).Clone()
            else:
                fineBin_histRes_tmp = f.Get(tmp_residual_path +
                                            str(fine_bin)).Clone()
                fineBin_histRes.Add(fineBin_histRes, fineBin_histRes_tmp, 1.0,
                                    1.0)

        # Get the quantile at 68% = 1 sigma = Resolution
        interval = np.array([0.])
        quantile = np.array([0.68])
        fineBin_histRes.GetQuantiles(1, interval, quantile)
        resolutions.append(round(interval[0], 2))

        if res_to_plot:
            plotting_resolution(variable, channel, fineBin_histRes,
                                round(interval[0], 2), i, bin_edges[i],
                                bin_edges[i + 1])

    return resolutions
Example #2
0
def parallelTreeWorker(item):
    import random, string, os
    filename, _tree, value, cut, _hist, weight, f = item
    rfile = File(filename)
    tree = rfile.Get(_tree)
    tree = asrootpy(tree)
    _hist = asrootpy(_hist)
    try:
        if weight is None:
            tree.Draw(value, selection=cut, hist=_hist)
        else:
            #_tree.Draw(value,selection="(%s)*(%s)"%(cut,weight),hist=self.hists[f])
            tmpFileName = ''.join(
                random.choice(string.ascii_lowercase) for i in range(4))
            tmpFile = File("/tmp/%s.root" % tmpFileName, "recreate")
            #sel_tree=_tree.copy_tree(selection=cut)
            sel_tree = asrootpy(tree.CopyTree(cut))
            ##print weight
            sel_tree.Draw(value, selection=weight, hist=_hist)
            tmpFile.Close()
            os.remove("/tmp/%s.root" % tmpFileName)
    except Exception as e:
        print(tree, value, cut, _hist, weight, f)
        log_plotlib.info("error:%s" % (e))
        log_plotlib.info("file :%s" % (f))
        log_plotlib.info("Perhaps try this one:")
        for i in tree.glob("*"):
            log_plotlib.info(i)
        raise RuntimeError("Will stop here!")
    rfile.Close()
    del (tree)
    return (_hist, f)
def get_merged_bin_resolution(res_file, var, low_bin, high_bin):
    '''
    Return mean value of resolutions in fine bins of the merged bin.
    '''

    bin_contents = []

    f = File(res_file)
    res_hist = f.Get('res_r_' + var).Clone()
    # change scope from file to memory
    res_hist.SetDirectory(0)
    f.close()

    low_bin_n = res_hist.GetXaxis().FindBin(low_bin)
    high_bin_n = res_hist.GetXaxis().FindBin(high_bin)

    for bin_i in range(low_bin_n, high_bin_n + 1):
        bin_content = res_hist.GetBinContent(bin_i)
        # resolution couldnt be reconstructed (High GeV with low stats)
        # remove these from list of resolutions
        if bin_content == 0: continue
        bin_contents.append(bin_content)

    # print(bin_contents)
    res = np.mean(bin_contents)
    return res
def get_response_histogram(responseFileName, variable, channel):
    '''
        clones the response matrix from file
    '''
    responseFile = File(responseFileName, 'read')
    folder = '{variable}_{channel}'.format(variable=variable, channel=channel)
    h_response = responseFile.Get(folder).responseVis_without_fakes.Clone()
    return asrootpy(h_response)
Example #5
0
def get_electron_normalisation(met_bin, b_tag):
    global electron_data_file
    input_file = File(electron_data_file)
    histogram_for_estimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_%s/electron_pfIsolation_03_%s' % (
        met_bin, b_tag)
    input_histogram = input_file.Get(histogram_for_estimation)
    result = estimate_with_fit_to_relative_isolation(input_histogram)
    value, error = result['value'], result['error']
    return value, error
def get_histograms( variable, options ):
    config = XSectionConfig( 13 )

    path_electron = ''
    path_muon = ''
    path_combined = ''    
    histogram_name = ''
    if options.visiblePhaseSpace:
        histogram_name = 'responseVis_without_fakes'
    else :
        histogram_name = 'response_without_fakes'

    if variable == 'HT':
        path_electron = 'unfolding_HT_analyser_electron_channel/%s' % histogram_name
        path_muon = 'unfolding_HT_analyser_muon_channel/%s' % histogram_name
        path_combined = 'unfolding_HT_analyser_COMBINED_channel/%s' % histogram_name

    else :
        path_electron = 'unfolding_%s_analyser_electron_channel_patType1CorrectedPFMet/%s' % ( variable, histogram_name )
        path_muon = 'unfolding_%s_analyser_muon_channel_patType1CorrectedPFMet/%s' % ( variable, histogram_name )
        path_combined = 'unfolding_%s_analyser_COMBINED_channel_patType1CorrectedPFMet/%s' % ( variable, histogram_name )

    histogram_information = [
                {'file': config.unfolding_central_raw,
                 'CoM': 13,
                 'path':path_electron,
                 'channel':'electron'},
                {'file':config.unfolding_central_raw,
                 'CoM': 13,
                 'path':path_muon,
                 'channel':'muon'},
                ]
    
    if options.combined:
        histogram_information = [
                    {'file': config.unfolding_central_raw,
                     'CoM': 13,
                     'path': path_combined,
                     'channel':'combined'},
                    ]

    for histogram in histogram_information:
        f = File( histogram['file'] )
        # scale to lumi
        # nEvents = f.EventFilter.EventCounter.GetBinContent( 1 )  # number of processed events 
        # config = XSectionConfig( histogram['CoM'] )
        # lumiweight = config.ttbar_xsection * config.new_luminosity / nEvents

        lumiweight = 1

        histogram['hist'] = f.Get( histogram['path'] ).Clone()
        histogram['hist'].Scale( lumiweight )
        # change scope from file to memory
        histogram['hist'].SetDirectory( 0 )
        f.close()

    return histogram_information
Example #7
0
def get_histograms(config, variable, args):
    '''
    Return a dictionary of the unfolding histogram informations (inc. hist)
    '''
    path_electron = ''
    path_muon = ''
    path_combined = ''
    histogram_name = 'response_without_fakes'
    if args.visiblePhaseSpace:
        histogram_name = 'responseVis_without_fakes'

    path_electron = '%s_electron/%s' % (variable, histogram_name)
    path_muon = '%s_muon/%s' % (variable, histogram_name)
    path_combined = '%s_combined/%s' % (variable, histogram_name)

    histogram_information = [
        {
            'file': config.unfolding_central_raw,
            'CoM': 13,
            'path': path_electron,
            'channel': 'electron'
        },
        {
            'file': config.unfolding_central_raw,
            'CoM': 13,
            'path': path_muon,
            'channel': 'muon'
        },
    ]

    if args.combined:
        histogram_information = [
            {
                'file': config.unfolding_central_raw,
                'CoM': 13,
                'path': path_combined,
                'channel': 'combined'
            },
        ]

    for histogram in histogram_information:
        lumiweight = 1
        f = File(histogram['file'])
        histogram['hist'] = f.Get(histogram['path']).Clone()

        # scale to current lumi
        lumiweight = config.luminosity_scale
        if round(lumiweight, 1) != 1.0:
            print("Scaling to {}".format(lumiweight))
        histogram['hist'].Scale(lumiweight)

        # change scope from file to memory
        histogram['hist'].SetDirectory(0)
        f.close()

    return histogram_information
Example #8
0
def root_file_to_hist(input_filename, hist_templates):

    hists = copy.deepcopy(hist_templates)

    root_file = TFile(input_filename, "read")

    for var in hists.keys():
        mod_hist = hists[var]
        hist = root_file.Get(var)
        mod_hist.replace_hist(hist)

    return hists
def getStystPlot(hist):
    import os
    if os.path.exists("syst/"+hist.replace("/","")+"_syst.root"):
        systFile=File("syst/"+hist.replace("/","")+"_syst.root","read")
        stysthist=systFile.Get(hist)
        stysthist.SetDirectory(0)
        stysthist.SetLineWidth(0)
        stysthist.SetLineColor(0)
    else:
        raise IOError("no syst for %s"%(hist))
    if "Phi" in hist:
        stysthist.Smooth()
    return stysthist
Example #10
0
def root_file_to_hist(input_filename, hist_templates, is_this_data):

    hists = copy.deepcopy(hist_templates)

    root_file = TFile(input_filename, "read")

    for var in hists.keys():

        index = 0

        # if var in ['hardest_pT', 'uncor_hardest_pT', 'hardest_eta']:
        # if var not in ['uncor_hardest_pT']:
        if is_this_data:
            for mod_hist in hists[var]:
                hist_name = "{}#{}".format(var, index)

                # Get hist from ROOT file.
                hist = root_file.Get(hist_name)

                mod_hist.replace_hist(hist)

                index += 1

        else:
            if var != 'uncor_hardest_pT':
                for mod_hist in hists[var]:
                    hist_name = "{}#{}".format(var, index)

                    # Get hist from ROOT file.
                    hist = root_file.Get(hist_name)

                    mod_hist.replace_hist(hist)

                    index += 1

    return hists
Example #11
0
def get_muon_normalisation(met_bin, b_tag):
    global path_to_files
    muon_qcd_file = path_to_files + 'central/QCD_Pt-20_MuEnrichedPt-15_5050pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'
    input_file = File(muon_qcd_file)
    histogram_for_estimation = 'TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_%s/muon_AbsEta_%s' % (
        met_bin, b_tag)
    #if not correctly scaled, rescale here
    #
    input_histogram = input_file.Get(histogram_for_estimation)
    scale_factor = 1.21
    value = input_histogram.Integral() * scale_factor
    error = sum([
        input_histogram.GetBinError(bin_i) * scale_factor
        for bin_i in range(1, input_histogram.nbins())
    ])
    return value, error
Example #12
0
def get_electron_absolute_eta_templates(b_tag):
    global electron_data_file, met_bins
    data = File(electron_data_file)
    hist_template = 'TTbarPlusMetAnalysis/EPlusJets/QCDConversions/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_%s/electron_AbsEta_0btag'

    histograms = []
    for met_bin in met_bins:
        histogram = data.Get(hist_template % met_bin).Clone(
            'qcd_template_absolute_eta_MET_bin_%s_%s' % (met_bin, b_tag))
        histogram.Sumw2()
        #rebin to 0.2 bin width
        current_bin_width = histogram.xwidth(
            1)  # assume all bins have the same size
        rebin = int(0.2 / current_bin_width)
        if rebin > 1:
            histogram.Rebin(rebin)
        n_events = histogram.Integral()
        if not n_events == 0:
            histogram.Scale(1 / n_events)
        histograms.append(histogram)
    return histograms
Example #13
0
def root_file_to_hist(input_filename, hist_templates):

    hists = copy.deepcopy(hist_templates)

    root_file = TFile(input_filename, "read")

    for var in hists.keys():

        index = 0

        for mod_hist in hists[var]:
            hist_name = "{}#{}".format(var, index)

            # Get hist from ROOT file.
            hist = root_file.Get(hist_name)

            mod_hist.replace_hist(hist)

            index += 1

    return hists
@author: kreczko
'''

from rootpy.io import File
from rootpy.plotting import Hist
from rootpy import asrootpy

rootpy_hist = Hist(100, 0, 100, type='F')

rootpy_hist.SetName('hist')
test_file = File('test.root', 'RECREATE')
test_file.mkdir("test")
test_file.cd('test')
rootpy_hist.Write()
test_file.Write()
test_file.Close()

read_file = File('test.root')
folder = read_file.Get('test')
hist = folder.hist
print hist.TYPE
read_file.Close()

hist = None

read_file = File('test.root')
hist = read_file.Get('test/hist')
hist1 = hist.empty_clone(type='D')
print hist1.TYPE
read_file.Close()
Example #15
0
import glob
from collections import OrderedDict
from ROOT import TFile

data = File("Data.root")
mcfiles = ("DYJetsToLL.root", "W+Jets.root", "tbar{t}.root", "VV.root",
           "SingleTop.root")
#mcfiles.remove("Tau.root")
mcfiles = [File(i) for i in mcfiles]

outputHists = OrderedDict()
dirs = set()
for dirpath, dirnames, filenames in data.walk():
    for filename in filenames:
        print dirpath
        h_data = data.Get(path.join(dirpath, filename))
        h_mc = h_data.empty_clone()
        for f in mcfiles:
            h = f.Get(path.join(dirpath, filename))
            h_mc += h
        h_data -= h_mc
        outputHists[dirpath, filename] = h_data
        dirs.add(dirpath)
output_file = root_open("nicolas_lowdpTandOS_QCDsubresult_051920.root", "NEW")
for d in dirs:
    output_file.cd()
    output_file.mkdir(d)

for dirpath, filename in outputHists:
    output_file.cd()
    output_file.cd(dirpath)
Example #16
0
    runLumis = None
    with open(jsonFile) as j:
        runLumis = json.load(j)

    fullRunLumi = []
    for run in runLumis:
        for lumiRange in runLumis[run]:
            for lumi in range(lumiRange[0], lumiRange[1] + 1):
                fullRunLumi.append([int(run), lumi])
    return fullRunLumi


inputFile = File(
    '/hdfs/TopQuarkGroup/run2/ntuples/v22/SingleMuonFullDCS/SingleMuon.root')
treeName = 'nTupleTree/tree'
tree = inputFile.Get(treeName)

jsonForFiltering = '/users/ec6821/lumiScripts/lcr2/lcr2/good_list.txt'
json = readJson(jsonForFiltering)

newFile = File('SingleMuonFiltered.root', 'RECREATE')
newFile.mkdir('nTupleTree')
newFile.cd('nTupleTree')
newTree = tree.CloneTree(0)

print 'Number of events in tree : ', tree.GetEntries()

for event in tree:
    run = event.__getattr__('Event.Run')
    lumi = event.__getattr__('Event.LumiSection')
Example #17
0
        'QCDStudy/PFIsolation_3jets_WithMETCutAndAsymJetCuts_0btag':
        1528. / 0.48,
        'QCDStudy/PFIsolation_3jets_WithMETCutAndAsymJetCuts_1orMoreBtag':
        491. / 0.67,
        'QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03_0btag':
        1398. / 0.61,
        'QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03_1btag':
        504. / 0.79,
        'QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03_2orMoreBtags':
        210 / 0.73
    }

    QCD_rate_estimation.relative_isolation_bias = 0.0
    results = {}
    for histogram_for_estimation in histograms_for_estimation:
        input_histogram = input_file.Get(histogram_for_estimation)
        result = estimate_with_fit_to_relative_isolation(input_histogram)
        results[histogram_for_estimation] = result
        #QCD normalised to lumi
        input_histogram_qcd = File(histogram_files['QCD']).Get(
            histogram_for_estimation.replace('_DR03_', '_'))
        input_histogram_qcd.Scale((1.0 * lumi) / 1959.75)
        input_histogram_qcd.Rebin(10)
        nQCD = input_histogram_qcd.GetBinContent(1)
        #        print nQCD
        #        nQCD = qcd_expected[histogram_for_estimation]
        fQCD = '---'
        if not nQCD == 0:
            fQCD = (result['value']) / nQCD
        print histogram_for_estimation, ':', result['value'], '+-', result[
            'error'], 'QCD: ', nQCD, ',f_QCD:', fQCD
                  help="set path to save tables")

    (options, args) = parser.parse_args()
    input_path = options.path
    output_folder = options.output_plots_folder
    output_pickle_folder = './pickle_files/'
    channel = 'electron'
    centre_of_mass = 7

    make_folder_if_not_exists(output_folder)
    make_folder_if_not_exists(output_pickle_folder)
    output_formats = ['pdf']

    data_histFile = input_path + '/2011/SingleElectron_2011_RunAB_had_leg.root'
    data_input_file = File(data_histFile)
    data_tree = data_input_file.Get('rootTupleTreeEPlusJets/ePlusJetsTree')

    reco_leptons_collection = 'selectedPatElectronsLoosePFlow'
    reco_jet_collection = 'cleanedJetsPFlowEPlusJets'
    trigger_object_lepton = 'TriggerObjectElectronIsoLeg'
    trigger_jet_collection = 'TriggerObjectHadronPFIsoLeg'

    print 'Number of events in data tree: ', data_tree.GetEntries()

    n_lepton_leg_events = 0

    for event in data_tree:
        run_number = event.__getattr__('Event.Run')

        if run_number >= 160404 and run_number <= 165633:
            trigger_name = 'HLT_Ele25_CaloIdVT_TrkIdT_CentralTriJet30'
Example #19
0
    set_root_defaults()

    measurement_config = XSectionConfig(13)
    # # caching of variables for shorter access
    files_for_pdfs = {
        'PDFWeights_%d' % (index - 9):
        File(measurement_config.unfolding_pdfweights[index])
        for index in range(9, 109)
    }
    files_central = File(measurement_config.unfolding_powheg_pythia8)

    path_to_unfolding = 'abs_lepton_eta_combined'
    i = 0
    canvas = TCanvas("CanvasRef", "CanvasTitle", 800, 600)

    folder = files_central.Get(path_to_unfolding)
    h_truth = asrootpy(folder.truthVis.Clone())
    h_measured = asrootpy(folder.measuredVis_without_fakes.Clone())
    h_measured.Draw()

    for k, f in files_for_pdfs.iteritems():
        folder = f.Get(path_to_unfolding)

        # folder = inputfile.Get( '%s_%s' % ( variable, channel ) )
        h_truth = asrootpy(folder.truthVis.Clone())
        h_measured = asrootpy(folder.measuredVis_without_fakes.Clone())
        h_measured.SetMarkerSize(0)
        h_measured.Draw("same")

    folder = files_central.Get(path_to_unfolding)
    h_truth = asrootpy(folder.truthVis.Clone())