def setUp(self):
     f = File('test.root', 'recreate')
     f.mkdir('TTbar_plus_X_analysis/EPlusJets/Ref selection', recurse=True)
     f.cd('TTbar_plus_X_analysis/EPlusJets/Ref selection')
     tree = create_test_tree()
     h = create_test_hist()
     h.write()
     tree.write()
     f.write()
     f.Close()
    def __return_histogram(self,
                           d_hist_info,
                           ignoreUnderflow=True,
                           useQCDControl=False,
                           useQCDSystematicControl=False):
        '''
        Takes basic histogram info and returns histo.
        Maybe this can move to ROOT_utilities?
        '''
        from rootpy.io.file import File
        from rootpy.plotting import Hist
        from dps.utils.hist_utilities import fix_overflow

        f = d_hist_info['input_file']
        tree = d_hist_info['tree']
        qcd_tree = d_hist_info["qcd_control_region"]
        qcd_tree_for_normalisation = d_hist_info["qcd_normalisation_region"]
        var = d_hist_info['branch']
        bins = d_hist_info['bin_edges']
        lumi_scale = d_hist_info['lumi_scale']
        scale = d_hist_info['scale']
        weights = d_hist_info['weight_branches']
        selection = d_hist_info['selection']

        if useQCDControl:
            # replace SR tree with CR tree
            if useQCDSystematicControl:
                tree = qcd_tree_for_normalisation
            else:
                tree = qcd_tree
            # Remove the Lepton reweighting for the datadriven qcd (SF not derived for unisolated leptons)
            for weight in weights:
                if 'Electron' in weight: weights.remove(weight)
                elif 'Muon' in weight: weights.remove(weight)

        weights = "*".join(weights)
        # Selection will return a weight 0 or 1 depending on whether event passes selection
        weights_and_selection = '( {0} ) * ( {1} )'.format(weights, selection)

        scale *= lumi_scale

        root_file = File(f)
        root_tree = root_file.Get(tree)

        root_histogram = Hist(bins)
        # Draw histogram of var for selection into root_histogram
        root_tree.Draw(var,
                       selection=weights_and_selection,
                       hist=root_histogram)
        root_histogram.Scale(scale)

        # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it
        if ignoreUnderflow:
            root_histogram.SetBinContent(0, 0)
            root_histogram.SetBinError(0, 0)

        # Fix overflow (Moves entries from overflow bin into last bin i.e. last bin not |..| but |--> )
        root_histogram = fix_overflow(root_histogram)

        root_file.Close()
        return root_histogram