Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuum spectrum #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hexsample/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def add_source_options(self) -> None:
"""Add an option group for the source properties.
"""
group = self.add_argument_group('source', 'X-ray source properties')
types = ['lines', 'continuum']
group.add_argument('--spectrumtype', type=str, choices=types, default='lines',
help='type of spectrum, choices are between a discrete\
and a continuum spectrum in the range [7000, 9000] eV.')
group.add_argument('--srcelement', type=str, default='Cu',
help='element generating the line forest')
group.add_argument('--srclevel', type=str, default='K',
Expand Down
8 changes: 6 additions & 2 deletions hexsample/bin/hxsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from hexsample.hexagon import HexagonalLayout
from hexsample.mc import PhotonList
from hexsample.roi import Padding
from hexsample.source import LineForest, GaussianBeam, Source
from hexsample.source import ContinuumSpectrum, LineForest, GaussianBeam, Source
from hexsample.sensor import Material, Sensor


Expand All @@ -58,7 +58,11 @@ def hxsim(**kwargs):
"""
# pylint: disable=too-many-locals, invalid-name
rng.initialize(seed=kwargs['seed'])
spectrum = LineForest(kwargs['srcelement'], kwargs['srclevel'])
spectrum_type = kwargs['spectrumtype']
if spectrum_type == 'continuum':
spectrum = ContinuumSpectrum()
else:
spectrum = LineForest(kwargs['srcelement'], kwargs['srclevel'])
beam = GaussianBeam(kwargs['srcposx'], kwargs['srcposy'], kwargs['srcsigma'])
source = Source(spectrum, beam)
material = Material(kwargs['actmedium'], kwargs['fano'])
Expand Down
1 change: 1 addition & 0 deletions hexsample/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def _fill_recon_row(row: tables.tableextension.Row, event: ReconEvent) -> None:
row['trigger_id'] = event.trigger_id
row['timestamp'] = event.timestamp
row['livetime'] = event.livetime
# Roi size is not implemented in all event type classes
#row['roi_size'] = event.roi_size
row['cluster_size'] = event.cluster.size()
row['energy'] = event.energy()
Expand Down
41 changes: 39 additions & 2 deletions hexsample/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ def plot(self) -> None:
"""
raise NotImplementedError



class LineForest(SpectrumBase):

"""Class describing a set of X-ray emission lines for a given element and
Expand Down Expand Up @@ -269,8 +267,47 @@ def __str__(self):
"""String formatting.
"""
return f'{self.line_dict}'

class ContinuumSpectrum(SpectrumBase):
"""Class describing a continuum and uniform x-ray energy spectrum between
a minimum and a maximum value. By now the continuum spectrum is set in the
range [7000, 9000] eV, the possibility to choose it in the sim has to be
implemented.

Arguments
---------
min_energy : float
Minimum energy of the spectrum
max_energy : float
Maximum energy of the spectrum
"""
def __init__(self, min_energy: float=7000, max_energy: float=9000) -> None:
self.minimum_energy = min_energy
self.maximum_energy = max_energy

def rvs(self, size: int = 1) -> np.ndarray:
"""Throw random energies from the line forest.

Arguments
---------
size : int
The number of X-ray energies to be generated.

Returns
-------
energy : np.ndarray of shape ``size``
The photon energies in eV.
"""
# Random generating the energies in [minimum_energy, maximum_energy]
return rng.generator.uniform(self.minimum_energy, self.maximum_energy, size)

def plot(self) -> None:
"""Plot the continuum spectrum for 1e4 generated photons.
"""
# pylint: disable=invalid-name
energies_ = self.rvs(10000)
plt.hist(energies_, density=True, color='tab:blue')
setup_gca(xlabel='Energy [eV]', ylabel='PDF', logy=True, grids=True)

class Source:

Expand Down
Loading