def plot_fit_results(fit_results, centre_of_mass, channel, variable, k_value, tau_value, output_folder, output_formats, bin_edges): h_mean = Hist(bin_edges, type='D') h_sigma = Hist(bin_edges, type='D') n_bins = h_mean.nbins() assert len(fit_results) == n_bins for i, fr in enumerate(fit_results): h_mean.SetBinContent(i + 1, fr.mean) h_mean.SetBinError(i + 1, fr.meanError) h_sigma.SetBinContent(i + 1, fr.sigma) h_sigma.SetBinError(i + 1, fr.sigmaError) histogram_properties = Histogram_properties() name_mpt = 'pull_distribution_mean_and_sigma_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format(variable, channel, centre_of_mass) histogram_properties.y_axis_title = r'$\mu_{\text{pull}}$ ($\sigma_{\text{pull}}$)' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] value = get_value_title(k_value, tau_value) title = 'pull distribution mean \& sigma for {0}'.format(value) histogram_properties.title = title histogram_properties.y_limits = [-0.5, 2] histogram_properties.xerr = True compare_measurements(models={ 'ideal $\mu$': make_line_hist(bin_edges, 0), 'ideal $\sigma$': make_line_hist(bin_edges, 1) }, measurements={ r'$\mu_{\text{pull}}$': h_mean, r'$\sigma_{\text{pull}}$': h_sigma }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def transfer_values_without_overflow(histogram): if histogram == None: return histogram histogram_new = None if 'TH1' in histogram.class_name(): histogram_new = Hist(list(histogram.xedges()), type='D') n_bins = histogram_new.nbins() for i in range(1, n_bins + 1): histogram_new.SetBinContent(i, histogram.GetBinContent(i)) histogram_new.SetBinError(i, histogram.GetBinError(i)) elif 'TH2' in histogram.class_name(): histogram_new = Hist2D(list(histogram.xedges()), list(histogram.yedges()), type='D') n_bins_x = histogram_new.nbins() n_bins_y = histogram_new.nbins(axis=1) for i in range(1, n_bins_x + 1): for j in range(1, n_bins_y + 1): histogram_new.SetBinContent(i, j, histogram.GetBinContent(i, j)) histogram_new.SetBinError(i, j, histogram.GetBinError(i, j)) else: raise Exception( "Unknown type of histogram in transfer_values_without_overflow") return histogram_new
def create_ratioplot(h, xbound_bin=14, ybound_bin=4): nbins_per_slice = 2 # ybound_bin = 4 ybound_val = h.yedges(ybound_bin + 1) h_vert = Hist(h.GetNbinsX() / nbins_per_slice, h.xaxis.min, h.xaxis.max, title=';|#Delta#phi|;High/low ratio', drawstyle='e1', legendstyle='LEP') for i in range(1, h.GetNbinsX() + 1, nbins_per_slice): lo = h.integral(i, i + nbins_per_slice - 1, 1, ybound_bin) hi = h.integral(i, i + nbins_per_slice - 1, ybound_bin + 1, h.GetNbinsY()) ratio, ratio_err = -1, 0. if lo > 0 and hi > 0: ratio = hi / lo ratio_err = ratio * math.sqrt(1 / lo + 1 / hi) h_vert.Fill(h.xedges(i), ratio) h_vert.SetBinError(int(i / nbins_per_slice) + 1, ratio_err) xax = h_vert.xaxis decorate_axis_pi(xax) nbins_per_slice = 2 # xbound_bin = 14 xbound_val = h.xedges(xbound_bin + 1) h_hori = Hist(h.GetNbinsY() / nbins_per_slice, h.yaxis.min, h.yaxis.max, title=';Iso;High/low ratio', drawstyle='e1', legendstyle='LEP') for i in range(1, h.GetNbinsY() + 1, nbins_per_slice): lo = h.integral(1, xbound_bin, i, i + nbins_per_slice - 1) hi = h.integral(xbound_bin + 1, h.GetNbinsX(), i, i + nbins_per_slice - 1) ratio, ratio_err = -1, 0. if lo > 0 and hi > 0: ratio = hi / lo ratio_err = ratio * math.sqrt(1 / lo + 1 / hi) h_hori.Fill(h.yedges(i), ratio) h_hori.SetBinError(int(i / nbins_per_slice) + 1, ratio_err) return h_vert, h_hori, ybound_val, xbound_val
def get_histograms_from_trees( trees = [], branch = 'var', weightBranch = 'EventWeight', selection = '1', files = {}, verbose = False, nBins = 40, xMin = 0, xMax = 100, ignoreUnderflow = True, ): histograms = {} nHistograms = 0 # Setup selection and weight string for ttree draw weightAndSelection = '( %s ) * ( %s )' % ( weightBranch, selection ) for sample, input_file in files.iteritems(): histograms[sample] = {} for tree in trees: tempTree = tree if 'data' in sample and ( 'Up' in tempTree or 'Down' in tempTree ) : tempTree = tempTree.replace('_'+tempTree.split('_')[-1],'') chain = None; if isinstance( input_file, list ): for f in input_file: chain.Add(f) else: chain = TreeChain(tempTree, [input_file]); weightAndSelection = '( %s ) * ( %s )' % ( weightBranch, selection ) root_histogram = Hist( nBins, xMin, xMax, type='D') chain.Draw(branch, weightAndSelection, hist = root_histogram) if not is_valid_histogram( root_histogram, tree, input_file): return # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it if ignoreUnderflow: root_histogram.SetBinContent(0, 0) root_histogram.SetBinError(0,0) gcd() nHistograms += 1 histograms[sample][tree] = root_histogram.Clone() return histograms
def get_hist(self, typ, wave1, wave2=None): """ Creates a ROOT histogram for the gibten waves and type @param typ: Type of the histogram: intensity: Intensity of wave1 (No wave2 needed) real: Real part of the intereference of wave1 and wave2 imag: Imaginary part of the intereference of wave1 and wave2 phase: Complex phase of the intereference of wave1 and wave2 @type typ: str @param wave1: Number of the first wave @type wave1: int @param wave2: Number of the second wave @type wave2: int @return: Chosen ROOT histogram @rtype: Hist """ mmin, mmax, nbin = self.get_mmin_mmax_nbin() hist = Hist(nbin, mmin, mmax) for bin in self.bins: mass = bin.center() n_bin = hist.FindBin(mass) if typ == "intensity": hist.SetBinContent(n_bin, bin.intens[wave1]) hist.SetBinError(n_bin, bin.errors_intens[wave1]) elif typ == "real": hist.SetBinContent(n_bin, bin.re[wave1][wave2]) hist.SetBinError(n_bin, bin.errors_re[wave1][wave2]) elif typ == "imag": hist.SetBinContent(n_bin, bin.im[wave1][wave2]) hist.SetBinError(n_bin, bin.errors_im[wave1][wave2]) elif typ == "phase": hist.SetBinContent(n_bin, bin.phases[wave1][wave2] * 180. / pi) hist.SetBinError(n_bin, bin.errors_phase[wave1][wave2] * 180. / pi) else: raise KeyError # Type not defined hist.SetTitle("mass-independent") hist.SetName("mass-independent") return hist
def getDataFromFile(variable, whichBinFromFile): dataTemplate = inputTemplates[variable]['data'][whichBinFromFile] nBins = len(inputTemplates[variable]['data'][whichBinFromFile]) h_data = Hist(nBins, 0, nBins, title='data') for bin in range(1, nBins + 1): h_data.SetBinContent(bin, dataTemplate[bin - 1]) pass h_data.Scale(absolute_eta_initialValues['data'][whichBinFromFile][0]) h_data.Sumw2() for bin in range(1, nBins + 1): h_data.SetBinError(bin, sqrt(h_data.GetBinContent(bin))) pass return h_data
def get_bias_corr(region): if region == "ms": bkg_bias_fname = "/lustre/cmswork/dcastrom/projects/hh/april_2017/CMSSW_8_0_25/src/Analysis/hh2bbbb_limit/notebooks/bias_22032018_with_weights_also_mass_cut/BM0/bias_correction_mass_cut_bigset_unscaled.json" bkg_bias_fname = "/lustre/cmswork/dcastrom/projects/hh/april_2017/CMSSW_8_0_25/src/Analysis/hh2bbbb_limit/bias_01062018BM0/bias_correction_mass_cut_bigset_unscaled.json" elif region == "btag": bkg_bias_fname = "/lustre/cmswork/dcastrom/projects/hh/april_2017/CMSSW_8_0_25/src/Analysis/hh2bbbb_limit/notebooks/bms_btagside_err_fixed/BM0/bias_correction_bigset_unscaled.json" elif region == "sig_unfixed": bkg_bias_fname = "/lustre/cmswork/dcastrom/projects/hh/april_2017/CMSSW_8_0_25/src/Analysis/hh2bbbb_limit/notebooks/bias_22032018_with_weights_also_mass_cut/BM0/bias_correction_bigset_unscaled.json" else: bkg_bias_fname = "/lustre/cmswork/dcastrom/projects/hh/april_2017/CMSSW_8_0_25/src/Analysis/hh2bbbb_limit/bias_01062018BM0/bias_correction_bigset_unscaled.json" with open(bkg_bias_fname, "r") as bkg_bias_file: json_dict = json.load(bkg_bias_file) print("using bias file: ", bkg_bias_file) histo = Hist(json_dict['bin_edges']) print histo #hcorr = histo.Clone("h_bias_corrected") #hcorr.Scale(4) #hbias = histo.Clone("h_bias") for n in range(len(json_dict['bias_corr'])): bias = json_dict['bias_corr'][n] var = json_dict['var'][n] bias_unc = json_dict['bias_corr_unc_bs'][n] bias_unc_stat = json_dict['bias_corr_unc_stat'][n] #bkg_pred_initial = hcorr.GetBinContent(n+1) #if var > np.sqrt(bkg_pred_initial): new_bkg_pred_stat = var #else: # new_bkg_pred_stat = np.sqrt(bkg_pred_initial) #new_bkg_pred_tot_unc = np.sqrt(new_bkg_pred_stat**2 + bias_unc**2 + bias_unc_stat**2) new_bkg_pred_tot_unc = np.sqrt(bias_unc**2 + bias_unc_stat**2) histo.SetBinContent(n + 1, bias) histo.SetBinError(n + 1, new_bkg_pred_tot_unc) histo.Scale(0.25) #hcorr.Add(hbias, -1) return histo
''' Created on 29 Oct 2012 @author: kreczko ''' from ROOT import TH1F from rootpy.plotting import Hist import rootpy.plotting.root2matplotlib as rplt import matplotlib.pyplot as plt #updated from http://rootpy.org/qa/questions/80/how-to-create-a-histogram-with-asymmetric-bins bins = [0, 25, 45, 70, 100, 2000] nbins = len(bins) - 1 rootpyhist = Hist(bins) for bin_i in range(nbins): rootpyhist.SetBinContent(bin_i + 1, bin_i + 1) rootpyhist.SetBinError(bin_i + 1, (bin_i + 1) * 0.1) plt.figure(figsize=(16, 10), dpi=100) plt.figure(1) rplt.errorbar(rootpyhist, label='test') plt.xlabel('x') plt.ylabel('y') plt.title('Testing') plt.legend(numpoints=1) plt.axis([bins[0], bins[-1], 0, nbins * 1.2]) plt.savefig('plots/AsymBinsExample.png') print 'Done'
def _project(tree, var, selection='', weight=1.0, bins=None, includeover=False): h = None if var.count(':') == 0: ## Hist (1D) if bins: if isinstance(bins, tuple): assert len(bins) == 3 h = Hist(*bins) elif isinstance(bins, list): h = Hist(bins) else: assert False else: assert False elif var.count(':') == 1: ## Hist2D ## like rootpy, we use a convention where var=x:y, unlike ROOT varx, vary = var.split(':') var = ':'.join([vary, varx]) if bins: if isinstance(bins, tuple): assert len(bins) == 6 h = Hist2D(*bins) elif isinstance(bins, list): ## TODO: support variable bins for Hist2D h = Hist2D(*bins) #assert False else: assert False else: assert False else: assert False assert h # kwargs['hist'] = h ## add the weight to the selection via a TCut weighted_selection = str(selection) if weight and weight != 1.0: weighted_selection = Cut(weighted_selection) weighted_selection = weighted_selection * weight weighted_selection = str(weighted_selection) tree.Draw('%s>>%s' % (var, h.GetName()), weighted_selection) ## print tree.GetSelectedRows() ## debuging # for event in t: # x = getattr(event, var) # h.fill(x) if h: h.SetDirectory(0) # elist = ROOT.gDirectory.Get('elist') # elist.Print('all') if var.count(':') == 0 and includeover: ## include overflow for Hist (1D) nbins = h.GetNbinsX() c1 = h.GetBinContent(nbins) c2 = h.GetBinContent(nbins + 1) e1 = h.GetBinError(nbins) e2 = h.GetBinError(nbins + 1) h.SetBinContent(nbins, c1 + c2) h.SetBinError( nbins, math.sqrt((c1 * e1 * e1 + c2 * e2 * e2) / (c1 + c2)) if c1 + c2 != 0.0 else 0.0) h.SetBinContent(nbins + 1, 0.0) h.SetBinError(nbins + 1, 0.0) return h
bin = h_response.FindBin(12, 12) h_response.SetBinContent(bin, 400) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) bin = h_response.FindBin(20, 20) h_response.SetBinContent(bin, 100) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) bin = h_response.FindBin(50, 50) h_response.SetBinContent(bin, 5) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) # Set errors in all histograms for bin in range(0, len(bins)): h_truth.SetBinError(bin, sqrt(h_truth.GetBinContent(bin))) h_measured.SetBinError(bin, sqrt(h_measured.GetBinContent(bin))) h_data.SetBinError(bin, sqrt(h_data.GetBinContent(bin))) # # Alternatively use histograms from our main analysis # # measurement_config = XSectionConfig( 13 ) # channel = 'electron' # h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = File( measurement_config.unfolding_central, 'read' ), # variable = 'MET', # channel = channel, # met_type = 'patType1CorrectedPFMet', # centre_of_mass = 13, # ttbar_xsection = measurement_config.ttbar_xsection,
h_response = inputFile.unfoldingAnalyserElectronChannel.response_withoutFakes_AsymBins #response_AsymBins # h_measured_new = h_measured - h_fakes # h_response = inputFile.unfoldingAnalyserElectronChannel.response_AsymBins #response_AsymBins nEvents = inputFile.EventFilter.EventCounter.GetBinContent(1) lumiweight = 164.5 * 5050 / nEvents h_truth.Scale(lumiweight) h_measured.Scale(lumiweight) h_fakes.Scale(lumiweight) h_response.Scale(lumiweight) unfolding = Unfolding(h_truth, h_measured, h_response, method = method) #should be identical to # unfolding = Unfolding(h_truth, h_measured, h_response, h_fakes, method = method) #test values for real data input h_data = Hist(bins.tolist()) h_data.SetBinContent(1, 2146) h_data.SetBinError(1, 145) h_data.SetBinContent(2, 3399) h_data.SetBinError(2, 254) h_data.SetBinContent(3, 3723) h_data.SetBinError(3, 69) h_data.SetBinContent(4, 2256) h_data.SetBinError(4, 53) h_data.SetBinContent(5, 1722) h_data.SetBinError(5, 91) checkOnMC(unfolding, method) doUnfoldingSequence(unfolding, h_data, method) print 'Done'
def plot(self): if self._ratio_plot: gs = gridspec.GridSpec(nrows=2, ncols=1, height_ratios=[3, 1], wspace=1.08, hspace=0., bottom=0.12, left=0.18) # gs.update(wspace=0.1, hspace=0.1, left=0.1, right=0.4, bottom=0.1, top=0.9) # gs.update(hspace=0.05) # set the spacing between axes. # gs.update(left=0.05, right=0.48, wspace=0.05) # self._plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) ax0 = self._plt.subplot(gs[0]) ax1 = self._plt.subplot(gs[1]) else: ax0 = self._plt.gca() # Set the logo.lin ax0.add_artist(self.logo_box()) # Set basic plot element formattings. lin self.set_formatting() # Normalize all the histograms. self.normalize_hists() z_indices = range(len(self._hists), 0, -1) if "error" in self._plot_types: z_indices[self._plot_types.index("error")] *= 10 if "theory" in self._plot_types: z_indices[self._plot_types.index("theory")] *= 5 # First, draw the regular "non-ratio" plot. legend_handles = [] for i in range(len(self._hists)): plot_type = self._plot_types[i] if plot_type == 'hist': #print("this is hist") #self._hists[i].hist().SetLineStyle(self._line_styles[i]) plot = self._rplt.hist(self._hists[i].hist(), axes=ax0, zorder=z_indices[i], emptybins=False) plot[1].set_dashes(self._line_styles[i]) legend_handles.append( plot ) # print legend_handles[i][1].get_dashes() elif plot_type == 'error': plot = self._rplt.errorbar(self._hists[i].hist(), axes=ax0, zorder=z_indices[i], emptybins=False, xerr=1, yerr=1, ls='None', marker='o', markersize=10, pickradius=8, capthick=5, capsize=8, elinewidth=5, alpha=1.0) legend_handles.append( plot ) elif plot_type == "theory": if self._x_scale == "log": x_s = np.exp( self._hists[i][0] ) else: x_s = self._hists[i][0] # There are two portions of x. # Find the index where we need to switch. np_correction_index = 0 for ab in range(len(x_s)): if x_s[ab] > self.get_np_correction_boundary(): np_correction_index = ab break # print np_correction_index, self.get_np_correction_boundary() # print x_s y_line_s = self._hists[i][2] y_min_s = self._hists[i][1] y_max_s = self._hists[i][3] # print x_s[ : np_correction_index] # Distribution. ax0.plot(x_s[ : np_correction_index + 1], y_line_s[ : np_correction_index + 1], lw=12, zorder=z_indices[i], color=self._plot_colors[i], ls="dotted") if "Track" not in self._x_label: ax0.plot(x_s[np_correction_index : ], y_line_s[np_correction_index : ], lw=8, zorder=z_indices[i], color=self._plot_colors[i]) ax0.fill_between(x_s[np_correction_index : ], y_max_s[np_correction_index : ], y_min_s[np_correction_index : ], zorder=z_indices[i], where=np.less_equal(y_min_s[np_correction_index : ], y_max_s[np_correction_index : ]), facecolor=self._plot_colors[i], color=self._plot_colors[i], interpolate=True, alpha=0.2, linewidth=0.) else: ax0.plot(x_s[np_correction_index : ], y_line_s[np_correction_index : ], ls="dotted", lw=12, zorder=z_indices[i], color=self._plot_colors[i]) theory_min_interpolate_function = self.extrap1d(interpolate.interp1d(x_s, y_min_s)) theory_line_interpolate_function = self.extrap1d(interpolate.interp1d(x_s, y_line_s)) theory_max_interpolate_function = self.extrap1d(interpolate.interp1d(x_s, y_max_s)) if self._hists[0].x_range() != (0, -1): # This is important for log plots because we don't want to consider the bins that's not visibile while figuring out how many bins to use for theory. # x_s_to_use = filter( lambda x: x >= self._hists[0].x_range()[0], self._plot_points_x_s[self._plot_types.index("error")] ) x_s_to_use = self._plot_points_x_s[self._plot_types.index("error")] else: x_s_to_use = self._plot_points_x_s[self._plot_types.index("error")] theory_extrapolated_min = theory_min_interpolate_function(x_s_to_use) theory_extrapolated_line = theory_line_interpolate_function(x_s_to_use) theory_extrapolated_max = theory_max_interpolate_function(x_s_to_use) # print "the data x points were", (x_s_to_use) if plot_type == "theory": self._plot_points_x_s.append( x_s ) self._plot_points_y_s.append( [] ) elif plot_type == "error": data_points_x = plot[0].get_xdata() data_points_y = plot[0].get_ydata() data_plot_points_x = [] data_plot_points_y = [] for i in range(0, len(data_points_x)): if float(data_points_x[i]) > 0.0: # This is just to ignore the points at the 0th bin. data_plot_points_x.append(data_points_x[i]) data_plot_points_y.append(data_points_y[i]) data_x_errors, data_y_errors = [], [] for x_segment in plot[2][0].get_segments(): data_x_errors.append((x_segment[1][0] - x_segment[0][0]) / 2.) for y_segment in plot[2][1].get_segments(): data_y_errors.append((y_segment[1][1] - y_segment[0][1]) / 2.) self._plot_points_x_s.append( data_plot_points_x ) self._plot_points_y_s.append( data_plot_points_y ) elif plot_type == "hist": bin_width = (self._hists[i].hist().upperbound() - self._hists[i].hist().lowerbound()) / self._hists[i].hist().nbins() if self._x_scale != "log": data_points_x = [x + bin_width / 2 for x in plot[1].get_xdata()][:-1] else: data_points_x = plot[1].get_xdata()[:-1] data_points_y = plot[1].get_ydata() data_plot_points_x = [] data_plot_points_y = [] for i in range(0, len(data_points_x)): if float(data_points_x[i]) > 0.0: # This is just to ignore the points at the 0th bin. data_plot_points_x.append(data_points_x[i]) data_plot_points_y.append(data_points_y[i]) self._plot_points_x_s.append( data_plot_points_x ) self._plot_points_y_s.append( data_plot_points_y ) if self._y_scale == 'log': ax0.set_yscale('log') pass # Ratio plot. if self._ratio_plot: if (type(self._ratio_to_index) == dict) or (type(self._ratio_to_index) == int and self._plot_types[self._ratio_to_index] == "hist" or self._plot_types[self._ratio_to_index] == "error"): # print "ratio to something else" for i in range(len(self._hists)): if type(self._ratio_to_index) == int: denominator_hist = self._hists[self._ratio_to_index].hist() elif type(self._ratio_to_index) == dict: denominator_hist = self._hists[self._ratio_to_index[i]].hist() plot_type = self._plot_types[i] ratio_hist = copy.deepcopy( self._hists[i].hist() ) #print(ratio_hist.lowerbound()) divided_hist = Hist(len(ratio_hist), ratio_hist.lowerbound(), ratio_hist.upperbound()) for k in range(len(ratio_hist)): num_val = ratio_hist.GetBinContent(k) denom_val = denominator_hist.GetBinContent(k) #print(i, num_val, denom_val) num_err = ratio_hist.GetBinError(k) denom_err = denominator_hist.GetBinError(k) if denom_val != 0.0: divided_hist.SetBinContent(k+1, num_val*1.0/denom_val) print(i, num_val*1.0/denom_val) if denom_err != 0.0: divided_hist.SetBinError(k+1, num_err*1.0/denom_val) print(i, num_err*1.0/denom_val) divided_hist.SetColor(self._plot_colors[i]) #ratio_hist.Divide(denominator_hist) if plot_type == 'hist': plot = self._rplt.hist(divided_hist, axes=ax1, zorder=z_indices[i], emptybins=False, lw=8) print("i", i) plot[1].set_dashes(self._line_styles[i]) elif plot_type == 'error': self._rplt.errorbar(divided_hist, axes=ax1, zorder=z_indices[i], emptybins=False, ls='None', marker='o', markersize=10, pickradius=8, capthick=5, capsize=8, elinewidth=5, alpha=1.0) # Ratio plot ends. handles, labels = legend_handles, self._plot_labels handler_map = {} if "theory" in self._plot_types: if "Track" not in self._x_label: th_line, = ax0.plot(range(1), linewidth=8, color='red') th_patch = mpatches.Patch(facecolor='red', alpha=0.2, linewidth=0., edgecolor='red') handles.insert( self._plot_types.index("theory"), (th_patch, th_line)) else: th_line, = ax0.plot(range(1), linewidth=12, ls="dotted", color='red') handles.insert( self._plot_types.index("theory"), (th_line, )) # labels.insert( self._plot_types.index("theory"), self._plot_labels[self._plot_types.index("theory")]) handler_map[th_line ] = HandlerLine2D(marker_pad=0) for i in range(len(handles)): if self._plot_types[i] == "hist": line, = ax0.plot(range(1), linewidth=8, color=self._plot_colors[i], dashes=self._line_styles[i]) handles[i] = line handler_map[line] = HandlerLine2D(marker_pad=0) legend = ax0.legend(handles, labels, frameon=0, fontsize=50, handler_map=handler_map, bbox_to_anchor=self._legend_location[1], loc=self._legend_location[0] ) # legend = ax0.legend(handles, labels, frameon=0, fontsize=60, handler_map=handler_map, bbox_to_anchor=self._legend_location, loc="upper left" ) ax0.add_artist(legend) # Any additional texts. extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0) # ax0.legend([extra]*len(labels), labels, loc=7, frameon=0, borderpad=0.1, fontsize=60, bbox_to_anchor=[1.00, 0.53]) # get the width of your widest label, since every label will need #to shift by this amount after we align to the right shift = max([t.get_window_extent(plt.gcf().canvas.get_renderer()).width for t in legend.get_texts()]) if self._text_outside_the_frame: outside_text = ax0.legend( [extra], ["CMS 2011 Open Data"], frameon=0, borderpad=0, fontsize=50, bbox_to_anchor=(1.0, 1.005), loc='lower right') ax0.add_artist(outside_text) if "Soft Drop" in self._plot_labels[0]: additional_text = self._hists[1].additional_text() else: additional_text = self._hists[0].additional_text() for position, anchor_location, text in additional_text: texts = text.split("\n") additional_info = ax0.legend( [extra] * len(texts), texts, frameon=0, borderpad=0, fontsize=50, bbox_to_anchor=position, loc=anchor_location) if position[0] > 0.30: for t in additional_info.get_texts(): t.set_ha('right') # ha is alias for horizontalalignment t.set_position((shift,0)) # if len() # Axes labels. # ax0.set_xlabel(self._x_label, fontsize=60) # if "$" in self._y_label: if self._y_label[0] == "$": ax0.set_ylabel(self._y_label, fontsize=105, y=0.5, rotation=0, labelpad=120) else: ax0.set_ylabel(self._y_label, fontsize=65, y=0.5, labelpad=50) if self._ratio_plot: ax1.set_xlabel(self._x_label, fontsize=70, labelpad=35) ax1.set_ylabel(self._ratio_label, fontsize=55, labelpad=25) else: ax0.set_xlabel(self._x_label, fontsize=70, labelpad=35) # Axes labels end. self._plt.sca(ax0) self._plt.tick_params(which='major', width=5, length=25, labelsize=70) self._plt.tick_params(which='minor', width=3, length=15) if self._ratio_plot: self._plt.sca(ax1) self._plt.tick_params(which='major', axis='x', width=5, length=25, labelsize=70) self._plt.tick_params(which='major', axis='y', width=5, length=25, labelsize=45) self._plt.tick_params(which='minor', width=3, length=15) if self._ratio_plot: self._plt.gcf().set_size_inches(30, 24, forward=1) else: self._plt.gcf().set_size_inches(30, 24, forward=1) self._plt.sca(ax0) self._plt.autoscale() self._plt.xscale(self._x_scale) self._plt.gca().xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) if self._ratio_plot: self._plt.sca(ax1) self._plt.autoscale() self._plt.xscale(self._x_scale) self._plt.gca().xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) self._plt.sca(ax0) self._plt.gca().get_xaxis().set_ticklabels([]) if self._x_lims[1] == -1: ax0.set_xlim( self._x_lims[0], ax0.get_xlim()[1] ) else: ax0.set_xlim( self._x_lims[0], self._x_lims[1] ) if self._y_lims[1] == -1: ax0.set_ylim( self._y_lims[0], ax0.get_ylim()[1] * 1.125 ) else: # print self._y_lims[0], self._y_lims[1] ax0.set_ylim( self._y_lims[0], self._y_lims[1] ) if self._ratio_plot: ax1.set_xlim( ax0.get_xlim()[0], ax0.get_xlim()[1] ) ax1.set_ylim(0., 2.5) if self._hists[0].axes_label_pi(): plt.sca(ax0) plt.gca().set_xticks( [0, round(0.5*np.pi, 3), round(np.pi, 3), round(1.5*np.pi, 3), round(2*np.pi, 3)] ) plt.gca().set_xticklabels( ["0", "$\pi / 2$", "$\pi$", "$3 \pi / 2$", "$2 \pi$"] ) if self._ratio_plot: plt.sca(ax1) plt.gca().set_xticks( [0, round(0.5*np.pi, 3), round(np.pi, 3), round(1.5*np.pi, 3), round(2*np.pi, 3)] ) plt.gca().set_xticklabels( ["0", "$\pi / 2$", "$\pi$", "$3 \pi / 2$", "$2 \pi$"] ) plt.sca(ax0) plt.gca().set_xticklabels( [] ) if self._x_scale == "log": if self._ratio_plot: self._plt.sca(ax1) # ax1.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e')) ax1.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter(useMathText=False)) self._plt.sca(ax0) # print self._plt.gca().get_xlim() upper_lim = self._plt.gca().get_xlim()[1] lower_lim = self._plt.gca().get_xlim()[0] # print lower_lim if lower_lim < 0.01: denominations = [9] # 1 + 9 = 10. else: denominations = [1, 3, 5] # 1 + 1 = 2; 2 + 3 = 5; 5 + 5 = 10. multiplier = lower_lim count = 0 x_ticks = [lower_lim] # print abs(x_ticks[-1] - upper_lim) > 1e-6 if float(upper_lim) <= 5.: while abs(x_ticks[-1] - upper_lim) > 1e-6: x_ticks.append( x_ticks[-1] + denominations[count] * multiplier ) # print x_ticks[-1] + denominations[count] * multiplier if count == (len(denominations) - 1): count = 0 multiplier = x_ticks[-1] else: count += 1 # print x_ticks[-1] else: # print abs(x_ticks[-1] - upper_lim) # while abs(x_ticks[-1] - upper_lim) > 20: # x_ticks.append( x_ticks[-1] + denominations[count] * multiplier ) # # print x_ticks[-1] + denominations[count] * multiplier # if count == 2: # count = 0 # multiplier = x_ticks[-1] # else: # count += 1 pass x_ticks = [100, 200, 500, 1000, 2000] # print x_ticks self._plt.xticks(x_ticks) if self._ratio_plot: self._plt.sca(ax1) self._plt.xticks(x_ticks) if self._ratio_plot: self._plt.sca(ax1) self._plt.gca().get_yaxis().set_ticks( [ x for x in self._plt.gca().get_yaxis().get_majorticklocs() if x < 2.5 and x > 0.] ) # Minor ticks. if not self._x_scale == "log": if len(ax0.get_xaxis().get_majorticklocs()) >= 2: factor = abs(ax0.get_xaxis().get_majorticklocs()[1] - ax0.get_xaxis().get_majorticklocs()[0]) / 10 ax0.xaxis.set_minor_locator(MultipleLocator(factor)) if self._ratio_plot: ax1.xaxis.set_minor_locator(MultipleLocator(factor)) if not self._y_scale == "log": if len(ax0.get_yaxis().get_majorticklocs()) >= 2: factor = abs(ax0.get_yaxis().get_majorticklocs()[1] - ax0.get_yaxis().get_majorticklocs()[0]) / 5 ax0.yaxis.set_minor_locator(MultipleLocator(factor)) if self._ratio_plot: ax1.yaxis.set_minor_locator(MultipleLocator(0.1)) def convert_to_axes_coordinates(x, y): x_bounds, y_bounds = ax0.get_xlim(), ax0.get_ylim() return (x - x_bounds[0]) / (x_bounds[1] - x_bounds[0]), (y - y_bounds[0]) / (y_bounds[1] - y_bounds[0]) # Any possible markers. for marker in self._mark_regions: #print marker if "Area" in self._x_label: ax0.plot([marker[0], marker[0]], [2, marker[1] ], zorder=9999, color='red', linewidth=8, linestyle="dashed") else: #print([marker[0], marker[0]]) #print( [ax0.get_ylim()[0], marker[1] ]) ax0.plot([marker[0], marker[0]], [ax0.get_ylim()[0], marker[1]], zorder=9999, color='red', linewidth=8, linestyle="dashed") unit_x_minor_tick_length = abs(ax0.get_xaxis().get_majorticklocs()[1] - ax0.get_xaxis().get_majorticklocs()[0]) / 10 unit_y_minor_tick_length = abs(ax0.get_yaxis().get_majorticklocs()[1] - ax0.get_yaxis().get_majorticklocs()[0]) / 5 # print ax0.get_yaxis().get_majorticklocs()[1], ax0.get_yaxis().get_majorticklocs()[0] if marker[2] != None: # Arrows. if marker[2] == "right": if self._y_scale == 'log': #ax0.arrow(marker[0], fy(marker[3]), marker[4], 0., head_width=unit_y_minor_tick_length, head_length=unit_x_minor_tick_length, fc='red', ec='red', transform=ax0.transAxes) #ax0.arrow(marker[0], fy(marker[3]), marker[4], 0., fc='red', ec='red', transform=ax0.transAxes) #ax0.arrow(marker[0], marker[3], marker[4], 0., head_width=unit_y_minor_tick_length, head_length=unit_x_minor_tick_length, fc='red', ec='red') x_o, y_o = convert_to_axes_coordinates(marker[0], marker[3]) delta_x, delta_y = convert_to_axes_coordinates(marker[4], 0) print(x_o, y_o, delta_x, delta_y) ax0.arrow(x_o, y_o, delta_x, delta_y, fc='red', ec='red', head_length=0.02, head_width=0.03, transform=ax0.transAxes) else: ax0.arrow(marker[0], marker[3], marker[4], 0., head_width=unit_y_minor_tick_length, head_length=unit_x_minor_tick_length, fc='red', ec='red') elif marker[2] == "left": if self._y_scale == 'log': x_o, y_o = convert_to_axes_coordinates(marker[0], marker[3]) delta_x, delta_y = convert_to_axes_coordinates(marker[4], 0) ax0.arrow(marker[0], marker[3], marker[4], 0., head_width=unit_y_minor_tick_length, head_length=unit_x_minor_tick_length, fc='red', ec='red', transform=ax0.transAxes) else: ax0.arrow(marker[0], marker[3], marker[4], 0., head_width=unit_y_minor_tick_length, head_length=unit_x_minor_tick_length, fc='red', ec='red') if "Area" in self._x_label: ax0.text(marker[0], 1., "$\pi \mathrm{R}^2$", color='red', fontsize=75, horizontalalignment='center') self._plt.gcf().set_snap(True)
os.path.basename(f) + ".hists") if (args.doNotOverwrite and os.path.isfile(out_file_path)): continue out_file = root_open(out_file_path, "RECREATE") tree = in_file.get(args.treename) if (args.hists != None and len(args.hists) > 0): out_file.cd() for hist in args.hists: in_file.Get(hist).Write() if args.copy_mbj_cutflow: cf = in_file.Get('cut_flow') cf2 = Hist(1, 0, 1, name='cutflow') cf2.Sumw2() cf2.SetEntries(cf.GetBinContent(1)) cf2.SetBinContent(1, cf.GetBinContent(2)) cf2.SetBinError( 1, np.sqrt(cf.GetBinContent(1)) * cf.GetBinContent(2) / cf.GetBinContent(1)) allDir = os.path.join(args.outdir, 'all') out_file.mkdir(allDir, recurse=True) try: out_file.cd(allDir) except: pass cf2.Write() else: out_file = root_open(f, "UPDATE") tree = out_file.get(args.treename) # create tdirectory and cd into it print "\tmaking tdirectory {0}".format(args.outdir)
sci_trigger_r_obj.t_trigger.get_entry(i) if not sci_trigger_r_obj.t_trigger.abs_gps_valid: continue trigger_hist.fill((sci_trigger_r_obj.t_trigger.abs_gps_week - sci_trigger_r_obj.start_week) * 604800 + (sci_trigger_r_obj.t_trigger.abs_gps_second - sci_trigger_r_obj.start_second)) for j in xrange(25): if sci_trigger_r_obj.t_trigger.trig_accepted[j]: modules_hist[j].fill((sci_trigger_r_obj.t_trigger.abs_gps_week - sci_trigger_r_obj.start_week) * 604800 + (sci_trigger_r_obj.t_trigger.abs_gps_second - sci_trigger_r_obj.start_second)) for i in xrange(1, nbins + 1): trigger_hist.SetBinContent(i, trigger_hist.GetBinContent(i) / args.bw) trigger_hist.SetBinError(i, trigger_hist.GetBinError(i) / args.bw) for j in xrange(25): modules_hist[j].SetBinContent( i, modules_hist[j].GetBinContent(i) / args.bw) modules_hist[j].SetBinError(i, modules_hist[j].GetBinError(i) / args.bw) print ' - drawing ... ' y_max = 0 for i in xrange(25): if modules_hist[i].GetMaximum() > y_max: y_max = modules_hist[i].GetMaximum() for i in xrange(25): modules_hist[i].SetMaximum(y_max * 1.1) canvas_trigger = Canvas(1000, 800,
def __return_histogram(self, d_hist_info, ignoreUnderflow=True, useQCDControl=False, useQCDSystematicControl=False): ''' Takes basic histogram info and returns histo. Maybe this can move to ROOT_utilities? ''' from rootpy.io.file import File from rootpy.plotting import Hist from dps.utils.hist_utilities import fix_overflow f = d_hist_info['input_file'] tree = d_hist_info['tree'] qcd_tree = d_hist_info["qcd_control_region"] qcd_tree_for_normalisation = d_hist_info["qcd_normalisation_region"] var = d_hist_info['branch'] bins = d_hist_info['bin_edges'] lumi_scale = d_hist_info['lumi_scale'] scale = d_hist_info['scale'] weights = d_hist_info['weight_branches'] selection = d_hist_info['selection'] if useQCDControl: # replace SR tree with CR tree if useQCDSystematicControl: tree = qcd_tree_for_normalisation else: tree = qcd_tree # Remove the Lepton reweighting for the datadriven qcd (SF not derived for unisolated leptons) for weight in weights: if 'Electron' in weight: weights.remove(weight) elif 'Muon' in weight: weights.remove(weight) weights = "*".join(weights) # Selection will return a weight 0 or 1 depending on whether event passes selection weights_and_selection = '( {0} ) * ( {1} )'.format(weights, selection) scale *= lumi_scale root_file = File(f) root_tree = root_file.Get(tree) root_histogram = Hist(bins) # Draw histogram of var for selection into root_histogram root_tree.Draw(var, selection=weights_and_selection, hist=root_histogram) root_histogram.Scale(scale) # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it if ignoreUnderflow: root_histogram.SetBinContent(0, 0) root_histogram.SetBinError(0, 0) # Fix overflow (Moves entries from overflow bin into last bin i.e. last bin not |..| but |--> ) root_histogram = fix_overflow(root_histogram) root_file.Close() return root_histogram
def main(parameters): input_files = parameters.input_files tree_name = parameters.input_tree means = Hist(4*len(input_files), 0, 4*len(input_files), name='mean values') rmss = Hist(4*len(input_files), 0, 4*len(input_files), name='rms values') cov_means = Hist(2*len(input_files), 0, 2*len(input_files), name='covariance mean values') cov_rmss = Hist(2*len(input_files), 0, 2*len(input_files), name='covariance rms values') means.SetYTitle('Variance mean [cm^{2}]') rmss.SetYTitle('Variance RMS [cm^{2}]') cov_means.SetYTitle('Covariance mean [cm^{2}]') cov_rmss.SetYTitle('Covariance RMS [cm^{2}]') with root_open(parameters.output_file, 'recreate') as output_file: for i, (name, input_file_name) in enumerate(input_files): means.GetXaxis().SetBinLabel(i*4+1, name+' x') means.GetXaxis().SetBinLabel(i*4+2, name+' y') means.GetXaxis().SetBinLabel(i*4+3, name+' u') means.GetXaxis().SetBinLabel(i*4+4, name+' v') rmss.GetXaxis().SetBinLabel(i*4+1, name+' x') rmss.GetXaxis().SetBinLabel(i*4+2, name+' y') rmss.GetXaxis().SetBinLabel(i*4+3, name+' u') rmss.GetXaxis().SetBinLabel(i*4+4, name+' v') cov_means.GetXaxis().SetBinLabel(i*2+1, name+' x/y') cov_rmss.GetXaxis().SetBinLabel(i*2+1, name+' x/y') cov_means.GetXaxis().SetBinLabel(i*2+2, name+' u/v') cov_rmss.GetXaxis().SetBinLabel(i*2+2, name+' u/v') with root_open(input_file_name) as input_file: tree = input_file.Get(tree_name) moment_2nd_x, moment_2nd_y, moment_2nd_xy, moment_2nd_u, moment_2nd_v, moment_2nd_uv = shape_distribution(tree, parameters.log_weights) print name print 'x variance Mu =', moment_2nd_x.GetMean(), '+/-', moment_2nd_x.GetMeanError(), 'RMS =', moment_2nd_x.GetRMS(), '+/-', moment_2nd_x.GetRMSError() print 'y variance Mu =', moment_2nd_y.GetMean(), '+/-', moment_2nd_y.GetMeanError(), 'RMS =', moment_2nd_y.GetRMS(), '+/-', moment_2nd_y.GetRMSError() print 'u variance Mu =', moment_2nd_u.GetMean(), '+/-', moment_2nd_u.GetMeanError(), 'RMS =', moment_2nd_u.GetRMS(), '+/-', moment_2nd_u.GetRMSError() print 'v variance Mu =', moment_2nd_v.GetMean(), '+/-', moment_2nd_v.GetMeanError(), 'RMS =', moment_2nd_v.GetRMS(), '+/-', moment_2nd_v.GetRMSError() print 'x-y covariance Mu =', moment_2nd_xy.GetMean(), '+/-', moment_2nd_xy.GetMeanError(), 'RMS =', moment_2nd_xy.GetRMS(), '+/-', moment_2nd_xy.GetRMSError() print 'u-v covariance Mu =', moment_2nd_uv.GetMean(), '+/-', moment_2nd_uv.GetMeanError(), 'RMS =', moment_2nd_uv.GetRMS(), '+/-', moment_2nd_uv.GetRMSError() # fill mean values of variances means.SetBinContent(i*4+1, moment_2nd_x.GetMean()) means.SetBinError(i*4+1, moment_2nd_x.GetMeanError()) means.SetBinContent(i*4+2, moment_2nd_y.GetMean()) means.SetBinError(i*4+2, moment_2nd_y.GetMeanError()) means.SetBinContent(i*4+3, moment_2nd_u.GetMean()) means.SetBinError(i*4+3, moment_2nd_u.GetMeanError()) means.SetBinContent(i*4+4, moment_2nd_v.GetMean()) means.SetBinError(i*4+4, moment_2nd_v.GetMeanError()) # fill rms values of variances rmss.SetBinContent(i*4+1, moment_2nd_x.GetRMS()) rmss.SetBinError(i*4+1, moment_2nd_x.GetRMSError()) rmss.SetBinContent(i*4+2, moment_2nd_y.GetRMS()) rmss.SetBinError(i*4+2, moment_2nd_y.GetRMSError()) rmss.SetBinContent(i*4+3, moment_2nd_u.GetRMS()) rmss.SetBinError(i*4+3, moment_2nd_u.GetRMSError()) rmss.SetBinContent(i*4+4, moment_2nd_v.GetRMS()) rmss.SetBinError(i*4+4, moment_2nd_v.GetRMSError()) # fill mean values of covariances cov_means.SetBinContent(i*2+1, moment_2nd_xy.GetMean()) cov_means.SetBinError(i*2+1, moment_2nd_xy.GetMeanError()) cov_means.SetBinContent(i*2+2, moment_2nd_uv.GetMean()) cov_means.SetBinError(i*2+2, moment_2nd_uv.GetMeanError()) # fill rms values of covariances cov_rmss.SetBinContent(i*2+1, moment_2nd_xy.GetRMS()) cov_rmss.SetBinError(i*2+1, moment_2nd_xy.GetRMSError()) cov_rmss.SetBinContent(i*2+2, moment_2nd_uv.GetRMS()) cov_rmss.SetBinError(i*2+2, moment_2nd_uv.GetRMSError()) # write variance distributions moment_2nd_x.SetName(moment_2nd_x.GetName()+'_'+name) moment_2nd_y.SetName(moment_2nd_y.GetName()+'_'+name) moment_2nd_xy.SetName(moment_2nd_xy.GetName()+'_'+name) moment_2nd_u.SetName(moment_2nd_u.GetName()+'_'+name) moment_2nd_v.SetName(moment_2nd_v.GetName()+'_'+name) output_file.cd() moment_2nd_x.Write() moment_2nd_y.Write() moment_2nd_xy.Write() moment_2nd_u.Write() moment_2nd_v.Write() means.Write() rmss.Write() cov_means.Write() cov_rmss.Write()