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
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 )
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) # 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, vmin=0.001) 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)
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_data_from_JSON(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 make_scatter_plot( input_file, histogram, channel, variable, title ): global output_folder, output_formats, options scatter_plot = get_histogram_from_file( histogram, input_file ) # scatter_plot.Rebin2D( 5, 5 ) x_limits = [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, vmin = 1 ) 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 )
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() t, m, r, _ = 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, ) self.h_truth = t self.h_response = r self.h_measured = m 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 = [] if self.phaseSpace == 'FullPS': edges = bin_edges[self.variable] elif self.phaseSpace == 'VisiblePS': edges = bin_edges_vis[self.variable] json_input = read_data_from_JSON(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 makePurityStabilityPlots(input_file, histogram, bin_edges, channel, variable, isVisiblePhaseSpace): global output_folder, output_formats hist = get_histogram_from_file(histogram, input_file) # get_bin_content = hist.ProjectionX().GetBinContent purities = calculate_purities(hist.Clone()) stabilities = calculate_stabilities(hist.Clone()) # n_events = [int( get_bin_content( i ) ) for i in range( 1, len( bin_edges ) )] 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)