Exemplo n.º 1
0
 def __str__(self):
     list_of_files = self.ch.GetListOfFiles()
     next_file = TIter(list_of_files)
     self.logger.logDEBUG("Input file list is given below.")
     self.logger.logDEBUG("Absolute path: ",
                          os.path.dirname(next_file.GetTitle()))
     ifile = next_file.Begin()
     for f in range(list_of_files.GetEntries()):
         self.logger.logDEBUG("\t{0}".format(
             os.path.basename(ifile.GetTitle())),
                              Stamp=False)
         ifile = next_file.Next()
     self.logger.logDEBUG("End of the input file list.")
     return 1
Exemplo n.º 2
0
    def LoadAllHists(self,
                     filename,
                     prefix="",
                     suffix="",
                     low=100,
                     high=1000,
                     binwidth=20,
                     setOverflow=True):
        #
        # Same as load_all_hists() but reduces the hist range
        # Range is hardcoded at the moment 100-550
        # Also, trying to merge electroweak category
        #

        # these histograms will be merged together under name in ewkName
        ewk = ['Wjets', 'WW', 'WZ', 'ZZ', 'Zjets', 'SingleToptW', 'SingleTopT']
        ewkName = 'Ewk'

        infile = TFile(filename, "read")
        _keys = TIter(infile.GetListOfKeys())
        _key = _keys.Begin()
        _nhists = 0
        while _key:
            _name = _key.GetName()
            _name = str(prefix + _name + suffix)

            # merge certain electroweak histos into one
            for hname in ewk:
                _name = _name.replace(hname + '_', ewkName + '_')

            if _name not in self.hist_names:
                self.hist_names.append(_name)

            #_id = tuple(_name.split('_'))
            _nhists += 1
            #self.hist_ids.append(_id)
            _xbins = array('d')
            _low = low
            _high = high
            _width = binwidth
            for ind in range(0, int((_high - _low) / _width + 0.5) + 1, 1):
                #for binLow in range(_low, int(_high + _width+0.1), _width):
                binLow = _low + ind * _width
                _xbins.append(binLow)
            _hist = copy.deepcopy(infile.Get(_key.GetName()))

            #firstOverflowBin = _hist.FindFirstBinAbove(_high-1.5*_width) # including the bin before overflow
            originalBinWidth = _hist.GetBinWidth(
                1)  # assume the input binned uniformly
            firstOverflowBin = int((_high - _low) / originalBinWidth)
            lastOverflowBin = _hist.GetNbinsX(
            ) + 1  # including the actual overflow
            overflowErr = ROOT.Double()
            _overflow = _hist.IntegralAndError(firstOverflowBin,
                                               lastOverflowBin, overflowErr)
            print originalBinWidth, firstOverflowBin, lastOverflowBin, _overflow, overflowErr
            if setOverflow:
                _hist.SetBinContent(firstOverflowBin, _overflow)
                _hist.SetBinError(firstOverflowBin, overflowErr)
            _hist = _hist.Rebin(len(_xbins) - 1, 'copyhist', _xbins)

            # if this hist already exists, append to it
            if _name in self.hists:
                self.hists[_name].Add(copy.deepcopy(_hist))
            else:
                self.hists[_name] = copy.deepcopy(_hist)
                self.hists[_name].SetName(_name)

            _key = _keys.Next()

        print 'Loaded', len(self.hists), 'histograms'
Exemplo n.º 3
0
    def load_all_hists(self,
                       filename,
                       prefix="",
                       suffix="",
                       zero_overflow=False,
                       projection_x=None,
                       projection_y=None,
                       merge_ewk=False,
                       merge_top=False,
                       strip_suffix=False,
                       rebin_ngroup=None,
                       rebin_low=None,
                       rebin_high=None,
                       move_overflow=False,
                       scale_bin_error=None,
                       excluded_list=[]):
        #
        # merge histograms, works on 1d, 2d...
        #

        legend = '[TprimeData.load_all_hists]:'

        #print legend, excluded_list

        # special prefix for 1D projections
        prefix_1d = 'hist_'

        # these histograms will be merged together under name in ewkName
        #ewk = ['Wjets', 'WW', 'WZ', 'ZZ', 'Zjets', 'SingleToptW', 'SingleTopT', 'SM', 'QCD','SinTop']
        ewk = [
            'SingleToptW', 'SingleTopT', 'SingleTopS', 'SingleTopbarT',
            'SingleTopbarS', 'SinTop', 'Wjets', 'WW', 'WZ', 'ZZ', 'Zjets',
            'SM', 'QCD', 'Ewk'
        ]
        ewkName = 'ewk'

        # these histograms will be merged together under name in topName
        top = ['TTjets', 'TOP', 'Top', 'top']
        topName = 'top'

        # these histogram names will be changed to tprimeName
        tprime = ['TPrime', 'Tprime', 'tprime', 'TPRIME']
        tprimeName = 'tprime'

        # these histogram names will be changed to dataName
        datanames = ['DATA', 'Data', 'data']
        dataName = 'DATA'

        infile = TFile(filename, "read")
        _keys = TIter(infile.GetListOfKeys())
        _key = _keys.Begin()
        _nhists = 0
        while _key:
            _name = _key.GetName()

            # fix double underscores
            #            _new_name = ''
            #            _was_underscore = False
            #            for _let in _name:
            #                if _let=='_':
            #                    if _was_underscore:
            #                        print 'double underscore fixed'
            #                    else:
            #                        _was_underscore = True
            #                        _new_name += _let
            #                else:
            #                    _new_name += _let
            #                    _was_underscore = False
            #
            #            _name = _new_name

            if strip_suffix:
                #_name = _name.strip().split('_')[0]
                _newname = _name.strip().split('_')[0]
                if '__jes__plus' in _name:
                    _newname += '__jes__plus'
                if '__jes__minus' in _name:
                    _newname += '__jes__minus'
                _name = _newname

            _name = prefix + _name + suffix

            # merge certain histos into one
            if merge_ewk:
                for hname in ewk:
                    #_name = _name.replace(hname+'_', ewkName+'_')
                    _name = _name.replace(hname, ewkName)

            # merge top templates
            if merge_top:
                for hname in top:
                    #_name = _name.replace(hname+'_', topName+'_')
                    _name = _name.replace(hname, topName)

            # uniform signal names
            for hname in tprime:
                _name = _name.replace(hname, tprimeName)

            # uniform data names
            for hname in datanames:
                _name = _name.replace(hname, dataName)

            # skip histos from the excluded list
            if _name in excluded_list:
                _key = _keys.Next()
                continue

            _hist = copy.deepcopy(infile.Get(_key.GetName()))

            # scale error in each bin (in case of multiple use of the same source)
            _nbinx = _hist.GetNbinsX()
            _nbiny = None
            if 'TH2' in _hist.ClassName():
                _nbiny = _hist.GetNbinsY()

            if scale_bin_error:
                print legend, 'histogram errors are multiplied by', float(
                    scale_bin_error)
                for i in range(0, _nbinx + 2):
                    for j in range(0, _nbiny + 2):
                        _ibin = _hist.GetBin(i, j)
                        _err = _hist.GetBinError(_ibin)
                        _hist.SetBinError(_ibin, _err * float(scale_bin_error))

            # move over- and underflow to adjacent visible bins
            # fixme: unfinished
            if move_overflow:
                _binx = _hist.GetNbinsX()
                _biny = None
                if 'TH2' in _hist.ClassName():
                    _biny = _hist.GetNbinsY()
                    for i in range(0, _binx + 2):
                        for j in range(0, _biny + 2):
                            _ibin = GetBin(i, j)

            # project onto 1D if requested and the hist is 2D
            _class_name = _hist.ClassName()
            if 'TH2' in _class_name:
                if projection_x or projection_y:

                    _tmpname = _name + '_tmp'
                    _hist.SetName(_tmpname)

                    if projection_x:
                        _hist = copy.deepcopy(
                            _hist.ProjectionX(_name + '_proj', 0, -1, 'e'))
                    if projection_y:
                        _hist = copy.deepcopy(
                            _hist.ProjectionY(_name + '_proj', 0, -1, 'e'))

                    _hist.SetName(_name)

                    # rebin if requested
                    if rebin_ngroup:
                        self.tmphist = _hist
                        _hist = self.Rebin1dHist(self.tmphist,
                                                 rebin_ngroup,
                                                 rebin_low,
                                                 rebin_high,
                                                 move_overflow=True)

            # add the hist to the list and dictionary
            if _name not in self.hist_names:
                self.hist_names.append(_name)
                self.hists[_name] = copy.deepcopy(_hist)
            else:
                self.hists[_name].Add(copy.deepcopy(_hist))

            _nhists += 1
            self.hists[_name].SetName(_name)

            # set True to remove overflow
            if zero_overflow:
                # zero overflow/underflow bins (assuming that they are already added to the adjacent visible bins)
                # print 'Will zero out overflow and underflow bins!!!'

                #print self.hists[_name].ClassName()
                if 'TH2' in self.hists[_name].ClassName():
                    print legend, 'TH2 detected...'
                    print legend, 'Will zero out overflow and underflow bins (no copying)!!!'
                    _binx = self.hists[_name].GetNbinsX()
                    _biny = self.hists[_name].GetNbinsY()
                    for _x in range(0, _binx + 2):
                        for _y in range(0, _biny + 2):
                            if _x == 0 or _x == _binx + 1 or _y == 0 or _y == _biny + 1:
                                # sanity check
                                #if self.hists[_name].GetBinContent(_x,_y) > 0.000001:
                                #    print 'Overflow:', self.hists[_name].GetBinContent(_x,_y), 'set to 0'
                                self.hists[_name].SetBinContent(_x, _y, 0.0)
                                self.hists[_name].SetBinError(_x, _y, 0.0)
                else:
                    print legend, 'cannot comply with request to zero out over-/underflow for 1D hist'

            _key = _keys.Next()

        # second loop over all hists
#        for name in self.hist_names:
#            # rebin
#            if rebin_ngroup:
#                self.hists[name] = copy.deepcopy(self.Rebin1dHist(self.hists[name],
#                                                                  rebin_ngroup, rebin_low, rebin_high,
#                                                                  move_overflow = True) )

        print 'Merged into', len(self.hists), 'histograms'