<commit_before>import ROOT
from PyAnalysisTools.base import _logger, InvalidInputError
from PyAnalysisTools.PlottingUtils import Formatting as FM
from PyAnalysisTools.PlottingUtils import PlottingTools as PT
from PyAnalysisTools.PlottingUtils.HistTools import get_colors


class RatioCalculator(object):
    def __init__(self, **kwargs):
        self.reference = kwargs["reference"]
        self.compare = kwargs["compare"]

    def calculate_ratio(self):
        if isinstance(self.reference, ROOT.TEfficiency):
            calc_func = "calculate_ratio_tefficiency"
        elif isinstance(self.reference, ROOT.TH1):
            calc_func = "calculate_ratio_hist"
        ratios = []
        if isinstance(self.compare, list):
            for obj in self.compare:
                ratios.append(getattr(self, calc_func)(obj))
        else:
            ratios.append(getattr(self, calc_func)(self.compare))
        return ratios

    def calculate_ratio_tefficiency(self, compare):
        ratio_graph = self.reference.GetPaintedGraph().Clone("ratio_" + compare.GetName())
        nbins = ratio_graph.GetN()
        for b in range(nbins):
            eff_compare = compare.GetEfficiency(b + 1)
            eff_reference = self.reference.GetEfficiency(b + 1)
            if eff_reference == 0. and eff_compare == 0.:
                ratio = 1.
            elif eff_reference == 0. and eff_compare != 0.:
                ratio = 0.
            else:
                ratio = eff_compare / eff_reference
            x = ROOT.Double(0.)
            y = ROOT.Double(0.)
            ratio_graph.GetPoint(b, x, y)
            ratio_graph.SetPoint(b, x, ratio)
        return ratio_graph

    def calculate_ratio_hist(self, compare):
        ratio_hist = compare.Clone("ratio_" + compare.GetName())
        ratio_hist.Divide(self.reference)
        FM.set_title_y(ratio_hist, "ratio")
        return ratio_hist


class RatioPlotter(object):
    def __init__(self, **kwargs):
        if not "reference" in kwargs:
            _logger.error("Missing reference")
            raise InvalidInputError("Missing reference")
        self.reference = kwargs["reference"]
        self.plot_config = kwargs["plot_config"]
        self.compare = kwargs["compare"]
        if not isinstance(self.compare, list):
            self.compare = [self.compare]
        if not self.plot_config.name.startswith("ratio"):
            self.plot_config.name = "ratio_" + self.plot_config.name
        self.ratio_calculator = RatioCalculator(**kwargs)

    def make_ratio_plot(self):
        ratios = self.ratio_calculator.calculate_ratio()
        if not isinstance(self.reference, ROOT.TEfficiency):
            return self.make_ratio_histogram(ratios)
        elif isinstance(self.reference, ROOT.TEfficiency):
            return self.make_ratio_tefficiency(ratios)

    def make_ratio_histogram(self, ratios):
        self.plot_config.xtitle = self.reference.GetXaxis().GetTitle()
        if len(self.compare) > 1:
            colors = get_colors(self.compare)
            self.plot_config.color = colors
        self.plot_config.ordering = None
        return PT.plot_histograms(ratios, self.plot_config)

    def make_ratio_tefficiency(self, ratios):
        c = ROOT.TCanvas("C", "C")
        c.cd()
        colors = None
        if len(self.compare) > 1:
            colors = get_colors(self.compare)
        self.reference.Draw("ap")
        self.plot_config.xtitle = self.reference.GetPaintedGraph().GetXaxis().GetTitle()
        self.plot_config.ytitle = "ratio"
        index = ROOT.gROOT.GetListOfCanvases().IndexOf(c)
        ROOT.gROOT.GetListOfCanvases().RemoveAt(index)
        if colors is not None:
            self.plot_config.color = colors[0]
        ratio_canvas = PT.plot_graph(ratios[0], self.plot_config)
        for ratio in ratios[1:]:
            if colors is not None:
                self.plot_config.color = colors[ratios.index(ratio)]
            PT.add_graph_to_canvas(ratio_canvas, ratio, self.plot_config)
        return ratio_canvas<commit_msg>Implement FONLL comparison to Pythia<commit_after>import ROOT
from PyAnalysisTools.base import _logger, InvalidInputError
from PyAnalysisTools.PlottingUtils import Formatting as fm
from PyAnalysisTools.PlottingUtils import PlottingTools as pt
from PyAnalysisTools.PlottingUtils import HistTools as ht
from PyAnalysisTools.PlottingUtils.HistTools import get_colors


class RatioCalculator(object):
    def __init__(self, **kwargs):
        self.reference = kwargs["reference"]
        self.compare = kwargs["compare"]
        self.rebin = kwargs["rebin"]

    def calculate_ratio(self):
        if isinstance(self.reference, ROOT.TEfficiency):
            calc_func = "calculate_ratio_tefficiency"
        elif isinstance(self.reference, ROOT.TH1):
            calc_func = "calculate_ratio_hist"
        ratios = []
        if isinstance(self.compare, list):
            for obj in self.compare:
                ratios.append(getattr(self, calc_func)(obj))
        else:
            ratios.append(getattr(self, calc_func)(self.compare))
        return ratios

    def calculate_ratio_tefficiency(self, compare):
        ratio_graph = self.reference.GetPaintedGraph().Clone("ratio_" + compare.GetName())
        nbins = ratio_graph.GetN()
        for b in range(nbins):
            eff_compare = compare.GetEfficiency(b + 1)
            eff_reference = self.reference.GetEfficiency(b + 1)
            if eff_reference == 0. and eff_compare == 0.:
                ratio = 1.
            elif eff_reference == 0. and eff_compare != 0.:
                ratio = 0.
            else:
                ratio = eff_compare / eff_reference
            x = ROOT.Double(0.)
            y = ROOT.Double(0.)
            ratio_graph.GetPoint(b, x, y)
            ratio_graph.SetPoint(b, x, ratio)
        return ratio_graph

    def calculate_ratio_hist(self, compare):
        if self.rebin:
            compare = ht.rebin(compare, self.rebin)
            self.reference = ht.rebin(self.reference, self.rebin)
        ratio_hist = compare.Clone("ratio_" + compare.GetName())
        ratio_hist.Divide(self.reference)
        fm.set_title_y(ratio_hist, "ratio")
        return ratio_hist


class RatioPlotter(object):
    def __init__(self, **kwargs):
        if not "reference" in kwargs:
            _logger.error("Missing reference")
            raise InvalidInputError("Missing reference")
        self.reference = kwargs["reference"]
        self.plot_config = kwargs["plot_config"]
        self.compare = kwargs["compare"]
        if not isinstance(self.compare, list):
            self.compare = [self.compare]
        if not self.plot_config.name.startswith("ratio"):
            self.plot_config.name = "ratio_" + self.plot_config.name
        self.ratio_calculator = RatioCalculator(**kwargs)

    def make_ratio_plot(self):
        ratios = self.ratio_calculator.calculate_ratio()
        if not isinstance(self.reference, ROOT.TEfficiency):
            return self.make_ratio_histogram(ratios)
        elif isinstance(self.reference, ROOT.TEfficiency):
            return self.make_ratio_tefficiency(ratios)

    def make_ratio_histogram(self, ratios):
        self.plot_config.xtitle = self.reference.GetXaxis().GetTitle()
        if len(self.compare) > 1:
            colors = get_colors(self.compare)
            self.plot_config.color = colors
        self.plot_config.ordering = None
        return pt.plot_histograms(ratios, self.plot_config)

    def make_ratio_tefficiency(self, ratios):
        c = ROOT.TCanvas("C", "C")
        c.cd()
        colors = None
        if len(self.compare) > 1:
            colors = get_colors(self.compare)
        self.reference.Draw("ap")
        self.plot_config.xtitle = self.reference.GetPaintedGraph().GetXaxis().GetTitle()
        self.plot_config.ytitle = "ratio"
        index = ROOT.gROOT.GetListOfCanvases().IndexOf(c)
        ROOT.gROOT.GetListOfCanvases().RemoveAt(index)
        if colors is not None:
            self.plot_config.color = colors[0]
        ratio_canvas = pt.plot_graph(ratios[0], self.plot_config)
        for ratio in ratios[1:]:
            if colors is not None:
                self.plot_config.color = colors[ratios.index(ratio)]
            pt.add_graph_to_canvas(ratio_canvas, ratio, self.plot_config)
        return ratio_canvas
{'fraction_non_alphanumeric': 0.10073062642232603, 'fraction_numerical': 0.003832794346628339, 'mean_word_length': 2.5039865715484684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1075337', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2394, 'n_words': 489}