Skip to content
Snippets Groups Projects
Commit 4612335b authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Implement filter type spectrum for IdealFilter.

not yet tested.
parent 31435f77
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ use opossum::{
analyzer::AnalyzerEnergy,
error::OpossumError,
lightdata::{DataEnergy, LightData},
nodes::{BeamSplitter, Detector, IdealFilter, Source},
nodes::{BeamSplitter, Detector, IdealFilter, Source, FilterType},
optic_scenery::OpticScenery, spectrum::create_he_ne_spectrum,
};
......@@ -20,7 +20,7 @@ fn main() -> Result<(), OpossumError> {
})),
);
let i_bs = scenery.add_element("Beam splitter", BeamSplitter::new(0.6));
let i_f = scenery.add_element("Filter", IdealFilter::new(0.5)?);
let i_f = scenery.add_element("Filter", IdealFilter::new(FilterType::Constant(0.5))?);
let i_d1 = scenery.add_element("Detector 1", Detector::default());
let i_d2 = scenery.add_element("Detector 2", Detector::default());
......
......@@ -3,14 +3,20 @@ use crate::error::OpossumError;
use crate::lightdata::{DataEnergy, LightData};
use crate::optic_node::{Dottable, LightResult, Optical};
use crate::optic_ports::OpticPorts;
use crate::spectrum::Spectrum;
use std::collections::HashMap;
type Result<T> = std::result::Result<T, OpossumError>;
#[derive(Debug, Clone)]
pub enum FilterType {
Constant(f64),
Spectrum(Spectrum),
}
#[derive(Debug)]
/// An ideal filter with given transmission or optical density.
pub struct IdealFilter {
transmission: f64,
filter_type: FilterType,
}
impl IdealFilter {
......@@ -19,16 +25,22 @@ impl IdealFilter {
/// # Errors
///
/// This function will return an error if a transmission factor > 1.0 is given (This would be an amplifiying filter :-) ).
pub fn new(transmission: f64) -> Result<Self> {
if transmission <= 1.0 {
Ok(Self { transmission })
} else {
Err(OpossumError::Other("attenuation must be <= 1.0".into()))
pub fn new(filter_type: FilterType) -> Result<Self> {
match filter_type {
FilterType::Constant(transmission) => {
if transmission < 0.0 || transmission > 1.0 {
return Err(OpossumError::Other("attenuation must be <= 1.0".into()))
}
},
_ => ()
}
Ok(Self {
filter_type,
})
}
/// Returns the transmission factor of this [`IdealFilter`].
pub fn transmission(&self) -> f64 {
self.transmission
/// Returns the filter type of this [`IdealFilter`].
pub fn filter_type(&self) -> FilterType {
self.filter_type.clone()
}
/// Sets the transmission of this [`IdealFilter`].
///
......@@ -37,7 +49,7 @@ impl IdealFilter {
/// This function will return an error if a transmission factor > 1.0 is given (This would be an amplifiying filter :-) ).
pub fn set_transmission(&mut self, transmission: f64) -> Result<()> {
if transmission <= 1.0 {
self.transmission = transmission;
self.filter_type = FilterType::Constant(transmission);
Ok(())
} else {
Err(OpossumError::Other("attenuation must be <=1.0".into()))
......@@ -50,27 +62,41 @@ impl IdealFilter {
/// This function will return an error if an optical density < 0.0 was given.
pub fn set_optical_density(&mut self, density: f64) -> Result<()> {
if density >= 0.0 {
self.transmission = f64::powf(10.0, -1.0 * density);
self.filter_type = FilterType::Constant(f64::powf(10.0, -1.0 * density));
Ok(())
} else {
Err(OpossumError::Other("optical densitiy must be >=0".into()))
}
}
/// Returns the transmission facotr of this [`IdealFilter`] expressed as optical density.
pub fn optical_density(&self) -> f64 {
-1.0 * f64::log10(self.transmission)
/// Returns the transmission factor of this [`IdealFilter`] expressed as optical density for the [`FilterType::Constant`].
///
/// This functions `None` if the filter type is not [`FilterType::Constant`].
pub fn optical_density(&self) -> Option<f64> {
match self.filter_type {
FilterType::Constant(t) => Some(-1.0 * f64::log10(t)),
_ => None,
}
}
fn analyze_energy(&mut self, incoming_data: LightResult) -> Result<LightResult> {
let input = incoming_data.get("front");
if let Some(Some(input)) = input {
match input {
LightData::Energy(e) => {
let mut out_spec=e.spectrum.clone();
if out_spec.scale_vertical(self.transmission).is_ok() {
let light_data = Some(LightData::Energy(DataEnergy {
spectrum: out_spec,
}));
return Ok(HashMap::from([("rear".into(), light_data)]));
let mut out_spec = e.spectrum.clone();
match &self.filter_type {
FilterType::Constant(t) => {
if out_spec.scale_vertical(*t).is_ok() {
let light_data =
Some(LightData::Energy(DataEnergy { spectrum: out_spec }));
return Ok(HashMap::from([("rear".into(), light_data)]));
}
}
FilterType::Spectrum(s) => {
out_spec.filter(&s);
let light_data =
Some(LightData::Energy(DataEnergy { spectrum: out_spec }));
return Ok(HashMap::from([("rear".into(), light_data)]));
},
}
}
_ => return Err(OpossumError::Analysis("expected energy value".into())),
......
......@@ -13,4 +13,5 @@ pub use node_group::NodeGroup;
pub use node_beam_splitter::BeamSplitter;
pub use node_source::Source;
pub use node_detector::Detector;
pub use ideal_filter::IdealFilter;
\ No newline at end of file
pub use ideal_filter::IdealFilter;
pub use ideal_filter::FilterType;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment