Beispiel #1
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)
Beispiel #2
0
 def addFileList(self, fileList):
     if type(fileList) == type(list()):
         for file in fileList:
             self.files[file] = File(self.basepath + "/" + file + ".root",
                                     "read")
     if isinstance(fileList, dict):
         import itertools
         useList = list(
             itertools.chain.from_iterable(list(fileList.values())))
         for file in useList:
             try:
                 self.files[file] = File(
                     self.basepath + "/" + file + ".root", "read")
             except:
                 yesno = input("file %s is not there continue? [y/n]" %
                               (file))
                 if yesno != "y":
                     import sys
                     sys.exit(1)
                 fileList = {
                     key: value
                     for key, value in list(fileList.items())
                     if file not in value
                 }
         self._joinList = fileList
     self._getGenNumbers()
     self._addToScaledView()
Beispiel #3
0
def convert_unfolding_histograms(file_name,
                                 histograms_to_load=[
                                     'truth',
                                     'fake',
                                     'measured',
                                     'response',
                                     'response_withoutFakes',
                                     'response_without_fakes',
                                     'EventCounter',
                                 ]):

    file_start = Timer()
    print 'Converting', file_name
    histograms = {}
    with File(file_name) as f:
        for path, _, objects in f.walk():
            # keep only unfolding and EventFilter
            if path.startswith('unfolding_') or path == 'EventFilter':
                histograms[path] = {}
                for hist_name in objects:
                    if hist_name in histograms_to_load:
                        hist = f.Get(path + '/' + hist_name).Clone()
                        hist.SetDirectory(0)
                        histograms[path][hist_name] = hist
    new_histograms = {}
    # rebin
    for path, hists in histograms.iteritems():
        new_histograms[path] = {}
        variable = ''
        if not path == 'EventFilter':
            variable = path.split('_')[1]
        for name, hist in hists.iteritems():
            if name == 'EventCounter':
                new_histograms[path][name] = hist.Clone()
            else:
                new_hist = hist.rebinned(bin_edges_vis[variable])
                if 'TH2' in new_hist.class_name():
                    new_hist = new_hist.rebinned(bin_edges_vis[variable],
                                                 axis=1)
                new_histograms[path][name] = new_hist

    # save_to_file
    output = File(file_name.replace('.root', '_asymmetric.root'), 'recreate')
    for path, hists in new_histograms.iteritems():
        directory = output.mkdir(path)
        directory.cd()
        for name, hist in hists.iteritems():
            if name == 'response_withoutFakes':  # fix this name
                hist.Write('response_without_fakes')
            else:
                hist.Write(name)
    output.close()
    secs = file_start.elapsed_time()
    print 'File %s converted in %d seconds' % (file_name, secs)
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
Beispiel #5
0
def get_histogram_from_file( histogram_path, input_file ):
    current_btag, found_btag = find_btag(histogram_path)

    root_file = File( input_file )
    get_histogram = root_file.Get


    if not found_btag or not current_btag in sumations.b_tag_summations.keys():
        root_histogram = get_histogram( histogram_path )
        if not is_valid_histogram( root_histogram, histogram_path, input_file ):
            return
    else:
        listOfExclusiveBins = sumations.b_tag_summations[current_btag]
        exclhists = []

        for excbin in listOfExclusiveBins:
            hist = get_histogram( histogram_path.replace( current_btag, excbin ) )
            if not is_valid_histogram( hist, histogram_path.replace( current_btag, excbin ), input_file ):
                return
            exclhists.append( hist )
        root_histogram = exclhists[0].Clone()

        for hist in exclhists[1:]:
            root_histogram.Add( hist )

    gcd()
    histogram = None
    # change from float to double
    if root_histogram.TYPE == 'F':
        histogram = root_histogram.empty_clone(type='D')
        histogram.Add(root_histogram)
    else:
        histogram = root_histogram.Clone()
    root_file.Close()
    return histogram
Beispiel #6
0
 def addAllFiles(self, tag="", veto=None, regexpr=None, joinName=None):
     if self.basepath == None:
         raise RuntimeError(
             "You must set a basepath to add all files from one directory!")
     if regexpr is not None:
         import re, os
         fileList = [
             f for f in os.listdir(self.basepath + "/")
             if re.search(r'%s' % (regexpr), f)
         ]
     else:
         import glob
         fileList = glob.glob(self.basepath + "/*" + tag + "*.root")
     tmpList = []
     for file in fileList:
         if veto is not None:
             vetoed = False
             for v in veto:
                 if v.lower() in file.split("/")[-1].lower():
                     vetoed = True
             if vetoed:
                 log_plotlib.info("vetoed file: %s" % file)
                 continue
         name = file.split("/")[-1].replace(".root", "")
         self.files[name] = File(file, "read")
         tmpList.append(name)
     self._getGenNumbers()
     self._addToScaledView()
     if joinName is not None:
         if self._joinList is not False:
             self._joinList[joinName] = tmpList
         else:
             self._joinList = OrderedDict()
             self._joinList[joinName] = tmpList
def main():
    config = XSectionConfig(13)
    #     method = 'RooUnfoldSvd'
    method = 'RooUnfoldBayes'
    file_for_unfolding = File(config.unfolding_central, 'read')
    for channel in ['electron', 'muon', 'combined']:
        for variable in bin_edges.keys():
            tau_value = get_tau_value(config, channel, variable)
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=file_for_unfolding,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )
            unfolding = Unfolding(h_truth,
                                  h_measured,
                                  h_response,
                                  h_fakes,
                                  method=method,
                                  k_value=-1,
                                  tau=tau_value)

            unfolded_data = unfolding.closureTest()
            plot_closure(h_truth, unfolded_data, variable, channel,
                         config.centre_of_mass_energy, method)
Beispiel #8
0
def find_maintenance(filename):
    aux_file = File(filename, 'read')
    aux_tree = aux_file.get('t_hk_obox')
    maintenance_start = False
    maintenance_list = []
    gps_time_list = []
    ship_time_list = []
    for entry in aux_tree:
        if entry.obox_is_bad > 0: continue
        if entry.obox_mode.encode('hex') == '04':
            if not maintenance_start:
                maintenance_start = True
            gps_time_list.append(entry.abs_gps_week * 604800 +
                                 entry.abs_gps_second)
            ship_time_list.append(entry.abs_ship_second)
        else:
            if maintenance_start:
                maintenance_start = False
                maintenance_list.append(
                    ((ship_time_list[0] + ship_time_list[-1]) / 2,
                     (gps_time_list[0] + gps_time_list[-1]) / 2))
                gps_time_list = []
                ship_time_list = []
    return [(int(x[0]), "%d:%d" % (int(x[1] / 604800), int(x[1] % 604800)))
            for x in maintenance_list]
Beispiel #9
0
def find_orbitstart(filename):
    LAT_LEN = 500
    lat_deque = deque()
    orbitstart_list = []
    ppd_file = File(filename, 'read')
    ppd_tree = ppd_file.get('t_ppd')
    ready_flag = True
    pre_diff = 0.0
    cur_diff = 0.0
    for entry in ppd_tree:
        if entry.flag_of_pos != 0x55: continue
        lat_deque.append(
            (entry.latitude, entry.ship_time_sec, entry.utc_time_sec))
        if len(lat_deque) < LAT_LEN:
            pre_diff = lat_deque[-1][0] - lat_deque[0][0]
            continue
        else:
            lat_deque.popleft()
            cur_diff = lat_deque[-1][0] - lat_deque[0][0]
        if ready_flag and pre_diff < 0 and cur_diff >= 0:
            orbitstart_list.append(((lat_deque[-1][1] + lat_deque[0][1]) / 2,
                                    (lat_deque[-1][2] + lat_deque[0][2]) / 2))
            ready_flag = False
        if not ready_flag and pre_diff > 0 and cur_diff <= 0:
            ready_flag = True
        pre_diff = cur_diff
    return [(int(x[0]), "%d:%d" % (int(x[1] / 604800), int(x[1] % 604800)))
            for x in orbitstart_list]
Beispiel #10
0
def read_timespan(filename):
    t_file = File(filename, 'read')
    m = ref_time.match(t_file.get('m_utc_span').GetTitle())
    week1, second1, week2, second2 = int(m.group(1)), int(m.group(2)), int(
        m.group(3)), int(m.group(4))
    t_file.close()
    return (week1 * 604800 + second1, week2 * 604800 + second2)
def checkOnMC(unfolding, method):
    global bins, nbins
    RooUnfold.SVD_n_toy = 1000
    pulls = []
    for sub in range(1,9):
        inputFile2 = File('../data/unfolding_merged_sub%d.root' % sub, 'read')
        h_data = asrootpy(inputFile2.unfoldingAnalyserElectronChannel.measured.Rebin(nbins, 'measured', bins))
        nEvents = inputFile2.EventFilter.EventCounter.GetBinContent(1)
        lumiweight = 164.5 * 5050 / nEvents
#        print sub, nEvents
        h_data.Scale(lumiweight)
        doUnfoldingSequence(unfolding, h_data, method, '_sub%d' %sub)
        pull = unfolding.pull_inputErrorOnly()
#        unfolding.printTable()
        pulls.append(pull)
        unfolding.Reset()
    allpulls = []

    for pull in pulls:
        allpulls.extend(pull)
    h_allpulls = Hist(100,-30,30)
    filling = h_allpulls.Fill
    for entry in allpulls:
        filling(entry)
    fit = h_allpulls.Fit('gaus', 'WWS')
    h_fit = asrootpy(h_allpulls.GetFunction("gaus").GetHistogram())
    canvas = Canvas(width=1600, height=1000)
    canvas.SetLeftMargin(0.15)
    canvas.SetBottomMargin(0.15)
    canvas.SetTopMargin(0.10)
    canvas.SetRightMargin(0.05)
    h_allpulls.Draw()
    fit.Draw('same')
    canvas.SaveAs('plots/Pull_allBins_withFit.png')
    
    
    
    plt.figure(figsize=(16, 10), dpi=100)
    rplt.errorbar(h_allpulls, label=r'Pull distribution for all bins',  emptybins=False)
    rplt.hist(h_fit, label=r'fit')
    plt.xlabel('(unfolded-true)/error', CMS.x_axis_title)
    plt.ylabel('entries', CMS.y_axis_title)
    plt.title('Pull distribution for all bins', CMS.title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.legend(numpoints=1)
    plt.savefig('plots/Pull_allBins.png')
    
    #individual bins
    for bin_i in range(nbins):
        h_pull = Hist(100,-30,30)
        for pull in pulls:
            h_pull.Fill(pull[bin_i])
        plt.figure(figsize=(16, 10), dpi=100)
        rplt.errorbar(h_pull, label=r'Pull distribution for bin %d' % (bin_i + 1), emptybins=False)
        plt.xlabel('(unfolded-true)/error', CMS.x_axis_title)
        plt.ylabel('entries', CMS.y_axis_title)
        plt.title('Pull distribution for  bin %d' % (bin_i + 1), CMS.title)
        plt.savefig('Pull_bin_%d.png' % (bin_i + 1))
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)
 def setUp(self):
     f = File('test.root', 'recreate')
     tree = create_test_tree()
     h = create_test_hist()
     h.write()
     tree.write()
     f.write()
     f.Close()
Beispiel #14
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 create_unfolding_pull_data(input_file_name,
                               method,
                               channel,
                               centre_of_mass,
                               variable,
                               n_toy_mc,
                               n_toy_data,
                               output_folder,
                               offset_toy_mc,
                               offset_toy_data,
                               k_value,
                               tau_value=-1,
                               run_matrix=None):
    '''
        Sets up all variables for check_multiple_data_multiple_unfolding
    '''
    timer = Timer()
    input_file = File(input_file_name, 'read')
    folder_template = '{path}/{centre_of_mass}TeV/{variable}/'
    folder_template += '{n_toy_mc}_input_toy_mc/{n_toy_data}_input_toy_data/'
    folder_template += '{vtype}_value_{value}/'

    msg_template = 'Producing unfolding pull data for {variable},'
    msg_template += ' {vtype}-value {value}'
    inputs = {
        'path': output_folder,
        'centre_of_mass': centre_of_mass,
        'variable': variable,
        'n_toy_mc': n_toy_mc,
        'n_toy_data': n_toy_data,
        'vtype': 'k',
        'value': k_value,
    }
    if tau_value >= 0:
        inputs['vtype'] = 'tau'
        inputs['value'] = round(tau_value, 1)

    output_folder = folder_template.format(**inputs)
    make_folder_if_not_exists(output_folder)
    print(msg_template.format(**inputs))
    print('Output folder: {0}'.format(output_folder))

    check_multiple_data_multiple_unfolding(
        input_file,
        method,
        channel,
        variable,
        n_toy_mc,
        n_toy_data,
        output_folder,
        offset_toy_mc,
        offset_toy_data,
        k_value,
        tau_value,
        run_matrix,
    )
    print('Runtime', timer.elapsed_time())
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
Beispiel #17
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
def main():
    config = XSectionConfig(13)
    #     method = 'RooUnfoldSvd'
    method = 'RooUnfoldBayes'
    file_for_data = File(config.unfolding_powheg_herwig, 'read')
    file_for_unfolding = File(config.unfolding_madgraphMLM, 'read')
    for channel in ['electron', 'muon', 'combined']:
        for variable in config.variables:
            tau_value = get_tau_value(config, channel, variable)
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=file_for_unfolding,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )
            h_data_model, h_data, _, _ = get_unfold_histogram_tuple(
                inputfile=file_for_data,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )

            unfolding = Unfolding(h_truth,
                                  h_measured,
                                  h_response,
                                  h_fakes,
                                  method=method,
                                  k_value=-1,
                                  tau=tau_value)

            unfolded_data = unfolding.unfold(h_data)
            plot_bias(h_truth, h_data_model, unfolded_data, variable, channel,
                      config.centre_of_mass_energy, method)
Beispiel #19
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
def can_open_ROOT_file(filename):
    passesCheck = False
    try:
        openFile = File(filename, 'r')
        if openFile:
            passesCheck = True
            openFile.Close()
    except:
        print "Could not open ROOT file"

    return passesCheck
Beispiel #21
0
 def getHistFromTree(self,
                     bins,
                     xmin,
                     xmax,
                     xtitle,
                     cut,
                     value,
                     tree,
                     weight=None):
     from rootpy.plotting import Hist
     import random, string, os
     self.clearHists()
     for f in self.files:
         try:
             _tree = self.files[f].Get(tree)
         except AttributeError as e:
             log_plotlib.warning("No %s in %s" % (tree, f))
             log_plotlib.warning(
                 "Will try without %s, and add an empty hist." % f)
             log_plotlib.warning(e)
             self.hists[f] = Hist(binns, xmin, xmax)
             continue
         self.hists[f] = Hist(bins, xmin, xmax)
         self.hists[f].GetXaxis().SetTitle(xtitle)
         try:
             if weight is None:
                 _tree.Draw(value, selection=cut, hist=self.hists[f])
             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=self.hists[f])
                 tmpFile.Close()
                 os.remove("/tmp/%s.root" % tmpFileName)
         except Exception as e:
             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!")
         self.hists[f].Scale(self._getWeight(f))
         self.hists[f].Sumw2()
     if self._joinList is not False:
         self.joinList(self._joinList)
     for hist in self.hists:
         if hist in self.style:
             self.hists[hist].decorate(**self.style[hist])
def create_toy_mc(input_files, sample, output_folder, n_toy, centre_of_mass, config):
    from dps.utils.file_utilities import make_folder_if_not_exists
    from dps.utils.toy_mc import generate_toy_MC_from_distribution, generate_toy_MC_from_2Ddistribution
    from dps.utils.Unfolding import get_unfold_histogram_tuple
    make_folder_if_not_exists(output_folder)
    output_file_name = get_output_file_name(output_folder, sample, n_toy, centre_of_mass)
    variable_bins = bin_edges_vis.copy()
    with root_open(output_file_name, 'recreate') as f_out:

        input_file_index = 0
        for input_file in input_files:

            input_file_hists = File(input_file)

            for channel in config.analysis_types.keys():
                if channel is 'combined':continue
                for variable in variable_bins:
                    output_dir = f_out.mkdir(str(input_file_index) + '/' + channel + '/' + variable, recurse=True)
                    cd = output_dir.cd
                    mkdir = output_dir.mkdir
                    h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(input_file_hists,
                                                                            variable,
                                                                            channel,
                                                                            centre_of_mass = centre_of_mass,
                                                                            visiblePS = True,
                                                                            load_fakes=False)

                    cd()

                    mkdir('Original')
                    cd ('Original')
                    h_truth.Write('truth')
                    h_measured.Write('measured')
                    h_response.Write('response')

                    for i in range(1, n_toy+1):
                        toy_id = 'toy_{0}'.format(i)
                        mkdir(toy_id)
                        cd(toy_id)
                        # create histograms
                        # add tuples (truth, measured, response) of histograms
                        truth = generate_toy_MC_from_distribution(h_truth)
                        measured = generate_toy_MC_from_distribution(h_measured)
                        response = generate_toy_MC_from_2Ddistribution(h_response)

                        truth.SetName('truth')
                        measured.SetName('measured')
                        response.SetName('response')

                        truth.Write()
                        measured.Write()
                        response.Write()
            input_file_index += 1
Beispiel #23
0
def get_muon_absolute_eta_templates(b_tag):
    global muon_data_file
    data = File(muon_data_file)

    histograms = []
    for met_bin in met_bins:  # same tempalte for all bins
        histogram = data.etaAbs_ge2j_data.Clone(
            'qcd_template_absolute_eta_MET_bin_%s_%s' %
            (met_bin, b_tag))  # already normalised to 1
        histogram.Sumw2()
        histograms.append(histogram)
    return histograms
Beispiel #24
0
 def open_file(self, filename, begin,
               end):  # cut data by utc time, utc_week:utc_second
     self.t_file_name = basename(filename)
     self.t_file_in = File(filename, 'read')
     self.t_tree_ppd = self.t_file_in.get('t_ppd')
     self.t_tree_ppd.create_buffer()
     self.utc_time_span = self.t_file_in.get('m_utc_span').GetTitle()
     m = re.compile(
         r'(\d+):(\d+)\[\d+\] => (\d+):(\d+)\[\d+\]; \d+/\d+').match(
             self.utc_time_span)
     self.first_utc_time_sec = float(m.group(1)) * 604800 + float(
         m.group(2))
     self.last_utc_time_sec = float(m.group(3)) * 604800 + float(m.group(4))
     if begin != 'begin':
         m = re.compile(r'(\d+):(\d+)').match(begin)
         self.begin_utc_time_sec = float(m.group(1)) * 604800 + float(
             m.group(2))
         if self.begin_utc_time_sec - self.first_utc_time_sec < _MIN_DIFF:
             print 'WARNING: begin utc time is out of range: ' + str(
                 self.begin_utc_time_sec - self.first_utc_time_sec)
             return False
     else:
         self.begin_utc_time_sec = -1
     if end != 'end':
         m = re.compile(r'(\d+):(\d+)').match(end)
         self.end_utc_time_sec = float(m.group(1)) * 604800 + float(
             m.group(2))
         if self.last_utc_time_sec - self.end_utc_time_sec < _MIN_DIFF:
             print 'WARNING: end utc time is out of range: ' + str(
                 self.last_utc_time_sec - self.end_utc_time_sec)
             return False
     else:
         self.end_utc_time_sec = -1
     if self.begin_utc_time_sec > 0 and self.end_utc_time_sec > 0 and self.end_utc_time_sec - self.begin_utc_time_sec < _MIN_DIFF:
         print 'WARNING: time span between begin and end utc time is too small: ' + str(
             self.end_utc_time_sec - self.begin_utc_time_sec)
         return False
     if self.begin_utc_time_sec > 0:
         self.begin_entry = self.__find_entry(self.begin_utc_time_sec)
         if self.begin_entry < 0:
             print "WARNING: cannot find begin entry."
             return False
     else:
         self.begin_entry = 0
     if self.end_utc_time_sec > 0:
         self.end_entry = self.__find_entry(self.end_utc_time_sec)
         if self.end_entry < 0:
             print "WARNING: cannot find end entry."
             return False
     else:
         self.end_entry = self.t_tree_ppd.get_entries()
     return True
Beispiel #25
0
def read_timespan(filename, dat_type):
    t_file = File(filename, 'read')
    m = ref_time.match(t_file.get(tnamed_dict[dat_type]).GetTitle())
    week1, second1, week2, second2 = int(m.group(1)), int(m.group(2)), int(
        m.group(3)), int(m.group(4))
    t_file.close()
    time_seconds_begin = week1 * 604800 + second1
    time_seconds_end = week2 * 604800 + second2
    beijing_time_begin = datetime(1980, 1, 6, 0, 0, 0) + timedelta(
        seconds=time_seconds_begin - leap_seconds_dict[dat_type] + 28800)
    beijing_time_end = datetime(1980, 1, 6, 0, 0, 0) + timedelta(
        seconds=time_seconds_end - leap_seconds_dict[dat_type] + 28800)
    return (beijing_time_begin, beijing_time_end)
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
Beispiel #27
0
    def __set_unfolding_histograms__( self ):
        # at the moment only one file is supported for the unfolding input
        files = set( 
            [self.truth['file'],
            self.gen_vs_reco['file'],
            self.measured['file']]
        )
        if len( files ) > 1:
            print "Currently not supported to have different files for truth, gen_vs_reco and measured"
            sys.exit()
            
        input_file = files.pop()
        visiblePS = self.phaseSpace

        t, m, r, f = get_unfold_histogram_tuple( 
            File(input_file),
            self.variable,
            self.channel,
            centre_of_mass = self.centre_of_mass_energy,
            ttbar_xsection=self.measurement_config.ttbar_xsection,
            luminosity=self.measurement_config.luminosity,
            load_fakes = True,
            visiblePS = visiblePS
        )

        self.h_truth = asrootpy ( t )
        self.h_response = asrootpy ( r )
        self.h_measured = asrootpy ( m )
        self.h_fakes = asrootpy ( f )
        self.h_refolded = None

        data_file = self.data['file']
        if data_file.endswith('.root'):
            self.h_data = get_histogram_from_file(self.data['histogram'], self.data['file'])
        elif data_file.endswith('.json') or data_file.endswith('.txt'):
            data_key = self.data['histogram']
            # assume configured bin edges
            edges = []
            edges = reco_bin_edges_vis[self.variable]

            json_input = read_tuple_from_file(data_file)

            if data_key == "": # JSON file == histogram
                self.h_data = value_error_tuplelist_to_hist(json_input, edges)
            else:
                self.h_data = value_error_tuplelist_to_hist(json_input[data_key], edges)
        else:
            print 'Unkown file extension', data_file.split('.')[-1]
def create_unfolding_pull_data(input_file_name,
                               method,
                               channel,
                               centre_of_mass,
                               variable,
                               sample,
                               responseFile,
                               n_toy_data,
                               output_folder,
                               tau_value,
                               run_matrix=None):
    '''
        Sets up all variables for check_multiple_data_multiple_unfolding
    '''
    set_root_defaults(msg_ignore_level=3001)
    timer = Timer()
    input_file = File(input_file_name, 'read')
    folder_template = '{path}/{centre_of_mass}TeV/{variable}/{sample}/'

    msg_template = 'Producing unfolding pull data for {variable},'
    msg_template += ' tau-value {value}'
    inputs = {
        'path': output_folder,
        'centre_of_mass': centre_of_mass,
        'variable': variable,
        'sample': sample,
        'value': round(tau_value, 4),
    }

    h_response = get_response_histogram(responseFile, variable, channel)
    output_folder = folder_template.format(**inputs)
    make_folder_if_not_exists(output_folder)
    print(msg_template.format(**inputs))
    print('Output folder: {0}'.format(output_folder))
    print('Response here :', h_response)
    output_file_name = check_multiple_data_multiple_unfolding(
        input_file,
        method,
        channel,
        variable,
        h_response,
        n_toy_data,
        output_folder,
        tau_value,
    )
    print('Runtime', timer.elapsed_time())

    return output_file_name
Beispiel #29
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
def cleanFile(filename):
    testfile = File(filename, 'update')
    nHistograms = 0
    print "Deleting inclusive bin histograms histograms"
    for folder, emptyThing, histograms in testfile:
        for histogram in histograms:
            currentPath = folder + '/' + histogram
            for btag in btag_bins_inclusive:
                if btag in histogram:
                    nHistograms += 1
                    #                    print 'Deleting:', currentPath
                    testfile.Delete(currentPath + ';*')
    print 'Deleted', nHistograms, 'histograms'
    print 'Closing file', filename
    testfile.Close()
    print 'Closed file'