def plot2d(histogram, plot_fn_base, width=1200, height=900): canvas = ROOT.TCanvas('c1', 'c1', width, height) ROOT.gStyle.SetOptStat(0) histogram.Draw('col text') canvas.SetLogy() canvas.SaveAs('%s.png' % plot_fn_base) canvas.SaveAs('%s.pdf' % plot_fn_base) del canvas
def plot(input_files, output_files, title, legend): handle_lhe = Handle('LHEEventProduct') label_lhe = ('externalLHEProducer') bins = array.array('f', [1. * i for i in range(0, 201)]) h = ROOT.TH1F(legend, legend, len(bins) - 1, bins) for input_file in input_files: events = Events(input_file) for event in events: event.getByLabel(label_lhe, handle_lhe) lhe = handle_lhe.product() invmass = [] for status, pdg, mom in zip(lhe.hepeup().ISTUP, lhe.hepeup().IDUP, lhe.hepeup().PUP): if status == 1 and abs(pdg) in [11, 13, 15]: l = ROOT.TLorentzVector(mom.x[0], mom.x[1], mom.x[2], mom.x[3]) invmass.append(l) if len(invmass) == 2: h.Fill((invmass[0] + invmass[1]).M()) else: raise RuntimeError( 'Did not find exactly 2 but %d LHE leptons in %s' % (len(invmass), input_file)) c = ROOT.TCanvas('c', 'c') c.SetLogy() c.SetGrid() h.SetTitle(title) h.GetXaxis().SetTitle('m_ll [GeV]') h.GetYaxis().SetTitle('# events') h.Draw() for output_file in output_files: c.SaveAs(output_file) del h del c
def plot(input_files, output_files, title, expected_neff, mode): histogram_dict = {} for sample_name, sample_entry in input_files.items(): if not hdfs.isfile(sample_entry['input']): logging.error('Could not find file {}'.format(sample_entry['input'])) continue root_file = ROOT.TFile.Open(sample_entry['input'], 'read') logging.debug('Opened file {}'.format(sample_entry['input'])) root_directories = list(filter( lambda root_dir: root_dir != None, [ root_file.Get(os.path.join(key.GetName(), mode, 'genEvt')) \ for key in root_file.GetListOfKeys() if key.GetClassName() == 'TDirectoryFile' ] )) if len(root_directories) != 1: raise RuntimeError('Expected single directory in %s' % sample_entry['input']) root_dir = root_directories[0] histogram_dirs = [ root_dir.Get(key.GetName()) \ for key in root_dir.GetListOfKeys() if key.GetClassName() == 'TDirectoryFile' ] if len(histogram_dirs) != 1: raise RuntimeError( 'Expected single directory containing lumiScale histograms in %s' % sample_entry['input'] ) histogram_dir = histogram_dirs[0] histograms = [ key.GetName() for key in histogram_dir.GetListOfKeys() \ if key.GetClassName().startswith('TH1') and 'lumiScale' in key.GetName() ] for histogram_name_actual in histograms: histogram_name = histogram_name_actual.replace('_lumiScale', '').replace('CMS_ttHl_', '') \ if histogram_name_actual != 'lumiScale' else 'central' histogram = histogram_dir.Get(histogram_name_actual).Clone() histogram.SetDirectory(0) if histogram.GetEntries() != sample_entry['nentries'] and mode == 'unbiased': raise RuntimeError('Expected {} entries from {} in file {}, but got {} entries'.format( sample_entry['nentries'], histogram_name, sample_entry['input'], histogram.GetEntries(), )) if histogram_name not in histogram_dict: histogram_dict[histogram_name] = { 'histogram' : histogram, 'nentries' : histogram.GetEntries(), 'nfiles' : 1, } else: histogram_dict[histogram_name]['histogram'].Add(histogram) histogram_dict[histogram_name]['nentries'] += histogram.GetEntries() histogram_dict[histogram_name]['nfiles'] += 1 root_file.Close() if not histogram_dict: logging.error('Could not find histograms for samples {}'.format(', '.join(list(input_files.keys())))) return if len(set(histogram_dict[histogram_name]['nfiles'] for histogram_name in histogram_dict)) != 1: raise RuntimeError( 'Inconsistent number of files found for samples %s' % ', '.join(list(input_files.keys())) ) if len(set(histogram_dict[histogram_name]['nentries'] for histogram_name in histogram_dict)) != 1: raise RuntimeError( 'Inconsistent number of entries found in samples %s' % ', '.join(list(input_files.keys())) ) min_y = -1 max_y = -1 nentries = -1 for histograms in histogram_dict.values(): histogram = histograms['histogram'] y_content = histogram.GetBinContent(1) y_error = histogram.GetBinError(1) y_down = y_content - y_error y_up = y_content + y_error if min_y < 0: min_y = y_down if max_y < 0: max_y = y_up if y_down < min_y: min_y = y_down if y_up > max_y: max_y = y_up if nentries < 0: nentries = histograms['nentries'] else: assert(nentries == histograms['nentries']) if not (y_down < expected_neff < y_up) and mode == 'unbiased': logging.warning( "Effective event count {} not within {} +- {}".format(expected_neff, y_content, y_error) ) if mode == 'unbiased': min_y = min(min_y, expected_neff) max_y = max(max_y, expected_neff) diff = 0.2 * (max_y - min_y) min_y -= diff max_y += diff canvas = ROOT.TCanvas('c', 'c', 1200, 900) canvas.SetGrid() ROOT.gStyle.SetOptStat(0) legend = ROOT.TLegend(0.1, 0.7, 0.48, 0.9) legend.SetHeader('N_{eff} (%d entries)' % nentries) expected_histogram = None line_width = 3 marker_style = 20 fill_style = 4000 lines = [] for idx, histogram_name in enumerate(sorted(histogram_dict.keys())): histogram = histogram_dict[histogram_name]['histogram'] color = 2 + idx histogram.SetTitle(title) histogram.SetAxisRange(min_y, max_y, "Y") histogram.SetLineColor(color) histogram.SetMarkerColor(color) histogram.SetLineWidth(line_width) histogram.SetMarkerStyle(marker_style) histogram.SetFillStyle(fill_style) histogram.Draw("l e1%s" % (" same" if idx > 0 else "")) y_content = histogram.GetBinContent(1) y_error = histogram.GetBinError(1) y_up = y_content + y_error y_down = y_content - y_error bin_width = histogram.GetBinWidth(1) bin_center = histogram.GetBinCenter(1) line_min_x = bin_center - bin_width / 4 line_max_x = bin_center + bin_width / 4 line_down = ROOT.TLine(line_min_x, y_down, line_max_x, y_down) line_down.SetLineColor(color) line_down.SetLineWidth(line_width) line_down.Draw() lines.append(line_down) line_up = ROOT.TLine(line_min_x, y_up, line_max_x, y_up) line_up.SetLineColor(color) line_up.SetLineWidth(line_width) line_up.Draw() lines.append(line_up) sig_digits = max(8 - int(math.ceil(math.log10(y_content))), 1) if y_content > 0. else 1 leg_pattern = '%s (%.{}f #pm %.{}f)'.format(sig_digits, sig_digits) leg_name = leg_pattern % (histogram_name, y_content, y_error) legend.AddEntry(histogram, leg_name) logging.debug( 'Effective event count for the sys unc option {} is {} +- {}'.format( histogram_name, y_content, y_error ) ) if not expected_histogram and mode == 'unbiased': expected_histogram = histogram.Clone() expected_histogram.Reset() expected_histogram.SetBinContent(1, expected_neff) expected_histogram.SetBinError(1, 0) expected_histogram.SetLineColor(ROOT.kBlack) expected_histogram.SetMarkerColor(ROOT.kBlack) expected_histogram.SetLineWidth(line_width) expected_histogram.SetMarkerStyle(marker_style) expected_histogram.SetLineStyle(9) expected_histogram.SetFillStyle(fill_style) if expected_histogram: logging.debug('Expecting {} events'.format(expected_neff)) expected_histogram.Draw("e2 same") legend.AddEntry(expected_histogram, 'expected (%.1f)' % expected_neff) legend.Draw() for output_file in output_files: canvas.SaveAs(output_file) canvas.Close() legend.Delete() if expected_histogram: expected_histogram.Delete() for histogram_name in histogram_dict: histogram_dict[histogram_name]['histogram'].Delete() for line in lines: line.Delete()
for idx in range(1, nof_files + 1): if idx in blacklist: continue input_file = os.path.join(base_path, '%04d' % (idx // 1000), 'tree_%d.root' % idx) output_file = os.path.join(current_subfolder, 'histogram_%d.root' % idx) file_exists = project(input_file, output_file, binning) if file_exists: output_files.append(output_file) hadd(output_files, final_output_file) root_file = ROOT.TFile.Open(final_output_file, 'read') for binning_key in binning: canvas = ROOT.TCanvas('c', 'c', 1200, 900) histogram = root_file.Get(binning_key) canvas.SetLogy() if binning_key == 'LHE_HT': canvas.SetLogx() histogram.SetTitle(sample_name) histogram.SetXTitle(binning_key) histogram.Draw() for extension in ['pdf', 'png']: plot_file = os.path.join( output_plot_dir, '%s_%s.%s' % (sample_name, binning_key, extension)) canvas.SaveAs(plot_file) del histogram del canvas
output_file_ptr.write('{:.6e}'.format(most_frequent_weight)) logging.info("Wrote output file: {}".format(output_file)) if plot_files: has_neg_weights = any(weight_freq[0] < 0 for weight_freq in weights_by_frequency) binning_max = MAX_CUTOFF * most_frequent_weight binning_min = -MAX_CUTOFF * most_frequent_weight if not has_neg_weights: binning_min = max(0., binning_min) binning = array.array( 'f', list(np.linspace(binning_min, binning_max, PLOT_BINS + 1))) histogram = ROOT.TH1D(GENWEIGHT_NAME, GENWEIGHT_NAME, PLOT_BINS, binning) for weight_freq in weights_by_frequency: histogram.Fill(weight_freq[0], weight_freq[1]) histogram.GetXaxis().SetRange(0, histogram.GetNbinsX() + 1) for plot_file in plot_files: title = os.path.splitext(os.path.basename(plot_file))[0] histogram.SetTitle(title) canvas = ROOT.TCanvas() canvas.SetCanvasSize(1000, 800) canvas.SetLogy(True) histogram.Draw('hist') canvas.SaveAs(plot_file) canvas.Close() del canvas del histogram