コード例 #1
0
class ShapeLoader:
    '''Load the histogram data from the shape file
    + Yields
    + Nuisance shapes and parameters'''
    _log = logging.getLogger('ShapeLoader')

    def __init__(self, path):
        self._src = ROOT.TFile.Open(path)
        self._yields = OrderedDict()

    def __del__(self):
        del self._src

    def yields(self):
        return self._yields.copy()

    def effects(self):
        return self._effects.copy()

    def load(self):
        # load the list of processes
        processes = sorted([ p.GetName() for p in self._src.Get('processes') ])
        # load the histograms and calculate the yields
        names = sorted([ k.GetName() for k in self._src.GetListOfKeys() ])
        names.remove('processes')


        self._nominals    = []
        self._systematics = []
        
        self._nominals = [ ('histo_'+p,p) for p in processes if 'histo_'+p in names] 
        if len(self._nominals) != len(processes):
            raise RuntimeError('Not all process shapes have been found')

        for p in processes:
            systre = re.compile('^histo_%s_(.+)(Up|Down)$' % p)
            systp = []
            for name in names:
                m = systre.match(name)
                if not m: continue
                systp.append( (name,p,m.group(1),m.group(2)) )
#             systs = [ (name,p, for name in names if systre.match(name) ]
#               print 'xxx',p,systp
#                print 'xxx',p,m.group(1),m.group(2)
            self._systematics += systp

        self._systematics = sorted(self._systematics)

        for name,process in self._nominals:
#             process = self._nomRegex.match(name).group(1)
            h = self._src.Get(name)
            N =  h.Integral(0,h.GetNbinsX())
            entries = h.GetEntries()

            # TODO: DYTT cleanup
#             if entries < 5: continue

            self._yields[process] = Yield( N, name=process, entries=entries ) 
#             self._yields[process] = Yield( N, name=process, entries=entries, shape=h ) 
        
        ups = {}
        downs = {}
        for name,process,effect,var in self._systematics:
            # check for Up/Down
#             (process,effect,var) = self._systRegex.match(name).group(1,2,3)
            if var == 'Up': 
                if effect not in ups: ups[effect]= []
                ups[effect].append(process)
            elif var == 'Down':
                if effect not in downs: downs[effect]= []
                downs[effect].append(process)

        # check 
        for effect in ups:
            if set(ups[effect]) != set(downs[effect]):
                sUp = set(ups[effect])
                sDown = set(downs[effect])
                raise RuntimeError('Some systematics shapes for '+effect+' not found in up and down variation: \n '+', '.join( (sUp | sDown) - ( sUp & sDown ) ))
        
        # all checks out, save only one
        self._effects = ups
コード例 #2
0
class ShapeLoader:
    '''Load the histogram data from the shape file
    + Yields
    + Nuisance shapes and parameters'''

    def __init__(self, path):
#         self._systRegex = re.compile('^histo_([^_]+)_CMS_(.+)(Up|Down)$')
        self._systRegex = re.compile('^histo_([^_]+)_(.+)(Up|Down)$')
        self._nomRegex  = re.compile('^histo_([^_]+)$')
        self._src = ROOT.TFile.Open(path)
        self._yields = OrderedDict()

    def __del__(self):
        del self._src

    def yields(self):
        return self._yields.copy()

    def effects(self):
        return self._effects.copy()

    def load(self):
        # load the histograms and calculate the yields
        names = [ k.GetName() for k in self._src.GetListOfKeys()]
        
        self._nominals = sorted([ name for name in names if self._nomRegex.match(name) ]) 
        self._systematics = sorted([ name for name in names if self._systRegex.match(name) ])
#         print '\n'.join(self._nominals)
#         print '\n'.join(self._systematics)
        for name in self._nominals:
            process = self._nomRegex.match(name).group(1)
            h = self._src.Get(name)
            N =  h.Integral(0,h.GetNbinsX())
            entries = h.GetEntries()

            self._yields[process] = Yield( N, name=process, entries=entries ) 
#             self._yields[process] = Yield( N, name=process, entries=entries, shape=h ) 
#             print process, '%.3f' % h.Integral(0,h.GetNbins())
        
#         print self._systematics
        ups = {}
        downs = {}
        for name in self._systematics:
            # check for Up/Down
            (process,effect,var) = self._systRegex.match(name).group(1,2,3)
            if var == 'Up': 
                if effect not in ups: ups[effect]= []
                ups[effect].append(process)
            elif var == 'Down':
                if effect not in downs: downs[effect]= []
                downs[effect].append(process)

#         del ups['p_scale_j'][0]
#         del ups['p_scale_e'][1]
        # check 
        for effect in ups:
            if set(ups[effect]) != set(downs[effect]):
                sUp = set(ups[effect])
                sDown = set(downs[effect])
                raise RuntimeError('Some systematics shapes for '+effect+' not found in up and down variation: \n '+', '.join( (sUp | sDown) - ( sUp & sDown ) ))
        
        # all checks out, save only one
        self._effects = ups
コード例 #3
0
ファイル: chkHists.py プロジェクト: alessandrothea/usercode
    
    entries = OrderedDict()
    for arg in args:
        (file,names,processes) = GetHistPaths(arg)

        if processes and not opt.all:
            hp = [ 'histo_'+p for p in processes]
            names = [ n for n in names if n in hp ]
        
        files[arg] = (file,names,processes)


    if opt.diff:
        for arg,(file,names,processes) in files.iteritems():
            
            others = files.copy()
            del others[arg]
            ofiles = set()
            map(lambda x: ofiles.update(x[1]), others.itervalues())

            only = set(names)-ofiles
            print '-'*80
            print 'Histograms only in',arg,':'
            print '-'*80
            for h in only: print '   ',h
        sys.exit(0)



    entries = OrderedDict()
コード例 #4
0
ファイル: mkDatacards.py プロジェクト: latinos/HWWAnalysis
class ShapeLoader:
    '''Load the histogram data from the shape file
    + Yields
    + Nuisance shapes and parameters'''
    _log = logging.getLogger('ShapeLoader')

    def __init__(self, path):
        self._src = ROOT.TFile.Open(path)
        self._yields = OrderedDict()

    def __del__(self):
        del self._src

    def yields(self):
        return self._yields.copy()

    def effects(self):
        return self._effects.copy()

    def load(self):
        # load the list of processes
        processes = sorted([ p.GetName() for p in self._src.Get('processes') ])
        # load the histograms and calculate the yields
        names = sorted([ k.GetName() for k in self._src.GetListOfKeys() ])
        names.remove('processes')


        self._nominals    = []
        self._systematics = []
        
        self._nominals = [ ('histo_'+p,p) for p in processes if 'histo_'+p in names] 
        if len(self._nominals) != len(processes):
            raise RuntimeError('Not all process shapes have been found')

        for p in processes:
            systre = re.compile('^histo_%s_(.+)(Up|Down)$' % p)
            systp = []
            for name in names:
                m = systre.match(name)
                if not m: continue
                systp.append( (name,p,m.group(1),m.group(2)) )
#             systs = [ (name,p, for name in names if systre.match(name) ]
#               print 'xxx',p,systp
#                print 'xxx',p,m.group(1),m.group(2)
            self._systematics += systp

        self._systematics = sorted(self._systematics)

        for name,process in self._nominals:
#             process = self._nomRegex.match(name).group(1)
            h = self._src.Get(name)
            N =  h.Integral(0,h.GetNbinsX())
            entries = h.GetEntries()

            # TODO: DYTT cleanup
#             if entries < 5: continue

            self._yields[process] = Yield( N, name=process, entries=entries ) 
#             self._yields[process] = Yield( N, name=process, entries=entries, shape=h ) 
        
        ups = {}
        downs = {}
        for name,process,effect,var in self._systematics:
            # check for Up/Down
#             (process,effect,var) = self._systRegex.match(name).group(1,2,3)
            print "process,effect,var = ", process,effect,var
            if var == 'Up': 
                if effect not in ups: ups[effect]= []
                ups[effect].append(process)
            elif var == 'Down':
                if effect not in downs: downs[effect]= []
                downs[effect].append(process)
        # check 
        print " ups = ", ups
        print " downs = ", downs

        for effect in ups:
            if set(ups[effect]) != set(downs[effect]):
                sUp = set(ups[effect])
                sDown = set(downs[effect])
                raise RuntimeError('Some systematics shapes for '+effect+' not found in up and down variation: \n '+', '.join( (sUp | sDown) - ( sUp & sDown ) ))

        # all checks out, save only one
        self._effects = ups