diff --git a/numass-control/src/main/kotlin/inr/numass/control/ControlExtensions.kt b/numass-control/src/main/kotlin/inr/numass/control/ControlExtensions.kt deleted file mode 100644 index 6e260342..00000000 --- a/numass-control/src/main/kotlin/inr/numass/control/ControlExtensions.kt +++ /dev/null @@ -1,83 +0,0 @@ -package inr.numass.control - -import hep.dataforge.control.devices.Stateful -import hep.dataforge.values.Value -import java.time.Instant -import kotlin.reflect.KProperty - -class StateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Value? = - thisRef.getState(stateName ?: property.name) - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Value?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class StringStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): String? = - thisRef.getState(stateName ?: property.name).stringValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: String?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class BooleanStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Boolean? = - thisRef.getState(stateName ?: property.name).booleanValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Boolean?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class TimeStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Instant? = - thisRef.getState(stateName ?: property.name).timeValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Instant?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class NumberStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Number? = - thisRef.getState(stateName ?: property.name).numberValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Number?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class DoubleStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Double? = - thisRef.getState(stateName ?: property.name).doubleValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Double?) { - thisRef.setState(stateName ?: property.name, value); - } -} - -class IntStateDelegate(private val stateName: String?) { - operator fun getValue(thisRef: Stateful, property: KProperty<*>): Int? = - thisRef.getState(stateName ?: property.name).intValue() - - operator fun setValue(thisRef: Stateful, property: KProperty<*>, value: Int?) { - thisRef.setState(stateName ?: property.name, value); - } -} - - - -/** - * Delegate states to read/write property - */ -fun Stateful.state(stateName: String? = null) = StateDelegate(stateName) - -fun Stateful.stringState(stateName: String? = null) = StringStateDelegate(stateName) -fun Stateful.booleanState(stateName: String? = null) = BooleanStateDelegate(stateName) -fun Stateful.timeState(stateName: String? = null) = TimeStateDelegate(stateName) -fun Stateful.numberState(stateName: String? = null) = NumberStateDelegate(stateName) -fun Stateful.doubleState(stateName: String? = null) = DoubleStateDelegate(stateName) -fun Stateful.intState(stateName: String? = null) = IntStateDelegate(stateName) diff --git a/numass-core/src/main/java/inr/numass/data/api/NumassAnalyzer.java b/numass-core/src/main/java/inr/numass/data/api/NumassAnalyzer.java deleted file mode 100644 index 5503f7c8..00000000 --- a/numass-core/src/main/java/inr/numass/data/api/NumassAnalyzer.java +++ /dev/null @@ -1,152 +0,0 @@ -package inr.numass.data.api; - -import hep.dataforge.meta.Meta; -import hep.dataforge.tables.*; -import hep.dataforge.values.Value; -import hep.dataforge.values.Values; -import inr.numass.data.analyzers.SmartAnalyzer; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; -import java.util.stream.IntStream; -import java.util.stream.Stream; - -import static hep.dataforge.tables.Adapters.*; -import static inr.numass.data.api.NumassPoint.HV_KEY; - -/** - * A general raw data analysis utility. Could have different implementations - * Created by darksnake on 06-Jul-17. - */ -public interface NumassAnalyzer { - static NumassAnalyzer DEFAULT_ANALYZER = new SmartAnalyzer(); - - short MAX_CHANNEL = 10000; - - /** - * Calculate number of counts in the given channel - * - * @param spectrum - * @param loChannel - * @param upChannel - * @return - */ - static long countInWindow(Table spectrum, short loChannel, short upChannel) { - return spectrum.getRows().filter(row -> { - int channel = row.getInt(CHANNEL_KEY); - return channel >= loChannel && channel < upChannel; - }).mapToLong(it -> it.getValue(COUNT_KEY).numberValue().longValue()).sum(); - } - - - String CHANNEL_KEY = "channel"; - String COUNT_KEY = "count"; - String LENGTH_KEY = "length"; - String COUNT_RATE_KEY = "cr"; - String COUNT_RATE_ERROR_KEY = "crErr"; - - /** - * Perform analysis on block. The values for count rate, its error and point length in nanos must - * exist, but occasionally additional values could also be presented. - * - * @param block - * @return - */ - Values analyze(NumassBlock block, Meta config); - - /** - * Analysis result for point including hv information - * @param point - * @param config - * @return - */ - default Values analyzePoint(NumassPoint point, Meta config) { - Map map = new HashMap<>(analyze(point, config).asMap()); - map.put(HV_KEY, Value.of(point.getVoltage())); - return new ValueMap(map); - } - - /** - * Return unsorted stream of events including events from frames - * - * @param block - * @return - */ - Stream getEvents(NumassBlock block, Meta meta); - - /** - * Analyze the whole set. And return results as a table - * - * @param set - * @param config - * @return - */ - Table analyzeSet(NumassSet set, Meta config); - - /** - * Calculate the energy spectrum for a given block. The s - * - * @param block - * @param config - * @return - */ - default Table getSpectrum(NumassBlock block, Meta config) { - TableFormat format = new TableFormatBuilder() - .addNumber(CHANNEL_KEY, X_VALUE_KEY) - .addNumber(COUNT_KEY) - .addNumber(COUNT_RATE_KEY, Y_VALUE_KEY) - .addNumber(COUNT_RATE_ERROR_KEY, Y_ERROR_KEY) - .updateMeta(metaBuilder -> metaBuilder.setNode("config", config)) - .build(); - - //optimized for fastest computation - //TODO requires additional performance optimization - AtomicLong[] spectrum = new AtomicLong[MAX_CHANNEL]; - getEvents(block, config).forEach(event -> { - if (spectrum[event.getChanel()] == null) { - spectrum[event.getChanel()] = new AtomicLong(1); - } else { - spectrum[event.getChanel()].incrementAndGet(); - } - }); - - double seconds = (double) block.getLength().toMillis() / 1000d; - return new ListTable.Builder(format) - .rows(IntStream.range(0, MAX_CHANNEL) - .filter(i -> spectrum[i] != null) - .mapToObj(i -> { - long value = spectrum[i].get(); - return ValueMap.of( - format.namesAsArray(), - i, - value, - (double) value / seconds, - Math.sqrt(value) / seconds - ); - }) - ).build(); - } - - /** - * Get the approximate number of events in block. Not all analyzers support precise event counting - * - * @param block - * @param config - * @return - */ - default long getCount(NumassBlock block, Meta config) { - return analyze(block, config).getValue(COUNT_KEY).numberValue().longValue(); - } - - /** - * Get approximate effective point length in nanos. It is not necessary corresponds to real point length. - * - * @param block - * @param config - * @return - */ - default long getLength(NumassBlock block, Meta config) { - return analyze(block, config).getValue(LENGTH_KEY).numberValue().longValue(); - } -}