Example #1
0
    def drawHistogram(self, var, cut_str, dtype="float", **kwargs):
        name = self.name + "_" + Histogram.unique_name(var, cut_str, kwargs.get("weight"))

        plot_range = kwargs.get("plot_range", [100, 0, 100])

        weight_str = kwargs.get("weight", None)

        ROOT.gROOT.cd()
        if dtype=="float":
            hist = ROOT.TH1F("htemp", "htemp", plot_range[0], plot_range[1], plot_range[2])
        elif dtype=="int":
            hist = ROOT.TH1I("htemp", "htemp", plot_range[0], plot_range[1], plot_range[2])

        hist.Sumw2()

        draw_cmd = var + ">>htemp"

        if weight_str:
            cutweight_cmd = weight_str + " * " + "(" + cut_str + ")"
        else:
            cutweight_cmd = "(" + cut_str + ")"

        self.logger.debug("Calling TTree.Draw('%s', '%s')" % (draw_cmd, cutweight_cmd))

        n_entries = self.tree.Draw(draw_cmd, cutweight_cmd, "goff")
        self.logger.debug("Histogram drawn with %d entries, integral=%.2f" % (n_entries, hist.Integral()))

        if n_entries<0:
            raise HistogramException("Could not draw histogram: %s" % self.name)

        if hist.Integral() != hist.Integral():
            raise HistogramException("Histogram had 'nan' Integral(), probably weight was 'nan'")
        if not hist:
            raise TObjectOpenException("Could not get histogram: %s" % hist)
        if hist.GetEntries() != n_entries:
            raise HistogramException("Histogram drawn with %d entries, but actually has %d" % (n_entries, hist.GetEntries()))
        hist_new = hist.Clone(filter_alnum(name))

        hist = hist_new
        hist.SetTitle(name)
        hist_ = Histogram()
        hist_.setHist(hist, histogram_entries=n_entries, var=var,
            cut=cut_str, weight=kwargs["weight"] if "weight" in kwargs.keys() else None,
            sample_name=self.name,
            sample_entries_total=self.getTotalEventCount(),
            sample_entries_cut=self.getEventCount(),

        )
        return hist_
Example #2
0
    def drawHistogram(self, var, cut_str, dtype="float", **kwargs):
        name = self.name + "_" + Histogram.unique_name(var, cut_str,
                                                       kwargs.get("weight"))

        plot_range = kwargs.get("plot_range", [100, 0, 100])

        weight_str = kwargs.get("weight", None)

        ROOT.gROOT.cd()
        if dtype == "float":
            hist = ROOT.TH1F("htemp", "htemp", plot_range[0], plot_range[1],
                             plot_range[2])
        elif dtype == "int":
            hist = ROOT.TH1I("htemp", "htemp", plot_range[0], plot_range[1],
                             plot_range[2])

        hist.Sumw2()

        draw_cmd = var + ">>htemp"

        if weight_str:
            cutweight_cmd = weight_str + " * " + "(" + cut_str + ")"
        else:
            cutweight_cmd = "(" + cut_str + ")"

        self.logger.debug("Calling TTree.Draw('%s', '%s')" %
                          (draw_cmd, cutweight_cmd))

        n_entries = self.tree.Draw(draw_cmd, cutweight_cmd, "goff")
        self.logger.debug("Histogram drawn with %d entries, integral=%.2f" %
                          (n_entries, hist.Integral()))

        if n_entries < 0:
            raise HistogramException("Could not draw histogram: %s" %
                                     self.name)

        if hist.Integral() != hist.Integral():
            raise HistogramException(
                "Histogram had 'nan' Integral(), probably weight was 'nan'")
        if not hist:
            raise TObjectOpenException("Could not get histogram: %s" % hist)
        if hist.GetEntries() != n_entries:
            raise HistogramException(
                "Histogram drawn with %d entries, but actually has %d" %
                (n_entries, hist.GetEntries()))
        hist_new = hist.Clone(filter_alnum(name))

        hist = hist_new
        hist.SetTitle(name)
        hist_ = Histogram()
        hist_.setHist(
            hist,
            histogram_entries=n_entries,
            var=var,
            cut=cut_str,
            weight=kwargs["weight"] if "weight" in kwargs.keys() else None,
            sample_name=self.name,
            sample_entries_total=self.getTotalEventCount(),
            sample_entries_cut=self.getEventCount(),
        )
        return hist_
Example #3
0
 def unique_name(var, cut, weight):
     cut_str = cut if cut is not None else "NOCUT"
     weight_str = weight if weight is not None else "NOWEIGHT"
     return filter_alnum(var + "_" + cut_str + "_" + weight_str)
Example #4
0
 def unique_name(var, cut, weight):
     cut_str = cut if cut is not None else "NOCUT"
     weight_str = weight if weight is not None else "NOWEIGHT"
     return filter_alnum(var + "_" + cut_str + "_" + weight_str)