Пример #1
0
    def test_parse_fail(self, mock_logger):
        exp_err = 'Cannot handle \'3,4:2\' in trying to parse a binning'
        binning = mh.parse_binning('3,4:2')
        mock_logger.error.assert_called_with(exp_err)
        npt.assert_equal(binning, np.array([]))


        exp_err = 'Could not parse binning from \'3.a, 3.3\' because '\
                  '\'invalid literal for float(): 3.a\''
        binning = mh.parse_binning('3.a, 3.3')
        mock_logger.error.assert_called_with(exp_err)
        npt.assert_equal(binning, np.array([]))
Пример #2
0
    def __init__(self, config):
        """
        Read all the info from the config and do some computation which are then
        needed throughout
        """
        # TODO: read the configuration from a json file and make that the
        # argument to the function
        self.binning = np.array([
            parse_binning(config['binning'][0]),
            parse_binning(config['binning'][1])
        ])
        # variables as they are defined and used for deciding the binning
        # inside ROOT with string cuts
        self.bin_cut_vars = config['bin_vars']

        # For the internal use and especially for naming the variables it is not
        # possible to have functional variable definitions. Simply use whatever
        # is the argument of func(var) (i.e. var)
        self.bin_vars = [parse_func_var(v)[0] for v in config['bin_vars']]

        self.bins = get_bins(self.binning[0], self.binning[1],
                             self.bin_vars[0], self.bin_vars[1])

        self.fit_var = config['fit_variable']
        self.fit_range = config['fit_range']
        self.expression_strings = config['expression_strings']
        self.proto_params = config['proto_parameters']

        self.sub_models = config['sub_models']
        self.nevent_yields = [m['event_yield'] for m in self.sub_models]

        self.components = [(m['name'], m['plot']) for m in self.sub_models]

        self.full_model = config['full_model']['name']

        # prototype full model expression, where the bin model can be achieved
        # with simple replacements
        self.proto_fexpr = _full_model_expr(config['full_model']['type'],
                                            self.full_model,
                                            [c[0] for c in self.components],
                                            self.nevent_yields)

        self.fix_vars = []
        if 'fix_vars' in config:
            for var in config['fix_vars']:
                self.fix_vars.append((var.keys()[0], var.values()[0]))

        self.plot_config = config['plot_config']
Пример #3
0
def get_binning(config):
    """
    Read the fit config json and get the bin variable and the binning
    """
    binning = parse_binning(config['binning'][1])
    bins = zip(binning[:-1], binning[1:])

    return parse_func_var(config['bin_vars'][1])[0], bins
Пример #4
0
def get_costh_bins(bin_str, binvar, data):
    """
    Get the bin edges from the binning_str and the data
    """
    auto_match = re.match(r'auto:(\d+)$', bin_str)
    if auto_match:
        costh_bins = get_costh_binning(data, int(auto_match.group(1)))
    else:
        binning = parse_binning(bin_str)
        if len(binning) == 0:
            sys.exit(1)  # bail out, there is nothing we can do here
        costh_bins = zip(binning[:-1], binning[1:])

    costh_means = get_bin_means(data, binvar, costh_bins)

    return costh_bins, costh_means
Пример #5
0
def main(args):
    """Main"""
    frames = args.frames.split(',')
    if args.bin_variable is not None:
        bin_var = parse_func_var(args.bin_variable)
        if args.binning is None:
            logging.error('You have to define a binning for \'{}\' if you want '
                          'to use it as binning variable'.format(bin_var))
            sys.exit(1)

        binning = parse_binning(args.binning)
    else:
        binning = None
        bin_var = None

    logging.info('Processing gen level file')
    gen_hists = create_histograms(get_dataframe(args.genlevelfile), frames,
                                  args.ncosth, args.nphi, bin_var, binning)

    logging.info('Processing reco level file')
    reco_hists = create_histograms(get_dataframe(args.recolevelfile), frames,
                                   args.ncosth, args.nphi, bin_var, binning,
                                   WEIGHT_F)

    logging.debug('Scaling gen level hists by '.format(args.scale_gen))
    for hist in gen_hists.values():
        hist.Scale(args.scale_gen)

    logging.info('calculating acceptance maps')
    acc_maps = OrderedDict()
    for name, hist in reco_hists.iteritems():
        gen_map_n = [n for n in gen_hists if n in name]
        if len(gen_map_n) > 1:
            logging.warning('Found more than 1 gen level map for \'{}\': {}'
                            .format(name, gen_map_n))

        # Still use just the first, since we should always just have 1
        acc_maps[name] = divide(hist, gen_hists[gen_map_n[0]])


    logging.debug('storing to output file')
    outfile = r.TFile(args.outfile, 'recreate' if args.recreate else 'update')
    store_hists(outfile, gen_hists, 'gen_costh_phi', bin_var)
    store_hists(outfile, reco_hists, 'reco_costh_phi', bin_var)
    store_hists(outfile, acc_maps, 'acc_map_costh_phi', bin_var)
Пример #6
0
    def test_parse_arange(self):
        binning = mh.parse_binning('1:10:2')
        npt.assert_allclose(binning, np.arange(1, 10, 2))

        binning = mh.parse_binning('-3.1:4.2:0.2')
        npt.assert_allclose(binning, np.arange(-3.1, 4.2, 0.2))
Пример #7
0
    def test_parse_linspace(self):
        binning = mh.parse_binning('1:10,25')
        npt.assert_allclose(binning, np.linspace(1, 10, 25))

        binning = mh.parse_binning('-3.8:4.,30')
        npt.assert_allclose(binning, np.linspace(-3.8, 4.0, 30))
Пример #8
0
    def test_parse_list_of_floats(self):
        binning = mh.parse_binning('1,2,3,4,5,6')
        npt.assert_allclose(binning, np.arange(1, 7))

        binning = mh.parse_binning('1.2, 3.4, 5.6,7.8')
        npt.assert_allclose(binning, np.array([1.2, 3.4, 5.6, 7.8]))
Пример #9
0
        ('chic1_R_18o31', 'chic2_R1_0_R2_25o58'),
        ('chic1_R_22o29', 'chic2_R1_0_R2_35o62'),
        ('chic1_R_14o33', 'chic2_R1_0_R2_5o18'),
        ('chic1_R_6o7', 'chic2_R1_0_R2_134o214'),
        ('chic1_R_2o3', 'chic2_R1_2o5_R2_2o5'),  # unpolarized
    ),
    'costh': (
        ('chic1_R_2o3', 'chic2_R1_2o5_R2_2o5'),  # unpolarized
        ('chic1_R_14o73', 'chic2_R1_0_R2_25o106'),  # nrqcd prediction
        ('chic1_R_0', 'chic2_R1_0_R2_0'),  # max negative
        ('chic1_R_1', 'chic2_R1_0_R2_1')  # max positive
    )
}

ANALYSIS_BINNING = {
    'phi': parse_binning("0:90,7"),
    'costh': parse_binning("0,0.075,0.15,0.225,0.3,0.375,0.45,0.625")
}

SCEN_ATTR = default_attributes(size=0, line=7)
LINE_STYLES = [7, 9, 2, 3, 5, 1]
for iatt, att in enumerate(SCEN_ATTR):
    att['color'] = 12
    att['line'] = LINE_STYLES[iatt % len(LINE_STYLES)]

RATIO_ATTR = default_attributes(open_markers=False)

LEG_POS = {'phi': (0.2, 0.15, 0.9, 0.33), 'costh': (0.2, 0.15, 0.65, 0.3)}


def get_func(variable):