• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JohannesBuchner / xars / 11677750078

22 Sep 2024 07:13PM UTC coverage: 90.64% (-0.2%) from 90.861%
11677750078

push

github

JohannesBuchner
[doc] typo

217 of 302 branches covered (71.85%)

Branch coverage included in aggregate %.

1807 of 1931 relevant lines covered (93.58%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/scripts/vizfek2.py
1
"""
2
This script demonstrates tracking photons through the simulation, and
3
picking up the outcoming FeK emission.
4
"""
5
import numpy
1✔
6

7
from xars.binning import bin2energy, energy2bin, nbins
1✔
8
from xars.coordtrans import to_cartesian
1✔
9
from xars.photons import PhotonBunch
1✔
10

11

12
def run(
1✔
13
    nphot, geometry,
14
    verbose=False, emin=0.5, PhoIndex=2
15
):
16

17
    energy_lo, energy_hi = bin2energy(range(nbins))
1✔
18
    energy = (energy_hi + energy_lo) / 2.
1✔
19
    # deltae = energy_hi - energy_lo
20

21
    photons = PhotonBunch(i=100, nphot=nphot, verbose=verbose, geometry=geometry)
1✔
22
    photons.energy = emin / numpy.random.power(PhoIndex, size=nphot)
1✔
23
    photons.energy[photons.energy > energy.max()] = energy.max()
1✔
24
    photons.binid = energy2bin(photons.energy)
1✔
25

26
    for n_interactions in range(1000):
1!
27
        prev_location = photons.rad.copy(), photons.theta.copy(), photons.phi.copy()
1✔
28
        emission, more = photons.pump()
1✔
29
        if emission is None and not more:
1✔
30
            break
×
31
        if emission is None:
1✔
32
            continue
×
33
        if len(emission['energy']) == 0:
1✔
34
            if not more:
×
35
                break
×
36
            continue
×
37
        if verbose:
1✔
38
            print(' received %d emitted photons (after %d interactions)' % (len(emission['energy']), n_interactions))
×
39
        beta = emission['beta']
1✔
40
        alpha = emission['alpha']
1✔
41
        # bins = emission['binid']
42
        energy = emission['energy']
1✔
43
        mask_escaped = emission['mask_escaped']
1✔
44

45
        # get previous location
46
        prev_rad, prev_theta, prev_phi = prev_location
1✔
47
        rad, theta, phi = prev_rad[mask_escaped], prev_theta[mask_escaped], prev_phi[mask_escaped]
1✔
48
        assert rad.shape == energy.shape
1✔
49
        if n_interactions == 0:
1✔
50
            continue  # we do not consider direct emission
1✔
51

52
        # filter to FeKa band
53
        mask = numpy.logical_and(emission['energy'] > 6.1, emission['energy'] < 6.5)
1✔
54

55
        if mask.any():
1✔
56
            # convert last scattering position to xyz
57
            x, y, z = to_cartesian((rad[mask], theta[mask], phi[mask]))
1✔
58

59
            yield x, y, z, alpha[mask], beta[mask], energy[mask]
1✔
60

61
        if not more:
1✔
62
            break
1✔
63

64

65
if __name__ == '__main__':
66
    nh = 1e24 / 1e22
67

68
    from xars.geometries.conetorus import ConeTorusGeometry
69
    geometry = ConeTorusGeometry(Theta_tor=80 / 180. * numpy.pi, NH=nh, verbose=False)
70

71
    from astropy.table import Table
72

73
    events = None
74
    # numpy.random.seed(101)
75

76
    for _ in range(10):
77
        for events_info in run(1000000, geometry, emin=6, PhoIndex=2):
78
            new_events = numpy.transpose(events_info)
79
            if events is None:
80
                events = new_events
81
            else:
82
                events = numpy.concatenate((events, new_events))
83

84
        print("events collected:", events.shape)
85
        # numpy.savetxt('vizfek.txt.gz', events)
86
        Table(events, names=['x', 'y', 'z', 'alpha', 'beta', 'energy']).write('vizfek.fits', format='fits', overwrite=True)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc