From 94dba3b02e88777c198739bfd753caee2a3faa34 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 31 May 2016 20:05:15 +0300 Subject: [PATCH] [no commit message] --- numass-main/build.gradle | 22 +- numass-main/nbproject/project.properties | 0 .../numass/scripts/CalculateLossTail.groovy | 94 ----- .../numass/scripts/OldFitResultReader.groovy | 110 ------ .../src/main/java/inr/numass/Main.java | 3 + .../main/java/inr/numass/NumassContext.java | 13 +- .../src/main/java/inr/numass/NumassIO.java | 20 +- .../numass/actions/MonitorCorrectAction.java | 12 + .../inr/numass/actions/PrepareDataAction.java | 22 +- .../actions/ReadNumassStorageAction.java | 4 - .../actions/ShowLossSpectrumAction.java | 10 +- .../inr/numass/actions/SummaryAction.java | 2 +- .../inr/numass/data/SpectrumInformation.java | 12 +- .../java/inr/numass/models/BetaSpectrum.java | 33 +- .../inr/numass/models/CustomNBkgSpectrum.java | 4 +- .../numass/models/EmpiricalLossSpectrum.java | 120 +++---- .../ExperimentalVariableLossSpectrum.java | 327 +++++++++--------- .../inr/numass/models/GaussResolution.java | 41 ++- .../numass/models/GaussSourceSpectrum.java | 149 ++++---- .../java/inr/numass/models/GunSpectrum.java | 18 +- .../inr/numass/models/GunTailSpectrum.java | 149 ++++---- .../inr/numass/models/LossCalculator.java | 30 +- .../inr/numass/models/ModularSpectrum.java | 15 +- .../java/inr/numass/models/NBkgSpectrum.java | 15 +- .../numass/models/NamedSpectrumCaching.java | 30 +- .../java/inr/numass/models/SimpleRange.java | 70 ++-- .../java/inr/numass/models/SpectrumRange.java | 26 +- .../java/inr/numass/models/Transmission.java | 11 +- .../models/TransmissionConvolution.java | 6 +- .../numass/models/TritiumSpectrumCaching.java | 12 +- .../numass/models/VariableLossSpectrum.java | 27 +- .../inr/numass/utils/ExpressionUtils.java | 33 ++ .../java/inr/numass/utils/OldDataReader.java | 3 + .../workbench/NumassWorkbenchController.java | 7 + .../java/inr/numass/workbench/Workbench.java | 9 + .../java/inr/numass/NumassContextTest.java | 32 ++ .../numass/actions/PrepareDataActionTest.java | 32 ++ .../inr/numass/run/NumassSpectrumTest.java | 31 ++ .../main/java/inr/numass/storage/NMPoint.java | 2 + .../inr/numass/storage/NumassDataLoader.java | 14 +- .../numass/storage/SetDirectionUtility.java | 73 ++++ 41 files changed, 853 insertions(+), 790 deletions(-) create mode 100644 numass-main/nbproject/project.properties delete mode 100644 numass-main/src/main/groovy/inr/numass/scripts/CalculateLossTail.groovy delete mode 100644 numass-main/src/main/groovy/inr/numass/scripts/OldFitResultReader.groovy create mode 100644 numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java create mode 100644 numass-main/src/test/java/inr/numass/NumassContextTest.java create mode 100644 numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java create mode 100644 numass-storage/src/main/java/inr/numass/storage/SetDirectionUtility.java diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 462a27c2..bd7a9f0e 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -7,10 +7,6 @@ mainClassName = mainClass description = """The main head of all numass projects""" -ext{ - defaultNumassPath = 'D:\\loss-2011\\' -} - dependencies { compile group: 'commons-cli', name: 'commons-cli', version:'1.+' compile group: 'commons-io', name: 'commons-io', version:'2.+' @@ -20,22 +16,12 @@ dependencies { compile project(':numass-storage') } -task runNumass(dependsOn: classes, type : JavaExec){ - main mainClass +task workbench(dependsOn: classes, type: JavaExec){ + main 'inr.numass.workbench.Workbench' classpath = sourceSets.main.runtimeClasspath - workingDir defaultNumassPath - description "run Main method in the current numass working directory" - group "numass" -} - -task debugNumass(dependsOn: classes, type: JavaExec) { - main mainClass - classpath = sourceSets.main.runtimeClasspath - workingDir defaultNumassPath - ignoreExitValue = true - debug = true - description "debug Main method in the current numass working directory" + description "start visual numass workbench" group "numass" + } task listActions(dependsOn: classes, type: JavaExec) { diff --git a/numass-main/nbproject/project.properties b/numass-main/nbproject/project.properties new file mode 100644 index 00000000..e69de29b diff --git a/numass-main/src/main/groovy/inr/numass/scripts/CalculateLossTail.groovy b/numass-main/src/main/groovy/inr/numass/scripts/CalculateLossTail.groovy deleted file mode 100644 index cdcafc18..00000000 --- a/numass-main/src/main/groovy/inr/numass/scripts/CalculateLossTail.groovy +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2015 Alexander Nozik. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package inr.numass.scripts - -import hep.dataforge.datafitter.ParamSet -import hep.dataforge.io.FittingIOUtils -import hep.dataforge.io.PrintFunction -import hep.dataforge.maths.NamedDoubleArray -import hep.dataforge.maths.NamedDoubleSet -import hep.dataforge.maths.NamedMatrix -import hep.dataforge.plots.PlotFrame -import hep.dataforge.plots.data.PlottableFunction -import hep.dataforge.plots.jfreechart.JFreeChartFrame -import inr.numass.actions.ShowLossSpectrumAction; -import inr.numass.models.ExperimentalVariableLossSpectrum -import inr.numass.models.LossCalculator -import org.apache.commons.math3.analysis.UnivariateFunction - - -NamedDoubleSet transformOldMeans(NamedDoubleSet old){ - String[] names = ["exPos", "ionPos", "exW", "ionW", "exIonRatio"]; - double[] values = new double[5]; - values[0] = old.getValue("loss_pos1"); - values[1] = old.getValue("loss_pos2"); - values[2] = old.getValue("loss_w1"); - values[3] = old.getValue("loss_w2"); - values[4] = old.getValue("loss_A1") / old.getValue("loss_A2"); - - return new NamedDoubleArray(names, values); - -} - -ParamSet transformOldParams(ParamSet old){ - String[] names = ["exPos", "ionPos", "exW", "ionW", "exIonRatio"]; - ParamSet res = new ParamSet(names); - - res.setPar("exPos", old.getValue("loss_pos1"), old.getError("loss_pos1")); - res.setPar("ionPos", old.getValue("loss_pos2"), old.getError("loss_pos2")); - res.setPar("exW", old.getValue("loss_w1"), old.getError("loss_w1")); - res.setPar("ionW", old.getValue("loss_w2"), old.getError("loss_w2")); - - double ratioValue = old.getValue("loss_A1") / old.getValue("loss_A2"); - - double a1RelErr = old.getError("loss_A1") / old.getValue("loss_A1"); - double a2RelErr = old.getError("loss_A2") / old.getValue("loss_A2"); - double ratioErr = Math.sqrt(a1RelErr*a1RelErr + a2RelErr*a2RelErr)*ratioValue; - res.setPar("exIonRatio", ratioValue, ratioErr); - - return res; -} - -PrintWriter out = new PrintWriter(System.out); - -String name = "log14_h2_new" -//String name = "log18_long" -//String name = "log25_long" - -OldFitResultReader reader = new OldFitResultReader(); -reader.readFile(new File("C:\\Users\\darksnake\\Dropbox\\Numass\\Analysis\\loss-2014_november\\old_loss\\results\\final\\${name}.txt")); - -double threshold = 17d; - -ParamSet means = transformOldParams (reader.getParamSet()); - -double ionRatio = ShowLossSpectrumAction.calcultateIonRatio(means, threshold); -NamedMatrix cov = NamedMatrix.diagonal(means.getParErrors()); -double ionRatioError = ShowLossSpectrumAction.calultateIonRatioError(name, means, cov, threshold); - -println String.format("The ionization ratio (using threshold %f) is %f%n", threshold, ionRatio); -println String.format("The ionization ratio standard deviation (using threshold %f) is %f%n", threshold, ionRatioError); -println "*** FIT RESULT ***" - -FittingIOUtils.printParamSet(out, means); - -println means.toString(); - -UnivariateFunction scatterFunction = LossCalculator.getSingleScatterFunction(means); -PlotFrame frame = JFreeChartFrame.drawFrame("Differential scattering crossection for "+name, null); -frame.add(new PlottableFunction("Cross-section", null, scatterFunction, 0, 100, 1000)); - -PrintFunction.printFunctionSimple(out, scatterFunction, 0, 100, 500); \ No newline at end of file diff --git a/numass-main/src/main/groovy/inr/numass/scripts/OldFitResultReader.groovy b/numass-main/src/main/groovy/inr/numass/scripts/OldFitResultReader.groovy deleted file mode 100644 index 90ba5e04..00000000 --- a/numass-main/src/main/groovy/inr/numass/scripts/OldFitResultReader.groovy +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2015 Alexander Nozik. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package inr.numass.scripts - -import hep.dataforge.datafitter.ParamSet -import hep.dataforge.maths.NamedDoubleArray -import hep.dataforge.maths.NamedDoubleSet -import hep.dataforge.maths.NamedMatrix -import java.util.regex.Pattern -import org.apache.commons.math3.linear.Array2DRowRealMatrix -import org.apache.commons.math3.linear.RealMatrix -import java.util.Scanner -import java.util.regex.Matcher - -/** - * - * @author darksnake - */ -class OldFitResultReader { - private static Pattern meanPattern = ~/\"(?.*)\"\s*:\s*(?[\d.]*)\s*/; - - private NamedDoubleSet means; - private RealMatrix covariance; - - void readMeans(String input){ - Scanner scan = new Scanner(input); - List names = new ArrayList<>(); - List values = new ArrayList<>(); - while(scan.hasNextLine()){ - String nextLine = scan.nextLine(); - if(!nextLine.isEmpty()){ - Matcher match = meanPattern.matcher(nextLine); - match.matches(); - names << match.group("name"); - values << Double.parseDouble(match.group("value")); - } - } - this.means = new NamedDoubleArray(names.toArray(new String[names.size()]), values.toArray(new double[values.size()])); - } - - void readCovariance(String input){ - Scanner scan = new Scanner(input); - List> matrix = new ArrayList<>(); - while(scan.hasNextLine()){ - String nextLine = scan.nextLine(); - if(!nextLine.isEmpty()){ - Scanner lineScan = new Scanner(nextLine); - List line = new ArrayList<>(); - while(lineScan.hasNextDouble()){ - line << lineScan.nextDouble(); - } - matrix << line; - } - } - - Array2DRowRealMatrix result = new Array2DRowRealMatrix(matrix.size(),matrix.size()); - for(int i = 0; i< matrix.size; i++){ - List line = matrix.get(i); - for(int j = 0; j< matrix.size; j++){ - result.setEntry(i,j,line[j]); - } - } - this.covariance = result; - } - - void readFile(File file){ - String text = file.getText(); - Pattern pattern = Pattern.compile(/.*The best fit values are:\s*(?.*)Covariation marix:\s*(?.*)Best Likelihood logarithm/, Pattern.DOTALL); - List results = text.findAll(pattern); - - String res = results.get(results.size()-1); - Matcher match = pattern.matcher(res); - match.matches(); - readMeans(match.group("parameters")); - readCovariance(match.group("covariance")); - } - - - NamedDoubleSet getMeans(){ - return means; - } - - ParamSet getParamSet(){ - ParamSet res = new ParamSet(); - NamedMatrix cov = getCovariance(); - for(String name: means.names()){ - res.setPar(name, means.getValue(name), Math.sqrt(Math.abs(cov.getElement(name, name)))); - } - return res; - } - - NamedMatrix getCovariance(){ - return new NamedMatrix(covariance, means.namesAsArray()); - } - -} - diff --git a/numass-main/src/main/java/inr/numass/Main.java b/numass-main/src/main/java/inr/numass/Main.java index dda4df92..ce3d43e3 100644 --- a/numass-main/src/main/java/inr/numass/Main.java +++ b/numass-main/src/main/java/inr/numass/Main.java @@ -40,6 +40,9 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; /** * diff --git a/numass-main/src/main/java/inr/numass/NumassContext.java b/numass-main/src/main/java/inr/numass/NumassContext.java index f0e71595..812a5723 100644 --- a/numass-main/src/main/java/inr/numass/NumassContext.java +++ b/numass-main/src/main/java/inr/numass/NumassContext.java @@ -17,6 +17,7 @@ package inr.numass; import hep.dataforge.actions.ActionManager; import hep.dataforge.context.Context; +import hep.dataforge.context.GlobalContext; import hep.dataforge.description.ActionDescriptor; import hep.dataforge.description.DescriptorFormatter; import hep.dataforge.description.DescriptorUtils; @@ -25,6 +26,7 @@ import hep.dataforge.exceptions.DescriptorException; import hep.dataforge.maths.integration.GaussRuleIntegrator; import hep.dataforge.maths.integration.UnivariateIntegrator; import hep.dataforge.meta.Meta; +import inr.numass.storage.SetDirectionUtility; import java.io.PrintWriter; /** @@ -52,8 +54,11 @@ public class NumassContext extends Context { } private void init() { - setIO(new NumassIO()); + GlobalContext.registerContext(this); + loadPlugin("hep.dataforge:actions"); loadPlugin("inr.numass:numass"); + setIO(new NumassIO()); + SetDirectionUtility.load(this); } public static void printDescription(Context context, boolean allowANSI) throws DescriptorException { @@ -75,4 +80,10 @@ public class NumassContext extends Context { writer.flush(); } + @Override + public void close() throws Exception { + SetDirectionUtility.save(this); + super.close(); + } + } diff --git a/numass-main/src/main/java/inr/numass/NumassIO.java b/numass-main/src/main/java/inr/numass/NumassIO.java index 3593594a..9768b8aa 100644 --- a/numass-main/src/main/java/inr/numass/NumassIO.java +++ b/numass-main/src/main/java/inr/numass/NumassIO.java @@ -60,18 +60,18 @@ public class NumassIO extends BasicIOManager { String dirName = String.join(File.separator, tokens); String fileName = name.removeNameSpace().toString() + ".out"; - return buildOut(getOutputDir(), dirName, fileName); + return buildOut(getWorkDirectory(), dirName, fileName); } - private File getOutputDir() { - String outputDirPath = getContext().getString(NUMASS_OUTPUT_CONTEXT_KEY, ".dataforge"); - File res = new File(getRootDirectory(), outputDirPath); - if (!res.exists()) { - res.mkdir(); - } - return res; - - } +// private File getOutputDir() { +// String outputDirPath = getContext().getString(NUMASS_OUTPUT_CONTEXT_KEY, "."); +// File res = new File(getRootDirectory(), outputDirPath); +// if (!res.exists()) { +// res.mkdir(); +// } +// return res; +// +// } protected OutputStream buildOut(File parentDir, String dirName, String fileName) { File outputFile; diff --git a/numass-main/src/main/java/inr/numass/actions/MonitorCorrectAction.java b/numass-main/src/main/java/inr/numass/actions/MonitorCorrectAction.java index 68f6ed12..0fba710a 100644 --- a/numass-main/src/main/java/inr/numass/actions/MonitorCorrectAction.java +++ b/numass-main/src/main/java/inr/numass/actions/MonitorCorrectAction.java @@ -111,15 +111,27 @@ public class MonitorCorrectAction extends OneToOneAction { } else { pb.putValue("Monitor", corrFactor); } + pb.putValue("CR", Value.of(dp.getValue("CR").doubleValue() / corrFactor)); pb.putValue("Window", Value.of(dp.getValue("Window").doubleValue() / corrFactor)); pb.putValue("Corrected", Value.of(dp.getValue("Corrected").doubleValue() / corrFactor)); pb.putValue("CRerr", Value.of(err)); + } else { + double corrFactor = dp.getValue("CR").doubleValue() / norm; + if (dp.names().contains("Monitor")) { + pb.putValue("Monitor", Value.of(dp.getValue("Monitor").doubleValue() / corrFactor)); + } else { + pb.putValue("Monitor", corrFactor); + } + pb.putValue("CR", norm); + } + if (meta.getBoolean("calculateRelative", false)) { pb.putValue("relCR", pb.build().getValue("CR").doubleValue() / norm); pb.putValue("relCRerr", pb.build().getValue("CRerr").doubleValue() / norm); } + dataList.add(pb.build()); } diff --git a/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java b/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java index d54610e7..d6fcc10c 100644 --- a/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java @@ -33,10 +33,13 @@ import hep.dataforge.tables.TableFormat; import inr.numass.storage.NMPoint; import inr.numass.storage.NumassData; import inr.numass.storage.RawNMPoint; +import inr.numass.utils.ExpressionUtils; import java.io.OutputStream; import java.time.Instant; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -46,6 +49,8 @@ import java.util.List; @ValueDef(name = "lowerWindow", type = "NUMBER", def = "0", info = "Base for the window lowerWindow bound") @ValueDef(name = "lowerWindowSlope", type = "NUMBER", def = "0", info = "Slope for the window lowerWindow bound") @ValueDef(name = "upperWindow", type = "NUMBER", info = "Upper bound for window") +@ValueDef(name = "correction", + info = "An expression to correct coun tumber depending on potential U, count rate CR and point length T") @ValueDef(name = "deadTime", type = "NUMBER", def = "0", info = "Dead time in us") public class PrepareDataAction extends OneToOneAction { @@ -55,7 +60,7 @@ public class PrepareDataAction extends OneToOneAction { double b = meta.getDouble("lowerWindow", 0); double a = meta.getDouble("lowerWindowSlope", 0); - return (int) (b + Uset * a); + return Math.max((int) (b + Uset * a), 0); } @Override @@ -77,13 +82,26 @@ public class PrepareDataAction extends OneToOneAction { int a = getLowerBorder(meta, Uset); int b = Math.min(upper, RawNMPoint.MAX_CHANEL); -// analyzer.setMonitorCorrector(corrector); + // count in window long wind = point.getCountInWindow(a, b); + // count in window with deadTime applied double corr = point.getCountRate(a, b, deadTime) * point.getLength();// - bkg * (b - a); + // count rate after all corrections double cr = point.getCountRate(a, b, deadTime); + // count rate error after all corrections double crErr = point.getCountRateErr(a, b, deadTime); + + if(meta.hasValue("correction")){ + Map exprParams = new HashMap<>(); + exprParams.put("U", point.getUread()); + exprParams.put("CR", cr); + exprParams.put("T", time); + double correctionFactor = ExpressionUtils.evaluate(meta.getString("correction"), exprParams); + cr = cr*correctionFactor; + crErr = crErr*correctionFactor; + } Instant timestamp = point.getStartTime(); diff --git a/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java b/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java index 67496852..04e1b673 100644 --- a/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java +++ b/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java @@ -80,9 +80,5 @@ public class ReadNumassStorageAction extends GenericAction { throw new RuntimeException("Failed to load storage", ex); } } - /* - - - */ } diff --git a/numass-main/src/main/java/inr/numass/actions/ShowLossSpectrumAction.java b/numass-main/src/main/java/inr/numass/actions/ShowLossSpectrumAction.java index 96d1fbd7..45a5ad92 100644 --- a/numass-main/src/main/java/inr/numass/actions/ShowLossSpectrumAction.java +++ b/numass-main/src/main/java/inr/numass/actions/ShowLossSpectrumAction.java @@ -27,7 +27,6 @@ import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.io.PrintFunction; import hep.dataforge.io.reports.Reportable; import hep.dataforge.maths.GridCalculator; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.maths.NamedMatrix; import hep.dataforge.maths.integration.UnivariateIntegrator; import hep.dataforge.meta.Laminate; @@ -41,6 +40,7 @@ import hep.dataforge.tables.ListTable; import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.Table; import hep.dataforge.tables.XYAdapter; +import hep.dataforge.values.NamedValueSet; import inr.numass.NumassContext; import inr.numass.models.ExperimentalVariableLossSpectrum; import inr.numass.models.LossCalculator; @@ -129,7 +129,7 @@ public class ShowLossSpectrumAction extends OneToOneAction { } // double integralThreshold = reader.getDouble("numass.eGun", 19005d) - reader.getDouble("integralThreshold", 14.82); -// double integralRatio = calculateIntegralExIonRatio(input.getDataSet(), input.getParameters().getValue("X"), integralThreshold); +// double integralRatio = calculateIntegralExIonRatio(input.getDataSet(), input.getParameters().getDouble("X"), integralThreshold); // writer.printf("The excitation to ionization ratio from integral spectrum (using threshold %f) is %f%n", integralThreshold, integralRatio); writer.println(); @@ -178,7 +178,7 @@ public class ShowLossSpectrumAction extends OneToOneAction { return input; } - public static double calcultateIonRatio(NamedDoubleSet set, double threshold) { + public static double calcultateIonRatio(NamedValueSet set, double threshold) { UnivariateIntegrator integrator = NumassContext.highDensityIntegrator; UnivariateFunction integrand = LossCalculator.getSingleScatterFunction(set); return 1d - integrator.integrate(integrand, 5d, threshold); @@ -211,7 +211,7 @@ public class ShowLossSpectrumAction extends OneToOneAction { } @SuppressWarnings("Unchecked") - public double calultateIonRatioError(Context context, String name, NamedDoubleSet parameters, NamedMatrix covariance, double threshold) { + public double calultateIonRatioError(Context context, String name, NamedValueSet parameters, NamedMatrix covariance, double threshold) { int number = 10000; double[] res = new GaussianParameterGenerator(parameters, covariance) @@ -233,7 +233,7 @@ public class ShowLossSpectrumAction extends OneToOneAction { return new DescriptiveStatistics(res).getStandardDeviation(); } - public static Table generateSpread(PrintWriter writer, String name, NamedDoubleSet parameters, NamedMatrix covariance) { + public static Table generateSpread(PrintWriter writer, String name, NamedValueSet parameters, NamedMatrix covariance) { int numCalls = 1000; int gridPoints = 200; double a = 8; diff --git a/numass-main/src/main/java/inr/numass/actions/SummaryAction.java b/numass-main/src/main/java/inr/numass/actions/SummaryAction.java index 7fa1c9b5..aaef30af 100644 --- a/numass-main/src/main/java/inr/numass/actions/SummaryAction.java +++ b/numass-main/src/main/java/inr/numass/actions/SummaryAction.java @@ -80,7 +80,7 @@ public class SummaryAction extends ManyToOneAction { Value[] values = new Value[names.length]; values[0] = Value.of(key); for (int i = 0; i < parNames.length; i++) { - Value val = Value.of(state.getParameters().getValue(parNames[i])); + Value val = Value.of(state.getParameters().getDouble(parNames[i])); values[2 * i + 1] = val; Value err = Value.of(state.getParameters().getError(parNames[i])); values[2 * i + 2] = err; diff --git a/numass-main/src/main/java/inr/numass/data/SpectrumInformation.java b/numass-main/src/main/java/inr/numass/data/SpectrumInformation.java index fa98e0ef..2d8b4e61 100644 --- a/numass-main/src/main/java/inr/numass/data/SpectrumInformation.java +++ b/numass-main/src/main/java/inr/numass/data/SpectrumInformation.java @@ -17,10 +17,10 @@ package inr.numass.data; import hep.dataforge.functions.ParametricFunction; import static hep.dataforge.maths.MatrixOperations.inverse; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.maths.NamedMatrix; import hep.dataforge.tables.DataPoint; import hep.dataforge.tables.ListTable; +import hep.dataforge.values.NamedValueSet; import inr.numass.utils.TritiumUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.linear.Array2DRowRealMatrix; @@ -38,7 +38,7 @@ public class SpectrumInformation { this.source = source; } - public NamedMatrix getExpetedCovariance(NamedDoubleSet set, ListTable data, String... parNames) { + public NamedMatrix getExpetedCovariance(NamedValueSet set, ListTable data, String... parNames) { String[] names = parNames; if (names.length == 0) { names = source.namesAsArray(); @@ -56,7 +56,7 @@ public class SpectrumInformation { * @param parNames * @return */ - public NamedMatrix getInformationMatrix(NamedDoubleSet set, ListTable data, String... parNames) { + public NamedMatrix getInformationMatrix(NamedValueSet set, ListTable data, String... parNames) { SpectrumDataAdapter reader = TritiumUtils.adapter(); String[] names = parNames; @@ -80,11 +80,11 @@ public class SpectrumInformation { } // формула правильная! - public double getPoinSignificance(NamedDoubleSet set, String name1, String name2, double x) { + public double getPoinSignificance(NamedValueSet set, String name1, String name2, double x) { return source.derivValue(name1, x, set) * source.derivValue(name2, x, set) / source.value(x, set); } - public NamedMatrix getPointInfoMatrix(NamedDoubleSet set, double x, double t, String... parNames) { + public NamedMatrix getPointInfoMatrix(NamedValueSet set, double x, double t, String... parNames) { assert source.names().contains(set.namesAsArray()); String[] names = parNames; @@ -119,7 +119,7 @@ public class SpectrumInformation { * @param name2 * @return */ - public UnivariateFunction getSignificanceFunction(final NamedDoubleSet set, final String name1, final String name2) { + public UnivariateFunction getSignificanceFunction(final NamedValueSet set, final String name1, final String name2) { return (double d) -> getPoinSignificance(set, name1, name2, d); } } diff --git a/numass-main/src/main/java/inr/numass/models/BetaSpectrum.java b/numass-main/src/main/java/inr/numass/models/BetaSpectrum.java index 54b3ddf1..71b1aa73 100644 --- a/numass-main/src/main/java/inr/numass/models/BetaSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/BetaSpectrum.java @@ -17,7 +17,8 @@ package inr.numass.models; import hep.dataforge.exceptions.NotDefinedException; import hep.dataforge.functions.AbstractParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import java.io.File; import static java.lang.Math.abs; import static java.lang.Math.exp; @@ -89,11 +90,11 @@ public class BetaSpectrum extends AbstractParametricFunction implements RangedNa return res; } - double derivRootsterile(String name, double E, NamedDoubleSet pars) throws NotDefinedException { - double E0 = pars.getValue("E0"); - double mnu2 = pars.getValue("mnu2"); - double mst2 = pars.getValue("msterile2"); - double u2 = pars.getValue("U2"); + double derivRootsterile(String name, double E, ValueProvider pars) throws NotDefinedException { + double E0 = pars.getDouble("E0"); + double mnu2 = pars.getDouble("mnu2"); + double mst2 = pars.getDouble("msterile2"); + double u2 = pars.getDouble("U2"); switch (name) { case "E0": @@ -117,7 +118,7 @@ public class BetaSpectrum extends AbstractParametricFunction implements RangedNa } @Override - public double derivValue(String name, double E, NamedDoubleSet pars) throws NotDefinedException { + public double derivValue(String name, double E, NamedValueSet pars) throws NotDefinedException { if (this.fss == null) { return this.derivRootsterile(name, E, pars); } @@ -137,12 +138,12 @@ public class BetaSpectrum extends AbstractParametricFunction implements RangedNa } @Override - public Double max(NamedDoubleSet set) { - return set.getValue("E0"); + public Double max(NamedValueSet set) { + return set.getDouble("E0"); } @Override - public Double min(NamedDoubleSet set) { + public Double min(NamedValueSet set) { return 0d; } @@ -184,11 +185,11 @@ public class BetaSpectrum extends AbstractParametricFunction implements RangedNa return res; } - double rootsterile(double E, NamedDoubleSet pars) { - double E0 = pars.getValue("E0"); - double mnu2 = pars.getValue("mnu2"); - double mst2 = pars.getValue("msterile2"); - double u2 = pars.getValue("U2"); + double rootsterile(double E, ValueProvider pars) { + double E0 = pars.getDouble("E0"); + double mnu2 = pars.getDouble("mnu2"); + double mst2 = pars.getDouble("msterile2"); + double u2 = pars.getDouble("U2"); if (u2 == 0) { return root(E0, mnu2, E); @@ -206,7 +207,7 @@ public class BetaSpectrum extends AbstractParametricFunction implements RangedNa } @Override - public double value(double E, NamedDoubleSet pars) { + public double value(double E, NamedValueSet pars) { if (this.fss == null) { return rootsterile(E, pars); } diff --git a/numass-main/src/main/java/inr/numass/models/CustomNBkgSpectrum.java b/numass-main/src/main/java/inr/numass/models/CustomNBkgSpectrum.java index 3dfef072..cf836f6d 100644 --- a/numass-main/src/main/java/inr/numass/models/CustomNBkgSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/CustomNBkgSpectrum.java @@ -6,7 +6,7 @@ package inr.numass.models; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; +import hep.dataforge.values.NamedValueSet; import inr.numass.NumassContext; import inr.numass.utils.TritiumUtils; import org.apache.commons.math3.analysis.UnivariateFunction; @@ -38,7 +38,7 @@ public class CustomNBkgSpectrum extends NBkgSpectrum { } @Override - public double value(double x, NamedDoubleSet set) { + public double value(double x, NamedValueSet set) { if (customBackgroundFunction == null) { return super.value(x, set); } else { diff --git a/numass-main/src/main/java/inr/numass/models/EmpiricalLossSpectrum.java b/numass-main/src/main/java/inr/numass/models/EmpiricalLossSpectrum.java index 70707618..af12ed45 100644 --- a/numass-main/src/main/java/inr/numass/models/EmpiricalLossSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/EmpiricalLossSpectrum.java @@ -13,63 +13,63 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.exceptions.NamingException; -import hep.dataforge.exceptions.NotDefinedException; -import hep.dataforge.functions.AbstractParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; -import hep.dataforge.maths.integration.GaussRuleIntegrator; -import hep.dataforge.maths.integration.UnivariateIntegrator; -import java.util.List; -import org.apache.commons.math3.analysis.BivariateFunction; -import org.apache.commons.math3.analysis.UnivariateFunction; - -/** - * - * @author Darksnake - */ -public class EmpiricalLossSpectrum extends AbstractParametricFunction { - - public static String[] names = {"X", "shift"}; - private final UnivariateFunction transmission; - private final double eMax; - - private final UnivariateIntegrator integrator; - - public EmpiricalLossSpectrum(UnivariateFunction transmission, double eMax) throws NamingException { - super(names); - integrator = new GaussRuleIntegrator(300); - this.transmission = transmission; - this.eMax = eMax; - } - - @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { - throw new NotDefinedException(); - } - - @Override - public double value(double U, NamedDoubleSet set) { - if (U >= eMax) { - return 0; - } - double X = set.getValue("X"); - final double shift = set.getValue("shift"); - - //FIXME тут толщины усреднены по длине источника, а надо брать чистого Пуассона - final List probs = LossCalculator.instance().getGunLossProbabilities(X); - final double noLossProb = probs.get(0); - final BivariateFunction lossFunction = (Ei, Ef) -> { - return LossCalculator.instance().getLossValue(probs, Ei, Ef); - }; - UnivariateFunction integrand = (double x) -> transmission.value(x) * lossFunction.value(x, U - shift); - return noLossProb * transmission.value(U - shift) + integrator.integrate(integrand, U, eMax); - } - - @Override - public boolean providesDeriv(String name) { - return false; - } - -} +package inr.numass.models; + +import hep.dataforge.exceptions.NamingException; +import hep.dataforge.exceptions.NotDefinedException; +import hep.dataforge.functions.AbstractParametricFunction; +import hep.dataforge.maths.integration.GaussRuleIntegrator; +import hep.dataforge.maths.integration.UnivariateIntegrator; +import hep.dataforge.values.NamedValueSet; +import java.util.List; +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.analysis.UnivariateFunction; + +/** + * + * @author Darksnake + */ +public class EmpiricalLossSpectrum extends AbstractParametricFunction { + + public static String[] names = {"X", "shift"}; + private final UnivariateFunction transmission; + private final double eMax; + + private final UnivariateIntegrator integrator; + + public EmpiricalLossSpectrum(UnivariateFunction transmission, double eMax) throws NamingException { + super(names); + integrator = new GaussRuleIntegrator(300); + this.transmission = transmission; + this.eMax = eMax; + } + + @Override + public double derivValue(String parName, double x, NamedValueSet set) { + throw new NotDefinedException(); + } + + @Override + public double value(double U, NamedValueSet set) { + if (U >= eMax) { + return 0; + } + double X = set.getDouble("X"); + final double shift = set.getDouble("shift"); + + //FIXME тут толщины усреднены по длине источника, а надо брать чистого Пуассона + final List probs = LossCalculator.instance().getGunLossProbabilities(X); + final double noLossProb = probs.get(0); + final BivariateFunction lossFunction = (Ei, Ef) -> { + return LossCalculator.instance().getLossValue(probs, Ei, Ef); + }; + UnivariateFunction integrand = (double x) -> transmission.value(x) * lossFunction.value(x, U - shift); + return noLossProb * transmission.value(U - shift) + integrator.integrate(integrand, U, eMax); + } + + @Override + public boolean providesDeriv(String name) { + return false; + } + +} diff --git a/numass-main/src/main/java/inr/numass/models/ExperimentalVariableLossSpectrum.java b/numass-main/src/main/java/inr/numass/models/ExperimentalVariableLossSpectrum.java index 002ad7db..d508d23f 100644 --- a/numass-main/src/main/java/inr/numass/models/ExperimentalVariableLossSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/ExperimentalVariableLossSpectrum.java @@ -13,166 +13,167 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.exceptions.NotDefinedException; -import hep.dataforge.functions.AbstractParametricFunction; -import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; -import org.apache.commons.math3.analysis.BivariateFunction; -import org.apache.commons.math3.analysis.UnivariateFunction; - -/** - * Experimental differential loss spectrum - * - * @author darksnake - */ -public class ExperimentalVariableLossSpectrum extends VariableLossSpectrum { - - public static ExperimentalVariableLossSpectrum withGun(double eGun, double resA, double eMax, double smootherW) { - return new ExperimentalVariableLossSpectrum(new GunSpectrum(), eMax,smootherW); - } - - public static ExperimentalVariableLossSpectrum withData(final UnivariateFunction transmission, double eMax, double smootherW) { - return new ExperimentalVariableLossSpectrum(new AbstractParametricFunction(new String[0]) { - - @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { - throw new NotDefinedException(); - } - - @Override - public boolean providesDeriv(String name) { - return false; - } - - @Override - public double value(double x, NamedDoubleSet set) { - return transmission.value(x); - } - }, eMax,smootherW); - } - - Loss loss; - -// private double smootherW; -// public ExperimentalVariableLossSpectrum(UnivariateFunction transmission, double eMax, double smootherW) throws NamingException { -// super(transmission, eMax); -// loss = new Loss(smootherW); -// } - public ExperimentalVariableLossSpectrum(ParametricFunction transmission, double eMax, double smootherW) { - super(transmission, eMax); - loss = new Loss(smootherW); - } - -// public ExperimentalVariableLossSpectrum(double eGun, double resA, double eMax, double smootherW) throws NamingException { -// super(eGun, resA, eMax); -// loss = new Loss(smootherW); -// } - @Override - public UnivariateFunction singleScatterFunction( - double exPos, - double ionPos, - double exW, - double ionW, - double exIonRatio) { - - return (double eps) -> { - if (eps <= 0) { - return 0; - } - - return (loss.excitation(exPos, exW).value(eps) * exIonRatio + loss.ionization(ionPos, ionW).value(eps)) / (1d + exIonRatio); - }; - } - - public static class Loss { - - private BivariateFunction smoother; - - private double smootherNorm; - - public Loss(double smootherW) { - if (smootherW == 0) { - smoother = (e1, e2) -> 0; - smootherNorm = 0; - } - - smoother = (e1, e2) -> { - double delta = e1 - e2; - if (delta < 0) { - return 0; - } else { - return Math.exp(-delta * delta / 2 / smootherW / smootherW); - } - }; - - smootherNorm = Math.sqrt(2 * Math.PI) * smootherW / 2; - } - - public UnivariateFunction total( - final double exPos, - final double ionPos, - final double exW, - final double ionW, - final double exIonRatio) { - return (eps) -> (excitation(exPos, exW).value(eps) * exIonRatio + ionization(ionPos, ionW).value(eps)) / (1d + exIonRatio); - } - - public UnivariateFunction total(NamedDoubleSet set) { - final double exPos = set.getValue("exPos"); - final double ionPos = set.getValue("ionPos"); - final double exW = set.getValue("exW"); - final double ionW = set.getValue("ionW"); - final double exIonRatio = set.getValue("exIonRatio"); - return total(exPos, ionPos, exW, ionW, exIonRatio); - } - - /** - * Excitation spectrum - * - * @param exPos - * @param exW - * @return - */ - public UnivariateFunction excitation(double exPos, double exW) { - return (double eps) -> { - double z = eps - exPos; - double res; - - double norm = smootherNorm + Math.sqrt(Math.PI / 2) * exW / 2; - - if (z < 0) { - res = Math.exp(-2 * z * z / exW / exW); - } else { - res = smoother.value(z, 0); - } - - return res / norm; - }; - } - - /** - * Ionization spectrum - * - * @param ionPos - * @param ionW - * @return - */ - public UnivariateFunction ionization(double ionPos, double ionW) { - return (double eps) -> { - double res; - double norm = smootherNorm + ionW * Math.PI / 4d; - - if (eps - ionPos > 0) { - res = 1 / (1 + 4 * (eps - ionPos) * (eps - ionPos) / ionW / ionW); - } else { - res = smoother.value(0, eps - ionPos); - } - - return res / norm; - }; - } - } - -} +package inr.numass.models; + +import hep.dataforge.exceptions.NotDefinedException; +import hep.dataforge.functions.AbstractParametricFunction; +import hep.dataforge.functions.ParametricFunction; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.analysis.UnivariateFunction; + +/** + * Experimental differential loss spectrum + * + * @author darksnake + */ +public class ExperimentalVariableLossSpectrum extends VariableLossSpectrum { + + public static ExperimentalVariableLossSpectrum withGun(double eGun, double resA, double eMax, double smootherW) { + return new ExperimentalVariableLossSpectrum(new GunSpectrum(), eMax,smootherW); + } + + public static ExperimentalVariableLossSpectrum withData(final UnivariateFunction transmission, double eMax, double smootherW) { + return new ExperimentalVariableLossSpectrum(new AbstractParametricFunction(new String[0]) { + + @Override + public double derivValue(String parName, double x, NamedValueSet set) { + throw new NotDefinedException(); + } + + @Override + public boolean providesDeriv(String name) { + return false; + } + + @Override + public double value(double x, NamedValueSet set) { + return transmission.value(x); + } + }, eMax,smootherW); + } + + Loss loss; + +// private double smootherW; +// public ExperimentalVariableLossSpectrum(UnivariateFunction transmission, double eMax, double smootherW) throws NamingException { +// super(transmission, eMax); +// loss = new Loss(smootherW); +// } + public ExperimentalVariableLossSpectrum(ParametricFunction transmission, double eMax, double smootherW) { + super(transmission, eMax); + loss = new Loss(smootherW); + } + +// public ExperimentalVariableLossSpectrum(double eGun, double resA, double eMax, double smootherW) throws NamingException { +// super(eGun, resA, eMax); +// loss = new Loss(smootherW); +// } + @Override + public UnivariateFunction singleScatterFunction( + double exPos, + double ionPos, + double exW, + double ionW, + double exIonRatio) { + + return (double eps) -> { + if (eps <= 0) { + return 0; + } + + return (loss.excitation(exPos, exW).value(eps) * exIonRatio + loss.ionization(ionPos, ionW).value(eps)) / (1d + exIonRatio); + }; + } + + public static class Loss { + + private BivariateFunction smoother; + + private double smootherNorm; + + public Loss(double smootherW) { + if (smootherW == 0) { + smoother = (e1, e2) -> 0; + smootherNorm = 0; + } + + smoother = (e1, e2) -> { + double delta = e1 - e2; + if (delta < 0) { + return 0; + } else { + return Math.exp(-delta * delta / 2 / smootherW / smootherW); + } + }; + + smootherNorm = Math.sqrt(2 * Math.PI) * smootherW / 2; + } + + public UnivariateFunction total( + final double exPos, + final double ionPos, + final double exW, + final double ionW, + final double exIonRatio) { + return (eps) -> (excitation(exPos, exW).value(eps) * exIonRatio + ionization(ionPos, ionW).value(eps)) / (1d + exIonRatio); + } + + public UnivariateFunction total(ValueProvider set) { + final double exPos = set.getDouble("exPos"); + final double ionPos = set.getDouble("ionPos"); + final double exW = set.getDouble("exW"); + final double ionW = set.getDouble("ionW"); + final double exIonRatio = set.getDouble("exIonRatio"); + return total(exPos, ionPos, exW, ionW, exIonRatio); + } + + /** + * Excitation spectrum + * + * @param exPos + * @param exW + * @return + */ + public UnivariateFunction excitation(double exPos, double exW) { + return (double eps) -> { + double z = eps - exPos; + double res; + + double norm = smootherNorm + Math.sqrt(Math.PI / 2) * exW / 2; + + if (z < 0) { + res = Math.exp(-2 * z * z / exW / exW); + } else { + res = smoother.value(z, 0); + } + + return res / norm; + }; + } + + /** + * Ionization spectrum + * + * @param ionPos + * @param ionW + * @return + */ + public UnivariateFunction ionization(double ionPos, double ionW) { + return (double eps) -> { + double res; + double norm = smootherNorm + ionW * Math.PI / 4d; + + if (eps - ionPos > 0) { + res = 1 / (1 + 4 * (eps - ionPos) * (eps - ionPos) / ionW / ionW); + } else { + res = smoother.value(0, eps - ionPos); + } + + return res / norm; + }; + } + } + +} diff --git a/numass-main/src/main/java/inr/numass/models/GaussResolution.java b/numass-main/src/main/java/inr/numass/models/GaussResolution.java index 5c3fd09c..5b598cec 100644 --- a/numass-main/src/main/java/inr/numass/models/GaussResolution.java +++ b/numass-main/src/main/java/inr/numass/models/GaussResolution.java @@ -19,8 +19,9 @@ import hep.dataforge.context.GlobalContext; import hep.dataforge.exceptions.NameNotFoundException; import hep.dataforge.functions.AbstractParametricFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import static hep.dataforge.names.NamedUtils.combineNamesWithEquals; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import static java.lang.Double.isNaN; import static java.lang.Math.abs; import static java.lang.Math.exp; @@ -28,6 +29,12 @@ import static java.lang.Math.sqrt; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.SimpsonIntegrator; import org.apache.commons.math3.analysis.integration.UnivariateIntegrator; +import static java.lang.Double.isNaN; +import static java.lang.Math.abs; +import static java.lang.Double.isNaN; +import static java.lang.Math.abs; +import static java.lang.Double.isNaN; +import static java.lang.Math.abs; /** * @@ -51,7 +58,7 @@ public class GaussResolution extends AbstractParametricFunction implements Trans } @Override - public double derivValue(String name, double X, NamedDoubleSet pars) { + public double derivValue(String name, double X, NamedValueSet pars) { if (abs(X - getPos(pars)) > cutoff * getW(pars)) { return 0; } @@ -71,7 +78,7 @@ public class GaussResolution extends AbstractParametricFunction implements Trans int maxEval = GlobalContext.instance().getInt("INTEGR_POINTS", 500); @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { + public double derivValue(String parName, double x, NamedValueSet set) { double a = getLowerBound(set); double b = getUpperBound(set); assert b > a; @@ -87,7 +94,7 @@ public class GaussResolution extends AbstractParametricFunction implements Trans } @Override - public double value(double x, NamedDoubleSet set) { + public double value(double x, NamedValueSet set) { double a = getLowerBound(set); double b = getUpperBound(set); assert b > a; @@ -97,11 +104,11 @@ public class GaussResolution extends AbstractParametricFunction implements Trans } @Override - public double getDeriv(String name, NamedDoubleSet set, double input, double output) { + public double getDeriv(String name, NamedValueSet set, double input, double output) { return this.derivValue(name, output - input, set); } - private UnivariateFunction getDerivProduct(final String name, final ParametricFunction bare, final NamedDoubleSet pars, final double x0) { + private UnivariateFunction getDerivProduct(final String name, final ParametricFunction bare, final NamedValueSet pars, final double x0) { return (double x) -> { double res1; double res2; @@ -119,17 +126,17 @@ public class GaussResolution extends AbstractParametricFunction implements Trans }; } - private double getLowerBound(final NamedDoubleSet pars) { + private double getLowerBound(final NamedValueSet pars) { return getPos(pars) - cutoff * getW(pars); } - private double getPos(NamedDoubleSet pars) { -// return pars.getValue("pos"); + private double getPos(ValueProvider pars) { +// return pars.getDouble("pos"); // вряд ли стоит ожидать, что разрешение будет сдвигать среднее, поэтому оставляем один параметр return 0; } - private UnivariateFunction getProduct(final ParametricFunction bare, final NamedDoubleSet pars, final double x0) { + private UnivariateFunction getProduct(final ParametricFunction bare, final NamedValueSet pars, final double x0) { return (double x) -> { double res = bare.value(x0 - x, pars) * GaussResolution.this.value(x, pars); assert !isNaN(res); @@ -137,17 +144,17 @@ public class GaussResolution extends AbstractParametricFunction implements Trans }; } - private double getUpperBound(final NamedDoubleSet pars) { + private double getUpperBound(final NamedValueSet pars) { return getPos(pars) + cutoff * getW(pars); } @Override - public double getValue(NamedDoubleSet set, double input, double output) { + public double getValue(NamedValueSet set, double input, double output) { return this.value(output - input, set); } - private double getW(NamedDoubleSet pars) { - return pars.getValue("w"); + private double getW(ValueProvider pars) { + return pars.getDouble("w"); } @Override @@ -156,11 +163,11 @@ public class GaussResolution extends AbstractParametricFunction implements Trans } @Override - public double value(double X, NamedDoubleSet pars) { - if (abs(X - getPos(pars)) > cutoff * getW(pars)) { + public double value(double x, NamedValueSet pars) { + if (abs(x - getPos(pars)) > cutoff * getW(pars)) { return 0; } - double aux = (X - getPos(pars)) / getW(pars); + double aux = (x - getPos(pars)) / getW(pars); return exp(-aux * aux / 2) / getW(pars) / sqrt(2 * Math.PI); } } diff --git a/numass-main/src/main/java/inr/numass/models/GaussSourceSpectrum.java b/numass-main/src/main/java/inr/numass/models/GaussSourceSpectrum.java index 18976ba0..4d69c85b 100644 --- a/numass-main/src/main/java/inr/numass/models/GaussSourceSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/GaussSourceSpectrum.java @@ -13,77 +13,78 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.exceptions.NotDefinedException; -import hep.dataforge.functions.AbstractParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; -import static java.lang.Math.exp; -import static java.lang.Math.sqrt; - -/** - * - * @author Darksnake - */ -public class GaussSourceSpectrum extends AbstractParametricFunction implements RangedNamedSetSpectrum { - - private static final String[] list = {"pos", "sigma"}; - private final double cutoff = 4d; - - public GaussSourceSpectrum() { - super(list); - } - - @Override - public double derivValue(String parName, double E, NamedDoubleSet set) { - switch (parName) { - case "pos": - return getGaussPosDeriv(E, getPos(set), getSigma(set)); - case "sigma": - return getGaussSigmaDeriv(E, getPos(set), getSigma(set)); - default: - throw new NotDefinedException(); - } - } - - double getGauss(double E, double pos, double sigma) { - double aux = (E - pos) / sigma; - return exp(-aux * aux / 2) / sigma / sqrt(2 * Math.PI); - } - - double getGaussPosDeriv(double E, double pos, double sigma) { - return getGauss(E, pos, sigma) * (E - pos) / sigma / sigma; - } - - double getGaussSigmaDeriv(double E, double pos, double sigma) { - return getGauss(E, pos, sigma) * ((E - pos) * (E - pos) / sigma / sigma / sigma - 1 / sigma); - } - - @Override - public Double max(NamedDoubleSet set) { - return getPos(set) + cutoff * getSigma(set); - } - - @Override - public Double min(NamedDoubleSet set) { - return getPos(set) - cutoff * getSigma(set); - } - - private double getPos(NamedDoubleSet set) { - return set.getValue("pos"); - } - - private double getSigma(NamedDoubleSet set) { - return set.getValue("sigma"); - } - - @Override - public boolean providesDeriv(String name) { - return this.names().contains(name); - } - - @Override - public double value(final double E, NamedDoubleSet set) { - return getGauss(E, getPos(set), getSigma(set)); - } -} +package inr.numass.models; + +import hep.dataforge.exceptions.NotDefinedException; +import hep.dataforge.functions.AbstractParametricFunction; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; +import static java.lang.Math.exp; +import static java.lang.Math.sqrt; + +/** + * + * @author Darksnake + */ +public class GaussSourceSpectrum extends AbstractParametricFunction implements RangedNamedSetSpectrum { + + private static final String[] list = {"pos", "sigma"}; + private final double cutoff = 4d; + + public GaussSourceSpectrum() { + super(list); + } + + @Override + public double derivValue(String parName, double E, NamedValueSet set) { + switch (parName) { + case "pos": + return getGaussPosDeriv(E, getPos(set), getSigma(set)); + case "sigma": + return getGaussSigmaDeriv(E, getPos(set), getSigma(set)); + default: + throw new NotDefinedException(); + } + } + + double getGauss(double E, double pos, double sigma) { + double aux = (E - pos) / sigma; + return exp(-aux * aux / 2) / sigma / sqrt(2 * Math.PI); + } + + double getGaussPosDeriv(double E, double pos, double sigma) { + return getGauss(E, pos, sigma) * (E - pos) / sigma / sigma; + } + + double getGaussSigmaDeriv(double E, double pos, double sigma) { + return getGauss(E, pos, sigma) * ((E - pos) * (E - pos) / sigma / sigma / sigma - 1 / sigma); + } + + @Override + public Double max(NamedValueSet set) { + return getPos(set) + cutoff * getSigma(set); + } + + @Override + public Double min(NamedValueSet set) { + return getPos(set) - cutoff * getSigma(set); + } + + private double getPos(ValueProvider set) { + return set.getDouble("pos"); + } + + private double getSigma(ValueProvider set) { + return set.getDouble("sigma"); + } + + @Override + public boolean providesDeriv(String name) { + return this.names().contains(name); + } + + @Override + public double value(final double E, NamedValueSet set) { + return getGauss(E, getPos(set), getSigma(set)); + } +} diff --git a/numass-main/src/main/java/inr/numass/models/GunSpectrum.java b/numass-main/src/main/java/inr/numass/models/GunSpectrum.java index 3955bdd6..e6b9446c 100644 --- a/numass-main/src/main/java/inr/numass/models/GunSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/GunSpectrum.java @@ -17,8 +17,8 @@ package inr.numass.models; import hep.dataforge.exceptions.NotDefinedException; import hep.dataforge.functions.AbstractParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.maths.integration.UnivariateIntegrator; +import hep.dataforge.values.NamedValueSet; import inr.numass.NumassContext; import static java.lang.Math.abs; import static java.lang.Math.exp; @@ -42,10 +42,10 @@ public class GunSpectrum extends AbstractParametricFunction { } @Override - public double derivValue(String parName, final double U, NamedDoubleSet set) { - final double pos = set.getValue("pos"); - final double sigma = set.getValue("sigma"); - final double resA = set.getValue("resA"); + public double derivValue(String parName, final double U, NamedValueSet set) { + final double pos = set.getDouble("pos"); + final double sigma = set.getDouble("sigma"); + final double resA = set.getDouble("resA"); if(sigma == 0) throw new NotDefinedException(); @@ -132,10 +132,10 @@ public class GunSpectrum extends AbstractParametricFunction { } @Override - public double value(final double U, NamedDoubleSet set) { - final double pos = set.getValue("pos"); - final double sigma = set.getValue("sigma"); - final double resA = set.getValue("resA"); + public double value(final double U, NamedValueSet set) { + final double pos = set.getDouble("pos"); + final double sigma = set.getDouble("sigma"); + final double resA = set.getDouble("resA"); if (sigma <1e-5 ) { return transmissionValueFast(U, pos, resA); diff --git a/numass-main/src/main/java/inr/numass/models/GunTailSpectrum.java b/numass-main/src/main/java/inr/numass/models/GunTailSpectrum.java index cbbded07..a499ee5d 100644 --- a/numass-main/src/main/java/inr/numass/models/GunTailSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/GunTailSpectrum.java @@ -13,77 +13,78 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.exceptions.NotDefinedException; -import hep.dataforge.maths.NamedDoubleSet; -import hep.dataforge.names.Names; -import static java.lang.Math.abs; -import static java.lang.Math.exp; -import static java.lang.Math.sqrt; -import org.apache.commons.math3.analysis.UnivariateFunction; - -public class GunTailSpectrum implements RangedNamedSetSpectrum { - - private final double cutoff = 4d; - - private final String[] list = {"pos", "tailShift", "tailAmp", "sigma"}; - - @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { - throw new NotDefinedException(); - } - - @Override - public Double max(NamedDoubleSet set) { - return set.getValue("pos") + cutoff * set.getValue("sigma"); - } - - @Override - public Double min(NamedDoubleSet set) { - return 0d; - } - - @Override - public Names names() { - return Names.of(list); - } - - @Override - public boolean providesDeriv(String name) { - return false; - } - - @Override - public double value(double E, NamedDoubleSet set) { - double pos = set.getValue("pos"); - double amp = set.getValue("tailAmp"); - double sigma = set.getValue("sigma"); - - if (E >= pos + cutoff * sigma) { - return 0d; - } - - return gauss(E, pos, sigma) * (1 - amp) + amp * tail(E, pos, set); - } - - double gauss(double E, double pos, double sigma) { - if (abs(E - pos) > cutoff * sigma) { - return 0; - } - double aux = (E - pos) / sigma; - return exp(-aux * aux / 2) / sigma / sqrt(2 * Math.PI); - } - - double tail(double E, double pos, NamedDoubleSet set) { - - double tailShift = set.getValue("tailShift"); - - double delta = Math.max(pos - E - tailShift, 1d); - UnivariateFunction func = (double d) -> 1d / d / d; -// double tailNorm = NumassContext.defaultIntegrator.integrate(func, 0d, 300d); - - return func.value(delta); - } - -} +package inr.numass.models; + +import hep.dataforge.exceptions.NotDefinedException; +import hep.dataforge.names.Names; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; +import static java.lang.Math.abs; +import static java.lang.Math.exp; +import static java.lang.Math.sqrt; +import org.apache.commons.math3.analysis.UnivariateFunction; + +public class GunTailSpectrum implements RangedNamedSetSpectrum { + + private final double cutoff = 4d; + + private final String[] list = {"pos", "tailShift", "tailAmp", "sigma"}; + + @Override + public double derivValue(String parName, double x, NamedValueSet set) { + throw new NotDefinedException(); + } + + @Override + public Double max(NamedValueSet set) { + return set.getDouble("pos") + cutoff * set.getDouble("sigma"); + } + + @Override + public Double min(NamedValueSet set) { + return 0d; + } + + @Override + public Names names() { + return Names.of(list); + } + + @Override + public boolean providesDeriv(String name) { + return false; + } + + @Override + public double value(double E, NamedValueSet set) { + double pos = set.getDouble("pos"); + double amp = set.getDouble("tailAmp"); + double sigma = set.getDouble("sigma"); + + if (E >= pos + cutoff * sigma) { + return 0d; + } + + return gauss(E, pos, sigma) * (1 - amp) + amp * tail(E, pos, set); + } + + double gauss(double E, double pos, double sigma) { + if (abs(E - pos) > cutoff * sigma) { + return 0; + } + double aux = (E - pos) / sigma; + return exp(-aux * aux / 2) / sigma / sqrt(2 * Math.PI); + } + + double tail(double E, double pos, ValueProvider set) { + + double tailShift = set.getDouble("tailShift"); + + double delta = Math.max(pos - E - tailShift, 1d); + UnivariateFunction func = (double d) -> 1d / d / d; +// double tailNorm = NumassContext.defaultIntegrator.integrate(func, 0d, 300d); + + return func.value(delta); + } + +} 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 bbca8679..acd6d12a 100644 --- a/numass-main/src/main/java/inr/numass/models/LossCalculator.java +++ b/numass-main/src/main/java/inr/numass/models/LossCalculator.java @@ -16,11 +16,11 @@ package inr.numass.models; import hep.dataforge.functions.FunctionCaching; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.maths.integration.GaussRuleIntegrator; import hep.dataforge.maths.integration.UnivariateIntegrator; import hep.dataforge.plots.XYPlotFrame; import hep.dataforge.plots.data.PlottableFunction; +import hep.dataforge.values.NamedValueSet; import static java.lang.Math.exp; import java.util.ArrayList; import java.util.HashMap; @@ -115,13 +115,13 @@ public class LossCalculator { return (e) -> func.value(e) / norm; } - public static UnivariateFunction getSingleScatterFunction(NamedDoubleSet set) { + public static UnivariateFunction getSingleScatterFunction(NamedValueSet set) { - final double exPos = set.getValue("exPos"); - final double ionPos = set.getValue("ionPos"); - final double exW = set.getValue("exW"); - final double ionW = set.getValue("ionW"); - final double exIonRatio = set.getValue("exIonRatio"); + final double exPos = set.getDouble("exPos"); + final double ionPos = set.getDouble("ionPos"); + final double exW = set.getDouble("exW"); + final double ionW = set.getDouble("ionW"); + final double exIonRatio = set.getDouble("exIonRatio"); return getSingleScatterFunction(exPos, ionPos, exW, ionW, exIonRatio); } @@ -146,27 +146,27 @@ public class LossCalculator { return instance; } - public static void plotScatter(XYPlotFrame frame, NamedDoubleSet set) { + public static void plotScatter(XYPlotFrame frame, NamedValueSet set) { //"X", "shift", "exPos", "ionPos", "exW", "ionW", "exIonRatio" // JFreeChartFrame frame = JFreeChartFrame.drawFrame("Differential scattering crosssection", null); - double X = set.getValue("X"); + double X = set.getDouble("X"); - final double exPos = set.getValue("exPos"); + final double exPos = set.getDouble("exPos"); - final double ionPos = set.getValue("ionPos"); + final double ionPos = set.getDouble("ionPos"); - final double exW = set.getValue("exW"); + final double exW = set.getDouble("exW"); - final double ionW = set.getValue("ionW"); + final double ionW = set.getDouble("ionW"); - final double exIonRatio = set.getValue("exIonRatio"); + final double exIonRatio = set.getDouble("exIonRatio"); UnivariateFunction scatterFunction = getSingleScatterFunction(exPos, ionPos, exW, ionW, exIonRatio); if (set.names().contains("X")) { final LossCalculator loss = LossCalculator.instance; - final List probs = loss.getGunLossProbabilities(set.getValue("X")); + final List probs = loss.getGunLossProbabilities(set.getDouble("X")); UnivariateFunction single = (double e) -> probs.get(1) * scatterFunction.value(e); frame.add(new PlottableFunction("Single scattering", x->single.value(x), 0, 100, 1000)); diff --git a/numass-main/src/main/java/inr/numass/models/ModularSpectrum.java b/numass-main/src/main/java/inr/numass/models/ModularSpectrum.java index 0a7891b5..e8463bfa 100644 --- a/numass-main/src/main/java/inr/numass/models/ModularSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/ModularSpectrum.java @@ -17,8 +17,9 @@ package inr.numass.models; import hep.dataforge.functions.AbstractParametricFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.names.NamedUtils; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.analysis.BivariateFunction; @@ -141,7 +142,7 @@ public class ModularSpectrum extends AbstractParametricFunction { } @Override - public double derivValue(String parName, double U, NamedDoubleSet set) { + public double derivValue(String parName, double U, NamedValueSet set) { if (U >= sourceSpectrum.max(set)) { return 0; } @@ -177,12 +178,12 @@ public class ModularSpectrum extends AbstractParametricFunction { } } - private double getTrap(NamedDoubleSet set) { - return set.getValue("trap"); + private double getTrap(ValueProvider set) { + return set.getDouble("trap"); } - private double getX(NamedDoubleSet set) { - return set.getValue("X"); + private double getX(ValueProvider set) { + return set.getDouble("X"); } @Override @@ -223,7 +224,7 @@ public class ModularSpectrum extends AbstractParametricFunction { } @Override - public double value(double U, NamedDoubleSet set) { + public double value(double U, NamedValueSet set) { if (U >= sourceSpectrum.max(set)) { return 0; } diff --git a/numass-main/src/main/java/inr/numass/models/NBkgSpectrum.java b/numass-main/src/main/java/inr/numass/models/NBkgSpectrum.java index 9a1cd8b5..7586796d 100644 --- a/numass-main/src/main/java/inr/numass/models/NBkgSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/NBkgSpectrum.java @@ -17,9 +17,10 @@ package inr.numass.models; import hep.dataforge.functions.AbstractParametricFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import static hep.dataforge.names.NamedUtils.combineNamesWithEquals; import hep.dataforge.utils.MultiCounter; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; /** * @@ -38,7 +39,7 @@ public class NBkgSpectrum extends AbstractParametricFunction { } @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { + public double derivValue(String parName, double x, NamedValueSet set) { this.counter.increase(parName); switch (parName) { case "N": @@ -50,12 +51,12 @@ public class NBkgSpectrum extends AbstractParametricFunction { } } - private double getBkg(NamedDoubleSet set) { - return set.getValue("bkg"); + private double getBkg(ValueProvider set) { + return set.getDouble("bkg"); } - private double getN(NamedDoubleSet set) { - return set.getValue("N"); + private double getN(ValueProvider set) { + return set.getDouble("N"); } @Override @@ -71,7 +72,7 @@ public class NBkgSpectrum extends AbstractParametricFunction { } @Override - public double value(double x, NamedDoubleSet set) { + public double value(double x, NamedValueSet set) { this.counter.increase("value"); return getN(set) * source.value(x, set) + getBkg(set); } diff --git a/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java b/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java index d9ca3aa1..72c46e5b 100644 --- a/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java +++ b/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java @@ -19,11 +19,14 @@ import hep.dataforge.functions.AbstractParametricFunction; import static hep.dataforge.functions.FunctionUtils.getSpectrumDerivativeFunction; import static hep.dataforge.functions.FunctionUtils.getSpectrumFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleArray; -import hep.dataforge.maths.NamedDoubleSet; +import hep.dataforge.maths.MathUtils; +import hep.dataforge.maths.NamedVector; import hep.dataforge.names.AbstractNamedSet; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.interpolation.SplineInterpolator; import org.slf4j.LoggerFactory; @@ -60,7 +63,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { } @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { + public double derivValue(String parName, double x, NamedValueSet set) { if (!isCachingEnabled()) { return source.derivValue(parName, x, set); } @@ -105,7 +108,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { return source.providesDeriv(name); } - protected boolean sameSet(NamedDoubleSet set1, NamedDoubleSet set2) { + protected boolean sameSet(ValueProvider set1, ValueProvider set2) { // if((set1 instanceof NamedDoubleSet)&&(set2 instanceof NamedDoubleSet)){ // double[] v1 = ((NamedDoubleSet)set1).getAllValues(); // double[] v2 = ((NamedDoubleSet)set2).getAllValues(); @@ -113,7 +116,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { // } for (String name : this.names()) { - if (set1.getValue(name) != set2.getValue(name)) { + if (!Objects.equals(set1.getDouble(name), set2.getDouble(name))) { return false; } } @@ -137,8 +140,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { /* * Подразумевается, что трансформация одна и та же и для спектра, и для производных. */ - protected double transformation(CacheElement cache, - NamedDoubleSet newSet, double x) throws TransformationNotAvailable { + protected double transformation(CacheElement cache, NamedValueSet newSet, double x) throws TransformationNotAvailable { /* @@ -150,7 +152,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { } @Override - public double value(double x, NamedDoubleSet set) { + public double value(double x, NamedValueSet set) { if (!isCachingEnabled()) { return source.value(x, set); } @@ -187,22 +189,22 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { protected class CacheElement extends AbstractNamedSet implements UnivariateFunction { private UnivariateFunction cachedSpectrum; - private final NamedDoubleSet cachedParameters; + private final NamedValueSet cachedParameters; String parName; - CacheElement(NamedDoubleSet parameters, String parName) { + CacheElement(NamedValueSet parameters, String parName) { super(source); //на всякий случай обрезаем набор параметров до необходимого String[] names = source.namesAsArray(); - this.cachedParameters = new NamedDoubleArray(names, parameters.getValues(names)); + this.cachedParameters = new NamedVector(names, MathUtils.getDoubleArray(parameters)); UnivariateFunction func = getSpectrumDerivativeFunction(parName, source, parameters); generate(func); } - CacheElement(NamedDoubleSet parameters) { + CacheElement(NamedValueSet parameters) { super(source); String[] names = source.namesAsArray(); - this.cachedParameters = new NamedDoubleArray(names, parameters.getValues(names)); + this.cachedParameters = new NamedVector(names, MathUtils.getDoubleArray(parameters)); UnivariateFunction func = getSpectrumFunction(source, parameters); generate(func); } @@ -227,7 +229,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { return this.cachedSpectrum.value(x); } - public NamedDoubleSet getCachedParameters() { + public NamedValueSet getCachedParameters() { return this.cachedParameters; } } diff --git a/numass-main/src/main/java/inr/numass/models/SimpleRange.java b/numass-main/src/main/java/inr/numass/models/SimpleRange.java index 9b8887dc..e9dc6df6 100644 --- a/numass-main/src/main/java/inr/numass/models/SimpleRange.java +++ b/numass-main/src/main/java/inr/numass/models/SimpleRange.java @@ -13,38 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.maths.NamedDoubleSet; - -/** - * - * @author Darksnake - */ -public class SimpleRange implements SpectrumRange{ - Double min; - Double max; - - public SimpleRange(Double min, Double max) { - if(min>=max){ - throw new IllegalArgumentException(); - } - this.min = min; - this.max = max; - } - - - - @Override - public Double max(NamedDoubleSet set) { - return max; - } - - @Override - public Double min(NamedDoubleSet set) { - return min; - } - - - -} +package inr.numass.models; + +import hep.dataforge.values.NamedValueSet; + +/** + * + * @author Darksnake + */ +public class SimpleRange implements SpectrumRange{ + Double min; + Double max; + + public SimpleRange(Double min, Double max) { + if(min>=max){ + throw new IllegalArgumentException(); + } + this.min = min; + this.max = max; + } + + + + @Override + public Double max(NamedValueSet set) { + return max; + } + + @Override + public Double min(NamedValueSet set) { + return min; + } + + + +} diff --git a/numass-main/src/main/java/inr/numass/models/SpectrumRange.java b/numass-main/src/main/java/inr/numass/models/SpectrumRange.java index dc0e6226..6b84089b 100644 --- a/numass-main/src/main/java/inr/numass/models/SpectrumRange.java +++ b/numass-main/src/main/java/inr/numass/models/SpectrumRange.java @@ -13,16 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.models; - -import hep.dataforge.maths.NamedDoubleSet; - - -/** - * Calculates a range in witch spectrum is not 0 - * @author Darksnake - */ -public interface SpectrumRange { - Double min(NamedDoubleSet set); - Double max(NamedDoubleSet set); -} +package inr.numass.models; + +import hep.dataforge.values.NamedValueSet; + + +/** + * Calculates a range in witch spectrum is not 0 + * @author Darksnake + */ +public interface SpectrumRange { + Double min(NamedValueSet set); + Double max(NamedValueSet set); +} diff --git a/numass-main/src/main/java/inr/numass/models/Transmission.java b/numass-main/src/main/java/inr/numass/models/Transmission.java index 1a432f85..0773b3b6 100644 --- a/numass-main/src/main/java/inr/numass/models/Transmission.java +++ b/numass-main/src/main/java/inr/numass/models/Transmission.java @@ -17,8 +17,9 @@ package inr.numass.models; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.names.NameSetContainer; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import org.apache.commons.math3.analysis.BivariateFunction; /** @@ -27,18 +28,18 @@ import org.apache.commons.math3.analysis.BivariateFunction; */ public interface Transmission extends NameSetContainer{ - double getValue(NamedDoubleSet set, double input, double output); - double getDeriv(String name, NamedDoubleSet set, double input, double output); + double getValue(NamedValueSet set, double input, double output); + double getDeriv(String name, NamedValueSet set, double input, double output); boolean providesDeriv(String name); ParametricFunction getConvolutedSpectrum(RangedNamedSetSpectrum bare); - default BivariateFunction getBivariateFunction(final NamedDoubleSet params){ + default BivariateFunction getBivariateFunction(final NamedValueSet params){ return (double input, double output) -> getValue(params, input, output); } - default BivariateFunction getBivariateDerivFunction(final String name, final NamedDoubleSet params){ + default BivariateFunction getBivariateDerivFunction(final String name, final NamedValueSet params){ return (double input, double output) -> getDeriv(name, params, input, output); } } diff --git a/numass-main/src/main/java/inr/numass/models/TransmissionConvolution.java b/numass-main/src/main/java/inr/numass/models/TransmissionConvolution.java index fbc483fe..d71ad6f5 100644 --- a/numass-main/src/main/java/inr/numass/models/TransmissionConvolution.java +++ b/numass-main/src/main/java/inr/numass/models/TransmissionConvolution.java @@ -17,7 +17,7 @@ package inr.numass.models; import hep.dataforge.functions.AbstractParametricFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; +import hep.dataforge.values.NamedValueSet; import inr.numass.NumassContext; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; @@ -45,7 +45,7 @@ class TransmissionConvolution extends AbstractParametricFunction { } @Override - public double derivValue(final String parName, final double U, NamedDoubleSet set) { + public double derivValue(final String parName, final double U, NamedValueSet set) { double min = range.min(set); double max = range.max(set); @@ -67,7 +67,7 @@ class TransmissionConvolution extends AbstractParametricFunction { } @Override - public double value(final double U, NamedDoubleSet set) { + public double value(final double U, NamedValueSet set) { double min = range.min(set); double max = range.max(set); diff --git a/numass-main/src/main/java/inr/numass/models/TritiumSpectrumCaching.java b/numass-main/src/main/java/inr/numass/models/TritiumSpectrumCaching.java index 46a881a7..b3184772 100644 --- a/numass-main/src/main/java/inr/numass/models/TritiumSpectrumCaching.java +++ b/numass-main/src/main/java/inr/numass/models/TritiumSpectrumCaching.java @@ -16,8 +16,8 @@ package inr.numass.models; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleArray; -import hep.dataforge.maths.NamedDoubleSet; +import hep.dataforge.maths.NamedVector; +import hep.dataforge.values.NamedValueSet; import static java.lang.Math.abs; import org.slf4j.LoggerFactory; @@ -39,11 +39,11 @@ public class TritiumSpectrumCaching extends NamedSpectrumCaching { } @Override - protected double transformation(CacheElement cache, NamedDoubleSet newSet, double x) throws TransformationNotAvailable { + protected double transformation(CacheElement cache, NamedValueSet newSet, double x) throws TransformationNotAvailable { double res; - NamedDoubleArray curSet = new NamedDoubleArray(newSet); - double E0new = newSet.getValue("E0"); - double E0old = cache.getCachedParameters().getValue("E0"); + NamedVector curSet = new NamedVector(newSet); + double E0new = newSet.getDouble("E0"); + double E0old = cache.getCachedParameters().getDouble("E0"); double E0delta = E0new - E0old; if (abs(E0delta) > delta) { LoggerFactory.getLogger(getClass()) diff --git a/numass-main/src/main/java/inr/numass/models/VariableLossSpectrum.java b/numass-main/src/main/java/inr/numass/models/VariableLossSpectrum.java index 864adffa..0bd7f49b 100644 --- a/numass-main/src/main/java/inr/numass/models/VariableLossSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/VariableLossSpectrum.java @@ -18,8 +18,9 @@ package inr.numass.models; import hep.dataforge.exceptions.NotDefinedException; import hep.dataforge.functions.AbstractParametricFunction; import hep.dataforge.functions.ParametricFunction; -import hep.dataforge.maths.NamedDoubleSet; import hep.dataforge.maths.integration.UnivariateIntegrator; +import hep.dataforge.values.NamedValueSet; +import hep.dataforge.values.ValueProvider; import inr.numass.NumassContext; import java.util.List; import org.apache.commons.math3.analysis.BivariateFunction; @@ -41,7 +42,7 @@ public class VariableLossSpectrum extends AbstractParametricFunction { return new VariableLossSpectrum(new AbstractParametricFunction(new String[0]) { @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { + public double derivValue(String parName, double x, NamedValueSet set) { throw new NotDefinedException(); } @@ -51,7 +52,7 @@ public class VariableLossSpectrum extends AbstractParametricFunction { } @Override - public double value(double x, NamedDoubleSet set) { + public double value(double x, NamedValueSet set) { return transmission.value(x); } }, eMax); @@ -68,17 +69,17 @@ public class VariableLossSpectrum extends AbstractParametricFunction { } @Override - public double derivValue(String parName, double x, NamedDoubleSet set) { + public double derivValue(String parName, double x, NamedValueSet set) { throw new NotDefinedException(); } @Override - public double value(double U, NamedDoubleSet set) { + public double value(double U, NamedValueSet set) { if (U >= eMax) { return 0; } - double X = set.getValue("X"); - final double shift = set.getValue("shift"); + double X = set.getDouble("X"); + final double shift = set.getDouble("shift"); final LossCalculator loss = LossCalculator.instance(); @@ -107,13 +108,13 @@ public class VariableLossSpectrum extends AbstractParametricFunction { return noLossProb * transmission.value(U - shift, set) + integrator.integrate(integrand, U, eMax); } - public UnivariateFunction singleScatterFunction(NamedDoubleSet set) { + public UnivariateFunction singleScatterFunction(ValueProvider set) { - final double exPos = set.getValue("exPos"); - final double ionPos = set.getValue("ionPos"); - final double exW = set.getValue("exW"); - final double ionW = set.getValue("ionW"); - final double exIonRatio = set.getValue("exIonRatio"); + final double exPos = set.getDouble("exPos"); + final double ionPos = set.getDouble("ionPos"); + final double exW = set.getDouble("exW"); + final double ionW = set.getDouble("ionW"); + final double exIonRatio = set.getDouble("exIonRatio"); return singleScatterFunction(exPos, ionPos, exW, ionW, exIonRatio); } diff --git a/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java new file mode 100644 index 00000000..95140165 --- /dev/null +++ b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java @@ -0,0 +1,33 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass.utils; + +import groovy.lang.Binding; +import groovy.lang.GroovyShell; +import java.util.Map; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.control.customizers.ImportCustomizer; + +/** + * + * @author Alexander Nozik + */ +public class ExpressionUtils { + + public static Double evaluate(String expression, Map binding) { + Binding b = new Binding(binding); + // Add imports for script. + ImportCustomizer importCustomizer = new ImportCustomizer(); + // import static com.mrhaki.blog.Type.* + importCustomizer.addStaticStars("java.lang.Math"); + + CompilerConfiguration configuration = new CompilerConfiguration(); + configuration.addCompilationCustomizers(importCustomizer); // Create shell and execute script. + + GroovyShell shell = new GroovyShell(b,configuration); + return (Double) shell.evaluate(expression); + } +} diff --git a/numass-main/src/main/java/inr/numass/utils/OldDataReader.java b/numass-main/src/main/java/inr/numass/utils/OldDataReader.java index 4832623d..f4af6bdb 100644 --- a/numass-main/src/main/java/inr/numass/utils/OldDataReader.java +++ b/numass-main/src/main/java/inr/numass/utils/OldDataReader.java @@ -26,6 +26,9 @@ import java.io.FileNotFoundException; import java.util.Locale; import static java.util.Locale.setDefault; import java.util.Scanner; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; /** * diff --git a/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java b/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java index a2a53890..13bbfd1c 100644 --- a/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java +++ b/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java @@ -132,6 +132,13 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder } private void buildContext(Meta config) { + if (this.context != null) { + try { + this.context.close(); + } catch (Exception ex) { + context.getLogger().error("Failed to close context", ex); + } + } this.context = this.contextFactory.build(parentContext, config); context.setIO(new WorkbenchIOManager(new NumassIO(), this)); processWindow = ProcessManagerFragment.attachToContext(context); diff --git a/numass-main/src/main/java/inr/numass/workbench/Workbench.java b/numass-main/src/main/java/inr/numass/workbench/Workbench.java index e3d1a021..b46be037 100644 --- a/numass-main/src/main/java/inr/numass/workbench/Workbench.java +++ b/numass-main/src/main/java/inr/numass/workbench/Workbench.java @@ -5,6 +5,7 @@ */ package inr.numass.workbench; +import hep.dataforge.context.GlobalContext; import inr.numass.NumassContext; import java.io.IOException; import java.text.ParseException; @@ -40,6 +41,14 @@ public class Workbench extends Application { }); } + @Override + public void stop() throws Exception { + GlobalContext.instance().close(); + super.stop(); + } + + + /** * @param args the command line arguments */ diff --git a/numass-main/src/test/java/inr/numass/NumassContextTest.java b/numass-main/src/test/java/inr/numass/NumassContextTest.java new file mode 100644 index 00000000..3a801db9 --- /dev/null +++ b/numass-main/src/test/java/inr/numass/NumassContextTest.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass; + +import inr.numass.storage.SetDirectionUtility; +import org.junit.Test; + +/** + * + * @author Alexander Nozik + */ +public class NumassContextTest { + + public NumassContextTest() { + } + + /** + * Test of close method, of class NumassContext. + */ + @Test + public void testClose() throws Exception { + System.out.println("close"); + NumassContext instance = new NumassContext(); + instance.close(); + assert SetDirectionUtility.cacheFile(instance).exists(); + + } + +} diff --git a/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java b/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java new file mode 100644 index 00000000..bdbf40cd --- /dev/null +++ b/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass.actions; + +import inr.numass.utils.ExpressionUtils; +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; + +import org.junit.Test; + +/** + * + * @author Alexander Nozik + */ +public class PrepareDataActionTest { + + public PrepareDataActionTest() { + } + + @Test + public void testExpression() { + Map exprParams = new HashMap<>(); + exprParams.put("U", 18000d); + double correctionFactor = ExpressionUtils.evaluate("1 + 13.265 * exp(- U / 2343.4)", exprParams); + Assert.assertEquals("Testing expression calculation", 1.006125, correctionFactor, 1e-5); + } + +} diff --git a/numass-main/src/test/java/inr/numass/run/NumassSpectrumTest.java b/numass-main/src/test/java/inr/numass/run/NumassSpectrumTest.java index adc40fc8..ade7388c 100644 --- a/numass-main/src/test/java/inr/numass/run/NumassSpectrumTest.java +++ b/numass-main/src/test/java/inr/numass/run/NumassSpectrumTest.java @@ -25,6 +25,37 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.Locale; import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; +import static java.util.Locale.setDefault; /** * diff --git a/numass-storage/src/main/java/inr/numass/storage/NMPoint.java b/numass-storage/src/main/java/inr/numass/storage/NMPoint.java index 6fe0dd13..dacd142f 100644 --- a/numass-storage/src/main/java/inr/numass/storage/NMPoint.java +++ b/numass-storage/src/main/java/inr/numass/storage/NMPoint.java @@ -129,6 +129,8 @@ public class NMPoint { return res; } + + //TODO move dead time out of here! public double getCountRate(int from, int to, double deadTime) { double wind = getCountInWindow(from, to) / getLength(); double res; diff --git a/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java b/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java index 426c024d..b1216cf3 100644 --- a/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java +++ b/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java @@ -316,12 +316,14 @@ public class NumassDataLoader extends AbstractLoader implements ObjectLoader points = getPoints(); - if (getPoints().size() >= 2) { - return readTime(points.get(0).meta()).isAfter(readTime(points.get(1).meta())); - } else { - return false; - } + return SetDirectionUtility.isReversed(getFullPath(), n -> { + List points = getPoints(); + if (getPoints().size() >= 2) { + return readTime(points.get(0).meta()).isAfter(readTime(points.get(1).meta())); + } else { + return false; + } + }); } @Override diff --git a/numass-storage/src/main/java/inr/numass/storage/SetDirectionUtility.java b/numass-storage/src/main/java/inr/numass/storage/SetDirectionUtility.java new file mode 100644 index 00000000..1bf5a212 --- /dev/null +++ b/numass-storage/src/main/java/inr/numass/storage/SetDirectionUtility.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass.storage; + +import hep.dataforge.context.Context; +import hep.dataforge.context.GlobalContext; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +/** + * A temporary utility to store set directions to avoid multiple file reading + * + * @author Alexander Nozik + */ +public class SetDirectionUtility { + + private static final String FILE_NAME = "numass_set_direction.map"; + + private static Map directionMap = new HashMap<>(); + + private static boolean isLoaded = false; + + static synchronized boolean isReversed(String setName, Function provider) { + if (!isLoaded) { + load(GlobalContext.instance()); + } + return directionMap.computeIfAbsent(setName, provider); + } + + public static File cacheFile(Context context) { + return new File(context.io().getTmpDirectory(), FILE_NAME); + } + + public static synchronized void load(Context context) { + context.getLogger().info("Loading set direction utility"); + File file = cacheFile(context); + if (file.exists()) { + try (ObjectInputStream st = new ObjectInputStream(new FileInputStream(file))) { + directionMap = (Map) st.readObject(); + context.getLogger().info("Set directions successfully loaded from file"); + } catch (ClassNotFoundException | IOException ex) { + context.getLogger().error("Failed to load numass direction mapping", ex); + } + } + + isLoaded = true; + } + + public static synchronized void save(Context context) { + try { + File file = cacheFile(context); + if (!file.exists()) { + file.createNewFile(); + } + try (ObjectOutputStream st = new ObjectOutputStream(new FileOutputStream(file))) { + st.writeObject(directionMap); + context.getLogger().info("Set directions successfully saved to file"); + } + } catch (IOException ex) { + context.getLogger().error("Failed to save numass direction mapping", ex); + } + } +}