Exemple #1
0
def multidim_np(config, spec, dimension, points=None, cl=None, freeze=True):
    outfiles = []
    freeze = ['--freeze'] if freeze is True else []
    def make_workspace(coefficients):
        workspace = os.path.join(config['outdir'], 'workspaces', '{}.root'.format('_'.join(coefficients)))
        cmd = [
            'text2workspace.py', os.path.join(config['outdir'], 'ttV_np.txt'),
            '-P', 'NPFit.NPFit.models:eft',
            '--PO', 'scan={}'.format(os.path.join(config['outdir'], 'cross_sections.npz')),
            ' '.join(['--PO process={}'.format(x) for x in config['processes']]),
            ' '.join(['--PO poi={}'.format(x) for x in coefficients]),
            '-o', workspace
        ]
        spec.add(['cross_sections.npz'], workspace, cmd)

        return workspace
    outfiles += [make_workspace(config['coefficients'])]
    for coefficients in sorted_combos(config['coefficients'], dimension):
        workspace = make_workspace(coefficients)
        if dimension == 1:
            label = coefficients[0]
        else:
            label = '{}{}'.format('_'.join(coefficients), '_frozen' if freeze else '')

        best_fit = os.path.join(config['outdir'], 'best-fit-{}.root'.format(label))
        fit_result = os.path.join(config['outdir'], 'fit-result-{}.root'.format(label))
        cmd = ['run', 'combine'] + freeze + list(coefficients) + [config['fn']]
        if dimension == 1 and cl is None:
            spec.add([workspace], [best_fit, fit_result], cmd)
            outfiles += [best_fit, fit_result]
        if points is None and cl is None:
            spec.add([workspace], [best_fit], cmd)
            outfiles += [best_fit]
        elif cl is not None:
            for level in cl:
                outfile = os.path.join(config['outdir'], 'cl_intervals/{}-{}.root'.format(label, level))
                cmd = ['run', 'combine'] + freeze + list(coefficients) + ['--cl', str(level), config['fn']]
                spec.add(['cross_sections.npz', workspace], outfile, cmd)
                outfiles += [outfile]
        else:
            cmd = ['run', 'combine', '--snapshot'] + freeze + list(coefficients) + [config['fn']]
            snapshot = os.path.join(config['outdir'], 'snapshots', '{}.root'.format(label))
            spec.add([workspace], snapshot, cmd)

            scans = []
            for index in range(int(np.ceil(points / config['np chunksize']))):
                scan = os.path.join(config['outdir'], 'scans', '{}_{}.root'.format(label, index))
                scans.append(scan)
                cmd = ['run', 'combine'] + freeze + list(coefficients) + ['-i', str(index), '-p', str(points), config['fn']]

                spec.add(['cross_sections.npz', snapshot], scan, cmd)

            total = os.path.join(config['outdir'], 'scans', '{}.total.root'.format(label))
            spec.add(scans, total, ['hadd', '-f', total] + scans)
            outfiles += [total]

    return list(set(outfiles))
Exemple #2
0
def fluctuate(config, spec):
    outfiles = []
    for coefficients in sorted_combos(config['coefficients'], 1):
        label = '_'.join(coefficients)
        fit_result = os.path.join(config['outdir'], 'fit-result-{}.root'.format(label))
        cmd = ['run', 'fluctuate', label, config['fluctuations'], config['fn']]
        outfile = os.path.join(config['outdir'], 'fluctuations-{}.npy'.format(label))
        spec.add([fit_result], outfile, cmd)
        outfiles += [outfile]

    return outfiles
Exemple #3
0
    def setup(self):
        # self.scan.fit(dimensions=[2])
        self.scan.fit()
        for process in self.processes:
            if process not in self.scan.fit_constants:
                raise RuntimeError('no fit provided for process {}'.format(process))
            self.modelBuilder.out.var(process)
            name = 'r_{0}'.format(process)

            pairs = sorted_combos(range(0, len(self.pois)), 2)
            fit_constants = self.scan.construct(process, tuple(self.pois))

            constant = ['1.0']
            linear = self.pois
            quad = ['{p} * {p}'.format(p=p) for p in self.pois]
            mixed = ['{p0} * {p1}'.format(p0=self.pois[p0], p1=self.pois[p1]) for p0, p1 in pairs]
            terms = ['({s:0.11f} * {c})'.format(s=s, c=c) for s, c in zip(fit_constants, constant + linear + quad + mixed)]
            model = 'expr::{name}("{terms}", {pois})'.format(name=name, terms=' + '.join(terms), pois=', '.join(self.pois))

            scale = self.modelBuilder.factory_(model.replace(' ', ''))
            self.modelBuilder.out._import(scale)
Exemple #4
0
    def write(self, config, args):
        sf = 2
        if self.dimension == 2:
            def contiguous(segments):
                for i in range(len(segments) - 1):
                    if max(segments[i]) < min(segments[i + 1]):
                        return False
                return True
            coefficients = sorted(config['coefficients'])
            indices = dict((i, c) for c, i in enumerate(coefficients))
            tables = dict((level, [[c] + ['-'] * len(coefficients) for c in coefficients]) for level in self.levels)
            for x, y in sorted_combos(config['coefficients'], 2):
                tag = '{}_{}{}'.format(x, y, '_frozen' if self.freeze else '')
                data = root2array(os.path.join(config['outdir'], 'scans/{}.total.root'.format(tag)))
                zi = 2 * data['deltaNLL']
                xi = data[x]
                yi = data[y]
                if not self.dimensionless:
                    xi *= conversion[x]
                    yi *= conversion[y]

                contour = plt.tricontour(
                    xi[zi != 0],
                    yi[zi != 0],
                    zi[zi != 0],
                    sorted(chi2.isf([1 - l for l in self.levels], 2))
                )
                for i, l in enumerate(self.levels):
                    res = {
                        x: [],
                        y: []
                    }
                    for path in contour.collections[i].get_paths():
                        polygons = path.to_polygons()
                        for p in polygons:
                            res[x].append((p[:, 0].min(), p[:, 0].max()))
                            res[y].append((p[:, 1].min(), p[:, 1].max()))
                    for key in res:
                        res[key].sort()
                        if contiguous(res[key]):
                            flattened = sum(res[key], ())
                            res[key] = [(min(flattened), max(flattened))]
                    intervals = ['[{}, {}]'.format(round(low, sf), round(high, sf)) for low, high in res[x]]
                    tables[l][indices[x]][indices[y] + 1] = ' and '.join(intervals)
                    intervals = ['[{}, {}]'.format(round(low, sf), round(high, sf)) for low, high in res[y]]
                    tables[l][indices[y]][indices[x] + 1] = ' and '.join(intervals)

            with open(os.path.join(config['outdir'], self.base + '.txt'), 'w') as f:
                headers = [""] + [x + ('' if self.dimensionless else '/Lambda^2') for x in coefficients]
                for level, table in tables.items():
                    f.write('\n\n{line} cl={level} {line}\n{text}'.format(
                        line='#' * 20,
                        level=level,
                        text=tb.tabulate(table, headers=headers))
                    )

            with open(os.path.join(config['outdir'], self.base + '.tex'), 'w') as f:
                headers = [""] + [x + ('' if self.dimensionless else '$/\Lambda^2$') for x in coefficients]
                for level, table in tables.items():
                    f.write('\n\n{line} cl={level} {line}\n{text}'.format(
                        line='#' * 20,
                        level=level,
                        text=tb.tabulate(table, headers=headers, tablefmt='latex_raw'))
                    )
        else:
            table = []
            for coefficients in sorted_combos(config['coefficients'], self.dimension):
                for index, coefficient in enumerate(sorted(coefficients)):
                    row = [coefficient]
                    for level in self.levels:
                        tag = '{}{}'.format('_'.join(coefficients), '_frozen' if self.freeze else '')
                        data = root2array(
                            os.path.join(config['outdir'], 'cl_intervals/{}-{}.root'.format(tag, level))
                        )
                        template = '[{}, {}]'
                        conversion_factor = 1. if self.dimensionless else conversion[coefficient]
                        low = data[coefficient][(index + 1) * 2 - 1] * conversion_factor
                        high = data[coefficient][(index + 1) * 2] * conversion_factor
                        row += [template.format(round(low, sf), round(high, sf))]
                    row += ['{}'.format(round(data[coefficient][0], sf + 1))]
                    table.append(row)

            headers = [''] + ['CL={}'.format(cl) for cl in self.levels] + ['best fit']
            with open(os.path.join(config['outdir'], self.base + '.txt'), 'w') as f:
                f.write('dimension: {} {}\n'.format(self.dimension, '' if self.dimensionless else '(all values in [TeV^(-2)])'))
                f.write(tb.tabulate(
                    [[x[0] + ('' if self.dimensionless else '/\Lambda^2')] + x[1:] for x in table],
                    headers,
                    tablefmt='plain') + '\n'
                )
            with open(os.path.join(config['outdir'], self.base + '.tex'), 'w') as f:
                table = [[label[x[0]] + ('' if self.dimensionless else '$/\Lambda^2$')] + x[1:] for x in table]
                f.write('dimension: {} {}\n'.format(self.dimension, '' if self.dimensionless else '(all values in [TeV^(-2)])'))
                f.write(tb.tabulate(table, headers, tablefmt='latex_raw') + '\n')