def render_mc_info(self,max_photons=1000,cher_only=True): #need to render photon tracking info if available self.gpu_geometries = [self.gpu_geometry] if self.sum_mode or self.ev is None: return if self.photon_display_mode == 'beg': photons = self.ev.photons_beg elif self.photon_display_mode == 'end': photons = self.ev.photons_end else: photons = None if photons is not None: geometry = Geometry() self.render_photons(geometry,photons) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry) if self.track_display_mode in ['geant4', 'both'] and self.ev.vertices is not None: geometry = Geometry() any = False for vertex in self.ev.vertices: if vertex.steps: any = True self.render_vertex(geometry,vertex,children=True) if any: geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry) if self.track_display_mode in ['chroma', 'both'] and self.ev.photon_tracks is not None: geometry = Geometry() print('Total Photons',len(self.ev.photon_tracks)) cherenkov = np.asarray([track.flags[0] & event.CHERENKOV == event.CHERENKOV for track in self.ev.photon_tracks]) ncherenkov = np.count_nonzero(cherenkov) nphotons = ncherenkov if cher_only else len(self.ev.photon_tracks) prob = max_photons/nphotons selector = np.random.random(len(self.ev.photon_tracks)) < prob nphotons = 0 for track in (t for s,t in zip(selector,self.ev.photon_tracks) if s): if cher_only and track.flags[0] & event.CHERENKOV == event.CHERENKOV: self.render_photon_track(geometry,track[:min(len(track),5)],sz=0.05) nphotons = nphotons + 1 elif not cher_only: self.render_photon_track(geometry,track[:min(len(track),5)]) nphotons = nphotons + 1 if nphotons > 0: print('Rendered Photons',nphotons) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry)
def render_particle_track(self): x = 10.0 h = x * np.sqrt(3) / 2 pyramid = make.linear_extrude([-x / 2, 0, x / 2], [-h / 2, h / 2, -h / 2], h, [0] * 3, [0] * 3) marker = Solid(pyramid, vacuum, vacuum) if self.photon_display_mode == 'beg': photons = self.ev.photons_beg else: photons = self.ev.photons_end geometry = Geometry() sample_factor = max(1, len(photons.pos) / 10000) for pos in photons.pos[::sample_factor]: geometry.add_solid(marker, displacement=pos, rotation=make_rotation_matrix( np.random.uniform(0, 2 * np.pi), uniform_sphere())) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries = [self.gpu_geometry, gpu_geometry]
def testAbort(self): '''Photons that hit a triangle at normal incidence should not abort. Photons that hit a triangle at exactly normal incidence can sometimes produce a dot product that is outside the range allowed by acos(). Trigger these with axis aligned photons in a box. ''' # Setup geometry cube = Geometry(vacuum) cube.add_solid(Solid(box(100, 100, 100), vacuum, vacuum)) geo = create_geometry_from_obj(cube, update_bvh_cache=False) # Initialize simulation (without geant4) sim = Simulation(geo, geant4_processes=0) # Create initial photons nphotons = 5 pos = np.tile([0, 0, 0], (nphotons, 1)).astype(np.float32) dir = np.tile([0, 0, 1], (nphotons, 1)).astype(np.float32) pol = np.zeros_like(pos) phi = np.random.uniform(0, 2 * np.pi, nphotons).astype(np.float32) pol[:, 0] = np.cos(phi) pol[:, 1] = np.sin(phi) t = np.zeros(nphotons, dtype=np.float32) wavelengths = np.empty(nphotons, np.float32) wavelengths.fill(400.0) photons = Photons(pos=pos, dir=dir, pol=pol, t=t, wavelengths=wavelengths) # First make one step to check for strangeness photons_end = sim.simulate([photons], keep_photons_end=True, max_steps=1).next().photons_end print "FIRST STEP" print photons_end.pos[0:10] self.assertFalse(np.isnan(photons_end.pos).any()) self.assertFalse(np.isnan(photons_end.dir).any()) self.assertFalse(np.isnan(photons_end.pol).any()) self.assertFalse(np.isnan(photons_end.t).any()) self.assertFalse(np.isnan(photons_end.wavelengths).any()) # Now let it run the usual ten steps photons_end = sim.simulate([photons], keep_photons_end=True, max_steps=10).next().photons_end aborted = (photons_end.flags & (1 << 31)) > 0 print 'aborted photon fracttion: %1.1f' % ( float(count_nonzero(aborted)) / nphotons) self.assertFalse(aborted.any()) print "LAST STEPS" print photons_end.pos[0:10]
def setUp(self): # Setup geometry cube = Detector(vacuum) cube.add_pmt(Solid(box(10.0,10,10), vacuum, vacuum, surface=r7081hqe_photocathode)) cube.set_time_dist_gaussian(1.2, -6.0, 6.0) cube.set_charge_dist_gaussian(1.0, 0.1, 0.5, 1.5) geo = create_geometry_from_obj(cube, update_bvh_cache=False) self.geo = geo self.sim = Simulation(self.geo, geant4_processes=0)
def loadlayer(self, layer): if layer is None: self.gpu_geometries = [self.gpu_geometry] else: try: gpu_geometry = self.bvh_layers[layer] except KeyError: geometry = create_geometry_from_obj(bvh_mesh(self.geometry, layer)) gpu_geometry = gpu.GPUGeometry(geometry, print_usage=False) self.bvh_layers[layer] = gpu_geometry self.gpu_geometries = [self.gpu_geometry, gpu_geometry] self.update()
def setUp(self): # Setup geometry cube = Detector(vacuum) cube.add_pmt( Solid(box(10.0, 10, 10), vacuum, vacuum, surface=r7081hqe_photocathode)) cube.set_time_dist_gaussian(1.2, -6.0, 6.0) cube.set_charge_dist_gaussian(1.0, 0.1, 0.5, 1.5) geo = create_geometry_from_obj(cube, update_bvh_cache=False) self.geo = geo self.sim = Simulation(self.geo, geant4_processes=0)
def testAbort(self): '''Photons that hit a triangle at normal incidence should not abort. Photons that hit a triangle at exactly normal incidence can sometimes produce a dot product that is outside the range allowed by acos(). Trigger these with axis aligned photons in a box. ''' # Setup geometry cube = Geometry(vacuum) cube.add_solid(Solid(box(100,100,100), vacuum, vacuum)) geo = create_geometry_from_obj(cube, update_bvh_cache=False) sim = Simulation(geo, geant4_processes=0) # Create initial photons nphotons = 10000 pos = np.tile([0,0,0], (nphotons,1)).astype(np.float32) dir = np.tile([0,0,1], (nphotons,1)).astype(np.float32) pol = np.zeros_like(pos) phi = np.random.uniform(0, 2*np.pi, nphotons).astype(np.float32) pol[:,0] = np.cos(phi) pol[:,1] = np.sin(phi) t = np.zeros(nphotons, dtype=np.float32) wavelengths = np.empty(nphotons, np.float32) wavelengths.fill(400.0) photons = Photons(pos=pos, dir=dir, pol=pol, t=t, wavelengths=wavelengths) # First make one step to check for strangeness photons_end = sim.simulate([photons], keep_photons_end=True, max_steps=1).next().photons_end self.assertFalse(np.isnan(photons_end.pos).any()) self.assertFalse(np.isnan(photons_end.dir).any()) self.assertFalse(np.isnan(photons_end.pol).any()) self.assertFalse(np.isnan(photons_end.t).any()) self.assertFalse(np.isnan(photons_end.wavelengths).any()) # Now let it run the usual ten steps photons_end = sim.simulate([photons], keep_photons_end=True, max_steps=10).next().photons_end aborted = (photons_end.flags & (1 << 31)) > 0 print 'aborted photons: %1.1f' % \ (float(count_nonzero(aborted)) / nphotons) self.assertFalse(aborted.any())
def bvh_mesh(geometry, layer): lower_bounds, upper_bounds = geometry.bvh.get_layer(layer).get_bounds() if len(lower_bounds) == 0 or len(upper_bounds) == 0: raise Exception('no nodes at layer %i' % layer) dx, dy, dz = upper_bounds[0] - lower_bounds[0] center = np.mean([upper_bounds[0],lower_bounds[0]], axis=0) geometry = Geometry() geometry.add_solid(Solid(make.box(dx,dy,dz,center), vacuum, vacuum, color=0x33ffffff)) for center, dx, dy, dz in list(zip(np.mean([lower_bounds,upper_bounds],axis=0), *list(zip(*upper_bounds-lower_bounds))))[1:]: geometry.add_solid(Solid(make.box(dx,dy,dz,center), vacuum, vacuum, color=0x33ffffff)) return create_geometry_from_obj(geometry)
def setUp(self): self.cube = Geometry(water) self.cube.add_solid(Solid(box(100,100,100), water, water)) self.geo = create_geometry_from_obj(self.cube, update_bvh_cache=False) self.sim = Simulation(self.geo, geant4_processes=0) nphotons = 100000 pos = np.tile([0,0,0], (nphotons,1)).astype(np.float32) dir = np.tile([0,0,1], (nphotons,1)).astype(np.float32) pol = np.zeros_like(pos) phi = np.random.uniform(0, 2*np.pi, nphotons).astype(np.float32) pol[:,0] = np.cos(phi) pol[:,1] = np.sin(phi) t = np.zeros(nphotons, dtype=np.float32) wavelengths = np.empty(nphotons, np.float32) wavelengths.fill(400.0) self.photons = Photons(pos=pos, dir=dir, pol=pol, t=t, wavelengths=wavelengths)
def render_particle_track(self): x = 10.0 h = x*np.sqrt(3)/2 pyramid = make.linear_extrude([-x/2,0,x/2], [-h/2,h/2,-h/2], h, [0]*3, [0]*3) marker = Solid(pyramid, vacuum, vacuum) if self.photon_display_mode == 'beg': photons = self.ev.photons_beg else: photons = self.ev.photons_end geometry = Geometry() sample_factor = max(1, len(photons.pos) / 10000) for pos in photons.pos[::sample_factor]: geometry.add_solid(marker, displacement=pos, rotation=make_rotation_matrix(np.random.uniform(0,2*np.pi), uniform_sphere())) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries = [self.gpu_geometry, gpu_geometry]
def setUp(self): # Setup geometry cube = Detector(vacuum) cube.add_pmt( Solid(box(10.0, 10.0, 10.0), vacuum, vacuum, surface=r7081hqe_photocathode)) cube.set_time_dist_gaussian(1.2, -6.0, 6.0) cube.set_charge_dist_gaussian(1.0, 0.1, 0.5, 1.5) geo = create_geometry_from_obj(cube, update_bvh_cache=True, read_bvh_cache=False) print "Number of channels in detector: ", geo.num_channels() self.geo = geo self.sim = Simulation(self.geo, geant4_processes=0, nthreads_per_block=1, max_blocks=1) self.rfile = rt.TFile("output_test_detector.root", "recreate") self.htime = rt.TH1D("htime", "Time;ns", 120, 80, 120) self.hcharge = rt.TH1D("hcharge", "Charge;pe", 100, 0.5, 1.5)
return nevents * nreps * ndaq / ufloat( (np.mean(run_times), np.std(run_times))) if __name__ == '__main__': from chroma import demo import gc tools.enable_debug_on_crash() # Default to run all tests tests = ['ray', 'load', 'propagate', 'pdf', 'pdf_eval'] if len(sys.argv) > 1: tests = sys.argv[1:] # unless test names given on command line detector = create_geometry_from_obj(demo.detector()) context = gpu.create_cuda_context() gpu_detector = gpu.GPUDetector(detector) if 'ray' in tests: print('%s ray intersections/sec.' % \ tools.ufloat_to_str(intersect(gpu_detector))) # run garbage collection since there is a reference loop # in the GPUArray class. gc.collect() if 'load' in tests: print('%s photons loaded/sec.' % tools.ufloat_to_str(load_photons())) gc.collect()
np.std(run_times)) if __name__ == '__main__': from chroma import demo import gc tools.enable_debug_on_crash() # Default to run all tests tests = ['ray', 'load', 'propagate', 'pdf', 'pdf_eval'] if len(sys.argv) > 1: tests = sys.argv[1:] # unless test names given on command line obj = demo.tiny() # demo.detector() too big for 2G DRAM detector = create_geometry_from_obj(obj) context = gpu.create_cuda_context() gpu_detector = gpu.GPUDetector(detector) if 'ray' in tests: print '%s ray intersections/sec.' % \ tools.ufloat_to_str(intersect(gpu_detector)) # run garbage collection since there is a reference loop # in the GPUArray class. gc.collect() if 'load' in tests: print '%s photons loaded/sec.' % tools.ufloat_to_str(load_photons()) gc.collect()
def render_mc_info_all_events(self): #function added to visualize all events in a root file at the same time self.gpu_geometries = [self.gpu_geometry] for i, ev in enumerate(self.rr): print('Evaluating event %i' % i) if self.sum_mode or ev is None: return if self.track_display_mode in ['chroma', 'both' ] and ev.photon_tracks is not None: geometry = Geometry() print('Total Photons', len(ev.photon_tracks)) def has(flags, test): return flags & test == test tracks = ev.photon_tracks if self.photons_detected_only: detected = np.asarray([ has(track.flags[-1], event.SURFACE_DETECT) for track in tracks ]) tracks = [t for t, m in zip(tracks, detected) if m] cherenkov = np.asarray([ has(track.flags[0], event.CHERENKOV) and not has(track.flags[-1], event.BULK_REEMIT) for track in tracks ]) scintillation = np.asarray([ has(track.flags[0], event.SCINTILLATION) and not has(track.flags[-1], event.BULK_REEMIT) for track in tracks ]) reemission = np.asarray([ has(track.flags[-1], event.BULK_REEMIT) for track in tracks ]) if self.photons_only_type is not None: if self.photons_only_type == 'cher': selector = cherenkov elif self.photons_only_type == 'scint': selector = scintillation elif self.photons_only_type == 'reemit': selector = reemission else: raise Exception('Unknown only type: %s' % only) tracks = [t for t, m in zip(tracks, selector) if m] cherenkov = cherenkov[selector] scintillation = scintillation[selector] reemission = reemission[selector] nphotons = len(tracks) prob = self.photons_max / nphotons if self.photons_max is not None and nphotons is not 0 else 1.0 selector = np.random.random(len(tracks)) < prob nphotons = np.count_nonzero(selector) for i, track in ((i, t) for i, (s, t) in enumerate(zip(selector, tracks)) if s): if cherenkov[i]: color = [255, 0, 0] elif scintillation[i]: color = [0, 0, 255] elif reemission[i]: color = [0, 255, 0] else: color = [255, 255, 255] steps = min( len(track), self.photons_max_steps ) if self.photons_max_steps is not None else len(track) self.render_photon_track(geometry, track[:steps], sz=self.photons_track_size, color=color) if nphotons > 0: print('Rendered Photons', nphotons) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry)
def render_mc_info(self): #need to render photon tracking info if available self.gpu_geometries = [self.gpu_geometry] if self.sum_mode or self.ev is None: return if self.photon_display_mode == 'beg': photons = self.ev.photons_beg elif self.photon_display_mode == 'end': photons = self.ev.photons_end else: photons = None if photons is not None: geometry = Geometry() self.render_photons(geometry, photons) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry) if self.track_display_mode in ['geant4', 'both' ] and self.ev.vertices is not None: geometry = Geometry() any = False for vertex in self.ev.vertices: if vertex.steps: any = True self.render_vertex(geometry, vertex, children=True) if any: geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry) if self.track_display_mode in ['chroma', 'both' ] and self.ev.photon_tracks is not None: geometry = Geometry() print('Total Photons', len(self.ev.photon_tracks)) def has(flags, test): return flags & test == test tracks = self.ev.photon_tracks if self.photons_detected_only: detected = np.asarray([ has(track.flags[-1], event.SURFACE_DETECT) for track in tracks ]) tracks = [t for t, m in zip(tracks, detected) if m] cherenkov = np.asarray([ has(track.flags[0], event.CHERENKOV) and not has(track.flags[-1], event.BULK_REEMIT) for track in tracks ]) scintillation = np.asarray([ has(track.flags[0], event.SCINTILLATION) and not has(track.flags[-1], event.BULK_REEMIT) for track in tracks ]) reemission = np.asarray( [has(track.flags[-1], event.BULK_REEMIT) for track in tracks]) if self.photons_only_type is not None: if self.photons_only_type == 'cher': selector = cherenkov elif self.photons_only_type == 'scint': selector = scintillation elif self.photons_only_type == 'reemit': selector = reemission else: raise Exception('Unknown only type: %s' % only) tracks = [t for t, m in zip(tracks, selector) if m] cherenkov = cherenkov[selector] scintillation = scintillation[selector] reemission = reemission[selector] nphotons = len(tracks) prob = self.photons_max / nphotons if self.photons_max is not None else 1.0 selector = np.random.random(len(tracks)) < prob nphotons = np.count_nonzero(selector) for i, track in ((i, t) for i, (s, t) in enumerate(zip(selector, tracks)) if s): if cherenkov[i]: color = [255, 0, 0] elif scintillation[i]: color = [0, 0, 255] elif reemission[i]: color = [0, 255, 0] else: color = [255, 255, 255] steps = min( len(track), self.photons_max_steps ) if self.photons_max_steps is not None else len(track) self.render_photon_track(geometry, track[:steps], sz=self.photons_track_size, color=color) if nphotons > 0: print('Rendered Photons', nphotons) geometry = create_geometry_from_obj(geometry) gpu_geometry = gpu.GPUGeometry(geometry) self.gpu_geometries.append(gpu_geometry)
def view(obj, size=(800, 600), **camera_kwargs): geometry = create_geometry_from_obj(obj) camera = Camera(geometry, size, **camera_kwargs) camera.start() camera.join()
def setUp(self): self.detector = create_geometry_from_obj(chroma.demo.tiny(), update_bvh_cache=False) self.vertex_gen = constant_particle_gun('e-', (0,0,0), (1,0,0), 10)
def setUp(self): self.detector = create_geometry_from_obj(chroma.demo.tiny(), update_bvh_cache=False) self.vertex_gen = constant_particle_gun('e-', (0,0,0), (1,0,0), 10)
def view(obj, size=(800,600), **camera_kwargs): geometry = create_geometry_from_obj(obj) camera = Camera(geometry, size, **camera_kwargs) camera.start() camera.join()
def testBulkReemission(self): '''Test bulk reemission Start a bunch of monoenergetic photons at the center of a wavelength- shifting sphere, forcing reemission, and check that the final wavelength distribution matches the wls spectrum. ''' import scipy.stats nphotons = 1e5 # set up detector -- a sphere of 'scintillator' surrounded by a # detecting sphere scint = Material('scint') scint.set('refractive_index', 1) scint.set('absorption_length', 1.0) scint.set('scattering_length', 1e7) scint.set('reemission_prob', 1) x = np.arange(0,1000,10) norm = scipy.stats.norm(scale=50, loc=600) pdf = 10 * norm.pdf(x) cdf = norm.cdf(x) scint.reemission_cdf = np.array(zip(x, cdf)) detector = Surface('detector') detector.set('detect', 1) world = Geometry(vacuum) world.add_solid(Solid(sphere(1000), vacuum, vacuum, surface=detector)) world.add_solid(Solid(sphere(500), scint, vacuum)) w = create_geometry_from_obj(world, update_bvh_cache=False) sim = Simulation(w, geant4_processes=0) # initial photons -- isotropic 250 nm at the origin pos = np.tile([0,0,0], (nphotons,1)).astype(np.float32) dir = np.random.rand(nphotons, 3).astype(np.float32) * 2 - 1 dir /= np.sqrt(dir[:,0]**2 + dir[:,1]**2 + dir[:,2]**2)[:,np.newaxis] pol = np.zeros_like(pos) t = np.zeros(nphotons, dtype=np.float32) wavelengths = np.ones(nphotons).astype(np.float32) * 250 photons = Photons(pos=pos, dir=dir, pol=pol, t=t, wavelengths=wavelengths) # run simulation and extract final wavelengths event = sim.simulate([photons], keep_photons_end=True).next() mask = (event.photons_end.flags & SURFACE_DETECT) > 0 final_wavelengths = event.photons_end.wavelengths[mask] # compare wavelength distribution to scintillator's reemission pdf hist, edges = np.histogram(final_wavelengths, bins=x) print 'detected', hist.sum(), 'of', nphotons, 'photons' hist_norm = 1.0 * hist / (1.0 * hist.sum() / 1000) pdf /= (1.0 * pdf.sum() / 1000) chi2 = scipy.stats.chisquare(hist_norm, pdf[:-1])[1] print 'chi2 =', chi2 # show histogram comparison #plt.figure(1) #width = edges[1] - edges[0] #plt.bar(left=edges, height=pdf, width=width, color='red') #plt.bar(left=edges[:-1], height=hist_norm, width=width) #plt.show() self.assertTrue(chi2 > 0.75)
def view_nofork(obj, size=(800, 600), **camera_kwargs): geometry = create_geometry_from_obj(obj) camera = Camera(geometry, size, **camera_kwargs) camera._run()