From 7cdd4286aa32923cd071d08c92f0eee0438c8fcf Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 10 Aug 2017 22:12:21 +0300 Subject: [PATCH] Corrections redesign --- .../data/analyzers/AbstractAnalyzer.java | 28 +++++++++++++------ .../numass/data/analyzers/SimpleAnalyzer.java | 12 ++------ .../numass/data/analyzers/TimeAnalyzer.java | 8 ++---- .../inr/numass/data/api/NumassAnalyzer.java | 2 +- .../main/java/inr/numass/NumassPlugin.java | 4 ++- .../inr/numass/actions/MergeDataAction.java | 4 +-- .../numass/actions/TransformDataAction.java | 19 +++++++------ .../inr/numass/models/LossCalculator.java | 2 +- .../models/sterile/NumassTransmission.java | 15 +++++++--- .../sterile/SterileNeutrinoSpectrum.java | 4 +-- .../numass/tasks/NumassTableFilterTask.java | 2 +- .../inr/numass/viewer/NumassLoaderView.kt | 3 +- 12 files changed, 58 insertions(+), 45 deletions(-) diff --git a/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java b/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java index 88b9ab9c..b3cebb37 100644 --- a/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java @@ -18,8 +18,10 @@ import static inr.numass.data.api.NumassPoint.HV_KEY; * Created by darksnake on 11.07.2017. */ public abstract class AbstractAnalyzer implements NumassAnalyzer { - public static String[] NAME_LIST = {LENGTH_KEY, COUNT_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY, "window", "timestamp"}; - public static String[] NAME_LIST_WITH_HV = {HV_KEY, LENGTH_KEY, COUNT_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY, "window", "timestamp"}; + public static String WINDOW_KEY = "window"; + + public static String[] NAME_LIST = {LENGTH_KEY, COUNT_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY, WINDOW_KEY, "timestamp"}; + public static String[] NAME_LIST_WITH_HV = {HV_KEY, LENGTH_KEY, COUNT_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY, WINDOW_KEY, "timestamp"}; @Nullable private final SignalProcessor processor; @@ -40,17 +42,25 @@ public abstract class AbstractAnalyzer implements NumassAnalyzer { * @return */ public Stream getEvents(NumassBlock block, Meta config) { + int loChannel = config.getInt("window.lo", 0); + int upChannel = config.getInt("window.up", Integer.MAX_VALUE); + Stream res = getAllEvents(block).filter(event -> { + short channel = event.getChanel(); + return channel >= loChannel && channel < upChannel; + }); + if (config.getBoolean("sort", false)) { + res = res.sorted(Comparator.comparing(NumassEvent::getTimeOffset)); + } + return res; + } + + protected Stream getAllEvents(NumassBlock block){ if (block.getFrames().count() == 0) { return block.getEvents(); } else if (getProcessor() == null) { throw new IllegalArgumentException("Signal processor needed to analyze frames"); } else { - //TODO - Stream res = Stream.concat(block.getEvents(), block.getFrames().flatMap(getProcessor()::analyze)); - if (config.getBoolean("sort", false)) { - res = res.sorted(Comparator.comparing(NumassEvent::getTimeOffset)); - } - return res; + return Stream.concat(block.getEvents(), block.getFrames().flatMap(getProcessor()::analyze)); } } @@ -63,7 +73,7 @@ public abstract class AbstractAnalyzer implements NumassAnalyzer { .addNumber(COUNT_KEY) .addNumber(COUNT_RATE_KEY, Y_VALUE_KEY) .addNumber(COUNT_RATE_ERROR_KEY, Y_ERROR_KEY) - .addColumn("window") + .addColumn(WINDOW_KEY) .addTime() .build(); diff --git a/numass-core/src/main/java/inr/numass/data/analyzers/SimpleAnalyzer.java b/numass-core/src/main/java/inr/numass/data/analyzers/SimpleAnalyzer.java index 9c30e448..10780815 100644 --- a/numass-core/src/main/java/inr/numass/data/analyzers/SimpleAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/analyzers/SimpleAnalyzer.java @@ -4,13 +4,10 @@ import hep.dataforge.meta.Meta; import hep.dataforge.tables.ValueMap; import hep.dataforge.values.Values; import inr.numass.data.api.NumassBlock; -import inr.numass.data.api.NumassEvent; import inr.numass.data.api.NumassPoint; import inr.numass.data.api.SignalProcessor; import org.jetbrains.annotations.Nullable; -import java.util.stream.Stream; - /** * A simple event counter * Created by darksnake on 07.07.2017. @@ -24,15 +21,12 @@ public class SimpleAnalyzer extends AbstractAnalyzer { public SimpleAnalyzer() { } - public Stream getEventStream(NumassBlock block, int loChannel, int upChannel) { - return getEvents(block, Meta.empty()).filter(it -> it.getChanel() >= loChannel && it.getChanel() < upChannel); - } @Override public Values analyze(NumassBlock block, Meta config) { int loChannel = config.getInt("window.lo", 0); int upChannel = config.getInt("window.up", Integer.MAX_VALUE); - long count = getEventStream(block, loChannel, upChannel).count(); + long count = getEvents(block, config).count(); double countRate = (double) count / block.getLength().toMillis() * 1000; double countRateError = Math.sqrt((double) count) / block.getLength().toMillis() * 1000; @@ -43,7 +37,7 @@ public class SimpleAnalyzer extends AbstractAnalyzer { count, countRate, countRateError, - new int[]{loChannel, upChannel}, + new Integer[]{loChannel, upChannel}, block.getStartTime()); } else { return ValueMap.of(NAME_LIST, @@ -51,7 +45,7 @@ public class SimpleAnalyzer extends AbstractAnalyzer { count, countRate, countRateError, - new int[]{loChannel, upChannel}, + new Integer[]{loChannel, upChannel}, block.getStartTime()); } } diff --git a/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java b/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java index 44fb8a87..77e11b3f 100644 --- a/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java @@ -37,10 +37,6 @@ public class TimeAnalyzer extends AbstractAnalyzer { AtomicLong totalT = new AtomicLong(0); getEventsWithDelay(block, config) - .filter(pair -> { - short channel = pair.getKey().getChanel(); - return channel >= loChannel && channel < upChannel; - }) .forEach(pair -> { totalN.incrementAndGet(); //TODO add progress listener here @@ -59,7 +55,7 @@ public class TimeAnalyzer extends AbstractAnalyzer { count, countRate, countRateError, - new int[]{loChannel, upChannel}, + new Integer[]{loChannel, upChannel}, block.getStartTime()); } else { return ValueMap.of(NAME_LIST, @@ -67,7 +63,7 @@ public class TimeAnalyzer extends AbstractAnalyzer { count, countRate, countRateError, - new int[]{loChannel, upChannel}, + new Integer[]{loChannel, upChannel}, block.getStartTime() ); } 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 index cd913b84..91221afd 100644 --- a/numass-core/src/main/java/inr/numass/data/api/NumassAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/api/NumassAnalyzer.java @@ -55,7 +55,7 @@ public interface NumassAnalyzer { * @param block * @return */ - Stream getEvents(NumassBlock block, Meta config); + Stream getEvents(NumassBlock block, Meta meta); /** * Analyze the whole set. And return results as a table diff --git a/numass-main/src/main/java/inr/numass/NumassPlugin.java b/numass-main/src/main/java/inr/numass/NumassPlugin.java index cbd8923d..15633221 100644 --- a/numass-main/src/main/java/inr/numass/NumassPlugin.java +++ b/numass-main/src/main/java/inr/numass/NumassPlugin.java @@ -33,6 +33,8 @@ import hep.dataforge.stat.models.XYModel; import hep.dataforge.tables.PointAdapter; import hep.dataforge.tables.XYAdapter; import inr.numass.actions.*; +import inr.numass.data.api.NumassAnalyzer; +import inr.numass.data.api.NumassPoint; import inr.numass.models.*; import inr.numass.models.sterile.SterileNeutrinoSpectrum; import inr.numass.tasks.*; @@ -296,7 +298,7 @@ public class NumassPlugin extends BasicPlugin { if (an.hasMeta(PointAdapter.DATA_ADAPTER_KEY)) { return new XYAdapter(an.getMeta(PointAdapter.DATA_ADAPTER_KEY)); } else { - return new XYAdapter("Uread", "CR", "CRerr"); + return new XYAdapter(NumassPoint.HV_KEY, NumassAnalyzer.COUNT_RATE_KEY, NumassAnalyzer.COUNT_RATE_ERROR_KEY); } } } diff --git a/numass-main/src/main/java/inr/numass/actions/MergeDataAction.java b/numass-main/src/main/java/inr/numass/actions/MergeDataAction.java index af0ae444..1cfcb9f7 100644 --- a/numass-main/src/main/java/inr/numass/actions/MergeDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/MergeDataAction.java @@ -34,8 +34,8 @@ import java.util.*; /** * @author Darksnake */ -@TypedActionDef(name = "merge", inputType = Table.class, outputType = Table.class, info = "Merge different numass data files into one.") -@NodeDef(name = "grouping", info = "The defenition of grouping rule for this merge", target = "method::hep.dataforge.actions.GroupBuilder.byMeta") +@TypedActionDef(name = "numass.merge", inputType = Table.class, outputType = Table.class, info = "Merge different numass data files into one.") +@NodeDef(name = "grouping", info = "The definition of grouping rule for this merge", target = "method::hep.dataforge.actions.GroupBuilder.byMeta") public class MergeDataAction extends ManyToOneAction { public static final String MERGE_NAME = "mergeName"; diff --git a/numass-main/src/main/java/inr/numass/actions/TransformDataAction.java b/numass-main/src/main/java/inr/numass/actions/TransformDataAction.java index c232151e..de263d15 100644 --- a/numass-main/src/main/java/inr/numass/actions/TransformDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/TransformDataAction.java @@ -7,6 +7,7 @@ import hep.dataforge.description.TypedActionDef; import hep.dataforge.description.ValueDef; import hep.dataforge.meta.Laminate; import hep.dataforge.meta.Meta; +import hep.dataforge.meta.MetaUtils; import hep.dataforge.names.Named; import hep.dataforge.tables.ColumnFormat; import hep.dataforge.tables.ColumnTable; @@ -14,11 +15,10 @@ import hep.dataforge.tables.ListColumn; import hep.dataforge.tables.Table; import hep.dataforge.values.Values; import inr.numass.utils.NumassUtils; +import javafx.util.Pair; import java.util.ArrayList; import java.util.List; -import java.util.function.Function; -import java.util.stream.Collectors; import static hep.dataforge.values.ValueType.NUMBER; import static hep.dataforge.values.ValueType.STRING; @@ -36,15 +36,18 @@ import static inr.numass.utils.NumassUtils.pointExpression; @ValueDef(name = "utransform", info = "Expression for voltage transformation. Uses U as input") @NodeDef(name = "correction", multiple = true, target = "method::inr.numass.actions.TransformDataAction.makeCorrection") public class TransformDataAction extends OneToOneAction { + @Override protected Table execute(Context context, String name, Table input, Laminate meta) { List corrections = new ArrayList<>(); - if (meta.optMeta("correction").isPresent()) { - corrections.addAll(meta.getMetaList("correction").stream() - .map((Function) this::makeCorrection) - .collect(Collectors.toList())); - } + + meta.optMeta("corrections").ifPresent(corrs -> + MetaUtils.nodeStream(corrs) + .map(Pair::getValue) + .map(this::makeCorrection) + .forEach(corrections::add) + ); if (meta.hasValue("correction")) { final String correction = meta.getString("correction"); @@ -112,7 +115,7 @@ public class TransformDataAction extends OneToOneAction { return new Correction() { @Override public String getName() { - return corrMeta.getString("name", ""); + return corrMeta.getString("name", corrMeta.getName()); } @Override diff --git a/numass-main/src/main/java/inr/numass/models/LossCalculator.java b/numass-main/src/main/java/inr/numass/models/LossCalculator.java index 91a72f44..aafab6d2 100644 --- a/numass-main/src/main/java/inr/numass/models/LossCalculator.java +++ b/numass-main/src/main/java/inr/numass/models/LossCalculator.java @@ -287,7 +287,7 @@ public class LossCalculator { * @return */ public List getLossProbabilities(double X) { - return lossProbCache.computeIfAbsent(X, (x) -> { + return lossProbCache.computeIfAbsent(X, x -> { List res = new ArrayList<>(); double prob; if (x > 0) { diff --git a/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java b/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java index 36d025c7..fc6319c1 100644 --- a/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java +++ b/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java @@ -29,9 +29,12 @@ public class NumassTransmission extends AbstractParametricBiFunction { private final LossCalculator calculator; private final BivariateFunction trapFunc; + private final boolean adjustX; + public NumassTransmission(Context context, Meta meta) { super(list); this.calculator = LossCalculator.instance(); + adjustX = meta.getBoolean("adjustX",false); if (meta.hasValue("trapping")) { String trapFuncStr = meta.getString("trapping"); if (trapFuncStr.startsWith("function::")) { @@ -50,12 +53,16 @@ public class NumassTransmission extends AbstractParametricBiFunction { } } - public static double getX(double eIn, Values set) { - //From our article - return set.getDouble("X") * Math.log(eIn / ION_POTENTIAL) * eIn * ION_POTENTIAL / 1.9580741410115568e6; + public double getX(double eIn, Values set) { + if(adjustX){ + //From our article + return set.getDouble("X") * Math.log(eIn / ION_POTENTIAL) * eIn * ION_POTENTIAL / 1.9580741410115568e6; + } else { + return set.getDouble("X"); + } } - public static double p0(double eIn, Values set) { + public double p0(double eIn, Values set) { return LossCalculator.instance().getLossProbability(0, getX(eIn, set)); } diff --git a/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java b/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java index 048bcadb..92bb2a65 100644 --- a/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java @@ -45,7 +45,7 @@ public class SterileNeutrinoSpectrum extends AbstractParametricFunction { /** * variables:Ein,Eout; parameters: "A" */ - private final ParametricBiFunction transmission; + private final NumassTransmission transmission; /** * variables:Eout,U; parameters: "X", "trap" */ @@ -193,7 +193,7 @@ public class SterileNeutrinoSpectrum extends AbstractParametricFunction { @Override public double value(double eIn, double u, Values set) { - double p0 = NumassTransmission.p0(eIn, set); + double p0 = transmission.p0(eIn, set); return p0 * resolution.value(eIn, u, set) + lossRes(transmission, eIn, u, set); } diff --git a/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java b/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java index 94aef4f9..b2b2c9d9 100644 --- a/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java +++ b/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java @@ -38,7 +38,7 @@ public class NumassTableFilterTask extends SingleActionTask { protected TaskModel transformModel(TaskModel model) { MetaBuilder metaBuilder = new MetaBuilder(model.meta()).removeNode("filter"); if (model.meta().hasMeta("empty")) { - model.dependsOn("substractEmpty", metaBuilder.build(), "prepare"); + model.dependsOn("subtractEmpty", metaBuilder.build(), "prepare"); } else { model.dependsOn("prepare", metaBuilder.build(), "prepare"); } diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/NumassLoaderView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/NumassLoaderView.kt index ba5d6bc3..e835bb43 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/NumassLoaderView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/NumassLoaderView.kt @@ -17,6 +17,7 @@ import hep.dataforge.storage.commons.JSONMetaWriter import hep.dataforge.tables.Table import hep.dataforge.tables.ValueMap import hep.dataforge.tables.XYAdapter +import inr.numass.data.NumassDataUtils import inr.numass.data.analyzers.SimpleAnalyzer import inr.numass.data.api.NumassAnalyzer import inr.numass.data.api.NumassPoint @@ -324,7 +325,7 @@ class NumassLoaderView : View() { PlottableData.plot( seriesName, XYAdapter(NumassAnalyzer.CHANNEL_KEY, valueAxis), - NumassAnalyzer.spectrumWithBinning(getSpectrum(point), binning) + NumassDataUtils.spectrumWithBinning(getSpectrum(point), binning) ).apply { configure(plottableConfig) }.also {