def hist2D(self, var1, nBins1, a1, b1, var2, nBins2, a2, b2, **kwargs):
        name = kwargs.get(
            'name',
            makeHistName(self.label + "_merged", "%s_vs_%s" % (var1, var2)))
        title = kwargs.get('title', self.label)
        blind = kwargs.get('blind', self.blind)
        kwargs['scale'] = self.scale * kwargs.get('scale',
                                                  1.0)  # pass scale down

        verbosity = kwargs.get('verbosity', 0)
        printVerbose(
            ">>>\n>>> Samples - %s, %s vs. %s: %s" %
            (color(name, color="grey"), var1, var2, self.filenameshort),
            verbosity)
        printVerbose(">>>    scale: %.4f" % (kwargs['scale']), verbosity)

        hist2D = TH2D(name, title, nBins2, a2, b2, nBins1, a1, b1)
        for sample in self.samples:
            if 'name' in kwargs:  # prevent memory leaks
                kwargs['name'] = makeHistName(
                    sample.label, name.replace(self.label + '_', ''))
            hist2D.Add(
                sample.hist2D(var1, nBins1, a1, b1, var2, nBins2, a2, b2,
                              **kwargs))

        return hist2D
    def hist(self, var, nBins, a, b, **kwargs):
        name = kwargs.get('name', makeHistName(self.label + "_merged", var))
        name += kwargs.get('append_name', "")
        title = kwargs.get('title', self.label)
        blind = kwargs.get('blind', self.blind)
        kwargs['scale'] = self.scale * kwargs.get('scale',
                                                  1.0)  # pass scale down

        verbosity = kwargs.get('verbosity', 0)
        printVerbose(
            ">>>\n>>> Samples - %s, %s: %s" %
            (color(name, color="grey"), var, self.filenameshort), verbosity)
        printVerbose(">>>    scale: %.4f" % (kwargs['scale']), verbosity)

        hist = TH1D(name, title, nBins, a, b)
        hist.Sumw2()
        for sample in self.samples:
            if 'name' in kwargs:  # prevent memory leaks
                kwargs['name'] = makeHistName(
                    sample.label, name.replace(self.label + '_', ''))
            hist_new = sample.hist(var, nBins, a, b, **kwargs)
            hist.Add(hist_new)
            printVerbose(
                ">>>    sample %s added with %.1f events (%d entries)" %
                (sample.label, hist_new.Integral(), hist_new.GetEntries()),
                verbosity,
                level=2)

        if verbosity > 2: printBinError(hist)
        return hist
Exemple #3
0
 def hist2D(self, var1, nBins1, a1, b1, var2, nBins2, a2, b2, **kwargs):
     """Make a 2D histogram with a tree."""
     
     scale   = kwargs.get('scale', 1.0) * self.scale
     tree    = self.file.Get(self.treeName)
     name    = kwargs.get('name',  makeHistName(self.label, "%s_vs_%s" % (var1,var2)))
     title   = kwargs.get('title', self.label)
     verbosity = kwargs.get('verbosity', 0)
     
     blindcuts = ""
     if var1 in self.blind: blindcuts += self.blind[var1]
     if var2 in self.blind: blindcuts += self.blind[var2]
     weight = combineWeights(self.weight, kwargs.get('weight', ""))
     cuts   = combineCuts(self.cuts, kwargs.get('cuts', ""), blindcuts, weight=weight)
     printVerbose(">>>>\n>>> Sample - %s, %s vs. %s: %s" % (color(name,color="grey"), var1, var2, self.filenameshort),verbosity)
     printVerbose(">>>    scale:  %.4f"    % (scale),verbosity)
     printVerbose(">>>    weight: %s"      % (weight),verbosity)
     printVerbose(">>>    %s" % (cuts),verbosity)
     
     hist2D = TH2F(name, title, nBins2, a2, b2, nBins1, a1, b1)
     out = tree.Draw("%s:%s >> %s" % (var1,var2,name), cuts, "gOff")
     if out < 0: print error("Drawing histogram for %s sample failed!" % (title))
     
     #if scale is not 1.0: hist.Scale(scale)
     #if scale is     0.0: print warning("Scale of %s is 0!" % self.label)
     return hist2D
Exemple #4
0
 def hist(self, var, nBins, a, b, **kwargs):
     """Make a histogram with a tree."""
     
     scale   = kwargs.get('scale', 1.0) * self.scale
     tree    = self.file.Get(self.treeName)
     name    = kwargs.get('name',  makeHistName(self.label, var))
     title   = kwargs.get('title', self.label)
     shift   = kwargs.get('shift', 0)
     smear   = kwargs.get('smear', 0)
     verbosity = kwargs.get('verbosity', 0)
     
     if self.isSignal and self.scale is not self.scaleBU and self.scaleBU:
         title += " (#times%d)" % (self.scale/self.scaleBU)
     
     blindcuts = ""
     if var in self.blind: blindcuts = self.blind[var]
     weight = combineWeights(self.weight, kwargs.get('weight', ""))
     cuts   = combineCuts(self.cuts, kwargs.get('cuts', ""), blindcuts, weight=weight)
     
     hist = TH1F(name, title, nBins, a, b)
     out = tree.Draw("%s >> %s" % (var,name), cuts, "gOff")
     
     if shift or smear:
         mean0 = hist.GetMean()
         #smear = min(1,smear)
         #smear = sqrt(smear*smear-1) #*sigma
         #tree.SetAlias("rng","sin(2*pi*rndm)*sqrt(-2*log(rndm))")
         var2 = "%s*%s + %s + %s*%s" % (var,smear,shift,(1-smear),mean0)
         tree.Draw("%s >> %s" % (var2,name), cuts, "gOff")
     if out < 0: print error("Drawing histogram for %s sample failed!" % (title))
     
     if scale is not 1.0: hist.Scale(scale)
     if scale is     0.0: print warning("Scale of %s is 0!" % self.label)
     #print hist.GetEntries()
     #gDirectory.Delete(label)
     
     printVerbose(">>>\n>>> Sample - %s, %s: %s" % (color(name,color="grey"), var, self.filenameshort),verbosity)
     printVerbose(">>>    scale:  %.4f (%.4f)" % (scale,self.scale),verbosity)
     printVerbose(">>>    weight: %s" % (("\n>>>%s*("%(' '*18)).join(weight.rsplit('*(',max(0,weight.count("*(")-1)))),verbosity)
     printVerbose(">>>    %s" % (cuts.replace("*(","\n>>>%s*("%(' '*18))),verbosity)
     return hist
    def hist(self, var, nBins, a, b, **kwargs):
        """Make a histogram with a tree."""

        scale = kwargs.get('scale', 1.0) * self.scale
        treeName = kwargs.get('treeName', self.treeName)
        name = kwargs.get('name', makeHistName(self.label, var))
        name += kwargs.get('append_name', "")
        title = kwargs.get('title', self.label)
        shift = kwargs.get('shift', 0)
        smear = kwargs.get('smear', 0)
        blind = kwargs.get('blind', self.blind)
        verbosity = kwargs.get('verbosity', 0)

        if self.isSignal and self.scale is not self.scaleBU and self.scaleBU:
            title += " (#times%d)" % (self.scale / self.scaleBU)

        blindcuts = ""
        if var in blind and "SS" not in name:
            blindcuts = blind[
                var]  # TODO: blind by removing bins from hist or rounding? FindBin(a), SetBinContent
        weight = combineWeights(self.weight, kwargs.get('weight', ""))
        cuts = combineCuts(self.cuts,
                           kwargs.get('cuts', ""),
                           blindcuts,
                           weight=weight)

        tree = self.file.Get(treeName)
        if not tree or not isinstance(tree, TTree):
            print error("Could not find tree \"%s\" for %s! Check %s" %
                        (treeName, self.label, self.filenameshort))

        hist = TH1D(name, title, nBins, a, b)
        hist.Sumw2()
        out = tree.Draw("%s >> %s" % (var, name), cuts, "gOff")

        if shift or (smear and smear != 1):
            mean0 = hist.GetMean()
            #smear = min(1,smear)
            #smear = sqrt(smear*smear-1) #*sigma
            #tree.SetAlias("rng","sin(2*pi*rndm)*sqrt(-2*log(rndm))")
            var2 = "%s*%s + %s + %s*%s" % (var, smear, shift,
                                           (1 - smear), mean0)
            tree.Draw("%s >> %s" % (var2, name), cuts, "gOff")
        if out < 0:
            print error("Drawing histogram for %s sample failed!" % (title))

        if scale is not 1.0: hist.Scale(scale)
        if scale is 0.0: print warning("Scale of %s is 0!" % self.label)
        if verbosity > 2: printBinError(hist)
        #print hist.GetEntries()
        #gDirectory.Delete(label)

        printVerbose(
            ">>>\n>>> Sample - %s, %s: %s (%s)" % (color(
                name, color="grey"), var, self.filenameshort, self.treeName),
            verbosity)
        printVerbose(">>>    scale:   %.4f (%.4f)" % (scale, self.scale),
                     verbosity)
        printVerbose(
            ">>>    weight:  %s" % (("\n>>>%s*(" % (' ' * 18)).join(
                weight.rsplit('*(', max(0,
                                        weight.count("*(") - 1)))), verbosity)
        printVerbose(
            ">>>    entries: %d (%.2f integral)" %
            (hist.GetEntries(), hist.Integral()), verbosity)
        printVerbose(
            ">>>    %s" % (cuts.replace("*(", "\n>>>%s*(" % (' ' * 18))),
            verbosity)
        return hist