def plot_weights(input_filename): weights = parse_weights(input_filename) hist = Hist(np.logspace(math.log(float(1e-8), math.e), math.log(1, math.e), 25, base=np.e)) hist.fill_array(weights) # plt.hist(weights, bins=200, normed=1) rplt.hist(hist) print max(weights) # print "Total number of events = ", len(weights) plt.xscale("log") plt.yscale("log") plt.autoscale() plt.xlabel("Weight") plt.ylabel("# of Events") plt.savefig("plots/weights.pdf") plt.clf()
def test_uniform(): hist = Hist(10, 0, 1) assert_true(hist.uniform()) hist = Hist2D(10, 0, 1, [1, 10, 100]) assert_false(hist.uniform()) assert_true(hist.uniform(axis=0))
def get_posterior(fn): logger.info("Loading pseudoexperiments from file %s" % fn) fi = ROOT.TFile(fn) fi.ls() t = fi.Get("unfolded") print "Tree:", t #Make a histogram with a nonspecific binning just to have a memory address hi = ROOT.TH1D() #ROOT wants a pointer, use AddressOf t.SetBranchAddress(brname, ROOT.AddressOf(hi)) #Initialize the histogram t.GetEntry(0) #Now we can have a proper histogram with the right bins binning = get_binning(hi) hi = Hist(binning, type='D') t.SetBranchAddress(brname, ROOT.AddressOf(hi)) #The last element in the binning is not the low edge, but the top edge, #which is needed to create the histogram nbins = len(binning)-1 nentries = t.GetEntries() #Create a vector for the bin contents. Rows - histos, cols - bins bins = np.empty((nentries, nbins), dtype='f') i=0 #The asymmetry distribution hasym = Hist(100, -2, 2, title='posterior PE') #Find the bin index where costheta=0 (center) center = find_bin_idx(list(hi.x()), 0) logger.info("Central bin index = %d" % center) #Fill the bin contents vector and the asymmetry histogram by looping over the events for ev in t: bins[i] = np.array(list(hi.y()), dtype='f') i += 1 #print asymmetry(hi, center), list(hi.y()) hasym.Fill(asymmetry(hi, center)) logger.info("Looped over %d entries" % i) #The distribution of the unfolded costheta posterior = Hist(get_binning(hi), type='D', title='PE') logger.debug("Posterior binning A = %s" % (str(get_binning(posterior)))) #Fill the posterior histogram for i in range(nbins): posterior.SetBinContent(i+1, np.mean(bins[:,i])) if nentries>1: posterior.SetBinError(i+1, np.std(bins[:,i])) #In case of only 1 unfolded histo (data), take the stat. error. #This is not used at the moment else: posterior.SetBinError(i+1, hi.GetBinError(i)) logger.debug("Posterior binning B = %s" % (str(get_binning(posterior)))) return posterior, hasym, binning
def test_overflow_underflow(): h1d = Hist(10, 0, 1) h1d.Fill(-1) h1d.Fill(2) assert_equal(h1d.underflow(), 1) assert_equal(h1d.overflow(), 1) h2d = Hist2D(10, 0, 1, 10, 0, 1) h2d.Fill(-1, .5) h2d.Fill(2, .5) assert_equal(h2d.underflow()[h2d.axis(1).FindBin(.5)], 1) assert_equal(h2d.overflow()[h2d.axis(1).FindBin(.5)], 1) h2d.Fill(.5, -1) h2d.Fill(.5, 2) assert_equal(h2d.underflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) assert_equal(h2d.overflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.Fill(-1, .5, .5) h3d.Fill(2, .5, .5) assert_equal(h3d.underflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal(h3d.overflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, -1, .5) h3d.Fill(.5, 2, .5) assert_equal(h3d.underflow(axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal(h3d.overflow(axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, .5, -1) h3d.Fill(.5, .5, 2) assert_equal(h3d.underflow(axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1) assert_equal(h3d.overflow(axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1)
def make_hist(n=5): hi = Hist(n, -1, 1) hi.Sumw2() for i in range(hi.nbins()): hi[i] = i hi.SetBinError(i+1, math.sqrt(hi[i])) return hi
def plot_bias_in_all_bins(biases, mean_bias, centre_of_mass, channel, variable, tau_value, output_folder, output_formats, bin_edges): h_bias = Hist(bin_edges, type='D') n_bins = h_bias.nbins() assert len(biases) == n_bins for i, bias in enumerate(biases): h_bias.SetBinContent(i+1, bias) histogram_properties = Histogram_properties() name_mpt = 'bias_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format( variable, channel, centre_of_mass ) histogram_properties.y_axis_title = 'Bias' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [0, 10] histogram_properties.xerr = True compare_measurements( models={ 'Mean bias':make_line_hist(bin_edges, mean_bias) }, measurements={ 'Bias': h_bias }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def test_uniform_binned(): h = Hist([-5, -4, -1, 2, 5]) assert_true(not h.uniform()) h.FillRandom('gaus') h_uniform = h.uniform_binned() assert_true(h_uniform.uniform()) assert_equal(h_uniform.entries, h.entries) assert_equal(h.integral(), h_uniform.integral())
def test_uniform_binning(): a = Hist([0, 10, 100, 10000, 100000]) for v in [5, 20, 200, 20000]: a.Fill(v, gauss(10, 5)) b = uniform_binning(a) assert_equal(list(a), list(b)) assert_equal(list(a.yerrh()), list(b.yerrh()))
def check_sample_category(analysis, sample, category): clf = analysis.get_clf(category, mass=125, load=True) scores, weights = sample.scores( clf, category, TARGET_REGION, systematics=False)['NOMINAL'] hist = Hist(20, scores.min() - 1E-5, scores.max() + 1E-5) fill_hist(hist, scores, weights) assert_almost_equal(sample.events(category, TARGET_REGION)[1].value, hist.integral(), 3)
def test_integral(): h = Hist(10, 0, 1) h.FillRandom('gaus', 100) h[0].value = 2 h[-1].value = 4 assert_equal(h.integral(), 100) assert_equal(h.integral(overflow=True), 106) assert_equal(h.integral(xbin1=1, overflow=True), 104) assert_equal(h.integral(xbin2=-2, overflow=True), 102)
def setUp( self ): # create histograms self.h1 = Hist( 100, 0, 100, title = '1D' ) self.h2 = Hist2D( 60, 40, 100, 60, 40, 100 ) self.simple = Hist( 100, 0, 100, title = '1D' ) # fill the histograms with our distributions map( self.h1.Fill, x1 ) self.h2.fill_array( np.random.multivariate_normal( mean = ( 50, 50 ), cov = np.arange( 4 ).reshape( 2, 2 ), size = ( int( 1E6 ), ) ) ) self.bins = [40, 45, 50, 60, 65, 70, 75, 80, 100] # rebin them self.h2_rebinned = self.h2.rebinned( self.bins, axis = 0 ) self.h2_rebinned = self.h2_rebinned.rebinned( self.bins, axis = 1 ) self.h2_rebinned_2 = rebin_2d( self.h2, self.bins, self.bins ) # we only test symmetric bins for now self.n_bins_x = 5 self.n_bins_y = 5 # only entries in diagonals, p = 1, s = 1 for all bins self.diagonals = Hist2D( self.n_bins_x, 0, 10, self.n_bins_y, 0, 10 ) # this creates # [0, 0, 0, 0, 1], # [0, 0, 0, 1, 0], # [0, 0, 1, 0, 0], # [0, 1, 0, 0, 0], # [1, 0, 0, 0, 0] for i in range( 1, self.n_bins_x + 1 ): self.diagonals.SetBinContent( i, i, 1 ) # the following should result in # [0, 0, 2], # [0, 2, 0], # [1, 0, 0] self.bin_edges_nice = [0, 2, 6, 10] self.result_nice = [1, 2, 2] # the following should result in # [0, 0, 0, 2], # [0, 0, 2, 0] # [0, 1, 0, 0] # [0, 0, 0, 0] self.bin_edges_out_of_bound = [-2, 0, 2, 6, 20] self.result_out_of_bound = [0, 1, 2, 2] # the following should result in # [0, 0, 2], # [0, 1, 0], # [2, 0, 0] self.bin_edges_not_on_boundaries = [0, 3.5, 6, 20] self.result_not_on_boundaries = [2, 1, 2] for i in range(100): self.simple.Fill(i, 1)
def test_bounds(): h = Hist(10, 0, 1) assert_equal(h.bounds(), (0, 1)) h = Hist2D(10, 0, 1, 10, 1, 2) assert_equal(h.bounds(axis=0), (0, 1)) assert_equal(h.bounds(axis=1), (1, 2)) h = Hist3D(10, 0, 1, 10, 1, 2, 10, 2, 3) assert_equal(h.bounds(axis=0), (0, 1)) assert_equal(h.bounds(axis=1), (1, 2)) assert_equal(h.bounds(axis=2), (2, 3))
def test_merge_bins(): h1d = Hist(10, 0, 1) h1d.FillRandom('gaus', 1000) h1d_merged = h1d.merge_bins([(0, -1)]) assert_equal(h1d_merged.nbins(0), 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') h3d_merged = h3d.merge_bins([(1, 3), (-4, -2)], axis=1) assert_equal(h3d.GetEntries(), h3d_merged.GetEntries()) assert_equal(h3d.GetSumOfWeights(), h3d_merged.GetSumOfWeights()) assert_equal(h3d_merged.nbins(1), 6) h = Hist(4, 0, 1) h.Fill(-1) h.Fill(2) h.Fill(0.8, 2) h.Fill(0.5, 1) # invalid ranges assert_raises(ValueError, h.merge_bins, [(0, 0)]) assert_raises(ValueError, h.merge_bins, [(0, 1, 2)]) assert_raises(ValueError, h.merge_bins, [(2, 1)]) assert_raises(ValueError, h.merge_bins, [(-2, 1)]) # overlapping windows assert_raises(ValueError, h.merge_bins, [(0, 1), (1, 2)]) assert_equal(list(h.y(overflow=True)), [1., 0., 0., 1., 2., 1.]) assert_equal(list(h.merge_bins([(1, 2)]).y(overflow=True)), [1., 0., 1., 2., 1.]) assert_equal(list(h.merge_bins([(-3, -2)]).y(overflow=True)), [1., 0., 0., 3., 1.] ) assert_equal(list(h.merge_bins([(-2, -1)]).y(overflow=True)), [1., 0., 0., 1., 3., 0.]) assert_equal(list(h.merge_bins([(0, 1), (-2, -1)]).y(overflow=True)), [0., 1., 0., 1., 3., 0.]) assert_equal(list(h.merge_bins([(0, 1)]).merge_bins([(-2, -1)]).y(overflow=True)), [0., 1., 0., 1., 3., 0.]) assert_equal(list(h.merge_bins([(1, 2), (3, 4)]).y(overflow=True)), [1., 0., 3., 1.])
def computeHisto(self, name, bins): if not name in self.data or not 'Data' in self.data[name]: raise StandardError('Cannot find data for {NAME}'.format(NAME=name)) values = self.data[name]['Data'][0] weights = self.data[name]['Data'][1] histo = Hist(bins, type='F') histo.Sumw2() histo.fill_array(values, weights=weights) histo.Scale(1,'width') #self.data[name]['Histo'] = histo self.data[name]['Histo_'+str(hash(str(bins)))] = histo
def test_compatibility(): a = Hist(10, 0, 1) b = Hist(10, 0, 1) c = Hist(10, 1, 2) d = Hist2D(10, 0, 1, 10, 0, 1) assert_true(a.compatible(a)) assert_true(a.compatible(b)) assert_true(a.compatible(c)) assert_false(a.compatible(c, check_edges=True)) assert_false(a.compatible(d))
def do_stuff(file_name, plot=True): tiq_data = TIQData(file_name) NFRAMES = 100 LFRAMES = 1024 tiq_data.read(nframes=NFRAMES, lframes=LFRAMES, sframes=1) center = tiq_data.center # do fft ff, pp, _ = tiq_data.get_fft() freq_lower = center + ff[0] / 1e6 freq_upper = center + ff[-1] / 1e6 # create hist h1 = Hist(NFRAMES * LFRAMES, freq_lower, freq_upper, name='h1', title='Frequency Spectrum', drawstyle='hist', legendstyle='F', fillstyle='/', linecolor='green') for i in range(len(pp)): h1.set_bin_content(i, pp[i]) # set visual attributes h1.GetXaxis().SetTitle('Freuqency [MHz]') h1.linecolor = 'green' # h1.fillcolor = 'green' # h1.fillstyle = '/' if plot: # plot c = Canvas(name='c1', width=700, height=500) c.set_left_margin(0.15) c.set_bottom_margin(0.15) c.set_top_margin(0.10) c.set_right_margin(0.05) c.toggle_editor() c.toggle_event_status() c.toggle_tool_bar() # c.set_crosshair() h1.Draw() # create the legend legend = Legend([h1], pad=c, header='Header', leftmargin=0.05, rightmargin=0.5) legend.Draw() # wait for key press wait()
def test_merge_bins(): h1d = Hist(10, 0, 1) h1d.FillRandom('gaus', 1000) h1d_merged = h1d.merge_bins([(0, -1)]) assert_equal(h1d_merged.nbins(0), 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') h3d_merged = h3d.merge_bins([(1, 3), (-4, -2)], axis=1) assert_equal(h3d.GetEntries(), h3d_merged.GetEntries()) assert_equal(h3d.GetSumOfWeights(), h3d_merged.GetSumOfWeights()) assert_equal(h3d_merged.nbins(1), 6)
def test_cdf_errors(self): hi = Hist(list(range(20))) hi.Sumw2() for i in range(1, hi.nbins()+1): hi.SetBinContent(i, i-1) hi.SetBinError(i, math.sqrt(hi[i-1])) self.print_hist(hi) hi_up = cdf_errors(hi, +1) hi_down = cdf_errors(hi, -1) print "up" self.print_hist(hi_up) print "down" self.print_hist(hi_down)
def test_edgesl(): h = Hist([1, 2, 3, 4]) assert_equal(list(h.xedgesl()), [1, 2, 3]) assert_equal(list(h.xedgesl(overflow=True)), [float('-inf'), 1, 2, 3, 4]) assert_equal(h.xedgesl(0), float('-inf')) assert_equal(h.xedgesl(-1), 4) assert_equal(h.xedgesl(4), 4) # wrap around assert_equal(h.xedgesl(5), float('-inf')) for i in range(1, h.nbins()): assert_equal(h.xedgesl(i), i)
def test_edgesh(): h = Hist([1, 2, 3, 4]) assert_equal(list(h.xedgesh()), [2, 3, 4]) assert_equal(list(h.xedgesh(overflow=True)), [1, 2, 3, 4, float('inf')]) assert_equal(h.xedgesh(0), 1) assert_equal(h.xedgesh(-1), float('inf')) assert_equal(h.xedgesh(4), float('inf')) # wrap around assert_equal(h.xedgesh(5), 1) for i in range(1, h.nbins()): assert_equal(h.xedgesh(i), i + 1)
def test_edges(): h = Hist([1, 2, 3, 4]) assert_equal(list(h.xedges()), [1, 2, 3, 4]) assert_equal(list(h.xedges(overflow=True)), [float('-inf'), 1, 2, 3, 4, float('inf')]) assert_equal(h.xedges(0), float('-inf')) assert_equal(h.xedges(-1), float('inf')) assert_equal(h.xedges(5), float('inf')) # wrap around assert_equal(h.xedges(6), float('-inf')) for i in range(1, h.nbins() + 1): assert_equal(h.xedges(i), i)
def test_rebinning(): h1d = Hist(100, 0, 1) h1d.FillRandom('gaus') assert_equal(h1d.rebinned(2).nbins(0), 50) assert_equal(h1d.rebinned((2,)).nbins(0), 50) assert_equal(h1d.rebinned([0, .5, 1]).nbins(0), 2) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') assert_equal(h3d.rebinned(2).nbins(0), 5) new = h3d.rebinned((2, 5, 1)) assert_equal(new.nbins(0), 5) assert_equal(new.nbins(1), 2) assert_equal(new.nbins(2), 10) new = h3d.rebinned([0, 5, 10], axis=1) assert_equal(new.nbins(1), 2)
def rate_test(et_values, event_runs, et_cuts): events = {} for et,(run_event) in zip(et_values, event_runs): event = tuple(run_event) if event in events: if et>events[event]: events[event] = et else: events[event] = et histo = Hist(256, -0.5, 255.5) for event,et in events.items(): histo.Fill(et) total_events = histo.integral(overflow=True) rates = [] for et_cut in et_cuts: bin = histo.GetXaxis().FindBin(et_cut) pass_events = histo.integral(bin, overflow=True) rates.append([et_cut, float(pass_events)/float(total_events)*nbunches*c_light/lhc_length*1.e-3]) return np.array(rates, dtype=np.float64)
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 mean_abs_pull = 0 for i, fr in enumerate(fit_results): mean_abs_pull += abs(fr.mean) 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) mean_abs_pull /= n_bins 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] histogram_properties.legend_location = (0.98, 0.48) value = get_value_title(k_value, tau_value) title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [-2, 2] histogram_properties.xerr = True compare_measurements( models={ # 'mean $|\mu|$':make_line_hist(bin_edges,mean_abs_pull), '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 staged_score(self, X, y, sample_weight, n_estimators=-1): """ calculate maximum signal significance """ bins = 50 for p in self.staged_predict_proba(X, n_estimators=n_estimators): scores = p[:,-1] # weighted mean accuracy y_pred = scores >= .5 acc = np.average((y_pred == y), weights=sample_weight) min_score, max_score = scores.min(), scores.max() b_hist = Hist(bins, min_score, max_score + 0.0001) s_hist = b_hist.Clone() scores_s, w_s = scores[y==1], sample_weight[y==1] scores_b, w_b = scores[y==0], sample_weight[y==0] # fill the histograms s_hist.fill_array(scores_s, w_s) b_hist.fill_array(scores_b, w_b) # reverse cumsum #bins = list(b_hist.xedges())[:-1] s_counts = np.array(s_hist) b_counts = np.array(b_hist) S = s_counts[::-1].cumsum()[::-1] B = b_counts[::-1].cumsum()[::-1] # S / sqrt(S + B) s_sig = np.divide(list(S), np.sqrt(list(S + B))) #max_bin = np.argmax(np.ma.masked_invalid(significance)) #+ 1 #max_sig = significance[max_bin] #max_cut = bins[max_bin] s_sig_max = np.max(np.ma.masked_invalid(s_sig)) yield s_sig_max * acc
def test_integral_error(): h = Hist(1, 0, 1) h.FillRandom('gaus') ref_integral, ref_error = h.integral(error=True) h1 = Hist(10, 0, 1) h1.FillRandom('gaus') integral, error = h1.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error) h2 = Hist2D(10, 0, 1, 10, 0, 1) h2.FillRandom(F2('x+y')) integral, error = h2.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error) h3 = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3.FillRandom(F3('x+y+z')) integral, error = h3.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error)
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 setUp( self ): # create histograms self.h1 = Hist( 60, 40, 100, title = '1D' ) self.h2 = Hist2D( 60, 40, 100, 100, 40, 140 ) # fill the histograms with our distributions map( self.h1.Fill, x1 ) self.h2.fill_array( np.random.multivariate_normal( mean = ( 100, 140 ), cov = np.arange( 4 ).reshape( 2, 2 ), size = ( int( 1E6 ), ) ) )
def add_variable(self, variable, bins=[]): from rootpy.plotting import Hist if variable in self.keys(): logger.warn('Variable {0} already exists!') return hist_names = [] add_name = hist_names.append for puBinLower, puBinUpper in pairwise(self._pileUpBins): for region in six.iterkeys(self._regions): name = '{0}_{1}_pu{2}To{3}'.format(variable, region, puBinLower, puBinUpper) if not self[puBinLower][variable][region]: add_name(name) self[puBinLower][variable][region] = Hist(bins, name=name) logger.debug('Created {0} histograms: {1}'.format( len(hist_names), ', '.join(hist_names)))
def test_chain_draw(): if sys.version_info[0] >= 3: raise SkipTest("Python 3 support not implemented") chain = TreeChain('tree', FILE_PATHS) hist = Hist(100, 0, 1) chain.draw('a_x', hist=hist) assert_equal(hist.Integral() > 0, True) assert_equal(hist.GetEntries(), 300) # check that Draw can be repeated hist2 = Hist(100, 0, 1) chain.draw('a_x', hist=hist2) assert_equal(hist.Integral(), hist2.Integral()) # draw into a graph graph = chain.draw("a_x:a_y") assert_true(isinstance(graph, _GraphBase)) assert_equal(len(graph), chain.GetEntries()) assert_equal(len(graph), 300)
def computeHisto(self, name, bins): if not name in self.data or not 'Data' in self.data[name]: raise StandardError( 'Cannot find data for {NAME}'.format(NAME=name)) values = self.data[name]['Data'][0] weights = self.data[name]['Data'][1] histo = Hist(bins, type='F') histo.Sumw2() histo.fill_array(values, weights=weights) histo.Scale(1, 'width') #self.data[name]['Histo'] = histo self.data[name]['Histo_' + str(hash(str(bins)))] = histo
def test_lhcb(): style = get_style('LHCb') with style: canvas = Canvas() hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") ROOT.gRandom.SetSeed() for i in xrange(1000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(80.) hpx.Draw() LHCb_label("R", "preliminary") if INTERACTIVE: wait()
def test_cmstdr(): style = get_style('CMSTDR') with style: canvas = Canvas() hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") ROOT.gRandom.SetSeed() for i in xrange(1000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(100.) hpx.Draw() CMS_label("Testing 2050", sqrts=100) if INTERACTIVE: wait()
def getHistFromTreeParallel(self, bins, xmin, xmax, xtitle, cut, value, tree, weight=None): from rootpy.plotting import Hist import random, string, os import multiprocessing self.clearHists() q = [] 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) filename = str(self.files[f].GetPath()).split(":")[0] q.append([filename, tree, value, cut, self.hists[f], weight, f]) p = multiprocessing.Pool(2) results = p.map_async(parallelTreeWorker, q) _results = results.get() for r in _results: self.hists[r[1]] = asrootpy(r[0]) for f in self.files: 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 apply_remove_window(hist, window): """ Remove a window of bins from a histogram """ low, high = window keep_bins = [] for bin in hist.bins(): if ((low < hist.xedgesl(bin.idx) < high) or (low < hist.xedgesh(bin.idx) < high) or (low < bin.x < high)): continue keep_bins.append(bin.idx) hist_window = Hist(len(keep_bins), 0, len(keep_bins), type=hist.TYPE, name=hist.name + '_window') for idx_window, idx in enumerate(keep_bins): hist_window[idx_window + 1] = hist[idx] return hist_window
def draw_stuff(): ROOT.TH1.AddDirectory(ROOT.kFALSE) #TH1.SetDirectory(0) f = root_open( "/eos/cms/store/group/dpg_dt/comm_dt/dtRootple2015/DTTree_Run259685.root", "r") f2 = root_open("tmp.root", "RECREATE") for path, dirs, objects in f.walk(): print(objects) tree = f.DTTree #for branch in f.DTTree.branchnames: # print branch canvas = Canvas() hist = Hist(100, 0.0, 1E3, name='h' + '_' + '_IPchi2', type='D') #tree.Draw('Ndigis', '', '', hist) selection = "digi_wheel==-1 & digi_station==4 & digi_sector==4 & digi_sl==1 & digi_layer==1 & (digi_time < 300 | digi_time > 700)" # & digi_wire=='wire0'" h = tree.Draw('timestamp', selection=selection, create_hist=True) #h.SetDirectory(0) canvas.SaveAs("test.png")
def add_variable(self, variable, vtype='position'): bins = ResolutionCollection.BINS['default'] if vtype in ResolutionCollection.BINS: bins = ResolutionCollection.BINS[vtype] if variable in self.keys(): logger.warn('Variable {0} already exists!') return hist_names = [] add_name = hist_names.append for puBinLower, puBinUpper in pairwise(self._pileUpBins): for region in six.iterkeys(self._regions): name = '{0}_{1}_pu{2}To{3}'.format(variable, region, puBinLower, puBinUpper) if not self[puBinLower][variable][region]: add_name(name) self[puBinLower][variable][region] = Hist(bins, name=name) logger.debug('Created {0} histograms: {1}'.format( len(hist_names), ', '.join(hist_names)))
def events(self, category=None, cuts=None, weighted=False, force_reopen=False): selection = Cut(self._cuts) if category is not None: selection &= self.cuts(category) if cuts is not None: selection &= cuts if weighted and self.weight_field is not None: if isinstance(self.weight_field, (list, tuple)): for w in self.weight_field: selection *= w else: selection *= self.weight_field return self.draw_helper(Hist(1, 0.5, 1.5), '1', selection, force_reopen=force_reopen)
def check_events(analysis, sample, category, region): clf = analysis.get_clf(category, mass=125, load=True) scores, weights = sample.scores( clf, category, region, systematics=False)['NOMINAL'] rec = sample.merged_records( category, region) sample_events = sample.events(category, region)[1].value hist = Hist(5, scores.min() - 1, scores.max() + 1) fill_hist(hist, scores, weights) clf_events = hist.integral() # test events consistency assert_equal(weights.shape[0], rec['weight'].shape[0]) assert_array_equal(weights, rec['weight']) assert_almost_equal(clf_events, weights.sum(), 1) assert_almost_equal(sample_events, rec['weight'].sum(), 1) assert_almost_equal(sample_events, clf_events, 1) # test draw_array hist = Hist(1, -1000, 1000) sample.draw_array({'tau1_charge': hist}, category, region) assert_almost_equal(hist.integral(), sample_events, 1) # test scaling orig_scale = sample.scale sample.scale *= 2. scores, weights = sample.scores( clf, category, region, systematics=False)['NOMINAL'] hist.Reset() fill_hist(hist, scores, weights) scale_clf_events = hist.integral() assert_almost_equal(scale_clf_events, weights.sum(), 1) assert_almost_equal(scale_clf_events, 2. * clf_events, 1) scale_sample_events = sample.events(category, region)[1].value assert_almost_equal(scale_sample_events, 2. * sample_events, 1) sample.scale = orig_scale
def make_root_1D_hist(data, bins, x_min, x_max, fill_style='solid', fill_color='green', linecolor='green', linewidth=0): h = Hist(bins, x_min, x_max) map(h.Fill, data) h.fillstyle = 'solid' h.fillcolor = 'white' h.linecolor = 'blue' h.linewidth = 1 return h
def test_rebinning(): h1d = Hist(100, 0, 1) h1d.FillRandom('gaus') assert_equal(h1d.rebinned(2).nbins(0), 50) assert_equal(h1d.rebinned((2, )).nbins(0), 50) assert_equal(h1d.rebinned([0, .5, 1]).nbins(0), 2) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') assert_equal(h3d.rebinned(2).nbins(0), 5) new = h3d.rebinned((2, 5, 1)) assert_equal(new.nbins(0), 5) assert_equal(new.nbins(1), 2) assert_equal(new.nbins(2), 10) new = h3d.rebinned([0, 5, 10], axis=1) assert_equal(new.nbins(1), 2)
def SlaveBegin(self, tree): print self.__class__.__module__+": SlaveBegin" zvtx_nbins, zvtx_low, zvtx_high = self.zvtx_bins # Count events of the following categories: valid_mc_valid_rec, # valid_mc_invalid_rec, invalid_mc_valid_rec, both_invalid self.valid_counter = Hist(4, 0, 4, name='MC_rec_discrepancy', title='Discrepancy between event validation of MCrec and MCtruth at thresh {}'.format(str(self.pt_threshold))) self.valid_counter.Sumw2() self.GetOutputList().Add(self.valid_counter) self.mean_pts = [] for i in range(0,4): self.mean_pts.append(Profile(10, 0, 100, name="mean_pt_case_{}".format(str(i)), title="mean pt for case {}".format(str(i)))) self.mean_pts[-1].Sumw2() self.GetOutputList().Add(self.mean_pts[-1]) # Helper histograms to find bins self._bin_zvtx = TH1I('bin_zvtx', 'used to find z vtx', zvtx_nbins, zvtx_low, zvtx_high)
def study1DVariable(tree, variable, channel, bins, variableName='', treeSuffix=''): # Storage for histograms and fitted m(W) binnedHists = {} binned_mw = {} variableLabel = variable if variableName: variableLabel = variableName # Loop over all bins for bin in bins: # Get bin limits downBinLimit = bins[bin][0] upBinLimit = bins[bin][1] # Make histogram for this bin binnedHists[bin] = Hist(80, 40, 500) # Work out selection cut = '%s >= %f && %s < %f' % (variable, downBinLimit, variable, upBinLimit) # Draw histgoram tree.Draw('mjj', cut, hist=binnedHists[bin]) # Perform fit and store fitted m(W) binned_mw[bin] = fitHistogram(binnedHists[bin], channel, variableLabel, bin, treeSuffix=treeSuffix) # Return histograms and fitted m(W) return binnedHists, binned_mw
def __init__(self, name, files): self.hists = { 'pfPtRes' : Hist(41,-.3,.3), 'tunePPtRes' : Hist(41,-.3,.3), 'resCompare' : Hist(31,-.2,.2), 'pfPtResUncor' : Hist(41,-.3,.3), 'tunePPtResUncor' : Hist(41,-.3,.3), 'resCompareUncor' : Hist(31,-.2,.2), 'resCompareBothCor' : Hist(31,-.2,.2), } self.hists2D = { 'pfPtResVsPt' : Hist2D(43,140.,1000.,41,-.15,.15), 'tunePPtResVsPt' : Hist2D(43,140.,1000.,41,-.15,.15), 'resCompareVsPt' : Hist2D(43,140.,1000.,21,-.1,.1), } if isinstance(files, str): self.files = files.split(',') else: self.files = files self.name = name
def rate_test(et_values, event_runs, et_cuts): events = {} for et, (run_event) in zip(et_values, event_runs): event = tuple(run_event) if event in events: if et > events[event]: events[event] = et else: events[event] = et histo = Hist(256, -0.5, 255.5) for event, et in events.items(): histo.Fill(et) total_events = histo.integral(overflow=True) rates = [] for et_cut in et_cuts: bin = histo.GetXaxis().FindBin(et_cut) pass_events = histo.integral(bin, overflow=True) rates.append([ et_cut, float(pass_events) / float(total_events) * nbunches * c_light / lhc_length * 1.e-3 ]) return np.array(rates, dtype=np.float64)
def test_overflow_underflow(): h1d = Hist(10, 0, 1) h1d.Fill(-1) h1d.Fill(2) assert_equal(h1d.underflow(), 1) assert_equal(h1d.overflow(), 1) h2d = Hist2D(10, 0, 1, 10, 0, 1) h2d.Fill(-1, .5) h2d.Fill(2, .5) assert_equal(h2d.underflow()[h2d.axis(1).FindBin(.5)], 1) assert_equal(h2d.overflow()[h2d.axis(1).FindBin(.5)], 1) h2d.Fill(.5, -1) h2d.Fill(.5, 2) assert_equal(h2d.underflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) assert_equal(h2d.overflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.Fill(-1, .5, .5) h3d.Fill(2, .5, .5) assert_equal( h3d.underflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal( h3d.overflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, -1, .5) h3d.Fill(.5, 2, .5) assert_equal( h3d.underflow( axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal( h3d.overflow(axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, .5, -1) h3d.Fill(.5, .5, 2) assert_equal( h3d.underflow( axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1) assert_equal( h3d.overflow(axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1)
def work(self): # get argument values local = self.args.local syst_terms = self.args.syst_terms datatype = self.metadata.datatype year = self.metadata.year verbose = self.args.student_verbose very_verbose = self.args.student_very_verbose redo_selection = self.args.redo_selection nominal_values = self.args.nominal_values # get the dataset name dsname = os.getenv('INPUT_DATASET_NAME', None) if dsname is None: # attempt to guess dsname from dirname if self.files: dsname = os.path.basename(os.path.dirname(self.files[0])) # is this a signal sample? # if so we will also keep some truth information in the output below is_signal = datatype == datasets.MC and ( '_VBFH' in dsname or '_ggH' in dsname or '_ZH' in dsname or '_WH' in dsname or '_ttH' in dsname) log.info("DATASET: {0}".format(dsname)) log.info("IS SIGNAL: {0}".format(is_signal)) # is this an inclusive signal sample for overlap studies? is_inclusive_signal = is_signal and '_inclusive' in dsname # is this a BCH-fixed sample? (temporary) is_bch_sample = 'r5470_r4540_p1344' in dsname if is_bch_sample: log.warning("this is a BCH-fixed r5470 sample") # onfilechange will contain a list of functions to be called as the # chain rolls over to each new file onfilechange = [] count_funcs = {} if datatype != datasets.DATA: # count the weighted number of events if local: def mc_weight_count(event): return event.hh_mc_weight else: def mc_weight_count(event): return event.mc_event_weight count_funcs = { 'mc_weight': mc_weight_count, } # three instances of the pileup reweighting tool are created to write # out the nominal, high and low pileup weights pileup_tool = None pileup_tool_high = None pileup_tool_low = None if local: # local means running on the skims, the output of this script # running on the grid if datatype == datasets.DATA: # merge the GRL fragments merged_grl = goodruns.GRL() def update_grl(student, grl, name, file, tree): grl |= str( file.Get('Lumi/%s' % student.metadata.treename).GetString()) onfilechange.append((update_grl, ( self, merged_grl, ))) if datatype == datasets.DATA: merged_cutflow = Hist(1, 0, 1, name='cutflow', type='D') else: merged_cutflow = Hist(2, 0, 2, name='cutflow', type='D') def update_cutflow(student, cutflow, name, file, tree): # record a cut-flow year = student.metadata.year datatype = student.metadata.datatype cutflow[1].value += file.cutflow_event[1].value if datatype != datasets.DATA: cutflow[2].value += file.cutflow_event_mc_weight[1].value onfilechange.append((update_cutflow, ( self, merged_cutflow, ))) else: # get pileup reweighting tool pileup_tool = get_pileup_reweighting_tool(year=year, use_defaults=True) pileup_tool_high = get_pileup_reweighting_tool(year=year, use_defaults=True, systematic='high') pileup_tool_low = get_pileup_reweighting_tool(year=year, use_defaults=True, systematic='low') if datatype not in (datasets.EMBED, datasets.MCEMBED): # merge TrigConfTrees metadirname = '%sMeta' % self.metadata.treename trigconfchain = ROOT.TChain('%s/TrigConfTree' % metadirname) map(trigconfchain.Add, self.files) metadir = self.output.mkdir(metadirname) metadir.cd() trigconfchain.Merge(self.output, -1, 'fast keep') self.output.cd() if datatype == datasets.DATA: # merge GRL XML strings merged_grl = goodruns.GRL() for fname in self.files: with root_open(fname) as f: for key in f.Lumi.keys(): merged_grl |= goodruns.GRL(str( key.ReadObj().GetString()), from_string=True) lumi_dir = self.output.mkdir('Lumi') lumi_dir.cd() xml_string = ROOT.TObjString(merged_grl.str()) xml_string.Write(self.metadata.treename) self.output.cd() self.output.cd() # create the output tree model = get_model(datatype, dsname, prefix=None if local else 'hh_', is_inclusive_signal=is_inclusive_signal) log.info("Output Model:\n\n{0}\n\n".format(model)) outtree = Tree(name=self.metadata.treename, model=model) if local: tree = outtree else: tree = outtree.define_object(name='tree', prefix='hh_') tree.define_object(name='tau', prefix='tau_') tree.define_object(name='tau1', prefix='tau1_') tree.define_object(name='tau2', prefix='tau2_') tree.define_object(name='truetau1', prefix='truetau1_') tree.define_object(name='truetau2', prefix='truetau2_') tree.define_object(name='jet1', prefix='jet1_') tree.define_object(name='jet2', prefix='jet2_') tree.define_object(name='jet3', prefix='jet3_') mmc_objects = [ tree.define_object(name='mmc0', prefix='mmc0_'), tree.define_object(name='mmc1', prefix='mmc1_'), tree.define_object(name='mmc2', prefix='mmc2_'), ] for mmc_obj in mmc_objects: mmc_obj.define_object(name='resonance', prefix='resonance_') trigger_emulation = TauTriggerEmulation(year=year, passthrough=local or datatype != datasets.MC or year > 2011, count_funcs=count_funcs) if not trigger_emulation.passthrough: onfilechange.append((update_trigger_trees, ( self, trigger_emulation, ))) trigger_config = None if datatype not in (datasets.EMBED, datasets.MCEMBED): # trigger config tool to read trigger info in the ntuples trigger_config = get_trigger_config() # update the trigger config maps on every file change onfilechange.append((update_trigger_config, (trigger_config, ))) # define the list of event filters if local and syst_terms is None and not redo_selection: event_filters = None else: tau_ntrack_recounted_use_ntup = False if year > 2011: # peek at first tree to determine if the extended number of # tracks is already stored with root_open(self.files[0]) as test_file: test_tree = test_file.Get(self.metadata.treename) tau_ntrack_recounted_use_ntup = ('tau_out_track_n_extended' in test_tree) event_filters = EventFilterList([ averageIntPerXingPatch( passthrough=(local or year < 2012 or datatype != datasets.MC), count_funcs=count_funcs), PileupTemplates( year=year, passthrough=(local or is_bch_sample or datatype not in (datasets.MC, datasets.MCEMBED)), count_funcs=count_funcs), RandomSeed(datatype=datatype, count_funcs=count_funcs), RandomRunNumber(tree=tree, datatype=datatype, pileup_tool=pileup_tool, passthrough=local, count_funcs=count_funcs), PileupReweight( year=year, tool=pileup_tool, tool_high=pileup_tool_high, tool_low=pileup_tool_low, tree=tree, passthrough=(local or (datatype not in (datasets.MC, datasets.MCEMBED))), count_funcs=count_funcs), TruthMatching(passthrough=datatype == datasets.DATA, count_funcs=count_funcs), JetIsPileup( passthrough=(local or year < 2012 or datatype not in (datasets.MC, datasets.MCEMBED)), count_funcs=count_funcs), HiggsPT(year=year, tree=tree, passthrough=not is_signal or local, count_funcs=count_funcs), MCWeight(datatype=datatype, tree=tree, passthrough=local or datatype == datasets.DATA, count_funcs=count_funcs), ClassifyInclusiveHiggsSample( tree=tree, passthrough=not is_inclusive_signal, count_funcs=count_funcs), ]) # set the event filters self.filters['event'] = event_filters # peek at first tree to determine which branches to exclude with root_open(self.files[0]) as test_file: test_tree = test_file.Get(self.metadata.treename) ignore_branches = test_tree.glob(hhbranches.REMOVE, exclude=hhbranches.KEEP) ignore_branches_output = test_tree.glob( hhbranches.REMOVE_OUTPUT, exclude=hhbranches.KEEP_OUTPUT) # initialize the TreeChain of all input files chain = TreeChain(self.metadata.treename, files=self.files, ignore_branches=ignore_branches, events=self.events, onfilechange=onfilechange, filters=event_filters, cache=True, cache_size=50000000, learn_entries=100) if local: copied = [ 'EventNumber', ] hh_buffer = TreeBuffer() buffer = TreeBuffer() for name, value in chain._buffer.items(): if name.startswith('hh_'): hh_buffer[name[3:]] = value elif name in copied: buffer[name] = value outtree.set_buffer(hh_buffer, create_branches=False, visible=True) outtree.set_buffer(buffer, create_branches=True, visible=False) else: # additional decorations on existing objects if year > 2011 and datatype in (datasets.MC, datasets.MCEMBED): class Decorations(TreeModel): jet_ispileup = stl.vector('bool') chain.set_buffer(Decorations(), create_branches=True) # include the branches in the input chain in the output tree # set branches to be removed in ignore_branches outtree.set_buffer(chain._buffer, ignore_branches=ignore_branches + ignore_branches_output, create_branches=True, ignore_duplicates=True, transfer_objects=True, visible=False) # define tree objects define_objects(chain, year) # create the MMC mmc = mass.MMC(year=year) # report which packages have been loaded externaltools.report() self.output.cd() # The main event loop # the event filters above are automatically run for each event and only # the surviving events are looped on for event in chain: if local and syst_terms is None and not redo_selection: outtree.Fill() continue # sort taus and jets in decreasing order by pT event.taus.sort(key=lambda tau: tau.pt, reverse=True) event.jets.sort(key=lambda jet: jet.pt, reverse=True) # tau1 is the leading tau # tau2 is the subleading tau taus = list(event.taus) if len(taus) >= 2: tau1, tau2 = taus[0], taus[1] jets = list(event.jets) jet1, jet2, jet3 = None, None, None beta = None if len(jets) >= 2: jet1, jet2 = jets[:2] # determine boost of system # determine jet CoM frame beta = (jet1.fourvect + jet2.fourvect).BoostVector() tree.jet_beta.copy_from(beta) jet1.fourvect_boosted.copy_from(jet1.fourvect) jet2.fourvect_boosted.copy_from(jet2.fourvect) jet1.fourvect_boosted.Boost(beta * -1) jet2.fourvect_boosted.Boost(beta * -1) tau1.fourvect_boosted.copy_from(tau1.fourvect) tau2.fourvect_boosted.copy_from(tau2.fourvect) tau1.fourvect_boosted.Boost(beta * -1) tau2.fourvect_boosted.Boost(beta * -1) tau1.min_dr_jet = min(tau1.fourvect.DeltaR(jet1.fourvect), tau1.fourvect.DeltaR(jet2.fourvect)) tau2.min_dr_jet = min(tau2.fourvect.DeltaR(jet1.fourvect), tau2.fourvect.DeltaR(jet2.fourvect)) # sphericity, aplanarity = eventshapes.sphericity_aplanarity( # [tau1.fourvect, # tau2.fourvect, # jet1.fourvect, # jet2.fourvect]) # sphericity # tree.sphericity = sphericity # aplanarity # tree.aplanarity = aplanarity # sphericity_boosted, aplanarity_boosted = eventshapes.sphericity_aplanarity( # [tau1.fourvect_boosted, # tau2.fourvect_boosted, # jet1.fourvect_boosted, # jet2.fourvect_boosted]) # sphericity # tree.sphericity_boosted = sphericity_boosted # aplanarity # tree.aplanarity_boosted = aplanarity_boosted # tau centrality (degree to which they are between the two jets) tau1.centrality = eventshapes.eta_centrality( tau1.fourvect.Eta(), jet1.fourvect.Eta(), jet2.fourvect.Eta()) tau2.centrality = eventshapes.eta_centrality( tau2.fourvect.Eta(), jet1.fourvect.Eta(), jet2.fourvect.Eta()) # boosted tau centrality tau1.centrality_boosted = eventshapes.eta_centrality( tau1.fourvect_boosted.Eta(), jet1.fourvect_boosted.Eta(), jet2.fourvect_boosted.Eta()) tau2.centrality_boosted = eventshapes.eta_centrality( tau2.fourvect_boosted.Eta(), jet1.fourvect_boosted.Eta(), jet2.fourvect_boosted.Eta()) # 3rd leading jet if len(jets) >= 3: jet3 = jets[2] jet3.fourvect_boosted.copy_from(jet3.fourvect) jet3.fourvect_boosted.Boost(beta * -1) elif len(jets) == 1: jet1 = jets[0] tau1.min_dr_jet = tau1.fourvect.DeltaR(jet1.fourvect) tau2.min_dr_jet = tau2.fourvect.DeltaR(jet1.fourvect) # sphericity, aplanarity = eventshapes.sphericity_aplanarity( # [tau1.fourvect, # tau2.fourvect, # jet1.fourvect]) # sphericity # tree.sphericity = sphericity # aplanarity #tree.aplanarity = aplanarity RecoJetBlock.set(tree, jet1, jet2, jet3, local=local) # mass of ditau + leading jet system if jet1 is not None: tree.mass_tau1_tau2_jet1 = (tau1.fourvect + tau2.fourvect + jet1.fourvect).M() # full sphericity and aplanarity # sphericity_full, aplanarity_full = eventshapes.sphericity_aplanarity( # [tau1.fourvect, tau2.fourvect] + [jet.fourvect for jet in jets]) # tree.sphericity_full = sphericity_full # tree.aplanarity_full = aplanarity_full # #################################### # number of tracks from PV minus taus # #################################### ntrack_pv = 0 ntrack_nontau_pv = 0 for vxp in event.vertices: # primary vertex if vxp.type == 1: ntrack_pv = vxp.nTracks ntrack_nontau_pv = ntrack_pv - tau1.numTrack - tau2.numTrack break tree.ntrack_pv = ntrack_pv tree.ntrack_nontau_pv = ntrack_nontau_pv # ######################## # MET variables # ######################## METx = event.MET.etx METy = event.MET.ety MET = event.MET.et MET_vect = Vector2(METx, METy) MET_4vect = LorentzVector() MET_4vect.SetPxPyPzE(METx, METy, 0., MET) MET_4vect_boosted = LorentzVector() MET_4vect_boosted.copy_from(MET_4vect) if beta is not None: MET_4vect_boosted.Boost(beta * -1) tree.MET_et = MET tree.MET_etx = METx tree.MET_ety = METy tree.MET_phi = event.MET.phi dPhi_tau1_tau2 = abs(tau1.fourvect.DeltaPhi(tau2.fourvect)) dPhi_tau1_MET = abs(tau1.fourvect.DeltaPhi(MET_4vect)) dPhi_tau2_MET = abs(tau2.fourvect.DeltaPhi(MET_4vect)) tree.dPhi_tau1_tau2 = dPhi_tau1_tau2 tree.dPhi_tau1_MET = dPhi_tau1_MET tree.dPhi_tau2_MET = dPhi_tau2_MET tree.dPhi_min_tau_MET = min(dPhi_tau1_MET, dPhi_tau2_MET) tree.MET_bisecting = is_MET_bisecting(dPhi_tau1_tau2, dPhi_tau1_MET, dPhi_tau2_MET) sumET = event.MET.sumet tree.MET_sumet = sumET if sumET != 0: tree.MET_sig = ( (2. * MET / GeV) / (utils.sign(sumET) * sqrt(abs(sumET / GeV)))) else: tree.MET_sig = -1. tree.MET_centrality = eventshapes.phi_centrality( tau1.fourvect, tau2.fourvect, MET_vect) tree.MET_centrality_boosted = eventshapes.phi_centrality( tau1.fourvect_boosted, tau2.fourvect_boosted, MET_4vect_boosted) tree.number_of_good_vertices = len(event.vertices) # ######################### # Jet and sum pt variables # ######################### tree.numJets = len(event.jets) # sum pT with only the two leading jets tree.sum_pt = sum([tau1.pt, tau2.pt] + [jet.pt for jet in jets[:2]]) # sum pT with all selected jets tree.sum_pt_full = sum([tau1.pt, tau2.pt] + [jet.pt for jet in jets]) # vector sum pT with two leading jets and MET tree.vector_sum_pt = sum([tau1.fourvect, tau2.fourvect] + [jet.fourvect for jet in jets[:2]] + [MET_4vect]).Pt() # vector sum pT with all selected jets and MET tree.vector_sum_pt_full = sum([tau1.fourvect, tau2.fourvect] + [jet.fourvect for jet in jets] + [MET_4vect]).Pt() # resonance pT tree.resonance_pt = sum( [tau1.fourvect, tau2.fourvect, MET_4vect]).Pt() # ############################ # tau <-> vertex association # ############################ tree.tau_same_vertex = (tau1.privtx_x == tau2.privtx_x and tau1.privtx_y == tau2.privtx_y and tau1.privtx_z == tau2.privtx_z) tau1.vertex_prob = ROOT.TMath.Prob(tau1.privtx_chiSquared, int(tau1.privtx_numberDoF)) tau2.vertex_prob = ROOT.TMath.Prob(tau2.privtx_chiSquared, int(tau2.privtx_numberDoF)) # ######################### # MMC Mass # ######################### mmc_result = mmc.mass(tau1, tau2, METx, METy, sumET, njets=len(event.jets)) for mmc_method, mmc_object in enumerate(mmc_objects): mmc_mass, mmc_resonance, mmc_met = mmc_result[mmc_method] if verbose: log.info("MMC (method %d): %f" % (mmc_method, mmc_mass)) mmc_object.mass = mmc_mass mmc_object.MET_et = mmc_met.Mod() mmc_object.MET_etx = mmc_met.X() mmc_object.MET_ety = mmc_met.Y() mmc_object.MET_phi = math.pi - mmc_met.Phi() if mmc_mass > 0: FourMomentum.set(mmc_object.resonance, mmc_resonance) # ########################### # collinear and visible mass # ########################### vis_mass, collin_mass, tau1_x, tau2_x = mass.collinearmass( tau1, tau2, METx, METy) tree.mass_vis_tau1_tau2 = vis_mass tree.mass_collinear_tau1_tau2 = collin_mass tau1.collinear_momentum_fraction = tau1_x tau2.collinear_momentum_fraction = tau2_x ########################### # Match jets to VBF partons ########################### #if datatype == datasets.MC and 'VBF' in dsname and year == 2011: # # get partons (already sorted by eta in hepmc) FIXME!!! # parton1, parton2 = hepmc.get_VBF_partons(event) # tree.mass_true_quark1_quark2 = (parton1.fourvect + parton2.fourvect).M() # # order here needs to be revised since jets are no longer # # sorted by eta but instead by pT # PartonBlock.set(tree, parton1, parton2) # if len(jets) >= 2: # jet1, jet2 = jets[:2] # for i, jet in zip((1, 2), (jet1, jet2)): # for parton in (parton1, parton2): # if utils.dR(jet.eta, jet.phi, parton.eta, parton.phi) < .8: # setattr(tree, 'jet%i_matched' % i, True) # Fill the tau block # This must come after the RecoJetBlock is filled since # that sets the jet_beta for boosting the taus RecoTauBlock.set(event, tree, datatype, tau1, tau2, local=local) if datatype != datasets.DATA: TrueTauBlock.set(tree, tau1, tau2) # fill the output tree outtree.Fill(reset=True) externaltools.report() # flush any baskets remaining in memory to disk self.output.cd() outtree.FlushBaskets() outtree.Write() if local: if datatype == datasets.DATA: xml_string = ROOT.TObjString(merged_grl.str()) xml_string.Write('lumi') merged_cutflow.Write()
#!/usr/bin/env python """ This benchmark compares the fill_array Hist method with Python's map() and NumPy's histogram function. """ print __doc__ from rootpy.plotting import Hist import numpy as np import cProfile import timeit h = Hist(1000, -5, 5) array = np.random.randn(1E6) def time_repeat(cmd, repeat=5, number=1): best_time = min( timeit.repeat(cmd, repeat=repeat, number=number, setup="from __main__ import h, array, np")) / number print "%d loops, best of %d: %fs per loop" % (number, repeat, best_time) print "Using Python's map()..." cProfile.run('map(h.Fill, array)') h.Reset() print "time without profiler overhead:" time_repeat('map(h.Fill, array)')
def makeDiscr(discr_dict, outfile, xtitle="discriminator"): c = ROOT.TCanvas("c", "c", 800, 500) ROOT.gStyle.SetOptStat(0) ROOT.gPad.SetMargin(0.15, 0.1, 0.2, 0.1) #ROOT.gPad.SetLogy(1) #ROOT.gPad.SetGrid(1,1) ROOT.gStyle.SetGridColor(17) l = TLegend(0.17, 0.75, 0.88, 0.88) l.SetTextSize(0.025) l.SetBorderSize(0) l.SetFillStyle(0) l.SetNColumns(2) colors = [2, 4, 8, ROOT.kCyan + 2, 1] counter = 0 for leg, discr in discr_dict.iteritems(): a = Hist(30, 0, 1) #fill_hist_with_ndarray(a, discr) a.fill_array(discr) a.SetLineColor(colors[counter]) a.SetLineWidth(2) a.GetXaxis().SetTitle(xtitle) a.GetXaxis().SetLabelSize(0.05) a.GetXaxis().SetTitleSize(0.05) a.GetXaxis().SetTitleOffset(1.45) a.GetYaxis().SetTitle("a.u.") a.GetYaxis().SetTickSize(0) a.GetYaxis().SetLabelSize(0) a.GetYaxis().SetTitleSize(0.06) a.GetYaxis().SetTitleOffset(0.9) a.Scale(1. / a.Integral()) #a.GetYaxis().SetRangeUser(0.00001,100) a.GetYaxis().SetRangeUser(0, 0.2) if counter == 0: a.draw("hist") else: a.draw("same hist") l.AddEntry(a, leg, "l") counter += 1 l.Draw("same") c.SaveAs(outfile)
dn = dn - delta delta = delta / 2. res = ROOT.TMath.KolmogorovProb(dn * sqrt(n)) return dn if __name__ == '__main__': """ this is an example of drawing a quantile-quantile plot with confidential level (CL) band by Zhiyi Liu, [email protected] """ ROOT.gROOT.SetStyle("Plain") ROOT.gStyle.SetOptStat(0) can = Canvas(name="can", title="can", width=600, height=450) rand = ROOT.TRandom3() h1 = Hist(100, -5, 5, name="h1", title="Histogram 1") h1.Sumw2() h1.SetLineColor(ROOT.kRed) h2 = Hist(100, -5, 5, name="h2", title="Histogram 2") h2.SetLineColor(ROOT.kBlue) for ievt in xrange(10000): #some test histograms: #1. let 2 histograms screwed #h1.Fill(rand.Gaus(0.5, 0.8)) #h2.Fill(rand.Gaus(0, 1)) #2. long tail and short tail h1.Fill(rand.Gaus(0, 0.8)) h2.Fill(rand.Gaus(0, 1))
import matplotlib.gridspec as gridspec from config import CMS # Setting this to True (default in rootpy) # changes how the histograms look in ROOT... ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) # create normal distributions mu1, mu2, sigma1, sigma2 = 100, 140, 15, 5 x1 = mu1 + sigma1 * np.random.randn(10000) x2 = mu2 + sigma2 * np.random.randn(500) x1_obs = mu1 + sigma1 * np.random.randn(10000) x2_obs = mu2 + sigma2 * np.random.randn(1000) # create histograms h1 = Hist(100, 40, 200, title='Background') h2 = h1.Clone(title='Signal') h3 = h1.Clone(title='Data') h3.markersize = 1.2 # fill the histograms with our distributions map(h1.Fill, x1) map(h2.Fill, x2) map(h3.Fill, x1_obs) map(h3.Fill, x2_obs) # set visual attributes h1.fillstyle = 'solid' h1.fillcolor = 'green' h1.linecolor = 'green' h1.linewidth = 0
#!/usr/bin/env python import cProfile import time import numpy as np from rootpy.io import root_open from rootpy.root2array import tree_to_ndarray from rootpy.plotting import Hist # this import is required import rootpy.tree h = Hist(10, 0, 1) def pyroot(tree): tree.Draw('a_x', '', 'goff', hist=h) v1 = tree.GetV1() return set(v1[n] for n in xrange(tree.GetEntries())) def rootpy(tree): return np.unique(tree_to_ndarray(tree, branches=["a_x"])) with root_open('test.root') as f: tree = f.test print "Trees has %i entries" % tree.GetEntries() print
from rootpy.plotting import Hist, Graph from rootpy import asrootpy import rootpy.plotting.root2matplotlib as rplt import matplotlib.pyplot as plt value_error_tuplelist = [(0.0, 0.0), (17808.0, 133.4466185408982), (12051.0, 109.77704678119193), (8626.0, 92.87626176801045), (6378.0, 79.86238163240563), (13160.0, 114.71704319759989), (5553.0, 74.51845409024533)] value_errors_tuplelist = [(20, 1, 0.5), (21, 2, 1), (18, 3, 4)] bin_edges = [0, 30, 35, 40, 45, 50, 70, 100] rootpy_hist = Hist(bin_edges) set_bin_value = rootpy_hist.SetBinContent set_bin_error = rootpy_hist.SetBinError for bin_i, (value, error) in enumerate(value_error_tuplelist): set_bin_value(bin_i + 1, value) set_bin_error(bin_i + 1, error) value_error_tuplelist = [(0.0, 0.0), (13146.0, 114.65600725648875), (11114.0, 105.4229576515476), (8373.0, 91.50409826887537), (6284.0, 79.27168473042566), (13022.0, 114.11397810960759), (5517.0, 74.27651041883968)] value_errors_tuplelist = [(20, 1, 0.5), (21, 2, 1), (18, 3, 4)] bin_edges = [0, 30, 35, 40, 45, 50, 70, 100] rootpy_hist_passed = Hist(bin_edges) set_bin_value = rootpy_hist_passed.SetBinContent set_bin_error = rootpy_hist_passed.SetBinError
set_style('ATLAS') mus = (0, -1, 2) sigmas = (2, 1, 0.5) events = (1000, 2000, 100) colors = ('lawngreen', 'forestgreen', 'mistyrose') styles = ('\\', '/', '-') canvas = Canvas() objects = [] # create a stack stack = HistStack() stack.Add( Hist(100, -5, 5, color='salmon', drawstyle='hist').FillRandom(F1('TMath::Gaus(x, 2, 1)'), 500)) stack.Add( Hist(100, -5, 5, color='powderblue', drawstyle='hist').FillRandom(F1('TMath::Gaus(x, 2, 0.6)'), 300)) objects.append(stack) # create some random histograms for i, (mu, sigma, n, c, s) in enumerate(zip(mus, sigmas, events, colors, styles)): hist = Hist(100, -5, 5, color=c, fillstyle=s, drawstyle='hist' if i % 2 == 0 else '') hist.FillRandom(F1('TMath::Gaus(x,{0},{1})'.format(mu, sigma)), n)
''' 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 start(self): self._bu_branchnames = [ "BToKMuMu_chi2", "BToKMuMu_cos2D", "BToKMuMu_eta", "BToKMuMu_fit_cos2D", "BToKMuMu_fit_eta", "BToKMuMu_fit_k_eta", "BToKMuMu_fit_k_phi", "BToKMuMu_fit_k_pt", "BToKMuMu_fit_l1_eta", "BToKMuMu_fit_l1_phi", "BToKMuMu_fit_l1_pt", "BToKMuMu_fit_l2_eta", "BToKMuMu_fit_l2_phi", "BToKMuMu_fit_l2_pt", "BToKMuMu_fit_mass", "BToKMuMu_fit_massErr", "BToKMuMu_fit_phi", "BToKMuMu_fit_pt", "BToKMuMu_l_xy", "BToKMuMu_l_xy_unc", "BToKMuMu_mass", "BToKMuMu_maxDR", "BToKMuMu_minDR", "BToKMuMu_mllErr_llfit", "BToKMuMu_mll_fullfit", "BToKMuMu_mll_llfit", "BToKMuMu_mll_raw", "BToKMuMu_phi", "BToKMuMu_pt", "BToKMuMu_svprob", "BToKMuMu_vtx_ex", "BToKMuMu_vtx_ey", "BToKMuMu_vtx_ez", "BToKMuMu_vtx_x", "BToKMuMu_vtx_y", "BToKMuMu_vtx_z", "BToKMuMu_charge", "BToKMuMu_kIdx", "BToKMuMu_l1Idx", "BToKMuMu_l2Idx", "BToKMuMu_pdgId", ] self._event_branchnames = [ "nMuon", "nBToKMuMu", "HLT_Mu7_IP4", "HLT_Mu8_IP6", "HLT_Mu8_IP5", "HLT_Mu8_IP3", "HLT_Mu8p5_IP3p5", "HLT_Mu9_IP6", "HLT_Mu9_IP5", "HLT_Mu9_IP4", "HLT_Mu10p5_IP3p5", "HLT_Mu12_IP6", "L1_SingleMu7er1p5", "L1_SingleMu8er1p5", "L1_SingleMu9er1p5", "L1_SingleMu10er1p5", "L1_SingleMu12er1p5", "L1_SingleMu22", "nProbeTracks", "nTrigObj", "nTriggerMuon", "event", ] self._muon_branchnames = [ "Muon_dxy", "Muon_dxyErr", "Muon_dz", "Muon_dzErr", "Muon_eta", "Muon_ip3d", "Muon_mass", "Muon_pfRelIso03_all", "Muon_pfRelIso03_chg", "Muon_pfRelIso04_all", "Muon_phi", "Muon_pt", "Muon_ptErr", "Muon_segmentComp", "Muon_sip3d", "Muon_vx", "Muon_vy", "Muon_vz", "Muon_charge", "Muon_isTriggering", "Muon_nStations", "Muon_pdgId", "Muon_tightCharge", "Muon_highPtId", "Muon_inTimeMuon", "Muon_isGlobal", "Muon_isPFcand", "Muon_isTracker", "Muon_mediumId", "Muon_mediumPromptId", "Muon_miniIsoId", "Muon_multiIsoId", "Muon_mvaId", "Muon_pfIsoId", "Muon_softId", "Muon_softMvaId", "Muon_tightId", "Muon_tkIsoId", "Muon_triggerIdLoose", ] self._track_branchnames = [ "ProbeTracks_DCASig", "ProbeTracks_dxy", "ProbeTracks_dxyS", "ProbeTracks_dz", "ProbeTracks_dzS", "ProbeTracks_eta", "ProbeTracks_mass", "ProbeTracks_phi", "ProbeTracks_pt", "ProbeTracks_vx", "ProbeTracks_vy", "ProbeTracks_vz", "ProbeTracks_charge", "ProbeTracks_isLostTrk", "ProbeTracks_isPacked", "ProbeTracks_pdgId", "ProbeTracks_isMatchedToEle", "ProbeTracks_isMatchedToLooseMuon", "ProbeTracks_isMatchedToMediumMuon", "ProbeTracks_isMatchedToMuon", "ProbeTracks_isMatchedToSoftMuon", ] self._trigobj_branchnames = [ "TrigObj_pt", "TrigObj_eta", "TrigObj_phi", "TrigObj_l1pt", "TrigObj_l1pt_2", "TrigObj_l2pt", "TrigObj_id", "TrigObj_l1iso", "TrigObj_l1charge", "TrigObj_filterBits", ] self._muon_trigmatched_branchnames = [ "TriggerMuon_eta", "TriggerMuon_mass", "TriggerMuon_phi", "TriggerMuon_pt", "TriggerMuon_vx", "TriggerMuon_vy", "TriggerMuon_vz", "TriggerMuon_charge", "TriggerMuon_pdgId", "TriggerMuon_trgMuonIndex", ] self._gen_branchnames = [ "nGenPart", "GenPart_eta", "GenPart_mass", "GenPart_phi", "GenPart_pt", "GenPart_vx", "GenPart_vy", "GenPart_vz", "GenPart_genPartIdxMother", "GenPart_pdgId", "GenPart_status", "GenPart_statusFlags", ] #if self._isMC: # self._input_branches.extend(['GenPart_pdgId', 'GenPart_genPartIdxMother']) self._mu_histograms = {} self._mu_histograms["nMuon"] = Hist(11,-0.5, 10.5, name="nMuon", title="", type="F") self._mu_histograms["nMuon_isTrig"] = Hist(11,-0.5, 10.5, name="nMuon_isTrig", title="", type="F") self._mu_histograms["Muon_pt"] = Hist(100, 0.0, 100.0, name="Muon_pt", title="", type="F") self._mu_histograms["Muon_pt_isTrig"] = Hist(100, 0.0, 100.0, name="Muon_pt_isTrig", title="", type="F") self._histograms = {} self._histograms = {} for tag_type in ["inclusive", "triggered", "tag", "probe"]: self._histograms[tag_type] = {} self._histograms[tag_type] = {} self._histograms[tag_type]["BToKMuMu_chi2"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_chi2".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_eta"] = Hist(50, -5.0, 5.0, name="{}_BToKMuMu_eta".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_cos2D"] = Hist(100, -1., 1., name="{}_BToKMuMu_fit_cos2D".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_eta"] = Hist(50, -5.0, 5.0, name="{}_BToKMuMu_fit_eta".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_mass"] = Hist(100, BU_MASS * 0.9, BU_MASS * 1.1, name="{}_BToKMuMu_fit_mass".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_phi"] = Hist(50, -2.0*math.pi, 2.0*math.pi, name="{}_BToKMuMu_fit_phi".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_pt"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_fit_pt".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_l_xy"] = Hist(50, -1.0, 4.0, name="{}_BToKMuMu_l_xy".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_l_xy_sig"] = Hist(50, -1.0, 4.0, name="{}_BToKMuMu_l_xy_sig".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l1_eta"] = Hist(50, -5.0, 5.0, name="{}_BToKMuMu_fit_l1_eta".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l1_phi"] = Hist(50, -2.0*math.pi, 2.0*math.pi, name="{}_BToKMuMu_fit_l1_phi".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l1_pt"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_fit_l1_pt".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l2_eta"] = Hist(50, -5.0, 5.0, name="{}_BToKMuMu_fit_l2_eta".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l2_phi"] = Hist(50, -2.0*math.pi, 2.0*math.pi, name="{}_BToKMuMu_fit_l2_phi".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l2_pt"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_fit_l2_pt".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_mass"] = Hist(500, BU_MASS * 0.9, BU_MASS * 1.1, name="{}_BToKMuMu_mass".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_mll_fullfit"] = Hist(500, JPSI_1S_MASS * 0.9, JPSI_1S_MASS * 1.1, name="{}_BToKMuMu_mll_fullfit".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_mll_llfit"] = Hist(500, JPSI_1S_MASS * 0.9, JPSI_1S_MASS * 1.1, name="{}_BToKMuMu_mll_llfit".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_mll_raw"] = Hist(500, JPSI_1S_MASS * 0.9, JPSI_1S_MASS * 1.1, name="{}_BToKMuMu_mll_raw".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_phi"] = Hist(50, -2.0*math.pi, 2.0*math.pi, name="{}_BToKMuMu_phi".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_pt"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_pt".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_svprob"] = Hist(100, -1.0, 1.0, name="{}_BToKMuMu_svprob".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_charge"] = Hist(3, -1.5, 1.5, name="{}_BToKMuMu_charge".format(tag_type), title="", type="F") self._histograms[tag_type]["BToKMuMu_fit_l_minpt"] = Hist(100, 0.0, 100.0, name="{}_BToKMuMu_fit_l_minpt".format(tag_type), title="", type="F") self._cutflow_names = [] self._cutflow_names.append("Inclusive") self._cutflow_names.append(self._trigger) self._cutflow_names.append("Inclusive SV") self._cutflow_names.append("Inclusive mu-K") self._cutflow_names.append("Inclusive Jpsi") self._cutflow_names.append("Tag") self._cutflow_names.append("Tag SV") self._cutflow_names.append("Tag mu-K") self._cutflow_names.append("Tag Jpsi") self._cutflow_names.append("Probe") self._cutflow_names.append("Probe SV") self._cutflow_names.append("Probe mu-K") self._cutflow_names.append("Probe Jpsi") self._cutflow_counts = {} for name in self._cutflow_names: self._cutflow_counts[name] = 0
#nBToKEE_selected = branches['BToKEE_event'][count_selection].values #_, nBToKEE_selected = np.unique(nBToKEE_selected[np.isfinite(nBToKEE_selected)], return_counts=True) prepareMVA = False for eType, eBool in ele_type.items(): if not eBool: continue output_branches[eType] = branches[eval(ele_selection[eType])] if args.hist: file_out = root_open('{}_{}.root'.format(outputfile, eType), 'recreate') hist_list = { hist_name: Hist(hist_bins['nbins'], hist_bins['xmin'], hist_bins['xmax'], name=hist_name, title='', type='F') for hist_name, hist_bins in sorted(outputbranches.items()) } for hist_name, hist_bins in sorted(outputbranches.items()): if hist_name in branches.keys(): branch_np = output_branches[eType][hist_name].values fill_hist(hist_list[hist_name], branch_np[np.isfinite(branch_np)]) hist_list[hist_name].write() file_out.close() else: if prepareMVA: output_branches[eType] = output_branches[eType].sample(frac=1)