diff --git a/build.gradle b/build.gradle index 62ae1e4f..e609d342 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = "1.2.61" + ext.kotlin_version = "1.2.70" repositories { jcenter() maven { diff --git a/numass-control/dante/src/main/kotlin/inr/numass/control/dante/DanteTest.kt b/numass-control/dante/src/main/kotlin/inr/numass/control/dante/DanteTest.kt index 6535d0a3..1c109657 100644 --- a/numass-control/dante/src/main/kotlin/inr/numass/control/dante/DanteTest.kt +++ b/numass-control/dante/src/main/kotlin/inr/numass/control/dante/DanteTest.kt @@ -23,7 +23,7 @@ import hep.dataforge.meta.KMetaBuilder import hep.dataforge.meta.buildMeta import hep.dataforge.nullable import hep.dataforge.plots.data.DataPlot -import hep.dataforge.plots.output.plot +import hep.dataforge.plots.output.plotFrame import hep.dataforge.tables.Adapters import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.SimpleAnalyzer @@ -78,7 +78,7 @@ fun NumassBlock.plotAmplitudeSpectrum(plotName: String = "spectrum", frameName: val lo = meta.optNumber("window.lo").nullable?.toInt() val up = meta.optNumber("window.up").nullable?.toInt() val data = SimpleAnalyzer().getAmplitudeSpectrum(this, meta.getMetaOrEmpty("spectrum")).withBinning(binning, lo, up) - context.plot(plotName) { + context.plotFrame(plotName) { val valueAxis = if (meta.getBoolean("normalize", false)) { NumassAnalyzer.COUNT_RATE_KEY } else { diff --git a/numass-main/src/main/kotlin/inr/numass/data/Visualization.kt b/numass-main/src/main/kotlin/inr/numass/data/Visualization.kt index ae8c715f..358888fa 100644 --- a/numass-main/src/main/kotlin/inr/numass/data/Visualization.kt +++ b/numass-main/src/main/kotlin/inr/numass/data/Visualization.kt @@ -23,7 +23,7 @@ import hep.dataforge.meta.KMetaBuilder import hep.dataforge.meta.buildMeta import hep.dataforge.nullable import hep.dataforge.plots.data.DataPlot -import hep.dataforge.plots.output.plot +import hep.dataforge.plots.output.plotFrame import hep.dataforge.tables.Adapters import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.SmartAnalyzer @@ -37,7 +37,7 @@ fun NumassBlock.plotAmplitudeSpectrum(plotName: String = "spectrum", frameName: val lo = meta.optNumber("window.lo").nullable?.toInt() val up = meta.optNumber("window.up").nullable?.toInt() val data = SmartAnalyzer().getAmplitudeSpectrum(this, meta.getMetaOrEmpty("spectrum")).withBinning(binning, lo, up) - context.plot(plotName) { + context.plotFrame(plotName) { val valueAxis = if (meta.getBoolean("normalize", false)) { NumassAnalyzer.COUNT_RATE_KEY } else { diff --git a/numass-main/src/main/kotlin/inr/numass/models/mc/BetaSampler.kt b/numass-main/src/main/kotlin/inr/numass/models/mc/BetaSampler.kt index f5b9b681..406f5d76 100644 --- a/numass-main/src/main/kotlin/inr/numass/models/mc/BetaSampler.kt +++ b/numass-main/src/main/kotlin/inr/numass/models/mc/BetaSampler.kt @@ -4,7 +4,7 @@ import hep.dataforge.context.Global import hep.dataforge.maths.chain.Chain import hep.dataforge.meta.buildMeta import hep.dataforge.plots.data.XYFunctionPlot -import hep.dataforge.plots.output.plot +import hep.dataforge.plots.output.plotFrame import hep.dataforge.stat.PolynomialDistribution import hep.dataforge.stat.fit.ParamSet import inr.numass.NumassPlugin @@ -46,7 +46,7 @@ fun main(args: Array) { 50 * distribution.density(18600.0 - it) } - Global.plot("beta") { + Global.plotFrame("beta") { add(spectrumPlot) add(distributionPlot) } diff --git a/numass-main/src/main/kotlin/inr/numass/models/misc/LossCalculator.kt b/numass-main/src/main/kotlin/inr/numass/models/misc/LossCalculator.kt index 507e87ea..d5950f41 100644 --- a/numass-main/src/main/kotlin/inr/numass/models/misc/LossCalculator.kt +++ b/numass-main/src/main/kotlin/inr/numass/models/misc/LossCalculator.kt @@ -124,7 +124,7 @@ object LossCalculator { * @param X * @return */ - fun getLossProbabilities(x: Double): List { + fun calculateLossProbabilities(x: Double): List { val res = ArrayList() var prob: Double if (x > 0) { @@ -151,6 +151,8 @@ object LossCalculator { return res } + fun getLossProbabilities(x: Double): List = lossProbCache.getOrPut(x) { calculateLossProbabilities(x) } + fun getLossProbability(order: Int, X: Double): Double { if (order == 0) { return if (X > 0) { diff --git a/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt b/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt index 4369b9c6..a16dc067 100644 --- a/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt +++ b/numass-main/src/main/kotlin/inr/numass/scripts/models/IntegralSpectrum.kt @@ -24,7 +24,7 @@ import hep.dataforge.meta.Meta import hep.dataforge.plots.Plot import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.jfreechart.JFreeChartPlugin -import hep.dataforge.plots.output.plot +import hep.dataforge.plots.output.plotFrame import hep.dataforge.stat.fit.FitManager import hep.dataforge.stat.fit.FitStage import hep.dataforge.stat.fit.FitState @@ -127,7 +127,7 @@ fun main(args: Array) { } - context.plot("fit", stage = "plots") { + context.plotFrame("fit", stage = "plots") { plots.configure { "showLine" to true "showSymbol" to false diff --git a/numass-main/src/main/kotlin/inr/numass/scripts/threshold/FitWithPower.kt b/numass-main/src/main/kotlin/inr/numass/scripts/threshold/FitWithPower.kt new file mode 100644 index 00000000..f50f3171 --- /dev/null +++ b/numass-main/src/main/kotlin/inr/numass/scripts/threshold/FitWithPower.kt @@ -0,0 +1,66 @@ +/* + * Copyright 2018 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.threshold + +import hep.dataforge.buildContext +import hep.dataforge.fx.output.FXOutputManager +import hep.dataforge.io.DirectoryOutput +import hep.dataforge.io.plus +import hep.dataforge.meta.buildMeta +import hep.dataforge.nullable +import hep.dataforge.plots.data.DataPlot +import hep.dataforge.plots.jfreechart.JFreeChartPlugin +import hep.dataforge.plots.plotData +import hep.dataforge.storage.files.FileStorage +import hep.dataforge.tables.Adapters +import inr.numass.NumassPlugin +import inr.numass.data.api.NumassSet +import inr.numass.data.storage.NumassDirectory +import inr.numass.displayChart +import inr.numass.subthreshold.Threshold + +fun main(args: Array) { + val context = buildContext("NUMASS", NumassPlugin::class.java, JFreeChartPlugin::class.java) { + rootDir = "D:\\Work\\Numass\\sterile\\2017_05" + dataDir = "D:\\Work\\Numass\\data\\2017_05" + output = FXOutputManager() + DirectoryOutput() + } + + val storage = NumassDirectory.read(context, "Fill_2") as? FileStorage ?: error("Storage not found") + + val meta = buildMeta { + "delta" to -200 + "method" to "pow" + "t0" to 15e3 + "window.lo" to 400 + "window.up" to 1600 + } + + val frame = displayChart("correction").apply { + plots.setType() + } + + listOf("set_2", "set_3", "set_4", "set_5").forEach { setName -> + val set = storage.provide(setName, NumassSet::class.java).nullable ?: error("Set does not exist") + + val correctionTable = Threshold.calculateSubThreshold(set, meta).filter { + it.getDouble("correction") in (1.0..1.2) + } + + frame.plotData(setName, correctionTable, Adapters.buildXYAdapter("U", "correction")) + } +} \ No newline at end of file diff --git a/numass-main/src/main/kotlin/inr/numass/scripts/tristan/FitTextTristanFiles.kt b/numass-main/src/main/kotlin/inr/numass/scripts/tristan/FitTextTristanFiles.kt new file mode 100644 index 00000000..4e40d734 --- /dev/null +++ b/numass-main/src/main/kotlin/inr/numass/scripts/tristan/FitTextTristanFiles.kt @@ -0,0 +1,162 @@ +/* + * Copyright 2018 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.tristan + +import hep.dataforge.buildContext +import hep.dataforge.configure +import hep.dataforge.data.DataNode +import hep.dataforge.fx.output.FXOutputManager +import hep.dataforge.io.ColumnedDataReader +import hep.dataforge.io.DirectoryOutput +import hep.dataforge.io.output.stream +import hep.dataforge.io.plus +import hep.dataforge.meta.Meta +import hep.dataforge.meta.buildMeta +import hep.dataforge.plots.data.DataPlot +import hep.dataforge.plots.jfreechart.JFreeChartPlugin +import hep.dataforge.plots.output.plotFrame +import hep.dataforge.stat.fit.FitHelper +import hep.dataforge.stat.fit.ParamSet +import hep.dataforge.tables.* +import hep.dataforge.values.ValueType +import inr.numass.NumassPlugin +import inr.numass.actions.MergeDataAction +import inr.numass.data.analyzers.NumassAnalyzer.Companion.COUNT_KEY +import inr.numass.data.analyzers.NumassAnalyzer.Companion.COUNT_RATE_ERROR_KEY +import inr.numass.data.analyzers.NumassAnalyzer.Companion.COUNT_RATE_KEY +import inr.numass.data.analyzers.NumassAnalyzer.Companion.LENGTH_KEY +import inr.numass.data.api.NumassPoint.Companion.HV_KEY +import java.io.PrintWriter +import java.nio.file.Files +import java.util.function.Predicate + +fun main(args: Array) { + val context = buildContext("NUMASS") { + plugin() + plugin() + rootDir = "D:\\Work\\Numass\\TristanText\\" + dataDir = "D:\\Work\\Numass\\TristanText\\data\\" + output = FXOutputManager() + DirectoryOutput() + } + context.load() + + + val tables = DataNode.build { + name = "tristan" + Files.list(context.dataDir).forEach { + val name = ".*(set_\\d+).*".toRegex().matchEntire(it.fileName.toString())!!.groupValues[1] + val table = ColumnedDataReader(Files.newInputStream(it), HV_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY).toTable() + .addColumn(LENGTH_KEY, ValueType.NUMBER) { 30 } + .addColumn(COUNT_KEY, ValueType.NUMBER) { getDouble(COUNT_RATE_KEY) * 30 } + putStatic(name, table) + } + } + val adapter = Adapters.buildXYAdapter(HV_KEY, COUNT_RATE_KEY, COUNT_RATE_ERROR_KEY) + + context.plotFrame("raw", "plots") { + configure { + "legend.show" to false + } + plots.configure { + "showLine" to true + "showSymbol" to false + } + tables.forEach { (key, value) -> + add(DataPlot.plot(key, value, adapter)) + } + } + + context.plotFrame("raw_normalized", "plots") { + configure { + "legend.show" to false + } + plots.configure { + "showLine" to true + "showSymbol" to false + } + tables.forEach { (key, table) -> + val norming = table.find { it.getDouble(HV_KEY) == 13000.0 }!!.getDouble(COUNT_RATE_KEY) + val normalizedTable = table + .replaceColumn(COUNT_RATE_KEY) { getDouble(COUNT_RATE_KEY) / norming } + .replaceColumn(COUNT_RATE_ERROR_KEY) { getDouble(COUNT_RATE_ERROR_KEY) / norming } + add(DataPlot.plot(key, normalizedTable, adapter)) + } + } + + val merge = MergeDataAction.runGroup(context, tables, Meta.empty()).get() + + val filtered = Tables.filter(merge, + Predicate { + val hv = it.getDouble(HV_KEY) + hv > 12200.0 && (hv < 15500 || hv > 16500) + } + ) + + context.plotFrame("merge", "plots") { + plots.configure { + "showLine" to true + "showSymbol" to false + } + add(DataPlot.plot("merge", merge, adapter)) + } + + val meta = buildMeta { + "model" to { + "modelName" to "sterile" + "resolution" to { + "width" to 8.3e-5 + "tail" to "function::numass.resolutionTail.2017.mod" + } + "transmission" to { + "trapping" to "function::numass.trap.nominal" + } + } + "stage" to { "freePars" to listOf("N", "bkg", "E0") } + } + + val params = ParamSet().apply { + setPar("N", 4e4, 6.0, 0.0, Double.POSITIVE_INFINITY) + setPar("bkg", 2.0, 0.03) + setPar("E0", 18575.0, 1.0) + setPar("mnu2", 0.0, 1.0) + setParValue("msterile2", (1000 * 1000).toDouble()) + setPar("U2", 0.0, 1e-3) + setPar("X", 0.1, 0.01) + setPar("trap", 1.0, 0.01) + } + + context.output["numass.fit", "text"].stream.use { out -> + val log = context.history.getChronicle("log") + val writer = PrintWriter(out) + writer.printf("%n*** META ***%n") + writer.println(meta.toString()) + writer.flush() + FitHelper(context) + .fit(filtered, meta) + .setListenerStream(out) + .report(log) + .apply { + params(params) + }.run() + + writer.println() + log.entries.forEach { entry -> writer.println(entry.toString()) } + writer.println() + + } + +} \ No newline at end of file diff --git a/numass-main/src/main/kotlin/inr/numass/subthreshold/Threshold.kt b/numass-main/src/main/kotlin/inr/numass/subthreshold/Threshold.kt index bf0583f9..66e8c523 100644 --- a/numass-main/src/main/kotlin/inr/numass/subthreshold/Threshold.kt +++ b/numass-main/src/main/kotlin/inr/numass/subthreshold/Threshold.kt @@ -11,11 +11,14 @@ import hep.dataforge.tables.ListTable import hep.dataforge.tables.Table import hep.dataforge.values.ValueMap import hep.dataforge.values.Values +import inr.numass.data.analyzers.NumassAnalyzer import inr.numass.data.analyzers.NumassAnalyzer.Companion.CHANNEL_KEY import inr.numass.data.analyzers.NumassAnalyzer.Companion.COUNT_RATE_KEY +import inr.numass.data.analyzers.SmartAnalyzer import inr.numass.data.analyzers.TimeAnalyzer import inr.numass.data.analyzers.withBinning import inr.numass.data.api.NumassPoint +import inr.numass.data.api.NumassSet import inr.numass.data.api.SimpleNumassPoint import inr.numass.data.storage.NumassDataLoader import inr.numass.data.storage.NumassDirectory @@ -137,6 +140,7 @@ object Threshold { } + /** * Exponential function $a e^{\frac{x}{\sigma}}$ */ @@ -226,9 +230,12 @@ object Threshold { } } - fun calculateSubThreshold(spectra: Map, config: Meta): Table { + fun calculateSubThreshold(set: NumassSet, config: Meta, analyzer: NumassAnalyzer = SmartAnalyzer()): Table { return ListTable.Builder().apply { - spectra.forEach { voltage, spectrum -> row(calculateSubThreshold(spectrum, voltage, config)) } + set.forEach{ + val spectrum = analyzer.getAmplitudeSpectrum(it,config) + row(calculateSubThreshold(spectrum,it.voltage,config)) + } }.build() } diff --git a/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt b/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt index c3ec1fb2..cf8d957c 100644 --- a/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt +++ b/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt @@ -16,6 +16,7 @@ import hep.dataforge.plots.data.XYFunctionPlot import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.plots.output.PlotOutput import hep.dataforge.plots.output.plot +import hep.dataforge.plots.output.plotFrame import hep.dataforge.stat.fit.FitHelper import hep.dataforge.stat.fit.FitResult import hep.dataforge.stat.models.XYModel @@ -351,7 +352,7 @@ val histogramTask = task("histogram") { context.output.render(table, stage = "numass.histogram", name = name, meta = meta) if (meta.getBoolean("plot", false)) { - context.plot("$name.plot", stage = "numass.histogram") { + context.plotFrame("$name.plot", stage = "numass.histogram") { plots.setType() plots.configure { "showSymbol" to false