Esempio n. 1
0
def draw(var1=None, cut=1, t1=None, t2=None, w1='1', w2='1',
         name1=None, name2=None,
         normalize=None, nbins=20, xmin=0, xmax=200, var2=None):
    if var2 is None:
        var2 = var1
    if t1 is None:
        t1 = tree1
    if t2 is None:
        t2 = tree2
    if name1 is None:
        name1 = a1
    if name2 is None:
        name2 = a2
    print 'tree1',
    print '\t var   : ' , var1
    print '\t weight:', w1
    print 'tree2',
    print '\t var   : ' , var2
    print '\t weight:', w2
    print 'cut', cut
    global legend
    h1 = TH1F(hname(), '', nbins, xmin, xmax)
    h1.Sumw2()
    t1.Project(h1.GetName(), var1,'({cut})*({w1})'.format(cut=cut,w1=w1),'')
    h2 = h1.Clone(hname())
    h2.Sumw2()
    t2.Project(h2.GetName(), var2,'({cut})*({w2})'.format(cut=cut,w2=w2),'')
    if normalize == None:
        pass
    elif normalize == -1:
        h1.Scale(1./h1.Integral())
        h2.Scale(1./h2.Integral())
    elif normalize>0:
        h2.Scale( normalize )
    sBlue.markerStyle = 25
    sBlue.formatHisto(h2)
    sData.formatHisto(h1)
    h2.SetFillStyle(1001)
    h2.SetFillColor(5)
    h1.SetMarkerSize(0.8)
    h2.SetMarkerSize(0.8)
    h1.SetStats(0)
    h2.SetStats(0)
    if name1 is None: name1 = t1.GetTitle()
    if name2 is None: name2 = t2.GetTitle()
    h1.SetTitle(name1)
    h2.SetTitle(name2)
    legend = TLegend(0.55,0.7,0.88,0.88)
    legend.SetFillColor(0)
    legend.AddEntry(h1, name1, 'lp')
    legend.AddEntry(h2, name2, 'lpf')

    print 'number of selected rows:', t1.GetSelectedRows(), t2.GetSelectedRows()

    comparator = HistComparator(var1, h1, h2)
    comparator.draw(opt2='e2')
    return comparator
Esempio n. 2
0
def draw(var1=None, cut=1, t1=None, t2=None, w1='1', w2='1',
         name1=None, name2=None,
         normalize=None, nbins=20, xmin=0, xmax=200, var2=None):
    if var2 is None:
        var2 = var1
    if t1 is None:
        t1 = tree1
    if t2 is None:
        t2 = tree2
    if name1 is None:
        name1 = a1
    if name2 is None:
        name2 = a2
    print 'tree1',
    print '\t var   : ' , var1
    print '\t weight:', w1
    print 'tree2',
    print '\t var   : ' , var2
    print '\t weight:', w2
    print 'cut', cut
    global legend
    h1 = TH1F(hname(), '', nbins, xmin, xmax)
    h1.Sumw2()
    t1.Project(h1.GetName(), var1,'({cut})*({w1})'.format(cut=cut,w1=w1),'')
    h2 = h1.Clone(hname())
    h2.Sumw2()
    t2.Project(h2.GetName(), var2,'({cut})*({w2})'.format(cut=cut,w2=w2),'')
    if normalize == None:
        pass
    elif normalize == -1:
        h1.Scale(1./h1.Integral())
        h2.Scale(1./h2.Integral())
    elif normalize>0:
        h2.Scale( normalize )
    sBlue.markerStyle = 25
    sBlue.formatHisto(h2)
    sBlack.formatHisto(h1)
    h2.SetFillStyle(1001)
    h2.SetFillColor(5)
    h1.SetMarkerSize(0.8)
    h2.SetMarkerSize(0.8)
    h1.SetStats(0)
    h2.SetStats(0)
    if name1 is None: name1 = t1.GetTitle()
    if name2 is None: name2 = t2.GetTitle()
    h1.SetTitle(name1)
    h2.SetTitle(name2)
    legend = TLegend(0.55,0.7,0.88,0.88)
    legend.SetFillColor(0)
    legend.AddEntry(h1, name1, 'lp')
    legend.AddEntry(h2, name2, 'lpf')

    print 'number of selected rows:', t1.GetSelectedRows(), t2.GetSelectedRows()

    comparator = HistComparator(var1, h1, h2)
    comparator.draw()
    return comparator
Esempio n. 3
0
class Comparator(object):
    '''Compare the histograms in a TDirectory to the histograms in another TDirectory'''
    def __init__(self,
                 info1,
                 info2,
                 outdir='Plots_Comparator',
                 filter='.*',
                 title1=None,
                 title2=None):
        '''

        info1 and info2 are of the form <root_file>:<directory_in_file>

        filter is a regexp pattern to select histograms to be compared according
        to their name.

        title1 and title2 are titles for both sets of histograms.

        outdir is the directory where all plots will be saved.
        '''
        self.info1 = info1
        self.info2 = info2
        self.outdir = outdir
        self.legend = None
        self.filter = re.compile(filter)
        self.hcomp = None
        self.title1 = title1
        self.title2 = title2

    def browse(self, wait=True):
        '''Browse the two directories and make the plots.

        if wait is True, waits for any key before moving to next histogram.
        '''
        # self.can = TCanvas ()
        # threshold = 0.3
        # self.pad_ratio = TPad ('ratio','ratio',0,0,1,threshold)
        # self.pad_ratio.Draw()
        # self.pad_main  = TPad ('main','main',0,threshold,1,1)
        # self.pad_main.Draw()
        maindir = self.outdir
        if os.path.isdir(maindir):
            os.system('rm -r ' + maindir)
        os.mkdir(maindir)
        for h1name, h1 in sorted(self.info1.hists.iteritems()):
            h2 = self.info2.hists.get(h1name, None)
            if h2 is None:
                pass
            # print h1name, 'not in', d2dir.GetName()
            else:
                plotdir = '/'.join([maindir, os.path.dirname(h1name)])
                try:
                    mkdir_p(plotdir)
                except:
                    pass
                h1.SetTitle(h1name)
                h2.SetTitle(h1name)
                if not self.filter.search(h1name):
                    print 'Skipping', h1name
                    continue
                self._drawHists(h1, h2, h1name)
                if wait: res = raw_input('')

    def _drawHists(self, h1, h2, h1name):
        '''Compare 2 histograms'''

        h1.SetMarkerColor(1)
        h1.SetMarkerStyle(21)
        h1.SetLineColor(1)
        h1.SetMarkerSize(0.8)

        h2.SetFillColor(16)
        h2.SetFillStyle(1001)
        h2.SetMarkerColor(1)
        h2.SetMarkerStyle(4)
        h2.SetLineColor(1)
        h2.SetMarkerSize(0.8)

        title1 = self.title1
        title2 = self.title2
        if title1 is None:
            title1 = self.info1.name
        if title2 is None:
            title2 = self.info2.name
        # import pdb; pdb.set_trace()
        if not self.hcomp:
            self.hcomp = HistComparator(h1name, h1, h2, title1, title2)
        else:
            self.hcomp.set(h1name, h1, h2, title1, title2)
        self.hcomp.draw()
        print 'Draw', h1name, 'done'
        pngname = '/'.join([self.outdir, h1name + '.png'])
        print pngname
        self.hcomp.can.SaveAs(pngname)
        return True
Esempio n. 4
0
class Comparator(object):
    '''Compare the histograms in a TDirectory to the histograms in another TDirectory'''

    def __init__(self, info1, info2, outdir='Plots_Comparator', filter='.*',
                 title1=None, title2=None):
        '''

        info1 and info2 are of the form <root_file>:<directory_in_file>

        filter is a regexp pattern to select histograms to be compared according
        to their name.

        title1 and title2 are titles for both sets of histograms.

        outdir is the directory where all plots will be saved.
        '''
        self.info1 = info1
        self.info2 = info2
        self.outdir = outdir
        self.legend = None
        self.filter = re.compile(filter)
        self.hcomp = None
        self.title1 = title1
        self.title2 = title2

    def browse(self, wait = True):
        '''Browse the two directories and make the plots.

        if wait is True, waits for any key before moving to next histogram.
        '''
        # self.can = TCanvas ()
        # threshold = 0.3
        # self.pad_ratio = TPad ('ratio','ratio',0,0,1,threshold)
        # self.pad_ratio.Draw()
        # self.pad_main  = TPad ('main','main',0,threshold,1,1)
        # self.pad_main.Draw()
        maindir = self.outdir
        if os.path.isdir(maindir):
            os.system( 'rm -r ' + maindir)
        os.mkdir(maindir)
        for h1name, h1 in sorted(self.info1.hists.iteritems()):
            h2 = self.info2.hists.get(h1name, None)
            if h2 is None:
                pass
            # print h1name, 'not in', d2dir.GetName()
            else:
                plotdir = '/'.join([maindir,os.path.dirname(h1name)])
                try:
                    mkdir_p( plotdir )
                except:
                    pass
                h1.SetTitle(h1name)
                h2.SetTitle(h1name)
                if not self.filter.search( h1name ):
                    print 'Skipping', h1name
                    continue
                self._drawHists(h1, h2, h1name)
                if wait : res = raw_input('')
                
    def _drawHists(self, h1, h2, h1name):
        '''Compare 2 histograms'''
        
        h1.SetMarkerColor(1) 
        h1.SetMarkerStyle(21) 
        h1.SetLineColor(1) 
        h1.SetMarkerSize(0.8)
        
        h2.SetFillColor(16) 
        h2.SetFillStyle(1001)
        h2.SetMarkerColor(1)                    
        h2.SetMarkerStyle(4)                   
        h2.SetLineColor(1)
        h2.SetMarkerSize(0.8)
        
        title1=self.title1
        title2=self.title2
        if title1 is None:
            title1 = self.info1.name
        if title2 is None:
            title2 = self.info2.name
        # import pdb; pdb.set_trace()
        if not self.hcomp:
            self.hcomp = HistComparator(h1name,h1, h2, title1, title2)
        else:
            self.hcomp.set(h1name, h1, h2, title1, title2)
        self.hcomp.draw()
        print 'Draw', h1name, 'done'
        pngname = '/'.join([self.outdir,h1name+'.png'])
        print pngname
        self.hcomp.can.SaveAs(pngname)
        return True