From 115cf378e4b7151b31b6c54062ae1cd8273bdeed Mon Sep 17 00:00:00 2001 From: darksnake Date: Mon, 31 Jul 2017 22:12:57 +0300 Subject: [PATCH] Numass block sorting --- .../data/analyzers/AbstractAnalyzer.java | 8 +++++++- .../numass/data/analyzers/TimeAnalyzer.java | 3 --- .../java/inr/numass/data/api/MetaBlock.java | 8 ++++++-- .../java/inr/numass/data/api/NumassEvent.java | 9 +-------- .../numass/data/api/SimpleNumassPoint.java | 3 +++ .../numass/data/storage/NumassDataLoader.java | 7 ++----- .../numass/data/storage/ProtoNumassPoint.java | 10 +++++++--- numass-main/build.gradle | 2 +- .../numass/scripts/underflow/Underflow.groovy | 20 +++++++++---------- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java b/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java index 25d4cded..88b9ab9c 100644 --- a/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/analyzers/AbstractAnalyzer.java @@ -8,6 +8,7 @@ import hep.dataforge.tables.TableFormatBuilder; import inr.numass.data.api.*; import org.jetbrains.annotations.Nullable; +import java.util.Comparator; import java.util.stream.Stream; import static hep.dataforge.tables.XYAdapter.*; @@ -44,7 +45,12 @@ public abstract class AbstractAnalyzer implements NumassAnalyzer { } else if (getProcessor() == null) { throw new IllegalArgumentException("Signal processor needed to analyze frames"); } else { - return Stream.concat(block.getEvents(), block.getFrames().flatMap(getProcessor()::analyze)); + //TODO + Stream res = Stream.concat(block.getEvents(), block.getFrames().flatMap(getProcessor()::analyze)); + if (config.getBoolean("sort", false)) { + res = res.sorted(Comparator.comparing(NumassEvent::getTimeOffset)); + } + return res; } } diff --git a/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java b/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java index 96d70af2..44fb8a87 100644 --- a/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java +++ b/numass-core/src/main/java/inr/numass/data/analyzers/TimeAnalyzer.java @@ -90,9 +90,6 @@ public class TimeAnalyzer extends AbstractAnalyzer { AtomicReference lastEvent = new AtomicReference<>(null); Stream eventStream = super.getEvents(block, config);//using super implementation - if (config.getBoolean("sort", false)) { - eventStream = eventStream.sorted(); - } return eventStream.map(event -> { if (lastEvent.get() == null) { diff --git a/numass-core/src/main/java/inr/numass/data/api/MetaBlock.java b/numass-core/src/main/java/inr/numass/data/api/MetaBlock.java index 33a785cb..1c809246 100644 --- a/numass-core/src/main/java/inr/numass/data/api/MetaBlock.java +++ b/numass-core/src/main/java/inr/numass/data/api/MetaBlock.java @@ -32,11 +32,15 @@ public class MetaBlock implements NumassBlock { @Override public Stream getEvents() { - return blocks.stream().flatMap(NumassBlock::getEvents).sorted(Comparator.comparing(NumassEvent::getTime)); + return blocks.stream() + .sorted(Comparator.comparing(NumassBlock::getStartTime)) + .flatMap(NumassBlock::getEvents); } @Override public Stream getFrames() { - return blocks.stream().flatMap(NumassBlock::getFrames).sorted(Comparator.comparing(NumassFrame::getTime)); + return blocks.stream() + .sorted(Comparator.comparing(NumassBlock::getStartTime)) + .flatMap(NumassBlock::getFrames); } } diff --git a/numass-core/src/main/java/inr/numass/data/api/NumassEvent.java b/numass-core/src/main/java/inr/numass/data/api/NumassEvent.java index 573cec14..eb48edcf 100644 --- a/numass-core/src/main/java/inr/numass/data/api/NumassEvent.java +++ b/numass-core/src/main/java/inr/numass/data/api/NumassEvent.java @@ -15,8 +15,6 @@ */ package inr.numass.data.api; -import org.jetbrains.annotations.NotNull; - import java.io.Serializable; import java.time.Instant; @@ -25,7 +23,7 @@ import java.time.Instant; * * @author Darksnake */ -public class NumassEvent implements Comparable, Serializable { +public class NumassEvent implements Serializable { // channel private final short chanel; //The time of the block start @@ -65,9 +63,4 @@ public class NumassEvent implements Comparable, Serializable { public Instant getTime() { return blockTime.plusNanos(timeOffset); } - - @Override - public int compareTo(@NotNull NumassEvent o) { - return this.getTime().compareTo(o.getTime()); - } } diff --git a/numass-core/src/main/java/inr/numass/data/api/SimpleNumassPoint.java b/numass-core/src/main/java/inr/numass/data/api/SimpleNumassPoint.java index 7269617d..b0dfac77 100644 --- a/numass-core/src/main/java/inr/numass/data/api/SimpleNumassPoint.java +++ b/numass-core/src/main/java/inr/numass/data/api/SimpleNumassPoint.java @@ -4,6 +4,7 @@ import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaBuilder; import hep.dataforge.utils.MetaHolder; +import java.util.Comparator; import java.util.List; import java.util.stream.Stream; @@ -21,11 +22,13 @@ public class SimpleNumassPoint extends MetaHolder implements NumassPoint { */ public SimpleNumassPoint(double voltage, List blocks) { this.blocks = blocks; + blocks.sort(Comparator.comparing(NumassBlock::getStartTime)); super.setMeta(new MetaBuilder("point").setValue(HV_KEY, voltage)); } public SimpleNumassPoint(Meta meta, List blocks) { super(meta); + blocks.sort(Comparator.comparing(NumassBlock::getStartTime)); this.blocks = blocks; } diff --git a/numass-core/src/main/java/inr/numass/data/storage/NumassDataLoader.java b/numass-core/src/main/java/inr/numass/data/storage/NumassDataLoader.java index 4b848d4d..7f4f7832 100644 --- a/numass-core/src/main/java/inr/numass/data/storage/NumassDataLoader.java +++ b/numass-core/src/main/java/inr/numass/data/storage/NumassDataLoader.java @@ -28,6 +28,7 @@ import hep.dataforge.storage.filestorage.FileEnvelope; import hep.dataforge.storage.filestorage.FileStorage; import hep.dataforge.storage.loaders.AbstractLoader; import hep.dataforge.tables.Table; +import hep.dataforge.values.Value; import inr.numass.data.api.NumassPoint; import inr.numass.data.api.NumassSet; import org.slf4j.LoggerFactory; @@ -221,11 +222,7 @@ public class NumassDataLoader extends AbstractLoader implements ObjectLoaderNumassSet.super.getStartTime()); } @Override diff --git a/numass-core/src/main/java/inr/numass/data/storage/ProtoNumassPoint.java b/numass-core/src/main/java/inr/numass/data/storage/ProtoNumassPoint.java index 5b968e27..e5a8f8fb 100644 --- a/numass-core/src/main/java/inr/numass/data/storage/ProtoNumassPoint.java +++ b/numass-core/src/main/java/inr/numass/data/storage/ProtoNumassPoint.java @@ -15,6 +15,7 @@ import java.nio.ByteBuffer; import java.nio.file.Path; import java.time.Duration; import java.time.Instant; +import java.util.Comparator; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -44,9 +45,12 @@ public class ProtoNumassPoint implements NumassPoint { @Override public Stream getBlocks() { - return getPoint().getChannelsList().stream().flatMap(channel -> - channel.getBlocksList().stream().map(block -> new ProtoBlock((int) channel.getNum(), block)) - ); + return getPoint().getChannelsList().stream() + .flatMap(channel -> + channel.getBlocksList().stream() + .map(block -> new ProtoBlock((int) channel.getNum(), block)) + .sorted(Comparator.comparing(ProtoBlock::getStartTime)) + ); } @Override diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 137f7c41..baaac5a0 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -58,6 +58,6 @@ task simulate(dependsOn: classes, type: JavaExec) { task underflow(dependsOn: classes, type: JavaExec) { group "numass" - main 'inr.numass.scripts.Underflow' + main 'inr.numass.scripts.underflow.Underflow' classpath = sourceSets.main.runtimeClasspath } \ No newline at end of file diff --git a/numass-main/src/main/groovy/inr/numass/scripts/underflow/Underflow.groovy b/numass-main/src/main/groovy/inr/numass/scripts/underflow/Underflow.groovy index 6236d855..57c44dd6 100644 --- a/numass-main/src/main/groovy/inr/numass/scripts/underflow/Underflow.groovy +++ b/numass-main/src/main/groovy/inr/numass/scripts/underflow/Underflow.groovy @@ -46,7 +46,7 @@ ctx.pluginManager().load(CachePlugin.class) Meta meta = buildMeta { data(dir: "D:\\Work\\Numass\\data\\2017_05\\Fill_2", mask: "set_.{1,3}") - generate(t0: 3e4, sort: true) + generate(t0: 3e4, sort: false) subtract(reference: 18600) fit(xlow: 450, xHigh: 700, upper: 3100, binning: 20) } @@ -76,13 +76,13 @@ new GrindShell(ctx).eval { def dataBuilder = DataSet.builder(NumassPoint); sets.sort { it.startTime } - .collectMany { it.points.collect() } - .groupBy { it.voltage } - .each { key, value -> - def point = new SimpleNumassPoint(key as double, value as List) - String name = (key as Integer).toString() - dataBuilder.putStatic(name, point, buildMeta(voltage: key)); - } + .collectMany { it.points.collect() } + .groupBy { it.voltage } + .each { key, value -> + def point = new SimpleNumassPoint(key as double, value as List) + String name = (key as Integer).toString() + dataBuilder.putStatic(name, point, buildMeta(voltage: key)); + } DataNode data = dataBuilder.build() @@ -102,8 +102,8 @@ new GrindShell(ctx).eval { spectraMap = spectra .findAll { it.name != referenceVoltage } .collectEntries { - return [(it.meta["voltage"].doubleValue()): NumassDataUtils.subtractSpectrum(it.get(), referencePoint)] - } + [(it.meta["voltage"].doubleValue()): NumassDataUtils.subtractSpectrum(it.get(), referencePoint)] + } } else { spectraMap = spectra.collectEntries { return [(it.meta["voltage"].doubleValue()): it.get()] } }