minor fixes

This commit is contained in:
Alexander Nozik 2018-05-16 13:49:00 +03:00
parent 1742d03b44
commit db378c4158
16 changed files with 55 additions and 57 deletions

View File

@ -48,7 +48,7 @@ abstract class AbstractAnalyzer @JvmOverloads constructor(private val processor:
val loChannel = meta.getInt("window.lo", 0)
val upChannel = meta.getInt("window.up", Integer.MAX_VALUE)
var res = getAllEvents(block).filter { event ->
event.amp.toInt() in loChannel..(upChannel - 1)
event.amplitude.toInt() in loChannel..(upChannel - 1)
}
if (meta.getBoolean("sort", false)) {
res = res.sorted(Comparator.comparing<NumassEvent, Long> { it.timeOffset })

View File

@ -156,7 +156,7 @@ fun getAmplitudeSpectrum(events: Sequence<NumassEvent>, length: Double, config:
//optimized for fastest computation
val spectrum: MutableMap<Int, AtomicLong> = HashMap()
events.forEach { event ->
val channel = event.amp.toInt()
val channel = event.amplitude.toInt()
spectrum.getOrPut(channel) {
AtomicLong(0)
}.incrementAndGet()

View File

@ -49,7 +49,7 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
override fun analyze(block: NumassBlock, config: Meta): Values {
//In case points inside points
if (block is ParentBlock) {
if (block is ParentBlock && config.getBoolean("separateBlocks", false)) {
return analyzeParent(block, config)
}
@ -99,9 +99,9 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
override fun analyzeParent(point: ParentBlock, config: Meta): Values {
//Average count rates, do not sum events
val res = point.blocks.stream()
.filter { it.events.findAny().isPresent }// filter for empty blocks
.map { it -> analyze(it, config) }
.toList().average()
.toList()
.average()
val map = HashMap(res.asMap())
if (point is NumassPoint) {
@ -166,7 +166,7 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
* @param config
* @return
*/
fun getEventsWithDelay(block: NumassBlock, config: Meta): Stream<Pair<NumassEvent, Long>> {
fun getEventsWithDelay(block: NumassBlock, config: Meta): Sequence<Pair<NumassEvent, Long>> {
val inverted = config.getBoolean("inverted", true)
return super.getEvents(block, config).asSequence().zipWithNext { prev, next ->
val delay = Math.max(next.timeOffset - prev.timeOffset, 0)
@ -175,7 +175,7 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
} else {
Pair(prev, delay)
}
}.asStream()
}
}
/**
@ -187,7 +187,7 @@ class TimeAnalyzer @JvmOverloads constructor(private val processor: SignalProces
*/
override fun getEvents(block: NumassBlock, meta: Meta): Stream<NumassEvent> {
val t0 = getT0(block, meta).toLong()
return getEventsWithDelay(block, meta).filter { pair -> pair.second >= t0 }.map { it.first }
return getEventsWithDelay(block, meta).filter { pair -> pair.second >= t0 }.asStream().map { it.first }
}
public override fun getTableFormat(config: Meta): TableFormat {

View File

@ -2,24 +2,17 @@ package inr.numass.data.api
import java.time.Duration
import java.time.Instant
import java.util.*
import java.util.stream.Stream
interface ParentBlock: NumassBlock{
val blocks: Collection<NumassBlock>
interface ParentBlock : NumassBlock {
val blocks: List<NumassBlock>
}
/**
* A block constructed from a set of other blocks. Internal blocks are not necessary subsequent. Blocks are automatically sorted.
* Created by darksnake on 16.07.2017.
*/
class MetaBlock(blocks: Collection<NumassBlock>) : ParentBlock {
override val blocks = TreeSet(Comparator.comparing<NumassBlock, Instant>{ it.startTime })
init{
this.blocks.addAll(blocks)
}
class MetaBlock(override val blocks: List<NumassBlock>) : ParentBlock {
override val startTime: Instant
get() = blocks.first().startTime
@ -28,14 +21,10 @@ class MetaBlock(blocks: Collection<NumassBlock>) : ParentBlock {
get() = Duration.ofNanos(blocks.stream().mapToLong { block -> block.length.toNanos() }.sum())
override val events: Stream<NumassEvent>
get() = blocks.stream()
.sorted(Comparator.comparing<NumassBlock, Instant>{ it.startTime })
.flatMap{ it.events }
get() = blocks.sortedBy { it.startTime }.stream().flatMap { it.events }
override val frames: Stream<NumassFrame>
get() = blocks.stream()
.sorted(Comparator.comparing<NumassBlock, Instant>{ it.startTime })
.flatMap{ it.frames }
get() = blocks.sortedBy { it.startTime }.stream().flatMap { it.frames }
}

View File

@ -7,6 +7,14 @@ import java.time.Duration
import java.time.Instant
import java.util.stream.Stream
open class OrphanNumassEvent(val amplitude: Short, val timeOffset: Long) : Serializable, Comparable<OrphanNumassEvent> {
operator fun component1() = amplitude
operator fun component2() = timeOffset
override fun compareTo(other: OrphanNumassEvent): Int {
return this.timeOffset.compareTo(other.timeOffset)
}
}
/**
* A single numass event with given amplitude and time.
@ -17,7 +25,7 @@ import java.util.stream.Stream
* @property owner an owner block for this event
*
*/
class NumassEvent(val amp: Short, val timeOffset: Long, val owner: NumassBlock) : Serializable {
class NumassEvent(amplitude: Short, timeOffset: Long, val owner: NumassBlock) : OrphanNumassEvent(amplitude, timeOffset), Serializable {
val channel: Int
get() = owner.channel
@ -57,20 +65,10 @@ interface NumassBlock {
val frames: Stream<NumassFrame>
}
typealias OrphanNumassEvent = Pair<Short, Long>
fun OrphanNumassEvent.adopt(parent: NumassBlock): NumassEvent {
return NumassEvent(this.first, this.second, parent)
return NumassEvent(this.amplitude, this.timeOffset, parent)
}
val OrphanNumassEvent.timeOffset: Long
get() = this.second
val OrphanNumassEvent.amp: Short
get() = this.first
/**
* A simple in-memory implementation of block of events. No frames are allowed
* Created by darksnake on 08.07.2017.
@ -80,7 +78,7 @@ class SimpleBlock(
override val length: Duration,
producer: suspend (NumassBlock) -> Iterable<NumassEvent>) : NumassBlock, Serializable {
private val eventList = runBlocking { producer(this@SimpleBlock).toList()}
private val eventList = runBlocking { producer(this@SimpleBlock).toList() }
override val frames: Stream<NumassFrame> get() = Stream.empty()

View File

@ -125,7 +125,7 @@ constructor(override val name: String, private val path: Path, meta: Meta) : Num
length = (length * 20).toShort()
}
val events = ArrayList<Pair<Short, Long>>()
val events = ArrayList<OrphanNumassEvent>()
var lab = readBlock(channel, 1).get().toInt()
while (lab == 0xBF) {
@ -216,7 +216,7 @@ constructor(override val name: String, private val path: Path, meta: Meta) : Num
else -> throw IOException("Event head expected")
}
return Pair(chanel, (time / timeDiv).toLong())
return OrphanNumassEvent(chanel, (time / timeDiv).toLong())
}
@Throws(IOException::class)

View File

@ -15,7 +15,7 @@ import java.nio.file.Path
import java.time.Duration
import java.time.Instant
import java.util.stream.Stream
import java.util.stream.StreamSupport
import kotlin.streams.asStream
/**
* Created by darksnake on 08.07.2017.
@ -51,7 +51,7 @@ class ClassicNumassPoint(private val envelope: Envelope) : NumassPoint {
override val length: Duration) : NumassBlock, Iterable<NumassEvent> {
override val events: Stream<NumassEvent>
get() = StreamSupport.stream(this.spliterator(), false)
get() = this.asSequence().asStream()
override fun iterator(): Iterator<NumassEvent> {
val timeCoef = envelope.meta.getDouble("time_coeff", 50.0)

View File

@ -21,9 +21,9 @@ class PointAnalyzer {
static Histogram histogram(NumassBlock point, int loChannel = 0, int upChannel = 10000, double binSize = 0.5, int binNum = 500) {
return UnivariateHistogram.buildUniform(0d, binSize * binNum, binSize)
.fill(
analyzer
.getEventsWithDelay(point, Grind.buildMeta("window.lo": loChannel, "window.up": upChannel))
.mapToDouble { it.second / 1000 as double }
kotlin.streams.jdk8.StreamsKt.asStream(analyzer.getEventsWithDelay(point, Grind.buildMeta("window.lo": loChannel, "window.up": upChannel))).mapToDouble {
it.second / 1000 as double
}
)
}

View File

@ -16,6 +16,7 @@ import inr.numass.data.analyzers.NumassAnalyzer
import inr.numass.data.analyzers.TimeAnalyzer
import inr.numass.data.analyzers.TimeAnalyzer.Companion.T0_KEY
import inr.numass.data.api.NumassPoint
import kotlin.streams.asStream
/**
* Plot time analysis graphics
@ -52,6 +53,7 @@ class TimeAnalyzerAction : OneToOneAction<NumassPoint, Table>() {
val histogram = UnivariateHistogram.buildUniform(0.0, binSize * binNum, binSize)
.fill(analyzer
.getEventsWithDelay(input, inputMeta)
.asStream()
.mapToDouble { it.second / 1000.0 }
).asTable()

View File

@ -15,6 +15,7 @@ import hep.dataforge.values.ValueType
import inr.numass.data.analyzers.NumassAnalyzer
import inr.numass.data.analyzers.TimeAnalyzer
import inr.numass.data.api.NumassPoint
import kotlin.streams.asStream
/**
* Plot time analysis graphics
@ -60,6 +61,7 @@ class TimeSpectrumAction : OneToOneAction<NumassPoint, Table>() {
val histogram = UnivariateHistogram.buildUniform(0.0, binSize * binNum, binSize)
.fill(analyzer
.getEventsWithDelay(input, inputMeta)
.asStream()
.mapToDouble { it.second / 1000.0 }
).asTable()

View File

@ -22,7 +22,10 @@
package inr.numass.data
import hep.dataforge.maths.chain.Chain
import inr.numass.data.api.*
import inr.numass.data.api.NumassBlock
import inr.numass.data.api.OrphanNumassEvent
import inr.numass.data.api.SimpleBlock
import inr.numass.data.api.adopt
import kotlinx.coroutines.experimental.runBlocking
import org.apache.commons.math3.random.RandomGenerator
import java.lang.Math.max
@ -30,6 +33,7 @@ import java.time.Duration
import java.time.Instant
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
/**
* @author [Alexander Nozik](mailto:altavir@gmail.com)
@ -121,7 +125,9 @@ class PileUpSimulator {
@Synchronized
fun generate() {
var next: OrphanNumassEvent
var lastRegisteredTime = 0.0 // Time of DAQ closing
//var lastRegisteredTime = 0.0 // Time of DAQ closing
val last = AtomicReference<OrphanNumassEvent>(OrphanNumassEvent(0,0))
//flag that shows that previous event was pileup
var pileupFlag = false
runBlocking {
@ -130,11 +136,11 @@ class PileUpSimulator {
generated.add(next)
//not counting double pileups
if (generated.size > 1) {
val delay = (next.timeOffset - lastRegisteredTime) / us //time between events in microseconds
if (nextEventRegistered(next.amp, delay)) {
val delay = (next.timeOffset - last.get().timeOffset) / us //time between events in microseconds
if (nextEventRegistered(next.amplitude, delay)) {
//just register new event
registered.add(next)
lastRegisteredTime = next.timeOffset.toDouble()
last.set(next)
pileupFlag = false
} else if (pileup(delay)) {
if (pileupFlag) {
@ -142,7 +148,7 @@ class PileUpSimulator {
doublePileup.incrementAndGet()
} else {
//pileup event
val newChannel = pileupChannel(delay, next.amp, next.amp)
val newChannel = pileupChannel(delay, last.get().amplitude, next.amplitude)
val newEvent = OrphanNumassEvent(newChannel, next.timeOffset)
//replace already registered event by event with new channel
registered.removeAt(registered.size - 1)
@ -158,7 +164,7 @@ class PileUpSimulator {
} else {
//register first event
registered.add(next)
lastRegisteredTime = next.timeOffset.toDouble()
last.set(next)
}
next = generator.next()
}

View File

@ -32,7 +32,7 @@ private fun correlation(sequence: Stream<NumassEvent>): Double {
val amplitudes: MutableList<Double> = ArrayList()
val times: MutableList<Double> = ArrayList()
sequence.forEach {
amplitudes.add(it.amp.toDouble())
amplitudes.add(it.amplitude.toDouble())
times.add(it.timeOffset.toDouble())
}

View File

@ -19,13 +19,15 @@ fun main(args: Array<String>) {
dataDir = "D:\\Work\\Numass\\data\\2018_04"
}
val storage = NumassStorageFactory.buildLocal(context, "Fill_3", true, false);
val storage = NumassStorageFactory.buildLocal(context, "Fill_2", true, false);
val meta = buildMeta {
"t0" to 3000
"binNum" to 200
"t0Step" to 100
"chunkSize" to 3000
"sort" to true
// "separateBlocks" to true
node("window") {
"lo" to 0
"up" to 4000

View File

@ -7,7 +7,6 @@ import inr.numass.NumassPlugin
import inr.numass.actions.TimeAnalyzerAction
import inr.numass.data.api.OrphanNumassEvent
import inr.numass.data.api.SimpleNumassPoint
import inr.numass.data.api.timeOffset
import inr.numass.data.generateBlock
import org.apache.commons.math3.random.JDKRandomGenerator
import org.apache.commons.math3.random.RandomGenerator

View File

@ -31,7 +31,7 @@ fun main(args: Array<String>) {
point.transformChain { first, second ->
val dt = second.timeOffset - first.timeOffset
if (second.channel == 4 && first.channel == 0 && dt > window && dt < 1000) {
Pair((first.amp + second.amp).toShort(), second.timeOffset)
Pair((first.amplitude + second.amplitude).toShort(), second.timeOffset)
} else {
null
}
@ -48,7 +48,7 @@ fun main(args: Array<String>) {
point.transformChain { first, second ->
val dt = second.timeOffset - first.timeOffset
if (second.channel == 0 && first.channel == 4 && dt > window && dt < 1000) {
Pair((first.amp + second.amp).toShort(), second.timeOffset)
Pair((first.amplitude + second.amplitude).toShort(), second.timeOffset)
} else {
null
}

View File

@ -37,7 +37,7 @@ fun main(args: Array<String>) {
val point: NumassPoint = set.points.first { it.index == 18 }
(0..99).forEach { bin ->
val times = point.events.filter { it.amp > 0 }.map { it.timeOffset }.toList()
val times = point.events.filter { it.amplitude > 0 }.map { it.timeOffset }.toList()
val count = times.filter { it > bin.toDouble() / 10 * 1e9 && it < (bin + 1).toDouble() / 10 * 1e9 }.count()
println("${bin.toDouble() / 10.0}: $count")
}