diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 28a286df..9360b26e 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -34,6 +34,10 @@ dependencies { compile "hep.dataforge:grind-terminal" //project(':dataforge-grind:grind-terminal') compile "hep.dataforge:kmath" compile "hep.dataforge:kodex-fx" + + // https://mvnrepository.com/artifact/org.ehcache/ehcache + compile group: 'org.ehcache', name: 'ehcache', version: '3.4.0' + } task repl(dependsOn: classes, type: JavaExec) { diff --git a/numass-main/src/main/kotlin/inr/numass/NumassPlugin.kt b/numass-main/src/main/kotlin/inr/numass/NumassPlugin.kt index 0f13d19e..dc2c3afd 100644 --- a/numass-main/src/main/kotlin/inr/numass/NumassPlugin.kt +++ b/numass-main/src/main/kotlin/inr/numass/NumassPlugin.kt @@ -61,18 +61,18 @@ class NumassPlugin : BasicPlugin() { val actions = context.pluginManager().getOrLoad(ActionManager::class.java) actions.attach(context) - actions.putTask(NumassTableFilterTask::class.java) actions.putTask(NumassFitScanTask::class.java) actions.putTask(NumassFitScanSummaryTask::class.java) - actions.putTask(NumassFitTask::class.java) actions.putTask(NumassFitSummaryTask::class.java) - actions.put(selectDataTask) + actions.put(selectTask) actions.put(analyzeTask) actions.put(mergeTask) actions.put(mergeEmptyTask) actions.put(monitorTableTask) actions.put(subtractEmptyTask) actions.put(transformTask) + actions.put(filterTask) + actions.put(fitTask) } private fun loadMath(math: MathPlugin) { @@ -281,7 +281,8 @@ class NumassPlugin : BasicPlugin() { * @param height * @return */ -@JvmOverloads fun displayJFreeChart(title: String, width: Double = 800.0, height: Double = 600.0, meta: Meta = Meta.empty()): JFreeChartFrame { +@JvmOverloads +fun displayJFreeChart(title: String, width: Double = 800.0, height: Double = 600.0, meta: Meta = Meta.empty()): JFreeChartFrame { val frame = JFreeChartFrame(meta) frame.configureValue("title", title) PlotContainer.display(frame, title, width, height) diff --git a/numass-main/src/main/kotlin/inr/numass/NumassUtils.kt b/numass-main/src/main/kotlin/inr/numass/NumassUtils.kt index f5cd1918..e847d9ed 100644 --- a/numass-main/src/main/kotlin/inr/numass/NumassUtils.kt +++ b/numass-main/src/main/kotlin/inr/numass/NumassUtils.kt @@ -30,6 +30,7 @@ import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.tables.ListTable import hep.dataforge.tables.Table import hep.dataforge.tables.ValueMap +import hep.dataforge.values.ValueType import hep.dataforge.values.Values import inr.numass.data.api.NumassAnalyzer import inr.numass.data.api.NumassPoint @@ -220,4 +221,20 @@ fun subtract(context: Context, merge: Table, empty: Table): Table { } return builder.build() +} + +fun Values.unbox(): Map { + val res = HashMap() + for (field in this.names) { + val value = this.getValue(field) + val obj: Any? = when (value.type) { + ValueType.BOOLEAN -> value.booleanValue() + ValueType.NUMBER -> value.doubleValue() + ValueType.STRING -> value.stringValue() + ValueType.TIME -> value.timeValue() + ValueType.NULL -> null + } + res.put(field, obj) + } + return res } \ No newline at end of file diff --git a/numass-main/src/main/kotlin/inr/numass/tasks/NumassFitTask.kt b/numass-main/src/main/kotlin/inr/numass/tasks/NumassFitTask.kt deleted file mode 100644 index daff33db..00000000 --- a/numass-main/src/main/kotlin/inr/numass/tasks/NumassFitTask.kt +++ /dev/null @@ -1,61 +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.tasks - -import hep.dataforge.actions.Action -import hep.dataforge.actions.ActionUtils -import hep.dataforge.data.DataNode -import hep.dataforge.meta.Meta -import hep.dataforge.plotfit.PlotFitResultAction -import hep.dataforge.stat.fit.FitAction -import hep.dataforge.stat.fit.FitResult -import hep.dataforge.tables.Table -import hep.dataforge.workspace.tasks.SingleActionTask -import hep.dataforge.workspace.tasks.TaskModel - -/** - * Created by darksnake on 16-Sep-16. - */ -class NumassFitTask : SingleActionTask() { - - override fun getName(): String { - return "fit" - } - - override fun gatherNode(data: DataNode<*>): DataNode { - return data.checked(Table::class.java) - } - - override fun validate(model: TaskModel) { - if (model.meta().isEmpty) { - throw RuntimeException("Fit element not found in model") - } - } - - override fun getAction(model: TaskModel): Action { - val action = FitAction() - return if (model.meta().getBoolean("frame", false)) { - ActionUtils.compose(action, PlotFitResultAction()) - } else { - action - } - } - - override fun buildModel(model: TaskModel.Builder, meta: Meta) { - model.dependsOn("filter", meta); - } -} diff --git a/numass-main/src/main/kotlin/inr/numass/tasks/NumassTableFilterTask.kt b/numass-main/src/main/kotlin/inr/numass/tasks/NumassTableFilterTask.kt deleted file mode 100644 index b2670db6..00000000 --- a/numass-main/src/main/kotlin/inr/numass/tasks/NumassTableFilterTask.kt +++ /dev/null @@ -1,77 +0,0 @@ -package inr.numass.tasks - -import hep.dataforge.actions.Action -import hep.dataforge.actions.OneToOneAction -import hep.dataforge.context.Context -import hep.dataforge.data.DataNode -import hep.dataforge.description.TypedActionDef -import hep.dataforge.meta.Laminate -import hep.dataforge.meta.Meta -import hep.dataforge.tables.Table -import hep.dataforge.tables.TableTransform -import hep.dataforge.values.ValueType -import hep.dataforge.values.Values -import hep.dataforge.workspace.tasks.SingleActionTask -import hep.dataforge.workspace.tasks.TaskModel -import inr.numass.data.api.NumassPoint -import inr.numass.utils.ExpressionUtils -import java.util.* - -/** - * Created by darksnake on 13-Aug-16. - */ -class NumassTableFilterTask : SingleActionTask() { - - override fun getName(): String { - return "filter" - } - - override fun gatherNode(data: DataNode<*>): DataNode
{ - return data.checked(Table::class.java) - } - - - override fun buildModel(model: TaskModel.Builder, meta: Meta) { - if (meta.hasMeta("empty")) { - model.dependsOn("dif", meta) - } else { - model.dependsOn("transform", meta) - } - } - - override fun getAction(model: TaskModel): Action { - return FilterTableAction() - } - - @TypedActionDef(name = "filterTable", inputType = Table::class, outputType = Table::class) - private inner class FilterTableAction : OneToOneAction() { - override fun execute(context: Context, name: String, input: Table, inputMeta: Laminate): Table { - if (inputMeta.hasValue("from") || inputMeta.hasValue("to")) { - val uLo = inputMeta.getDouble("from", 0.0)!! - val uHi = inputMeta.getDouble("to", java.lang.Double.POSITIVE_INFINITY)!! - getLogger(context, inputMeta).debug("Filtering finished") - return TableTransform.filter(input, NumassPoint.HV_KEY, uLo, uHi) - } else if (inputMeta.hasValue("condition")) { - return TableTransform.filter(input) { ExpressionUtils.condition(inputMeta.getString("condition"), unbox(it)) } - } else { - throw RuntimeException("No filtering condition specified") - } - } - } - - private fun unbox(dp: Values): Map { - val res = HashMap() - for (field in dp.names) { - val value = dp.getValue(field) - val obj: Any? = when (value.type) { - ValueType.BOOLEAN -> value.booleanValue() - ValueType.NUMBER -> value.doubleValue() - ValueType.STRING -> value.stringValue() - ValueType.TIME -> value.timeValue() - ValueType.NULL -> null - } - res.put(field, obj) - } - return res - } -} 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 d085da08..b8432984 100644 --- a/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt +++ b/numass-main/src/main/kotlin/inr/numass/tasks/NumassTasks.kt @@ -14,8 +14,11 @@ import hep.dataforge.meta.MetaUtils import hep.dataforge.plots.PlotFrame import hep.dataforge.plots.data.DataPlot import hep.dataforge.plots.jfreechart.JFreeChartFrame +import hep.dataforge.stat.fit.FitAction +import hep.dataforge.stat.fit.FitResult import hep.dataforge.tables.ListTable import hep.dataforge.tables.Table +import hep.dataforge.tables.TableTransform import hep.dataforge.tables.XYAdapter import hep.dataforge.values.ValueType import inr.numass.NumassUtils @@ -24,10 +27,13 @@ import inr.numass.actions.MergeDataAction.MERGE_NAME import inr.numass.actions.TransformDataAction import inr.numass.addSetMarkers import inr.numass.data.analyzers.SmartAnalyzer +import inr.numass.data.api.NumassPoint import inr.numass.data.api.NumassSet import inr.numass.subtract +import inr.numass.unbox +import inr.numass.utils.ExpressionUtils -val selectDataTask = task("select") { +val selectTask = task("select") { model { meta -> data("*") configure(meta.getMetaOrEmpty("data")) @@ -40,7 +46,7 @@ val selectDataTask = task("select") { @ValueDef(name = "showPlot", type = arrayOf(ValueType.BOOLEAN), info = "Show plot after complete") val monitorTableTask = task("monitor") { model { meta -> - dependsOn("select", meta) + dependsOn(selectTask, meta) configure(meta.getMetaOrEmpty("analyzer")) } join { @@ -90,7 +96,7 @@ val monitorTableTask = task("monitor") { val analyzeTask = task("analyze") { model { meta -> - dependsOn("select", meta); + dependsOn(selectTask, meta); configure(MetaUtils.optEither(meta, "analyzer", "prepare").orElse(Meta.empty())) } pipe { @@ -106,7 +112,7 @@ val analyzeTask = task("analyze") { val mergeTask = task("merge") { model { meta -> - dependsOn("analyze", meta) + dependsOn(analyzeTask, meta) configure(meta.getMetaOrEmpty("merge")) } action(MergeDataAction()) @@ -123,7 +129,7 @@ val mergeEmptyTask = task("empty") { .removeNode("empty") .setNode("data", meta.getMeta("empty")) .setValue("merge." + MERGE_NAME, meta.getString("merge." + MERGE_NAME, "") + "_empty"); - dependsOn("merge", newMeta) + dependsOn(mergeTask, newMeta) } transform { data -> val builder = DataSet.builder(Table::class.java) @@ -137,8 +143,8 @@ val mergeEmptyTask = task("empty") { val subtractEmptyTask = task("dif") { model { meta -> - dependsOn("merge", meta, "data") - dependsOn("empty", meta, "empty") + dependsOn(mergeTask, meta, "data") + dependsOn(mergeEmptyTask, meta, "empty") } transform { data -> val builder = DataTree.builder(Table::class.java) @@ -167,14 +173,42 @@ val transformTask = task("transform") { model { meta -> if (meta.hasMeta("merge")) { if (meta.hasMeta("empty")) { - dependsOn("dif", meta) + dependsOn(subtractEmptyTask, meta) } else { - dependsOn("merge", meta); + dependsOn(mergeTask, meta); } } else { - dependsOn("analyze", meta); + dependsOn(analyzeTask, meta); } configure(MetaUtils.optEither(meta, "transform", "prepare").orElse(Meta.empty())) } action(TransformDataAction()); } + +val filterTask = task("filter") { + model { meta -> + dependsOn(transformTask, meta) + } + pipe { + result { data -> + if (meta.hasValue("from") || meta.hasValue("to")) { + val uLo = meta.getDouble("from", 0.0)!! + val uHi = meta.getDouble("to", java.lang.Double.POSITIVE_INFINITY)!! + this.log.report("Filtering finished") + TableTransform.filter(data, NumassPoint.HV_KEY, uLo, uHi) + } else if (meta.hasValue("condition")) { + TableTransform.filter(data) { ExpressionUtils.condition(meta.getString("condition"), it.unbox()) } + } else { + throw RuntimeException("No filtering condition specified") + } + } + } +} + +val fitTask = task("fit") { + model { meta -> + dependsOn(filterTask, meta) + configure(meta.getMeta("fit")) + } + action(FitAction()) +}