def overlay_with_emu(self, emu_plotter, with_fits=False): hists = [] labels = [] fits = [] hist = self.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) hist.drawstyle = "EP" hist.SetMarkerSize(0.5) hist.SetMarkerColor(1) # if with_fits: # fit = self.fits.get_bin_contents([threshold]) # fits.append(fit) hists.append(hist) labels.append("HW") emu_hist = emu_plotter.plots.get_bin_contents([bn.Base.everything]) emu_hist = cumulative_hist(emu_hist) emu_hist.drawstyle = "EP" emu_hist.SetMarkerSize(0.5) emu_hist.SetMarkerColor(2) # if with_fits: # emu_fit = self.fits.get_bin_contents([threshold]) # fits.append(emu_fit) hists.append(emu_hist) labels.append("Emu") self.__make_overlay(hists, fits, labels, "# Events", setlogy=True)
def overlay(self, other_plotters=None, with_fits=False, comp=False): hists = [] labels = [] fits = [] suffix = '__emu_overlay' titles = ['Hw', 'Emu'] if comp: suffix = '__comparison' titles = [ other_plotter.comp_title for other_plotter in other_plotters ] titles.insert(0, self.comp_title) hist = self.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) hist = normalise_to_collision_rate(hist) hist.drawstyle = "HIST" hist.SetLineWidth(3) hist.SetMarkerColor(1) # if with_fits: # fit = self.fits.get_bin_contents([threshold]) # fits.append(fit) hists.append(hist) labels.append('L1 ' + titles[0]) for other_plotter in other_plotters: hist = other_plotter.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) hist = normalise_to_collision_rate(hist) hist.drawstyle = "HIST" hist.SetLineWidth(3) hist.SetMarkerColor(2) hist.markerstyle = 21 + other_plotters.index(other_plotter) # if with_fits: # fit = self.fits.get_bin_contents([threshold]) # fits.append(fit) hists.append(hist) labels.append('L1 ' + titles[other_plotters.index(other_plotter) + 1]) self.__make_overlay(hists, fits, labels, "Rate (kHz)", suffix, setlogy=True)
def draw(self, with_fits=False): hists = [] labels = [] fits = [] for (pile_up, ), hist in self.plots.flat_items_all(): h = cumulative_hist(hist) h = normalise_to_collision_rate(h) if pile_up == bn.Base.everything: h.linestyle = "dashed" label = "Everything" elif isinstance(pile_up, int): h.drawstyle = "EP" label = "~ {:.0f}".format( self.pileup_bins.get_bin_center(pile_up)) else: continue h.SetMarkerSize(0.5) hists.append(h) labels.append(label) # if with_fits: # fits.append(self.fits.get_bin_contents([pile_up])) self.__make_overlay(hists, fits, labels, "Number of events") normed_hists = list(normalise_to_unit_area(hists)) self.__make_overlay(normed_hists, fits, labels, "Fraction of events", "__shapes")
def make_plots(self): # TODO: implement this in BaseAnalyzer # make_plots -> make_plots(plot_func) # Get EMU thresholds for each HW threshold. if self.thresholds is None: print( 'Error: Please specify thresholds in the config .yaml in dictionary format' ) # calculate cumulative histograms for plot in self.all_plots: hist = plot.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) hist = normalise_to_collision_rate(hist) setattr(self, plot.online_name, hist) plot.draw() print(' thresholds:') for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue h = getattr(self, histo_name) h_emu = getattr(self, histo_name + "_Emu") bin1 = h.get_bin_content(1) if bin1 != 0.: h.Scale(40000000. / bin1) bin1_emu = h_emu.get_bin_content(1) if bin1_emu != 0.: h_emu.Scale(40000000. / bin1_emu) thresholds = self.thresholds.get(histo_name) emu_thresholds = [] for thresh in thresholds: rate_delta = [] hw_rate = h.get_bin_content(thresh) for i in range(h.nbins()): emu_rate = h_emu.get_bin_content(i) if hw_rate == 0. or emu_rate == 0.: rate_delta.append(40000000.) else: rate_delta.append(abs(hw_rate - emu_rate)) emu_thresholds.append(rate_delta.index(min(rate_delta))) outputline = ( ' {0}: {1}'.format(histo_name, thresholds) + '\n' + ' {0}: {1}'.format(histo_name + '_Emu', emu_thresholds)) print(outputline) ''' for histo_name in object_types: h = getattr(self, histo_name) plot(h, histo_name, self.output_folder) ''' return True
def write_histograms(self): # calculate cumulative histograms cumul_hists = {} for puBin, histograms in six.iteritems(self.rates): cumul_hists[puBin] = {} for hist_name, hist in six.iteritems(histograms): h = cumulative_hist(hist) h = normalise_to_collision_rate(h) cumul_hists[puBin][h.name] = h self.rates[puBin].update(cumul_hists) # add them to self.rates self.rates.to_root(self.get_histogram_filename()) return True
def draw(self, with_fits=False): hists = [] labels = [] fits = [] for (pile_up, ), hist in self.plots.flat_items_all(): h = cumulative_hist(hist) h = normalise_to_collision_rate(h) if pile_up == bn.Base.everything: h.linestyle = "dashed" label = "L1 Rate" # elif isinstance(pile_up, int): # h.drawstyle = "HIST" # label = str(self.pileup_bins.bins[pile_up]) else: continue hists.append(h) labels.append(label) # if with_fits: # fits.append(self.fits.get_bin_contents([pile_up])) self.__make_overlay(hists, fits, labels, "Rate (kHz)", setlogy=True) normed_hists = list(normalise_to_unit_area(hists)) self.__make_overlay(normed_hists, fits, labels, "Fraction of events", "__shapes")
def make_plots(self, other_analyzers=None): # TODO: implement this in BaseAnalyzer # make_plots -> make_plots(plot_func) if 'thresholds' not in self.params: print( 'Error: Please specify thresholds in the config .yaml in dictionary format' ) # print hw vs emu for rates and rate vs pileup for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue if self._doEmu: plotter = getattr(self, histo_name + '_rates') emu_plotter = getattr(self, histo_name + '_Emu_rates') plotter.overlay([emu_plotter]) if self._doComp: plotter.overlay([ getattr(other_analyzer, histo_name + '_rates') for other_analyzer in other_analyzers ], comp=True) emu_plotter.overlay([ getattr(other_analyzer, histo_name + '_Emu_rates') for other_analyzer in other_analyzers ], comp=True) plotter = getattr(self, histo_name + '_rate_vs_pileup') emu_plotter = getattr(self, histo_name + "_Emu_rate_vs_pileup") plotter.overlay([emu_plotter]) if self._doComp: plotter.overlay([ getattr(other_analyzer, histo_name + '_rate_vs_pileup') for other_analyzer in other_analyzers ], comp=True) emu_plotter.overlay([ getattr(other_analyzer, histo_name + '_Emu_rate_vs_pileup') for other_analyzer in other_analyzers ], comp=True) # if not doEmu just print hw rates else: plotter = getattr(self, histo_name + '_rates') rvp_plotter = getattr(self, histo_name + '_rate_vs_pileup') plotter.draw() rvp_plotter.draw() if self._doComp: plotter.overlay([ getattr(other_analyzer, histo_name + '_rates') for other_analyzer in other_analyzers ], comp=True) rvp_plotter.overlay([ getattr(other_analyzer, histo_name + '_rate_vs_pileup') for other_analyzer in other_analyzers ], comp=True) outstring = '\n thresholds:' # calculate cumulative histograms for plot in self.all_plots: if 'rate_vs_pileup' not in plot.filename_format: hist = plot.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) setattr(self, plot.online_name, hist) if self.menuRates: for histo_name in self._sumTypes + self._jetTypes: h = getattr(self, histo_name) h = normalise_to_collision_rate(h) targetRates = self.menuRates.get(histo_name.replace( '_Emu', '')) for targetRate in targetRates: closestRateDiff = 999999999 threshold = None for i in range(h.nbins()): if abs(h.get_bin_content(i) - targetRate) < closestRateDiff: closestRateDiff = abs( h.get_bin_content(i) - targetRate) threshold = h.get_bin_low_edge(i) outputLine = '\n {0}: [{2}] \tthreshold gives {1} KHz rate'.format( histo_name.ljust(15, ' '), targetRate, int(threshold)) outstring += outputLine outstring += '\n unpacked vs emulated thresholds for fixed rate [thresholds] [rates]:' if self._doEmu: for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue h = getattr(self, histo_name) h_emu = getattr(self, histo_name + "_Emu") thresholds = self.thresholds.get(histo_name) emu_thresholds = [] hw_rates = [] for thresh in thresholds: rate_delta = [] hw_rate = h.get_bin_content(thresh) hw_rates.append(int(hw_rate)) for i in range(h.nbins()): emu_rate = h_emu.get_bin_content(i) if hw_rate == 0. or emu_rate == 0.: rate_delta.append(40000000.) else: rate_delta.append(abs(hw_rate - emu_rate)) emu_thresholds.append(rate_delta.index(min(rate_delta))) outputlineA = ' {0}:'.format(histo_name).ljust( 20) + '\t{0}\t\t rates: \t{1}'.format( thresholds, hw_rates) outputlineB = ' {0}:'.format(histo_name + '_Emu').ljust( 20) + '\t{0}\t\t rates: \t{1}'.format( emu_thresholds, hw_rates) outputline = '\n' + outputlineA + '\n' + outputlineB outstring += outputline with open(os.path.join(self.output_folder, 'thresholds_and_rates.txt'), 'a') as outfile: outfile.write(outstring) print(outstring) ''' for histo_name in object_types: h = getattr(self, histo_name) plot(h, histo_name, self.output_folder) ''' return True
def make_plots(self): # TODO: implement this in BaseAnalyzer # make_plots -> make_plots(plot_func) # Get EMU thresholds for each HW threshold. if 'thresholds' not in self.params: print( 'Error: Please specify thresholds in the config .yaml in dictionary format') # print hw vs emu for rates and rate vs pileup for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue plotter = getattr(self, histo_name + '_rates') emu_plotter = getattr(self, histo_name + '_Emu_rates') plotter.overlay_with_emu(emu_plotter) plotter = getattr(self, histo_name + '_rate_vs_pileup') emu_plotter = getattr(self, histo_name + "_Emu" + '_rate_vs_pileup') plotter.overlay_with_emu(emu_plotter) # calculate cumulative histograms for plot in self.all_plots: if 'rate_vs_pileup' not in plot.filename_format: hist = plot.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) hist = normalise_to_collision_rate(hist) setattr(self, plot.online_name, hist) # plot.draw() print(' thresholds:') for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue h = getattr(self, histo_name) h_emu = getattr(self, histo_name + "_Emu") bin1 = h.get_bin_content(1) if bin1 != 0.: h.Scale(40000000. / bin1) bin1_emu = h_emu.get_bin_content(1) if bin1_emu != 0.: h_emu.Scale(40000000. / bin1_emu) thresholds = self.thresholds.get(histo_name) emu_thresholds = [] for thresh in thresholds: rate_delta = [] hw_rate = h.get_bin_content(thresh) for i in range(h.nbins()): emu_rate = h_emu.get_bin_content(i) if hw_rate == 0. or emu_rate == 0.: rate_delta.append(40000000.) else: rate_delta.append(abs(hw_rate - emu_rate)) emu_thresholds.append(rate_delta.index(min(rate_delta))) msg = ' {histo_name}: {thresholds}\n {histo_name}_Emu: {emu_thresholds}' outputline = msg.format( histo_name=histo_name, thresholds=thresholds, emu_thresholds=emu_thresholds, ) print(outputline) ''' for histo_name in object_types: h = getattr(self, histo_name) plot(h, histo_name, self.output_folder) ''' return True
def make_plots(self, other_analyzers=None): # TODO: implement this in BaseAnalyzer # make_plots -> make_plots(plot_func) # Get EMU thresholds for each HW threshold. if 'thresholds' not in self.params: print( 'Error: Please specify thresholds in the config .yaml in dictionary format') # print hw vs emu for rates and rate vs pileup for histo_name in self._sumTypes + self._jetTypes: plotter = getattr(self, histo_name + '_rates') if self._doEmu: if "_Emu" in histo_name: continue emu_plotter = getattr(self, histo_name + '_Emu_rates') plotter.overlay([emu_plotter]) if self._doComp: plotter.overlay([getattr(other_analyzer, histo_name + '_rates') for other_analyzer in other_analyzers]) plotter = getattr(self, histo_name + '_rate_vs_pileup') if self._doEmu: emu_plotter = getattr(self, histo_name + "_Emu" + '_rate_vs_pileup') plotter.overlay([emu_plotter]) if self._doComp: plotter.overlay([getattr(other_analyzer, histo_name + '_rate_vs_pileup') for other_analyzer in other_analyzers]) # calculate cumulative histograms for plot in self.all_plots: if 'rate_vs_pileup' not in plot.filename_format: hist = plot.plots.get_bin_contents([bn.Base.everything]) hist = cumulative_hist(hist) setattr(self, plot.online_name, hist) if not self._doEmu: plot.draw() print(' thresholds:') if self.menuRates: for histo_name in self._sumTypes + self._jetTypes: h = getattr(self, histo_name) h = normalise_to_collision_rate(h) targetRate = self.menuRates.get(histo_name.replace('_Emu', '')) threshold = None closestRateDiff = 999999999 threshold = None for i in range(h.nbins()): if abs(h.get_bin_content(i) - targetRate) < closestRateDiff: closestRateDiff = abs(h.get_bin_content(i) - targetRate) threshold = h.get_bin_low_edge(i) outputLine = ' {0}:\t{1} KHz at threshold of \t\t{2}\tGeV'.format( histo_name.ljust(15, ' '), targetRate, threshold) print(outputLine) print('\n') if self._doEmu: for histo_name in self._sumTypes + self._jetTypes: if "_Emu" in histo_name: continue h = getattr(self, histo_name) h_emu = getattr(self, histo_name + "_Emu") thresholds = self.thresholds.get(histo_name) emu_thresholds = [] for thresh in thresholds: rate_delta = [] hw_rate = h.get_bin_content(thresh) for i in range(h.nbins()): emu_rate = h_emu.get_bin_content(i) if hw_rate == 0. or emu_rate == 0.: rate_delta.append(40000000.) else: rate_delta.append(abs(hw_rate - emu_rate)) emu_thresholds.append(rate_delta.index(min(rate_delta))) outputlineA = ' {0}: {1}'.format(histo_name, thresholds) outputlineB = ' {0}: {1}'.format(histo_name + '_Emu', emu_thresholds) outputline = outputlineA + '\n' + outputlineB print(outputline) ''' for histo_name in object_types: h = getattr(self, histo_name) plot(h, histo_name, self.output_folder) ''' return True