Ejemplo n.º 1
0
def getSkimmedEvents(files):

    totalInitialEvents = 0
    for file in files:
        skimHist = get_histogram_from_file( pathToSkimHist, file )
        firstBinContent = list(skimHist.y())[0]
        totalInitialEvents += firstBinContent

    return totalInitialEvents
Ejemplo n.º 2
0
def makePurityStabilityPlots(input_file, histogram, bin_edges, channel, variable, isVisiblePhaseSpace):
    global output_folder, output_formats
 
    hist = get_histogram_from_file( histogram, input_file )
    print "bin edges contents   : ", bin_edges
    new_hist = rebin_2d( hist, bin_edges, bin_edges ).Clone()

    # get_bin_content = hist.ProjectionX().GetBinContent
    purities = calculate_purities( new_hist.Clone() )
    stabilities = calculate_stabilities( new_hist.Clone() )
    # n_events = [int( get_bin_content( i ) ) for i in range( 1, len( bin_edges ) )]
    print "purities contents    : ", purities
    print "stabilities contents : ", stabilities

    hist_stability = value_tuplelist_to_hist(stabilities, bin_edges)
    hist_purity = value_tuplelist_to_hist(purities, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    x_limits = [bin_edges[0], bin_edges[-1]]
    y_limits = [0,1]
    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
#     ax0.grid( True, 'major', linewidth = 2 )
#     ax0.grid( True, 'minor' )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12
    rplt.hist( hist_stability , stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Purity' )

    ax0.set_xlim( x_limits )
    ax0.set_ylim( y_limits )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$ [GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plt.savefig('test.pdf')
    save_as_name = 'purityStability_'+channel + '_' + variable + '_' + str(options.CoM) + 'TeV'
    for output_format in output_formats:
        plt.savefig( output_folder + save_as_name + '.' + output_format )
Ejemplo n.º 3
0
def make_scatter_plot( input_file, histogram, bin_edges, channel, variable, title ):
    global output_folder, output_formats, options
    scatter_plot = get_histogram_from_file( histogram, input_file )

    # Finding max value in scatterplot for colourmap normalisation
    max_bin = scatter_plot.GetMaximumBin()
    max_bin_content = scatter_plot.GetBinContent(max_bin)
    norm = mpl.colors.LogNorm(vmin = 1, vmax = int(max_bin_content+1))

#     scatter_plot.Rebin2D( 5, 5 )

    x_limits = [bin_edges[variable][0], bin_edges[variable][-1]]
    y_limits = x_limits

    x_title = 'Reconstructed $' + variables_latex[variable] + '$ [GeV]'
    y_title = 'Generated $' + variables_latex[variable] + '$ [GeV]'
    save_as_name = channel + '_' + variable + '_' + str(options.CoM) + 'TeV'

    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
#     ax0.grid( True, 'major', linewidth = 2 )
#     ax0.grid( True, 'minor' )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12

    im = rplt.imshow( scatter_plot, axes = ax0, cmap = my_cmap, norm=norm )

    colorbar = plt.colorbar( im )
    colorbar.ax.tick_params( **CMS.axis_label_major )

    # draw lines at bin edges values
    for edge in bin_edges[variable]:
        # do not inclue first and last values
        if ( edge != bin_edges[variable][0] ) and ( edge != bin_edges[variable][-1] ):
            plt.axvline( x = edge, color = 'red', linewidth = 4, alpha = 0.5 )
            plt.axhline( y = edge, color = 'red', linewidth = 4, alpha = 0.5 )

    ax0.set_xlim( x_limits )
    ax0.set_ylim( y_limits )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    plt.xlabel( x_title, CMS.x_axis_title )
    plt.ylabel( y_title, CMS.y_axis_title )
    plt.title( title, CMS.title )

    plt.tight_layout()

    for output_format in output_formats:
        plt.savefig( output_folder + save_as_name + '.' + output_format )
Ejemplo n.º 4
0
 def read_sample(self, sample):
     if self.samples[sample].has_key('input'):
         i = self.samples[sample]['input']
         if isinstance(i, dict):
             i = Input(**self.samples[sample]['input'])
         self.histograms[sample] = i.read()
         return
     input_file = self.samples[sample]['input_file']
     if self.samples[sample].has_key('hist'):
         hist = self.samples[sample]['hist']
         self.histograms[sample] = get_histogram_from_file(
             hist, input_file)
Ejemplo n.º 5
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 __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 = False
        if self.phaseSpace == 'VisiblePS':
            visiblePS = True

        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 )

        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]
Ejemplo n.º 7
0
    def read(self):
        if not self.isValid():
            raise ValueError('Inputs are not valid {0}'.format(self.error))

        if self.hist_name:
            self.hist = get_histogram_from_file(
                self.hist_name, self.file)

        if self.tree_name:
            self.hist = get_histogram_from_tree(
                tree=self.tree_name,
                branch=self.branch,
                weight_branches=self.weight_branches,
                selection=self.selection,
                input_file=self.file,
                **self.kwargs
            )
        self.hist.Scale(self.lumi_scale * self.scale)
        return self.hist