def test_binomial(self): """ Check that we can generate a binomial distribution for event number """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'spill') bunch_weights = [] for bunch in bunch_dict.values(): bunch_weights.append(bunch.bunch_weight()) canvas = common.make_root_canvas("") hist = common.make_root_histogram("generated distribution", \ bunch_weights, "bunch weights", BIN_N+1, xmin=-0.5, xmax=BIN_N+0.5) hist.Fill(0, 1000 - hist.GetSumOfWeights()) # xboa ignores empty spills hist.Draw() test_hist = ROOT.TH1D("test_hist", "reference distribution", BIN_N + 1, -0.5, BIN_N + 0.5) # pylint: disable = E1101, C0301 for i in range(BIN_N + 1): test_hist.SetBinContent(i, \ (ROOT.TMath.BinomialI(BIN_P, BIN_N, i-1)- ROOT.TMath.BinomialI(BIN_P, BIN_N, i))*N_SPILLS) # pylint: disable = E1101, C0301 print i, test_hist.GetBinContent(i), hist.GetBinContent(i) test_hist.SetLineStyle(2) test_hist.Draw("SAME") ks_value = test_hist.KolmogorovTest(hist) canvas.Update() canvas.Print(PLOT_DIR + "/binomial_distribution_test.png") print "binomial ks_value", ks_value self.assertGreater(ks_value, 1e-3)
def test_gaussian(self): """ Check that the weight of each sub-beam is close to the expected weight """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid')[-13] for key, value in {'energy':226.0, 'z':3.0, 'x':0., 'y':0., \ 'px':0., 'py':0.}.iteritems(): sigma = 5.*bunch.moment([key, key])**0.5\ /float(bunch.bunch_weight())**0.5 self.assertLess(abs(bunch.mean([key])[key] - value), sigma + 1.) covs = bunch.covariance_matrix(['x', 'px', 'y', 'py']) test = numpy.array(\ [[1.05668599e+03, -6.33950201e+02, 0.00000000e+00, 6.34327423e+02], [-6.33950201e+02, 1.14145263e+03, -6.34327423e+02, 0.00000000e+00], [0.00000000e+00, -6.34327423e+02, 1.05668599e+03, -6.33950201e+02], [6.34327423e+02, 0.00000000e+00, -6.33950201e+02, 1.14145263e+03]] ) msg_ = "Determinants:"+str(numpy.linalg.det(test))+ \ str(numpy.linalg.det(covs)) self.assertLess(\ 2.*abs(numpy.linalg.det(test) - numpy.linalg.det(covs))/ \ (numpy.linalg.det(test) + numpy.linalg.det(covs)), 0.2, msg=msg_) self.__cmp_matrix(test, covs)
def test_gaussian(self): """ Check that the weight of each sub-beam is close to the expected weight """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid')[-13] for key, value in {'energy':226.0, 'z':3.0, 'x':0., 'y':0., \ 'px':0., 'py':0.}.iteritems(): sigma = 5.*bunch.moment([key, key])**0.5\ /float(bunch.bunch_weight())**0.5 self.assertLess(abs(bunch.mean([key])[key]-value), sigma+1.) covs = bunch.covariance_matrix(['x', 'px', 'y', 'py']) test = numpy.array(\ [[1.05668599e+03, -6.33950201e+02, 0.00000000e+00, 6.34327423e+02], [-6.33950201e+02, 1.14145263e+03, -6.34327423e+02, 0.00000000e+00], [0.00000000e+00, -6.34327423e+02, 1.05668599e+03, -6.33950201e+02], [6.34327423e+02, 0.00000000e+00, -6.33950201e+02, 1.14145263e+03]] ) msg_ = "Determinants:"+str(numpy.linalg.det(test))+ \ str(numpy.linalg.det(covs)) self.assertLess(\ 2.*abs(numpy.linalg.det(test) - numpy.linalg.det(covs))/ \ (numpy.linalg.det(test) + numpy.linalg.det(covs)), 0.2, msg=msg_) self.__cmp_matrix(test, covs)
def test_binomial(self): """ Check that we can generate a binomial distribution for event number """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'spill') bunch_weights = [] for bunch in bunch_dict.values(): bunch_weights.append(bunch.bunch_weight()) canvas = common.make_root_canvas("") hist = common.make_root_histogram("generated distribution", \ bunch_weights, "bunch weights", BIN_N+1, xmin=-0.5, xmax=BIN_N+0.5) hist.Fill(0, 1000-hist.GetSumOfWeights()) # xboa ignores empty spills hist.Draw() test_hist = ROOT.TH1D("test_hist", "reference distribution", BIN_N+1, -0.5, BIN_N+0.5) # pylint: disable = E1101, C0301 for i in range(BIN_N+1): test_hist.SetBinContent(i, \ (ROOT.TMath.BinomialI(BIN_P, BIN_N, i-1)- ROOT.TMath.BinomialI(BIN_P, BIN_N, i))*N_SPILLS) # pylint: disable = E1101, C0301 print i, test_hist.GetBinContent(i), hist.GetBinContent(i) test_hist.SetLineStyle(2) test_hist.Draw("SAME") ks_value = test_hist.KolmogorovTest(hist) canvas.Update() canvas.Print(PLOT_DIR+"/binomial_distribution_test.png") print "binomial ks_value", ks_value self.assertGreater(ks_value, 1e-3)
def test_weighting(self): """ Check that the weight of each sub-beam is close to the expected weight """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid') test_weights = {} sum_weights = 0 for pid, bunch in bunch_dict.iteritems(): test_weights[pid] = bunch.bunch_weight() sum_weights += test_weights[pid] for pid, weight in test_weights.iteritems(): print pid, weight, WEIGHTS_TO_PID[pid]*sum_weights, sum_weights**0.5 self.assertLess(abs(weight - WEIGHTS_TO_PID[pid]*sum_weights), \ sum_weights**0.5 )
def test_weighting(self): """ Check that the weight of each sub-beam is close to the expected weight """ # make a dict of bunches of xboa.Hits separated by event (spill) number bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid') test_weights = {} sum_weights = 0 for pid, bunch in bunch_dict.iteritems(): test_weights[pid] = bunch.bunch_weight() sum_weights += test_weights[pid] for pid, weight in test_weights.iteritems(): print pid, weight, WEIGHTS_TO_PID[ pid] * sum_weights, sum_weights**0.5 self.assertLess(abs(weight - WEIGHTS_TO_PID[pid]*sum_weights), \ sum_weights**0.5 )
def test_sawtooth_time(self): """ Check that beam maker generates sawtooth t distribution correctly """ bunch = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid')[-13] canvas, hist = bunch.root_histogram('t', 'ns', xmin=-1e6, xmax=1e6) cmp_hist = ROOT.TH1D("test", "test", hist.GetNbinsX(), -1e6, 1e6) # pylint: disable = E1101, C0301 for h_bin in range(1, hist.GetNbinsX()+1): my_x = hist.GetBinCenter(h_bin) my_y = 2.*hist.GetSumOfWeights()*h_bin/hist.GetNbinsX()**2 cmp_hist.Fill(my_x, my_y) cmp_hist.SetLineStyle(2) cmp_hist.Draw("SAME") ks_value = cmp_hist.KolmogorovTest(hist) print "sawtooth t ks_value", ks_value canvas.Update() canvas.Print(PLOT_DIR+"/sawtooth_time_distribution_test.png") self.assertGreater(ks_value, 1e-3)
def test_sawtooth_time(self): """ Check that beam maker generates sawtooth t distribution correctly """ bunch = Bunch.new_dict_from_read_builtin('maus_json_primary', \ BIN_SIM, 'pid')[-13] canvas, hist = bunch.root_histogram('t', 'ns', xmin=-1e6, xmax=1e6) cmp_hist = ROOT.TH1D("test", "test", hist.GetNbinsX(), -1e6, 1e6) # pylint: disable = E1101, C0301 for h_bin in range(1, hist.GetNbinsX() + 1): my_x = hist.GetBinCenter(h_bin) my_y = 2. * hist.GetSumOfWeights() * h_bin / hist.GetNbinsX()**2 cmp_hist.Fill(my_x, my_y) cmp_hist.SetLineStyle(2) cmp_hist.Draw("SAME") ks_value = cmp_hist.KolmogorovTest(hist) print "sawtooth t ks_value", ks_value canvas.Update() canvas.Print(PLOT_DIR + "/sawtooth_time_distribution_test.png") self.assertGreater(ks_value, 1e-3)