def test_global_weight(self): hitcore_list_1, hitcore_list_2 = [], [] for spill in range(3): for event in range(3): for particle in range(3): hitcore_1 = Hitcore() hitcore_1.set('spill', spill) hitcore_1.set('event_number', event) hitcore_1.set('particle_number', particle) hitcore_list_1.append(hitcore_1) hitcore_2 = Hitcore() hitcore_2.set('spill', spill) hitcore_2.set('event_number', event) hitcore_2.set('particle_number', particle) hitcore_list_2.append(hitcore_2) for i, ref_hitcore in enumerate(hitcore_list_1): ref_hitcore.set('global_weight', 0.2) for hitcore_list in hitcore_list_1, hitcore_list_2: for j, test_hitcore in enumerate(hitcore_list): if i == j: self.assertAlmostEqual( test_hitcore.get('global_weight'), 0.2) elif j < i: self.assertAlmostEqual( test_hitcore.get('global_weight'), 0.9) else: self.assertAlmostEqual( test_hitcore.get('global_weight'), 1.0) ref_hitcore.set('global_weight', 0.9) Hitcore.clear_global_weights() for i, ref_hitcore in enumerate(hitcore_list): self.assertAlmostEqual(test_hitcore.get('global_weight'), 1.0)
def hit_global_weight_test(): Hit.clear_global_weights() hit = Hit.new_from_dict({ "spill": 1, "event_number": 2, "particle_number": 3 }) if (abs(hit.get("global_weight") - 1.) > 1e-9): return 'fail' hit.set('global_weight', 0.5) if (abs(hit.get_global_weight() - 0.5) > 1e-9): return 'fail' Hitcore.clear_global_weights() if (abs(hit.get_global_weight() - 1.) > 1e-9): return 'fail' return 'pass'
def test_cut_double_global(self): for i in self.events: hitcore = self.bunchcore.get_item(i) self.assertAlmostEqual(hitcore.get('global_weight'), 1) self.bunchcore.cut_double("x", operator.gt, 10.5, True) for i in self.events: hitcore = self.bunchcore.get_item(i) if i > 10: self.assertAlmostEqual(hitcore.get('global_weight'), 0) else: self.assertAlmostEqual(hitcore.get('global_weight'), 1) Hitcore.clear_global_weights() for i in self.events: hitcore = self.bunchcore.get_item(i) self.assertAlmostEqual(hitcore.get('global_weight'), 1)
def test_cut_double_bad(self): try: self.bunchcore.cut_double("none", operator.gt, 10.5, True) raise RuntimeError("Should have thrown") except TypeError: pass try: self.bunchcore.cut_double("x", self.bad_callable, 10.5, True) raise RuntimeError("Should have thrown") except TypeError: pass try: self.bunchcore.cut_double("x", operator.gt, "string", True) raise RuntimeError("Should have thrown") except TypeError: pass # this is okay, "string" evaluates to true self.bunchcore.cut_double("x", operator.gt, 10.5, "string") # now clear any weightings applied! Hitcore.clear_global_weights()
def clear_global_weights(): """Set all global weights to 1""" Hitcore.clear_global_weights()