def define_variables(self): """Define variables for correlated and uncorrelated uncertainties. Correlated uncertainties are represented as WeightedSums of fixed uncertainties from HM model multiplied by single weight for each reactor and shared for isotopes within same reactor. Uncorrelated uncertainties are represented as VarArrays of uncertain parameters. Each parameter uncertainty corresponds to uncorrelated uncertainty from HM model multiplied for given isotope in given bin. All parameters are uncorrelated between reactors and isotopes. """ num_of_bins = len(self.bins) - 1 with self.unc_ns: for reac_idx in self.nidx_major.get_subset(self.cfg.reac_idx): reac, = reac_idx.current_values() corr_name = "corr_unc." + reac_idx.current_format() self.unc_ns.reqparameter( corr_name, central=0., sigma=1., label=f"Correlated uncertainty in {reac}") for iso_idx in self.nidx_major.get_subset(self.cfg.iso_idx): iso, = iso_idx.current_values() idx = iso_idx + reac_idx uncorr_temp = "uncorr_unc." + idx.current_format( ) + ".{bin}" vars = [] for bin, iso_unc in zip(range(num_of_bins), self.uncertainties['uncorr'][iso]): name = uncorr_temp.format(bin=bin) vars.append(name) self.unc_ns.reqparameter( name, central=0., sigma=iso_unc, label= f'''Uncorrelated uncertainty for {iso} in {reac}, bin {bin}''' ) uncorr_unc = C.VarArray( vars, ns=self.unc_ns, labels=f'Uncorrelated uncertainties for {iso} in {reac}' ) self.uncorrelated_vars[reac][iso] = uncorr_unc tmp = C.Points( self.uncertainties['corr'][iso], labels= f'Fixed array of correlated uncertainties for {iso} in {reac}' ) corr_unc = C.WeightedSum( [corr_name], [tmp], ns=self.unc_ns, labels=f"Correlated uncertainties for {iso} in {reac}") self.correlated_vars[reac][iso] = corr_unc self.total_unc[reac][iso] = C.Sum([uncorr_unc, corr_unc])
def build(self): '''Bringing uncertainties together with spectrum. Spectrum values are grouped by bins in antineutrino energy (binning is read together with uncertainties). Each bin is corrected accordingly and corrected spectrum is provided as output. ''' order = ','.join((self.cfg.iso_idx, self.cfg.reac_idx)) reversed_order = ','.join((self.cfg.reac_idx, self.cfg.iso_idx)) order_from_idx = self.nidx_major.comma_list() if order_from_idx == order: normal_order = True elif order_from_idx == reversed_order: normal_order = False else: raise ValueError("Indicies from config and real don't match") for idx in self.nidx_major: if normal_order: iso, reac, = idx.current_values() else: reac, iso, = reac_idx.current_values() unc = self.total_unc[reac][iso] nominal = R.FillLike(1., labels="Nominal weight for spectrum") unc.single() >> nominal.single_input() total = C.Sum( outputs=[unc, nominal], labels=f"Nominal weigts + correction for {iso} in {reac}") correction = R.ReactorSpectrumUncertainty(self.binning.single(), total.single()) correction.insegment.setLabel( f"Segments in HM-bins for {iso} in {reac}") correction.transformations.in_bin_product.setLabel( f"Correction to antineutrino spectrum in HM model for {iso} in {reac}" ) self.set_input("corrected_spectrum", idx, correction.insegment.points, argument_number=0) self.set_input("corrected_spectrum", idx, correction.transformations.in_bin_product.spectrum, argument_number=1) self.set_output( "corrected_spectrum", idx, correction.transformations.in_bin_product.corrected_spectrum)
def init(self): self.components = [] labels = list(reversed(self.opts.labels)) if self.opts.chi2: clabel = labels and labels.pop() or '' self.chi2 = ROOT.Chi2(labels=clabel) for analysis in self.opts.chi2: for block in analysis: self.chi2.add(block.theory, block.data, block.cov) self.components.append(self.chi2) if self.opts.logpoisson_legacy: clabel = labels and labels.pop() or '' self.logpoisson_legacy = ROOT.Poisson(self.opts.poisson_approx, labels='') for analysis in self.opts.logpoisson_legacy: for block in analysis: self.logpoisson_legacy.add(block.theory, block.data) self.components.append(self.logpoisson_legacy) if self.opts.logpoisson: clabel = labels and labels.pop() or '' self.logpoisson = ROOT.LogPoissonSplit(self.opts.poisson_approx, labels=(clabel + ' (const)', clabel)) for analysis in self.opts.logpoisson: for block in analysis: self.logpoisson.add(block.theory, block.data) self.components.append(self.logpoisson) if len(self.components) == 1: self.statistic = self.components[0] else: clabel = labels and labels.pop() or '' self.statistic = C.Sum( [comp.transformations.back() for comp in self.components], labels=clabel) self.env.parts.statistic[self.opts.name] = self.statistic
#!/usr/bin/env python from load import ROOT as R import gna.constructors as C number = 1.2345 p0 = C.Points([number]) print(p0) print(p0.transformations[0]) print(p0.transformations[0].outputs[0]) print(p0.single()) s1 = C.Sum() s1.add_input('first') # Default binding syntax s1.sum.inputs[0].connect(p0.points.outputs[0]) s2 = C.Sum() # Siplified access s2.add_input('first') s2.single_input().connect(s1.single()) s3 = C.Sum() s3.add_input('first') # Even simpler way s2.sum.sum >> s3.sum.first s4 = C.Sum() s4.add_input('first') # The other direction
#!/usr/bin/env python from load import ROOT as R import gna.constructors as C import numpy as np # Create several points instances n, n1, n2 = 12, 3, 4 points_list = [ C.Points(np.arange(i, i+n).reshape(n1, n2)) for i in range(5) ] # Create sum instances tsum_constructor = C.Sum([p.points.points for p in points_list]) tsum_add = R.Sum() tsum_add_input = R.Sum() for i, p in enumerate(points_list): out = p.points.points tsum_add.add(out) an_input = tsum_add_input.add_input('input_{:02d}'.format(i)) an_input(out) # Print the structure of Sum object print('Sum, configured via constructor') tsum_constructor.print() print() # Print the structure of Sum object print('Sum, configured via add() method') tsum_add.print() print()
#!/usr/bin/env python from tutorial import tutorial_image_name, savegraph from load import ROOT as R import gna.constructors as C import numpy as np n, n1, n2 = 12, 3, 4 points_list = [C.Points(np.arange(i, i + n).reshape(n1, n2)) for i in range(5)] tfactor = C.Points(np.linspace(0.5, 2.0, n).reshape(n1, n2)) tsum = C.Sum([p.points.points for p in points_list]) tprod = C.Product([tsum.sum.sum, tfactor.points.points]) for i, p in enumerate(points_list): p.points.setLabel('Sum input:\nP{:d}'.format(i)) tfactor.points.setLabel('Scale S') tsum.sum.setLabel('Sum of matrices') tprod.product.setLabel('Scaled matrix') tprod.product.product.setLabel('result') tsum.print() print() tprod.print() print() print('The sum:') print(tsum.transformations[0].outputs[0].data()) print()