def test_random_radius(self): """ Tests the random gaussian smearing method for radius. Creates a delta function and fits the mean and sigma after smearing. Mean and sigma are checked against set values within 1 %. Integral of smeared spectrum is checked against original number of entries. """ num_entries = 100000 test_spectra = spectra.Spectra("Test", num_entries) radius = 1000. # mm for i in range(num_entries): test_spectra.fill(0, radius, 0) smearing = smear.Smear() smearing._position_resolution = 100. # mm smeared_spectra = smearing.random_gaussian_radius_spectra(test_spectra) mean, sigma, integral = self.fit_gaussian_radius(smeared_spectra) self.assertTrue(radius < 1.01*mean and radius > 0.99*mean) self.assertTrue(smearing._position_resolution < 1.01*sigma and smearing._position_resolution > 0.99*sigma) self.assertAlmostEqual(integral/float(num_entries), 1.0)
def test_random_energy(self): """ Tests the random gaussian smearing method for energy. Creates a delta function and fits the mean and sigma after smearing. Mean and sigma are checked against set values within 1 %. Integral of smeared spectrum is checked against original number of entries. """ num_entries = 100000 test_spectra = spectra.Spectra("Test",num_entries) energy = 2.5 # MeV for i in range(num_entries): test_spectra.fill(energy, 0, 0) smearing = smear.Smear() smearing._light_yield = 200. # NHit per MeV smeared_spectra = smearing.random_gaussian_energy_spectra(test_spectra) expected_sigma = numpy.sqrt(energy/200.) mean, sigma, integral = self.fit_gaussian_energy(smeared_spectra) self.assertTrue(energy < 1.01*mean and energy > 0.99*mean) self.assertTrue(expected_sigma < 1.01*sigma and expected_sigma > 0.99*sigma) self.assertAlmostEqual(integral/float(num_entries), 1.0)
parser = argparse.ArgumentParser() parser.add_argument("-m", "--smear_method", nargs='?', const="weight", type=str, default="weight", help="specify the smearing method to use") parser.add_argument("path", type=str, help="specify path to hdf5 file") args = parser.parse_args() directory = args.path[:args.path.rfind("/") + 1] # strip filename # strip directory and extension filename = args.path[args.path.rfind("/") + 1:args.path.rfind(".")] smearer = smear.Smear() spectrum = store.load(args.path) if args.smear_method == "weight": # Use default smear method smeared_spectrum = smearer.weight_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.weight_gaussian_radius_spectra( smeared_spectrum) elif args.smear_method == "random": smeared_spectrum = smearer.random_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.random_gaussian_radius_spectra( smeared_spectrum) else: # Not a valid smear method parser.error(args.smear_method + " is not a valid smear method") filename = directory + filename + "_smeared" + ".hdf5" store.dump(filename, smeared_spectrum)