diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 8492c8cd..462a27c2 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -16,8 +16,8 @@ dependencies { compile group: 'commons-io', name: 'commons-io', version:'2.+' compile project(':dataforge-fitting:dataforge-minuit') compile project(':dataforge-fx') - compile project(':dataforge-grind') compile project(':dataforge-plots') + compile project(':numass-storage') } task runNumass(dependsOn: classes, type : JavaExec){ diff --git a/numass-main/gradle.properties b/numass-main/gradle.properties deleted file mode 100644 index 824aba27..00000000 --- a/numass-main/gradle.properties +++ /dev/null @@ -1,15 +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. -# diff --git a/numass-main/src/main/java/inr/numass/NumassIO.java b/numass-main/src/main/java/inr/numass/NumassIO.java index 4c75946b..3593594a 100644 --- a/numass-main/src/main/java/inr/numass/NumassIO.java +++ b/numass-main/src/main/java/inr/numass/NumassIO.java @@ -20,9 +20,9 @@ import hep.dataforge.data.binary.Binary; import hep.dataforge.io.BasicIOManager; import hep.dataforge.meta.Meta; import hep.dataforge.names.Name; -import inr.numass.data.NumassDataReader; +import inr.numass.storage.NumassDataReader; import inr.numass.data.NumassPawReader; -import inr.numass.data.RawNMFile; +import inr.numass.storage.RawNMFile; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; diff --git a/numass-main/src/main/java/inr/numass/WorkspaceTest.java b/numass-main/src/main/java/inr/numass/WorkspaceTest.java new file mode 100644 index 00000000..5ad1c17c --- /dev/null +++ b/numass-main/src/main/java/inr/numass/WorkspaceTest.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 hep.dataforge.meta.MetaBuilder; +import hep.dataforge.storage.commons.StorageDataFactory; +import hep.dataforge.workspace.BasicWorkspace; +import hep.dataforge.workspace.Workspace; + +/** + * + * @author Alexander Nozik + */ +public class WorkspaceTest { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + String storagepath = "D:\\Work\\Numass\\data\\"; + Workspace workspace = BasicWorkspace.builder() + .setContext(new NumassContext()) + .loadData("", new StorageDataFactory(), new MetaBuilder("storage").putValue("path", storagepath)) + + .build(); + } + +} diff --git a/numass-main/src/main/java/inr/numass/actions/BorderData.java b/numass-main/src/main/java/inr/numass/actions/BorderData.java index de6e349a..e274a194 100644 --- a/numass-main/src/main/java/inr/numass/actions/BorderData.java +++ b/numass-main/src/main/java/inr/numass/actions/BorderData.java @@ -18,8 +18,8 @@ package inr.numass.actions; import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.SimplePointSource; import hep.dataforge.values.Value; -import inr.numass.data.NMFile; -import inr.numass.data.NMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.NMPoint; import java.util.HashMap; import java.util.Map; diff --git a/numass-main/src/main/java/inr/numass/actions/DebunchAction.java b/numass-main/src/main/java/inr/numass/actions/DebunchAction.java index e07cada0..d111d13b 100644 --- a/numass-main/src/main/java/inr/numass/actions/DebunchAction.java +++ b/numass-main/src/main/java/inr/numass/actions/DebunchAction.java @@ -22,8 +22,8 @@ import hep.dataforge.description.ValueDef; import hep.dataforge.exceptions.ContentException; import hep.dataforge.io.reports.Reportable; import hep.dataforge.meta.Laminate; -import inr.numass.data.RawNMFile; -import inr.numass.data.RawNMPoint; +import inr.numass.storage.RawNMFile; +import inr.numass.storage.RawNMPoint; import inr.numass.debunch.DebunchReport; import inr.numass.debunch.FrameAnalizer; import java.io.PrintWriter; diff --git a/numass-main/src/main/java/inr/numass/actions/FindBorderAction.java b/numass-main/src/main/java/inr/numass/actions/FindBorderAction.java index a51ed141..dd7c628e 100644 --- a/numass-main/src/main/java/inr/numass/actions/FindBorderAction.java +++ b/numass-main/src/main/java/inr/numass/actions/FindBorderAction.java @@ -22,8 +22,8 @@ import hep.dataforge.exceptions.ContentException; import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.io.reports.Reportable; import hep.dataforge.meta.Laminate; -import inr.numass.data.NMFile; -import inr.numass.data.NMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.NMPoint; import java.io.OutputStream; /** 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 72518560..cfd35a73 100644 --- a/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java @@ -30,9 +30,9 @@ import hep.dataforge.tables.ListTable; import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.Table; import hep.dataforge.tables.TableFormat; -import inr.numass.data.NMFile; -import inr.numass.data.NMPoint; -import inr.numass.data.RawNMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.NMPoint; +import inr.numass.storage.RawNMPoint; import java.io.OutputStream; import java.time.Instant; import java.util.ArrayList; diff --git a/numass-main/src/main/java/inr/numass/actions/ReadNumassDataAction.java b/numass-main/src/main/java/inr/numass/actions/ReadNumassDataAction.java index 0e5847b5..28e2f529 100644 --- a/numass-main/src/main/java/inr/numass/actions/ReadNumassDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/ReadNumassDataAction.java @@ -25,8 +25,8 @@ import hep.dataforge.exceptions.ContentException; import hep.dataforge.io.reports.Reportable; import hep.dataforge.meta.Laminate; import static inr.numass.NumassIO.getNumassData; -import inr.numass.data.NMFile; -import inr.numass.data.RawNMFile; +import inr.numass.storage.NMFile; +import inr.numass.storage.RawNMFile; /** * diff --git a/numass-main/src/main/java/inr/numass/actions/ShowSpectrumAction.java b/numass-main/src/main/java/inr/numass/actions/ShowSpectrumAction.java index df9724fc..330324d6 100644 --- a/numass-main/src/main/java/inr/numass/actions/ShowSpectrumAction.java +++ b/numass-main/src/main/java/inr/numass/actions/ShowSpectrumAction.java @@ -26,8 +26,8 @@ import hep.dataforge.meta.Meta; import hep.dataforge.plots.fx.FXPlotUtils; import hep.dataforge.plots.jfreechart.JFreeChartFrame; import inr.numass.data.ESpectrum; -import inr.numass.data.NMFile; -import inr.numass.data.NMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.NMPoint; import java.awt.Color; import java.io.OutputStream; import java.util.ArrayList; diff --git a/numass-main/src/main/java/inr/numass/actions/SlicedData.java b/numass-main/src/main/java/inr/numass/actions/SlicedData.java index 695746aa..9a2fcb1a 100644 --- a/numass-main/src/main/java/inr/numass/actions/SlicedData.java +++ b/numass-main/src/main/java/inr/numass/actions/SlicedData.java @@ -19,8 +19,8 @@ import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.SimplePointSource; import hep.dataforge.tables.TableFormat; import hep.dataforge.values.Value; -import inr.numass.data.NMFile; -import inr.numass.data.NMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.NMPoint; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; diff --git a/numass-main/src/main/java/inr/numass/actions/SlicingAction.java b/numass-main/src/main/java/inr/numass/actions/SlicingAction.java index 7a14425d..7048f0af 100644 --- a/numass-main/src/main/java/inr/numass/actions/SlicingAction.java +++ b/numass-main/src/main/java/inr/numass/actions/SlicingAction.java @@ -23,8 +23,8 @@ import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.io.reports.Reportable; import hep.dataforge.meta.Laminate; import hep.dataforge.meta.Meta; -import inr.numass.data.NMFile; -import inr.numass.data.RawNMPoint; +import inr.numass.storage.NMFile; +import inr.numass.storage.RawNMPoint; import java.io.OutputStream; import java.util.LinkedHashMap; import java.util.List; diff --git a/numass-main/src/main/java/inr/numass/data/ESpectrum.java b/numass-main/src/main/java/inr/numass/data/ESpectrum.java index 17da5bcb..04d66d2f 100644 --- a/numass-main/src/main/java/inr/numass/data/ESpectrum.java +++ b/numass-main/src/main/java/inr/numass/data/ESpectrum.java @@ -15,6 +15,7 @@ */ package inr.numass.data; +import inr.numass.storage.NMPoint; import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.SimplePointSource; diff --git a/numass-main/src/main/java/inr/numass/data/NumassPawReader.java b/numass-main/src/main/java/inr/numass/data/NumassPawReader.java index c7b7812f..4c2b00e3 100644 --- a/numass-main/src/main/java/inr/numass/data/NumassPawReader.java +++ b/numass-main/src/main/java/inr/numass/data/NumassPawReader.java @@ -15,6 +15,7 @@ */ package inr.numass.data; +import inr.numass.storage.RawNMFile; import hep.dataforge.data.binary.Binary; import java.io.FileInputStream; import java.io.FileNotFoundException; diff --git a/numass-main/src/main/java/inr/numass/debunch/DebunchData.java b/numass-main/src/main/java/inr/numass/debunch/DebunchData.java index 9f260b88..b50a745e 100644 --- a/numass-main/src/main/java/inr/numass/debunch/DebunchData.java +++ b/numass-main/src/main/java/inr/numass/debunch/DebunchData.java @@ -13,195 +13,195 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Хранит сортированный набор событий с возможностью вырезать куски и склеивать - * концы - * - * @author Darksnake - */ -class DebunchData { - - /** - * Удаляет из листа события в определенном диапазоне времени. При этом общее - * время не изменяется, поэтому скорость счета меняется. Возвращает - * количество удаленных событий. - * - * @param from - * @param to - * @return - */ - private static List removeFrame(List events, Frame frame) { - List res = new ArrayList<>(); - for (NMEvent event : events) { - if (event.getTime() >= frame.getEnd()) { - res.add(new NMEvent(event.getChanel(), event.getTime() - frame.length())); - } else if (event.getTime() <= frame.getBegin()) { - res.add(event); - } - } - return res; - } - - private final List bunches = new ArrayList<>(); - private final List events; - private final double length; - - public DebunchData(RawNMPoint point) { - events = point.getEvents(); - Collections.sort(events, new EventComparator()); - length = point.getLength(); - } - - public Frame countInFrame(double start, double length, int lowerChanel, int upperChanel) { - double end; - - if (start + length < this.getLength()) { - end = start + length; - } else { - end = this.getLength(); - } - - ArrayList sum = new ArrayList<>(); - - int i = 0; - while ((i < this.size()) && (events.get(i).getTime() < start)) { - i++; - } - while ((i < this.size()) && (events.get(i).getTime() < end)) { - if ((events.get(i).getChanel() >= lowerChanel) && (events.get(i).getChanel() <= upperChanel)) { - sum.add(getEvents().get(i)); - } - - i++; - } - - return new Frame(start, end, sum); - - } - - /** - * Same as CountInFrame, but it does not copy all of the event times, only - * total count in frame. - * - * @param start - * @param length - * @return - */ - public Frame countInFrameFast(double start, double length, int lowerChanel, int upperChanel) { - //PENDING самый долгий метод - if (start > this.getLength()) { - throw new IllegalArgumentException(); - } - - double end; - - if (start + length < this.getLength()) { - end = start + length; - } else { - end = this.getLength(); - } - - int sumCount = 0; - - int i = 0; - while ((i < this.size()) && (events.get(i).getTime() < start)) { - i++; - } - while ((i < this.size()) && (events.get(i).getTime() < end)) { - if ((events.get(i).getChanel() >= lowerChanel) && (events.get(i).getChanel() <= upperChanel)) { - sumCount++; - } - i++; - } - - return new Frame(start, end, sumCount); - - } - - public List getBunches() { - return bunches; - } - - /** - * Возвращает скорректированную скорость счета по всему интервалу - * - * @return - */ - public double getCountRate() { - return this.size() / this.getLength(); - } - - /** - * Медленный метод, вызывать минимальное количество рах - * - * @return - */ - public List getDebunchedEvents() { - List res = getEvents(); - for (Frame frame : getBunches()) { - res = removeFrame(res, frame); - } - return res; - } - - /** - * Медленный метод, вызывать минимальное количество рах - * - * @return - */ - public double getDebunchedLength() { - double res = length; - for (Frame frame : getBunches()) { - res -= frame.length(); - } - if (res > 0) { - return res; - } else { - throw new RuntimeException("Zero length point after debunching"); - } - } - - public List getEvents() { - return events; - } - - /** - * @return the length - */ - public double getLength() { - return length; - } - - public void setAsBunch(Frame bunch) { - //FIXME сделать проверку пересечения кадров - this.bunches.add(bunch); - } - - public void setAsBunch(double from, double to) { - assert to > from; - setAsBunch(countInFrame(from, to - from,0, RawNMPoint.MAX_CHANEL)); - } - - public long size() { - return this.getEvents().size(); - } - - private static class EventComparator implements Comparator { - - @Override - public int compare(NMEvent o1, NMEvent o2) { - return (int) Math.signum(o1.getTime() - o2.getTime()); - } - - } - -} +package inr.numass.debunch; + +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * Хранит сортированный набор событий с возможностью вырезать куски и склеивать + * концы + * + * @author Darksnake + */ +class DebunchData { + + /** + * Удаляет из листа события в определенном диапазоне времени. При этом общее + * время не изменяется, поэтому скорость счета меняется. Возвращает + * количество удаленных событий. + * + * @param from + * @param to + * @return + */ + private static List removeFrame(List events, Frame frame) { + List res = new ArrayList<>(); + for (NMEvent event : events) { + if (event.getTime() >= frame.getEnd()) { + res.add(new NMEvent(event.getChanel(), event.getTime() - frame.length())); + } else if (event.getTime() <= frame.getBegin()) { + res.add(event); + } + } + return res; + } + + private final List bunches = new ArrayList<>(); + private final List events; + private final double length; + + public DebunchData(RawNMPoint point) { + events = point.getEvents(); + Collections.sort(events, new EventComparator()); + length = point.getLength(); + } + + public Frame countInFrame(double start, double length, int lowerChanel, int upperChanel) { + double end; + + if (start + length < this.getLength()) { + end = start + length; + } else { + end = this.getLength(); + } + + ArrayList sum = new ArrayList<>(); + + int i = 0; + while ((i < this.size()) && (events.get(i).getTime() < start)) { + i++; + } + while ((i < this.size()) && (events.get(i).getTime() < end)) { + if ((events.get(i).getChanel() >= lowerChanel) && (events.get(i).getChanel() <= upperChanel)) { + sum.add(getEvents().get(i)); + } + + i++; + } + + return new Frame(start, end, sum); + + } + + /** + * Same as CountInFrame, but it does not copy all of the event times, only + * total count in frame. + * + * @param start + * @param length + * @return + */ + public Frame countInFrameFast(double start, double length, int lowerChanel, int upperChanel) { + //PENDING самый долгий метод + if (start > this.getLength()) { + throw new IllegalArgumentException(); + } + + double end; + + if (start + length < this.getLength()) { + end = start + length; + } else { + end = this.getLength(); + } + + int sumCount = 0; + + int i = 0; + while ((i < this.size()) && (events.get(i).getTime() < start)) { + i++; + } + while ((i < this.size()) && (events.get(i).getTime() < end)) { + if ((events.get(i).getChanel() >= lowerChanel) && (events.get(i).getChanel() <= upperChanel)) { + sumCount++; + } + i++; + } + + return new Frame(start, end, sumCount); + + } + + public List getBunches() { + return bunches; + } + + /** + * Возвращает скорректированную скорость счета по всему интервалу + * + * @return + */ + public double getCountRate() { + return this.size() / this.getLength(); + } + + /** + * Медленный метод, вызывать минимальное количество рах + * + * @return + */ + public List getDebunchedEvents() { + List res = getEvents(); + for (Frame frame : getBunches()) { + res = removeFrame(res, frame); + } + return res; + } + + /** + * Медленный метод, вызывать минимальное количество рах + * + * @return + */ + public double getDebunchedLength() { + double res = length; + for (Frame frame : getBunches()) { + res -= frame.length(); + } + if (res > 0) { + return res; + } else { + throw new RuntimeException("Zero length point after debunching"); + } + } + + public List getEvents() { + return events; + } + + /** + * @return the length + */ + public double getLength() { + return length; + } + + public void setAsBunch(Frame bunch) { + //FIXME сделать проверку пересечения кадров + this.bunches.add(bunch); + } + + public void setAsBunch(double from, double to) { + assert to > from; + setAsBunch(countInFrame(from, to - from,0, RawNMPoint.MAX_CHANEL)); + } + + public long size() { + return this.getEvents().size(); + } + + private static class EventComparator implements Comparator { + + @Override + public int compare(NMEvent o1, NMEvent o2) { + return (int) Math.signum(o1.getTime() - o2.getTime()); + } + + } + +} diff --git a/numass-main/src/main/java/inr/numass/debunch/DebunchEvent.java b/numass-main/src/main/java/inr/numass/debunch/DebunchEvent.java index 080b477c..b9f4e54b 100644 --- a/numass-main/src/main/java/inr/numass/debunch/DebunchEvent.java +++ b/numass-main/src/main/java/inr/numass/debunch/DebunchEvent.java @@ -13,66 +13,66 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.NMEvent; - -/** - * - * @author Darksnake - */ -public class DebunchEvent extends NMEvent { - - public static double getEventWeight(NMEvent event) { - if (event instanceof DebunchEvent) { - return ((DebunchEvent) event).getWeight(); - } else { - return 1; - } - - } - - private double shift = 0; - /** - * В общем случае принимает значение от 0 (событие полностью выкинуто) до - * 1(событие полностью принято) - */ - private double weight; - - public DebunchEvent(NMEvent event, double weight) { - super(event.getChanel(), event.getTime()); - this.weight = weight; - } - - protected DebunchEvent(double weight, double shift, short chanel, double time) { - super(chanel, time); - this.weight = weight; - this.shift = shift; - } - - @Override - public DebunchEvent clone() { - return new DebunchEvent(weight, shift, chanel, time); - } - - @Override - public double getTime() { - return super.getTime() + shift; - } - - public double getWeight() { - return this.weight; - } - - /** - * @param marker - */ - public void setWeight(int marker){ - this.weight = marker; - } - - public void shiftTime(double shift) { - this.shift += shift; - } - -} +package inr.numass.debunch; + +import inr.numass.storage.NMEvent; + +/** + * + * @author Darksnake + */ +public class DebunchEvent extends NMEvent { + + public static double getEventWeight(NMEvent event) { + if (event instanceof DebunchEvent) { + return ((DebunchEvent) event).getWeight(); + } else { + return 1; + } + + } + + private double shift = 0; + /** + * В общем случае принимает значение от 0 (событие полностью выкинуто) до + * 1(событие полностью принято) + */ + private double weight; + + public DebunchEvent(NMEvent event, double weight) { + super(event.getChanel(), event.getTime()); + this.weight = weight; + } + + protected DebunchEvent(double weight, double shift, short chanel, double time) { + super(chanel, time); + this.weight = weight; + this.shift = shift; + } + + @Override + public DebunchEvent clone() { + return new DebunchEvent(weight, shift, chanel, time); + } + + @Override + public double getTime() { + return super.getTime() + shift; + } + + public double getWeight() { + return this.weight; + } + + /** + * @param marker + */ + public void setWeight(int marker){ + this.weight = marker; + } + + public void shiftTime(double shift) { + this.shift += shift; + } + +} diff --git a/numass-main/src/main/java/inr/numass/debunch/DebunchReport.java b/numass-main/src/main/java/inr/numass/debunch/DebunchReport.java index 3deb78b0..f0445ad4 100644 --- a/numass-main/src/main/java/inr/numass/debunch/DebunchReport.java +++ b/numass-main/src/main/java/inr/numass/debunch/DebunchReport.java @@ -13,23 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; -import java.util.List; - -/** - * - * @author Darksnake - */ -public interface DebunchReport { - RawNMPoint getInitialPoint(); - RawNMPoint getPoint(); - - List getBunches(); - List getBunchEvents(); - - double eventsFiltred(); - double timeFiltred(); -} +package inr.numass.debunch; + +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; +import java.util.List; + +/** + * + * @author Darksnake + */ +public interface DebunchReport { + RawNMPoint getInitialPoint(); + RawNMPoint getPoint(); + + List getBunches(); + List getBunchEvents(); + + double eventsFiltred(); + double timeFiltred(); +} diff --git a/numass-main/src/main/java/inr/numass/debunch/DebunchReportImpl.java b/numass-main/src/main/java/inr/numass/debunch/DebunchReportImpl.java index 326f1d66..57e00c6b 100644 --- a/numass-main/src/main/java/inr/numass/debunch/DebunchReportImpl.java +++ b/numass-main/src/main/java/inr/numass/debunch/DebunchReportImpl.java @@ -15,8 +15,8 @@ */ package inr.numass.debunch; -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; import java.util.ArrayList; import java.util.List; diff --git a/numass-main/src/main/java/inr/numass/debunch/Debuncher.java b/numass-main/src/main/java/inr/numass/debunch/Debuncher.java index cad39a7f..6f198fd0 100644 --- a/numass-main/src/main/java/inr/numass/debunch/Debuncher.java +++ b/numass-main/src/main/java/inr/numass/debunch/Debuncher.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.RawNMPoint; - -/** - * - * @author Darksnake - */ -public interface Debuncher { - DebunchReport debunchPoint(RawNMPoint point); -} +package inr.numass.debunch; + +import inr.numass.storage.RawNMPoint; + +/** + * + * @author Darksnake + */ +public interface Debuncher { + DebunchReport debunchPoint(RawNMPoint point); +} diff --git a/numass-main/src/main/java/inr/numass/debunch/Frame.java b/numass-main/src/main/java/inr/numass/debunch/Frame.java index d7f72d61..008c3dc8 100644 --- a/numass-main/src/main/java/inr/numass/debunch/Frame.java +++ b/numass-main/src/main/java/inr/numass/debunch/Frame.java @@ -13,89 +13,89 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.NMEvent; -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.math3.distribution.PoissonDistribution; - -/** - * - * @author Darksnake - */ -public class Frame { - - private final double begin; - private final double end; - private List events; - private final int eventsCount; - - public Frame(double begin, double end, List events) { - assert end > begin; - this.begin = begin; - this.end = end; - this.events = events; - this.eventsCount = events.size(); - } - - /** - * Сокращенная версия для экономии памяти - * - * @param begin - * @param end - * @param count - */ - public Frame(double begin, double end, int count) { - assert end > begin; - this.begin = begin; - this.end = end; - this.eventsCount = count; - } - - public Frame cloneFast() { - return new Frame(begin, end, eventsCount); - } - - public double getBegin() { - return begin; - } - - public int getCount() { - if (this.events != null) { - return events.size(); - } else { - return eventsCount; - } - } - - public double getCountRate(){ - return this.getCount() / this.length(); - } - - public double getCountRateError(){ - return Math.sqrt(this.getCount()) / this.length(); - } - - public double getEnd() { - return end; - } - - public List getEvents() { - if(events!=null) - return events; - else - return new ArrayList<>(); - } - - public double getProbability(double cr){ - PoissonDistribution distr = new PoissonDistribution(cr * this.length()); - return distr.probability(getCount()); - } - - public double length() { - assert this.end > this.begin; - return this.end - this.begin; - } - -} +package inr.numass.debunch; + +import inr.numass.storage.NMEvent; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.math3.distribution.PoissonDistribution; + +/** + * + * @author Darksnake + */ +public class Frame { + + private final double begin; + private final double end; + private List events; + private final int eventsCount; + + public Frame(double begin, double end, List events) { + assert end > begin; + this.begin = begin; + this.end = end; + this.events = events; + this.eventsCount = events.size(); + } + + /** + * Сокращенная версия для экономии памяти + * + * @param begin + * @param end + * @param count + */ + public Frame(double begin, double end, int count) { + assert end > begin; + this.begin = begin; + this.end = end; + this.eventsCount = count; + } + + public Frame cloneFast() { + return new Frame(begin, end, eventsCount); + } + + public double getBegin() { + return begin; + } + + public int getCount() { + if (this.events != null) { + return events.size(); + } else { + return eventsCount; + } + } + + public double getCountRate(){ + return this.getCount() / this.length(); + } + + public double getCountRateError(){ + return Math.sqrt(this.getCount()) / this.length(); + } + + public double getEnd() { + return end; + } + + public List getEvents() { + if(events!=null) + return events; + else + return new ArrayList<>(); + } + + public double getProbability(double cr){ + PoissonDistribution distr = new PoissonDistribution(cr * this.length()); + return distr.probability(getCount()); + } + + public double length() { + assert this.end > this.begin; + return this.end - this.begin; + } + +} diff --git a/numass-main/src/main/java/inr/numass/debunch/FrameAnalizer.java b/numass-main/src/main/java/inr/numass/debunch/FrameAnalizer.java index 5c091ba7..5232ec86 100644 --- a/numass-main/src/main/java/inr/numass/debunch/FrameAnalizer.java +++ b/numass-main/src/main/java/inr/numass/debunch/FrameAnalizer.java @@ -13,230 +13,230 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.debunch; - -import inr.numass.data.RawNMPoint; -import org.apache.commons.math3.analysis.UnivariateFunction; -import org.apache.commons.math3.analysis.interpolation.LinearInterpolator; -import org.apache.commons.math3.util.FastMath; - -/** - * - * @author Darksnake - */ -public class FrameAnalizer implements Debuncher { - - private static final double POISSON_THRESHOLD = 1e-80; - - static double getGaussianCRThreshold(double cr, double frameLength, double prob) { - double[] xs = {9, 8, 7, 6, 5, 4, 3, 2, 1}; - double[] probs = {1.15e-19, 6.22e-16, 1.27e-12, 9.86e-10, 2.86e-7, 3.167e-5, 0.001349, 0.0227, 0.1586}; - LinearInterpolator interpolator = new LinearInterpolator(); - UnivariateFunction function = interpolator.interpolate(probs, xs); - double sigmas = function.value(prob); - return cr + sigmas * Math.sqrt(cr / frameLength); - } - //double frameShift; - double frameLength; - int lowerChanel = 0; - - int numCircles = 1; - double rejectionProb; - int upperChanel = RawNMPoint.MAX_CHANEL; - - - - public FrameAnalizer(double rejectionProb, double frameLength) { - this.rejectionProb = rejectionProb; - this.frameLength = frameLength; - } - - public FrameAnalizer(double rejectionProb, double frameLength, int lower, int upper) { - assert upper > lower; - this.rejectionProb = rejectionProb; - this.frameLength = frameLength; - this.lowerChanel = lower; - this.upperChanel = upper; - } - - public FrameAnalizer(double rejectionProb, double frameLength, int numCircles) { - this.rejectionProb = rejectionProb; - this.frameLength = frameLength; - this.numCircles = numCircles; - } - - /** - * Полный аналог Сережиной программы - * - * @param numCicles - * @param prob - * @param frameShift - * @param frameLength - * @return - */ - private DebunchReport cicledDebunch(RawNMPoint point, int numCicles, double prob, double frameShift, double frameLength) { - DebunchReport res = this.debunch(point, prob, frameShift, frameLength); - for (int i = 0; i < numCicles-1; i++) { - res = this.debunch(res, prob, frameShift, frameLength); - } - return res; - } - - private DebunchReport debunch(DebunchReport res, double prob, double frameShift, double frameLength) { - return debunch(res.getPoint(), prob, frameShift, frameLength); - } - - private DebunchReport debunch(RawNMPoint point, double prob, double frameShift, double frameLength) { - double cr = point.selectChanels(lowerChanel, upperChanel).getCR(); - return debunch(point, cr, prob, frameShift, frameLength); - } - - private DebunchReport debunch(RawNMPoint point, double averageCR, double prob, double frameShift, double frameLength) { - - DebunchData data = new DebunchData(point); - - double timeTotal = data.getLength(); -// long countTotal = data.size(); - - double curPos = 0; - double baseThreshold = getCRThreshold(averageCR, frameLength, prob); - Frame workFrame; - boolean bunchFlag = false;// Флаг символизирует, находимся ли мы в состоянии пачки - - while (curPos < (data.getLength() - frameLength)) { - workFrame = data.countInFrameFast(curPos, frameLength,lowerChanel,upperChanel); - - if (workFrame.getCountRate() > baseThreshold) { - /* - * Если счет в рамке превышает порог, то выкидываем рамку из результата и сдвигаем - * каретку на один шаг. При этом выставляем флаг. - * Если видим флаг,то вырезаем только последний шаг, чтобы избежать двойного вырезания - */ - - if (bunchFlag) { - /*Тут возможен косяк, когда две пачки рядом, но не вплотную. Можно сделать - * так, чтобы запоминалось не состояние флага, а конец последнего вырезанного кадра - */ - workFrame = data.countInFrameFast(curPos + frameLength - frameShift, frameShift,lowerChanel,upperChanel); - } - - data.setAsBunch(workFrame); - timeTotal -= workFrame.length(); - if (timeTotal <= 0) { - throw new RuntimeException("Total time after cleaning is zero."); - } - - bunchFlag = true; - - } else { - /* - * Если пачки нет, то просто сдвигаем каретку к следующей рамке и убираем флаг - */ - bunchFlag = false; - } - curPos += frameShift; - } - return new DebunchReportImpl(point, data); - } - - @Override - public DebunchReport debunchPoint(RawNMPoint point) { - return cicledDebunch(point, numCircles, rejectionProb, frameLength/4, frameLength); - } - - private double getCRThreshold(double cr, double frameLength, double prob) { - if (cr * frameLength > 20) { - return getGaussianCRThreshold(cr, frameLength, prob); - } else { - return getPoissonThreshold(cr * frameLength, prob) / frameLength; - } - } - - /** - * Returns set of intervals begining with frameStarts[i]. All FrameStart - * should be inside data region - * - * @param frameStarts - * @param frameLength - * @param fast - * @return - */ - private Frame[] getIntervals(DebunchData data, double[] frameStarts, double frameLength, boolean fast) { - Frame[] res = new Frame[frameStarts.length]; - - for (int i = 0; i < frameStarts.length; i++) { - if (fast) { - res[i] = data.countInFrameFast(frameStarts[i], frameLength,lowerChanel,upperChanel); - } else { - res[i] = data.countInFrame(frameStarts[i], frameLength,lowerChanel,upperChanel); - } - } - return res; - } - - /** - * Returns count rate in consequent frames with the length of frameLength. - * The last frame could be shorter than the overs. This method could be used - * for fast distribution calculation. - * - * @param frameLength - * @return - */ - private double[] getNonIntercectFramesCountRate(DebunchData data, double frameLength) { - double dataLength = data.getLength(); - int maxFramesCount = (int) Math.ceil(dataLength / frameLength); - if (maxFramesCount < 2) { - throw new IllegalArgumentException("The frameLength is too large."); - } - - double[] res = new double[maxFramesCount]; - double frameBegin; - - for (int i = 0; i < res.length; i++) { - frameBegin = i * frameLength; - res[i] = data.countInFrameFast(frameBegin, frameLength,lowerChanel,upperChanel).getCountRate(); - - } - return res; - - } - - private int getPoissonThreshold(double mean, double prob) { - /* - * Находим точку "обнуления" распределения и значения коммулятивной плотности в этой точке. - */ - double pdf = FastMath.exp(-mean); - double cdf = pdf; - int k = 0; - while (pdf > POISSON_THRESHOLD) { - k++; - pdf *= mean / k; - cdf += pdf; - } - /* - * Начинаем считать комулятивную плотность в обратном порядке - */ - cdf = 1 - cdf; - if (pdf <= 0) { - throw new Error();// Проверяем чтобы там точно не было нуля; - } - while (cdf < prob) { - k--; - pdf *= k / mean; - cdf += pdf; - } - return k; - } - - private Frame[] getUniformShiftedIntervals(DebunchData data, double frameShift, double frameLength, boolean fast) { - double dataLength = data.getLength(); - int maxFramesCount = (int) Math.ceil(dataLength / frameShift); - - double[] frameStarts = new double[maxFramesCount]; - for (int i = 0; i < frameStarts.length; i++) { - frameStarts[i] = i * frameShift; - - } - return getIntervals(data, frameStarts, frameLength, fast); - } -} +package inr.numass.debunch; + +import inr.numass.storage.RawNMPoint; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.interpolation.LinearInterpolator; +import org.apache.commons.math3.util.FastMath; + +/** + * + * @author Darksnake + */ +public class FrameAnalizer implements Debuncher { + + private static final double POISSON_THRESHOLD = 1e-80; + + static double getGaussianCRThreshold(double cr, double frameLength, double prob) { + double[] xs = {9, 8, 7, 6, 5, 4, 3, 2, 1}; + double[] probs = {1.15e-19, 6.22e-16, 1.27e-12, 9.86e-10, 2.86e-7, 3.167e-5, 0.001349, 0.0227, 0.1586}; + LinearInterpolator interpolator = new LinearInterpolator(); + UnivariateFunction function = interpolator.interpolate(probs, xs); + double sigmas = function.value(prob); + return cr + sigmas * Math.sqrt(cr / frameLength); + } + //double frameShift; + double frameLength; + int lowerChanel = 0; + + int numCircles = 1; + double rejectionProb; + int upperChanel = RawNMPoint.MAX_CHANEL; + + + + public FrameAnalizer(double rejectionProb, double frameLength) { + this.rejectionProb = rejectionProb; + this.frameLength = frameLength; + } + + public FrameAnalizer(double rejectionProb, double frameLength, int lower, int upper) { + assert upper > lower; + this.rejectionProb = rejectionProb; + this.frameLength = frameLength; + this.lowerChanel = lower; + this.upperChanel = upper; + } + + public FrameAnalizer(double rejectionProb, double frameLength, int numCircles) { + this.rejectionProb = rejectionProb; + this.frameLength = frameLength; + this.numCircles = numCircles; + } + + /** + * Полный аналог Сережиной программы + * + * @param numCicles + * @param prob + * @param frameShift + * @param frameLength + * @return + */ + private DebunchReport cicledDebunch(RawNMPoint point, int numCicles, double prob, double frameShift, double frameLength) { + DebunchReport res = this.debunch(point, prob, frameShift, frameLength); + for (int i = 0; i < numCicles-1; i++) { + res = this.debunch(res, prob, frameShift, frameLength); + } + return res; + } + + private DebunchReport debunch(DebunchReport res, double prob, double frameShift, double frameLength) { + return debunch(res.getPoint(), prob, frameShift, frameLength); + } + + private DebunchReport debunch(RawNMPoint point, double prob, double frameShift, double frameLength) { + double cr = point.selectChanels(lowerChanel, upperChanel).getCR(); + return debunch(point, cr, prob, frameShift, frameLength); + } + + private DebunchReport debunch(RawNMPoint point, double averageCR, double prob, double frameShift, double frameLength) { + + DebunchData data = new DebunchData(point); + + double timeTotal = data.getLength(); +// long countTotal = data.size(); + + double curPos = 0; + double baseThreshold = getCRThreshold(averageCR, frameLength, prob); + Frame workFrame; + boolean bunchFlag = false;// Флаг символизирует, находимся ли мы в состоянии пачки + + while (curPos < (data.getLength() - frameLength)) { + workFrame = data.countInFrameFast(curPos, frameLength,lowerChanel,upperChanel); + + if (workFrame.getCountRate() > baseThreshold) { + /* + * Если счет в рамке превышает порог, то выкидываем рамку из результата и сдвигаем + * каретку на один шаг. При этом выставляем флаг. + * Если видим флаг,то вырезаем только последний шаг, чтобы избежать двойного вырезания + */ + + if (bunchFlag) { + /*Тут возможен косяк, когда две пачки рядом, но не вплотную. Можно сделать + * так, чтобы запоминалось не состояние флага, а конец последнего вырезанного кадра + */ + workFrame = data.countInFrameFast(curPos + frameLength - frameShift, frameShift,lowerChanel,upperChanel); + } + + data.setAsBunch(workFrame); + timeTotal -= workFrame.length(); + if (timeTotal <= 0) { + throw new RuntimeException("Total time after cleaning is zero."); + } + + bunchFlag = true; + + } else { + /* + * Если пачки нет, то просто сдвигаем каретку к следующей рамке и убираем флаг + */ + bunchFlag = false; + } + curPos += frameShift; + } + return new DebunchReportImpl(point, data); + } + + @Override + public DebunchReport debunchPoint(RawNMPoint point) { + return cicledDebunch(point, numCircles, rejectionProb, frameLength/4, frameLength); + } + + private double getCRThreshold(double cr, double frameLength, double prob) { + if (cr * frameLength > 20) { + return getGaussianCRThreshold(cr, frameLength, prob); + } else { + return getPoissonThreshold(cr * frameLength, prob) / frameLength; + } + } + + /** + * Returns set of intervals begining with frameStarts[i]. All FrameStart + * should be inside data region + * + * @param frameStarts + * @param frameLength + * @param fast + * @return + */ + private Frame[] getIntervals(DebunchData data, double[] frameStarts, double frameLength, boolean fast) { + Frame[] res = new Frame[frameStarts.length]; + + for (int i = 0; i < frameStarts.length; i++) { + if (fast) { + res[i] = data.countInFrameFast(frameStarts[i], frameLength,lowerChanel,upperChanel); + } else { + res[i] = data.countInFrame(frameStarts[i], frameLength,lowerChanel,upperChanel); + } + } + return res; + } + + /** + * Returns count rate in consequent frames with the length of frameLength. + * The last frame could be shorter than the overs. This method could be used + * for fast distribution calculation. + * + * @param frameLength + * @return + */ + private double[] getNonIntercectFramesCountRate(DebunchData data, double frameLength) { + double dataLength = data.getLength(); + int maxFramesCount = (int) Math.ceil(dataLength / frameLength); + if (maxFramesCount < 2) { + throw new IllegalArgumentException("The frameLength is too large."); + } + + double[] res = new double[maxFramesCount]; + double frameBegin; + + for (int i = 0; i < res.length; i++) { + frameBegin = i * frameLength; + res[i] = data.countInFrameFast(frameBegin, frameLength,lowerChanel,upperChanel).getCountRate(); + + } + return res; + + } + + private int getPoissonThreshold(double mean, double prob) { + /* + * Находим точку "обнуления" распределения и значения коммулятивной плотности в этой точке. + */ + double pdf = FastMath.exp(-mean); + double cdf = pdf; + int k = 0; + while (pdf > POISSON_THRESHOLD) { + k++; + pdf *= mean / k; + cdf += pdf; + } + /* + * Начинаем считать комулятивную плотность в обратном порядке + */ + cdf = 1 - cdf; + if (pdf <= 0) { + throw new Error();// Проверяем чтобы там точно не было нуля; + } + while (cdf < prob) { + k--; + pdf *= k / mean; + cdf += pdf; + } + return k; + } + + private Frame[] getUniformShiftedIntervals(DebunchData data, double frameShift, double frameLength, boolean fast) { + double dataLength = data.getLength(); + int maxFramesCount = (int) Math.ceil(dataLength / frameShift); + + double[] frameStarts = new double[maxFramesCount]; + for (int i = 0; i < frameStarts.length; i++) { + frameStarts[i] = i * frameShift; + + } + return getIntervals(data, frameStarts, frameLength, fast); + } +} diff --git a/numass-main/src/main/java/inr/numass/generators/BunchGenerator.java b/numass-main/src/main/java/inr/numass/generators/BunchGenerator.java index a8e5d005..f9ebe4da 100644 --- a/numass-main/src/main/java/inr/numass/generators/BunchGenerator.java +++ b/numass-main/src/main/java/inr/numass/generators/BunchGenerator.java @@ -13,192 +13,192 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.generators; - -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; -import java.util.ArrayList; -import org.apache.commons.math3.random.MersenneTwister; -import org.apache.commons.math3.random.RandomGenerator; -import org.apache.commons.math3.random.SynchronizedRandomGenerator; - -/** - * - * @author Darksnake - */ -public class BunchGenerator { - - private double bunchCr; // additional count rate in bunch - private double bunchDist;// average distance between bunches - private double bunchLength; // length of bunches - private double cr; // count rate of normal events - private ExpGenerator expGen; - -// private ExponentialDistribution expGen; - public BunchGenerator(double cr, double bunchLength, double bunchDist, double bunchCr) { - this.cr = cr; - this.bunchLength = bunchLength; - this.bunchDist = bunchDist; - this.bunchCr = bunchCr; - expGen = new ExpGenerator(new SynchronizedRandomGenerator(new MersenneTwister())); - } - - public BunchGenerator(double cr, double bunchLength, double bunchDist, double bunchCr, RandomGenerator gen) { - this.cr = cr; - this.bunchLength = bunchLength; - this.bunchDist = bunchDist; - this.bunchCr = bunchCr; - expGen = new ExpGenerator(gen); - } - - public ArrayList generate(double dist, double length, double timeShift, boolean isBunch) { - ArrayList res = new ArrayList<>(); - ArrayList events = generateEvents(dist, length); - for (Double event : events) { - if (event < length) { - res.add(new NMEvent((short)0,event + timeShift)); -// if (isBunch) { -// res.add(new DebunchEvent(event + timeShift, 10)); -// } else { -// res.add(new DebunchEvent(event + timeShift)); -// } - - } - } - return res; - } - - ArrayList generateEvents(double dist, double timeTotal) { - ArrayList res = new ArrayList<>(); - double timeCount = 0; - double delta; - while (timeCount < timeTotal) { - delta = expGen.nextExp(dist); - timeCount += delta; - if (timeCount < timeTotal) { - res.add(timeCount); - } - } - return res; - } - - /** - * Создает пачку с треугольным распределением - * - * @param dist - * @param timeTotal - * @return - */ - ArrayList generateEventsTriangle(double dist, double timeTotal) { - ArrayList res = new ArrayList<>(); - double timeCount = 0; - double delta; - while (timeCount < timeTotal) { - delta = expGen.nextExp(dist * timeTotal / (timeTotal - timeCount)); - timeCount += delta; - if (timeCount < timeTotal) { - res.add(timeCount); - } - } - return res; - } - - public ArrayList generateNormalEvents(double measurementTime) { - return generate(1 / cr, measurementTime, 0, false); - } - - public ArrayList generateTriangle(double dist, double length, double timeShift, boolean isBunch) { - ArrayList res = new ArrayList<>(); - ArrayList events = generateEventsTriangle(dist, length); - for (Double event : events) { - if (event < length) { - res.add(new NMEvent((short)0,event + timeShift)); -// if (isBunch) { -// res.add(new DebunchEvent(event + timeShift, 10)); -// } else { -// res.add(new DebunchEvent(event + timeShift)); -// } - - } - } - return res; - } - - /** - * - * @param measurementTime - total measurement time - * @return - */ - public RawNMPoint generateWithBunches(double measurementTime) { - ArrayList res = generateNormalEvents(measurementTime); - ArrayList bunchList = generateEvents(bunchDist, measurementTime); - for (Double bunchPos : bunchList) { - res.addAll(generate(1 / bunchCr, bunchLength, bunchPos, true)); - } - return new RawNMPoint(0, res, measurementTime); - } - - /** - * - * @param measurementTime - total measurement time - * @return - */ - public RawNMPoint generateWithRandomBunches(double measurementTime) { - ArrayList res = generateNormalEvents(measurementTime); - ArrayList bunchList = generateEvents(bunchDist, measurementTime); - for (Double bunchPos : bunchList) { - double l = expGen.nextSafeGaussian(bunchLength, bunchLength / 3); - double lambda = expGen.nextSafeGaussian(1 / bunchCr, 1 / bunchCr / 3); - res.addAll(generate(lambda, l, bunchPos, true)); - } - return new RawNMPoint(0, res, measurementTime); - } - - public RawNMPoint generateWithTriangleBunches(double measurementTime) { - ArrayList res = generateNormalEvents(measurementTime); - ArrayList bunchList = generateEvents(bunchDist, measurementTime); - for (Double bunchPos : bunchList) { - res.addAll(generateTriangle(1 / bunchCr, bunchLength, bunchPos, true)); - } - return new RawNMPoint(0, res, measurementTime); - } - - public void setSeed(int seed) { - this.expGen.setSeed(seed); - } - - private static class ExpGenerator { - - private final RandomGenerator generator; - - public ExpGenerator(RandomGenerator generator) { - this.generator = generator; - } - - public ExpGenerator(RandomGenerator generator, int seed) { - this.generator = generator; - this.generator.setSeed(seed); - } - - void setSeed(int seed) { - generator.setSeed(seed); - } - - double nextUniform() { - return generator.nextDouble(); - } - - double nextExp(double mean) { - double rand = this.nextUniform(); - return -mean * Math.log(1 - rand); - } - - double nextSafeGaussian(double mean, double sigma) { - double res = -1; - while (res <= 0) { - res = mean + generator.nextGaussian() * sigma; - } - return res; - } - } -} +package inr.numass.generators; + +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; +import java.util.ArrayList; +import org.apache.commons.math3.random.MersenneTwister; +import org.apache.commons.math3.random.RandomGenerator; +import org.apache.commons.math3.random.SynchronizedRandomGenerator; + +/** + * + * @author Darksnake + */ +public class BunchGenerator { + + private double bunchCr; // additional count rate in bunch + private double bunchDist;// average distance between bunches + private double bunchLength; // length of bunches + private double cr; // count rate of normal events + private ExpGenerator expGen; + +// private ExponentialDistribution expGen; + public BunchGenerator(double cr, double bunchLength, double bunchDist, double bunchCr) { + this.cr = cr; + this.bunchLength = bunchLength; + this.bunchDist = bunchDist; + this.bunchCr = bunchCr; + expGen = new ExpGenerator(new SynchronizedRandomGenerator(new MersenneTwister())); + } + + public BunchGenerator(double cr, double bunchLength, double bunchDist, double bunchCr, RandomGenerator gen) { + this.cr = cr; + this.bunchLength = bunchLength; + this.bunchDist = bunchDist; + this.bunchCr = bunchCr; + expGen = new ExpGenerator(gen); + } + + public ArrayList generate(double dist, double length, double timeShift, boolean isBunch) { + ArrayList res = new ArrayList<>(); + ArrayList events = generateEvents(dist, length); + for (Double event : events) { + if (event < length) { + res.add(new NMEvent((short)0,event + timeShift)); +// if (isBunch) { +// res.add(new DebunchEvent(event + timeShift, 10)); +// } else { +// res.add(new DebunchEvent(event + timeShift)); +// } + + } + } + return res; + } + + ArrayList generateEvents(double dist, double timeTotal) { + ArrayList res = new ArrayList<>(); + double timeCount = 0; + double delta; + while (timeCount < timeTotal) { + delta = expGen.nextExp(dist); + timeCount += delta; + if (timeCount < timeTotal) { + res.add(timeCount); + } + } + return res; + } + + /** + * Создает пачку с треугольным распределением + * + * @param dist + * @param timeTotal + * @return + */ + ArrayList generateEventsTriangle(double dist, double timeTotal) { + ArrayList res = new ArrayList<>(); + double timeCount = 0; + double delta; + while (timeCount < timeTotal) { + delta = expGen.nextExp(dist * timeTotal / (timeTotal - timeCount)); + timeCount += delta; + if (timeCount < timeTotal) { + res.add(timeCount); + } + } + return res; + } + + public ArrayList generateNormalEvents(double measurementTime) { + return generate(1 / cr, measurementTime, 0, false); + } + + public ArrayList generateTriangle(double dist, double length, double timeShift, boolean isBunch) { + ArrayList res = new ArrayList<>(); + ArrayList events = generateEventsTriangle(dist, length); + for (Double event : events) { + if (event < length) { + res.add(new NMEvent((short)0,event + timeShift)); +// if (isBunch) { +// res.add(new DebunchEvent(event + timeShift, 10)); +// } else { +// res.add(new DebunchEvent(event + timeShift)); +// } + + } + } + return res; + } + + /** + * + * @param measurementTime - total measurement time + * @return + */ + public RawNMPoint generateWithBunches(double measurementTime) { + ArrayList res = generateNormalEvents(measurementTime); + ArrayList bunchList = generateEvents(bunchDist, measurementTime); + for (Double bunchPos : bunchList) { + res.addAll(generate(1 / bunchCr, bunchLength, bunchPos, true)); + } + return new RawNMPoint(0, res, measurementTime); + } + + /** + * + * @param measurementTime - total measurement time + * @return + */ + public RawNMPoint generateWithRandomBunches(double measurementTime) { + ArrayList res = generateNormalEvents(measurementTime); + ArrayList bunchList = generateEvents(bunchDist, measurementTime); + for (Double bunchPos : bunchList) { + double l = expGen.nextSafeGaussian(bunchLength, bunchLength / 3); + double lambda = expGen.nextSafeGaussian(1 / bunchCr, 1 / bunchCr / 3); + res.addAll(generate(lambda, l, bunchPos, true)); + } + return new RawNMPoint(0, res, measurementTime); + } + + public RawNMPoint generateWithTriangleBunches(double measurementTime) { + ArrayList res = generateNormalEvents(measurementTime); + ArrayList bunchList = generateEvents(bunchDist, measurementTime); + for (Double bunchPos : bunchList) { + res.addAll(generateTriangle(1 / bunchCr, bunchLength, bunchPos, true)); + } + return new RawNMPoint(0, res, measurementTime); + } + + public void setSeed(int seed) { + this.expGen.setSeed(seed); + } + + private static class ExpGenerator { + + private final RandomGenerator generator; + + public ExpGenerator(RandomGenerator generator) { + this.generator = generator; + } + + public ExpGenerator(RandomGenerator generator, int seed) { + this.generator = generator; + this.generator.setSeed(seed); + } + + void setSeed(int seed) { + generator.setSeed(seed); + } + + double nextUniform() { + return generator.nextDouble(); + } + + double nextExp(double mean) { + double rand = this.nextUniform(); + return -mean * Math.log(1 - rand); + } + + double nextSafeGaussian(double mean, double sigma) { + double res = -1; + while (res <= 0) { + res = mean + generator.nextGaussian() * sigma; + } + return res; + } + } +} diff --git a/numass-main/src/main/java/inr/numass/generators/EventChainGenerator.java b/numass-main/src/main/java/inr/numass/generators/EventChainGenerator.java index 0bcbe180..607c89be 100644 --- a/numass-main/src/main/java/inr/numass/generators/EventChainGenerator.java +++ b/numass-main/src/main/java/inr/numass/generators/EventChainGenerator.java @@ -15,8 +15,8 @@ */ package inr.numass.generators; -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; import static java.lang.Math.max; import java.util.ArrayList; import java.util.List; diff --git a/numass-main/src/main/java/inr/numass/generators/EventGenerator.java b/numass-main/src/main/java/inr/numass/generators/EventGenerator.java index 72476a9b..cf66c1c4 100644 --- a/numass-main/src/main/java/inr/numass/generators/EventGenerator.java +++ b/numass-main/src/main/java/inr/numass/generators/EventGenerator.java @@ -13,102 +13,102 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.generators; - -import inr.numass.data.NMEvent; -import inr.numass.data.RawNMPoint; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.apache.commons.math3.distribution.EnumeratedRealDistribution; -import org.apache.commons.math3.distribution.RealDistribution; -import org.apache.commons.math3.random.EmpiricalDistribution; -import org.apache.commons.math3.random.JDKRandomGenerator; -import org.apache.commons.math3.random.RandomGenerator; - - -/** - * - * @author Darksnake - */ -public class EventGenerator { - - double cr; -// UnivariateFunction signalShape; - RealDistribution distribution; - - private final RandomGenerator generator; - - public EventGenerator(double cr) { - this.cr = cr; - generator = new JDKRandomGenerator(); - } - - public void loadFromPoint(RawNMPoint point,int minChanel, int maxChanel){ - List shorts = new ArrayList<>(); - for (NMEvent event : point.getEvents()) { - if((event.getChanel()>minChanel)&&(event.getChanel() spectrum, int minChanel, int maxChanel){ - assert minChanel > 0; - assert maxChanel < RawNMPoint.MAX_CHANEL; - - double[] chanels = new double[spectrum.size()]; - double[] values = new double[spectrum.size()]; - int i = 0; - for (Map.Entry entry : spectrum.entrySet()) { - chanels[i] = entry.getKey(); - values[i] = entry.getValue(); - i++; - } - distribution = new EnumeratedRealDistribution(chanels, values); - } - - public NMEvent nextEvent(NMEvent prev) { - //пока без канала - short chanel = 1600; - - if(distribution!=null){ - chanel = (short) distribution.sample(); - } - - - return new NMEvent(chanel, prev.getTime() + nextExp(1 / cr)); - } - - double nextExp(double mean) { - double rand = this.nextUniform(); - return -mean * Math.log(1 - rand); - } - - double nextPositiveGaussian(double mean, double sigma) { - double res = -1; - while (res <= 0) { - res = mean + generator.nextGaussian() * sigma; - } - return res; - } - - double nextUniform() { - return generator.nextDouble(); - } - - void setSeed(int seed) { - generator.setSeed(seed); - } - -} +package inr.numass.generators; + +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMPoint; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.commons.math3.distribution.EnumeratedRealDistribution; +import org.apache.commons.math3.distribution.RealDistribution; +import org.apache.commons.math3.random.EmpiricalDistribution; +import org.apache.commons.math3.random.JDKRandomGenerator; +import org.apache.commons.math3.random.RandomGenerator; + + +/** + * + * @author Darksnake + */ +public class EventGenerator { + + double cr; +// UnivariateFunction signalShape; + RealDistribution distribution; + + private final RandomGenerator generator; + + public EventGenerator(double cr) { + this.cr = cr; + generator = new JDKRandomGenerator(); + } + + public void loadFromPoint(RawNMPoint point,int minChanel, int maxChanel){ + List shorts = new ArrayList<>(); + for (NMEvent event : point.getEvents()) { + if((event.getChanel()>minChanel)&&(event.getChanel() spectrum, int minChanel, int maxChanel){ + assert minChanel > 0; + assert maxChanel < RawNMPoint.MAX_CHANEL; + + double[] chanels = new double[spectrum.size()]; + double[] values = new double[spectrum.size()]; + int i = 0; + for (Map.Entry entry : spectrum.entrySet()) { + chanels[i] = entry.getKey(); + values[i] = entry.getValue(); + i++; + } + distribution = new EnumeratedRealDistribution(chanels, values); + } + + public NMEvent nextEvent(NMEvent prev) { + //пока без канала + short chanel = 1600; + + if(distribution!=null){ + chanel = (short) distribution.sample(); + } + + + return new NMEvent(chanel, prev.getTime() + nextExp(1 / cr)); + } + + double nextExp(double mean) { + double rand = this.nextUniform(); + return -mean * Math.log(1 - rand); + } + + double nextPositiveGaussian(double mean, double sigma) { + double res = -1; + while (res <= 0) { + res = mean + generator.nextGaussian() * sigma; + } + return res; + } + + double nextUniform() { + return generator.nextDouble(); + } + + void setSeed(int seed) { + generator.setSeed(seed); + } + +} diff --git a/numass-storage/build.gradle b/numass-storage/build.gradle index dc8bb30a..50c93d01 100644 --- a/numass-storage/build.gradle +++ b/numass-storage/build.gradle @@ -1,10 +1,4 @@ -configurations{ - all*.exclude module: 'commons-math3' - all*.exclude group: 'org.jfree' -} - dependencies { - compile(project(':numass-main')){transitive = false} compile project(':dataforge-storage') compile project(':dataforge-grind') } diff --git a/numass-storage/numass-client/build.gradle b/numass-storage/numass-client/build.gradle index e4c413ee..3b0197de 100644 --- a/numass-storage/numass-client/build.gradle +++ b/numass-storage/numass-client/build.gradle @@ -18,8 +18,7 @@ task runClient(type: JavaExec) { } dependencies { - compile(project(':numass-storage')){transitive = false} - compile project(':dataforge-storage') + compile(project(':numass-storage')) compile 'commons-cli:commons-cli:1.3.1' compile 'org.zeroturnaround:zt-zip:1.9' } \ No newline at end of file diff --git a/numass-main/src/main/java/inr/numass/data/NMEvent.java b/numass-storage/src/main/java/inr/numass/storage/NMEvent.java similarity index 93% rename from numass-main/src/main/java/inr/numass/data/NMEvent.java rename to numass-storage/src/main/java/inr/numass/storage/NMEvent.java index 699abd3c..e56c60ce 100644 --- a/numass-main/src/main/java/inr/numass/data/NMEvent.java +++ b/numass-storage/src/main/java/inr/numass/storage/NMEvent.java @@ -13,37 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; - -/** - * - * @author Darksnake - */ -public class NMEvent implements Cloneable{ - protected final short chanel; - protected final double time; - - public NMEvent(short chanel, double time) { - this.chanel = chanel; - this.time = time; - } - - @Override - public NMEvent clone() { - return new NMEvent(chanel, time); - } - - /** - * @return the chanel - */ - public short getChanel() { - return chanel; - } - - /** - * @return the time - */ - public double getTime(){ - return time; - } -} +package inr.numass.storage; + +/** + * + * @author Darksnake + */ +public class NMEvent implements Cloneable{ + protected final short chanel; + protected final double time; + + public NMEvent(short chanel, double time) { + this.chanel = chanel; + this.time = time; + } + + @Override + public NMEvent clone() { + return new NMEvent(chanel, time); + } + + /** + * @return the chanel + */ + public short getChanel() { + return chanel; + } + + /** + * @return the time + */ + public double getTime(){ + return time; + } +} diff --git a/numass-main/src/main/java/inr/numass/data/NMFile.java b/numass-storage/src/main/java/inr/numass/storage/NMFile.java similarity index 95% rename from numass-main/src/main/java/inr/numass/data/NMFile.java rename to numass-storage/src/main/java/inr/numass/storage/NMFile.java index 8e174046..d9bf1eb1 100644 --- a/numass-main/src/main/java/inr/numass/data/NMFile.java +++ b/numass-storage/src/main/java/inr/numass/storage/NMFile.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; +package inr.numass.storage; import hep.dataforge.description.ValueDef; import hep.dataforge.meta.Meta; diff --git a/numass-main/src/main/java/inr/numass/data/NMPoint.java b/numass-storage/src/main/java/inr/numass/storage/NMPoint.java similarity index 96% rename from numass-main/src/main/java/inr/numass/data/NMPoint.java rename to numass-storage/src/main/java/inr/numass/storage/NMPoint.java index 2e8e5334..6fe0dd13 100644 --- a/numass-main/src/main/java/inr/numass/data/NMPoint.java +++ b/numass-storage/src/main/java/inr/numass/storage/NMPoint.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; +package inr.numass.storage; import hep.dataforge.tables.DataPoint; import hep.dataforge.tables.MapPoint; diff --git a/numass-main/src/main/java/inr/numass/data/NumassData.java b/numass-storage/src/main/java/inr/numass/storage/NumassData.java similarity index 89% rename from numass-main/src/main/java/inr/numass/data/NumassData.java rename to numass-storage/src/main/java/inr/numass/storage/NumassData.java index b160f547..c1cec023 100644 --- a/numass-main/src/main/java/inr/numass/data/NumassData.java +++ b/numass-storage/src/main/java/inr/numass/storage/NumassData.java @@ -3,8 +3,9 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package inr.numass.data; +package inr.numass.storage; +import inr.numass.storage.NMPoint; import hep.dataforge.meta.Meta; import hep.dataforge.names.Named; import java.time.Instant; 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 dd3d4b26..8c61bfc6 100644 --- a/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java +++ b/numass-storage/src/main/java/inr/numass/storage/NumassDataLoader.java @@ -24,10 +24,6 @@ import hep.dataforge.meta.MetaBuilder; import hep.dataforge.storage.api.ObjectLoader; import hep.dataforge.storage.api.Storage; import hep.dataforge.storage.loaders.AbstractLoader; -import inr.numass.data.NMEvent; -import inr.numass.data.NMPoint; -import inr.numass.data.NumassData; -import inr.numass.data.RawNMPoint; import java.io.File; import java.io.IOException; import java.io.InputStream; diff --git a/numass-main/src/main/java/inr/numass/data/NumassDataReader.java b/numass-storage/src/main/java/inr/numass/storage/NumassDataReader.java similarity index 94% rename from numass-main/src/main/java/inr/numass/data/NumassDataReader.java rename to numass-storage/src/main/java/inr/numass/storage/NumassDataReader.java index 13b509e7..e5763a11 100644 --- a/numass-main/src/main/java/inr/numass/data/NumassDataReader.java +++ b/numass-storage/src/main/java/inr/numass/storage/NumassDataReader.java @@ -13,8 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; +package inr.numass.storage; +import inr.numass.storage.NMEvent; +import inr.numass.storage.RawNMFile; +import inr.numass.storage.RawNMPoint; import hep.dataforge.data.FileDataFactory; import hep.dataforge.data.binary.Binary; import hep.dataforge.meta.Meta; diff --git a/numass-storage/src/main/java/inr/numass/storage/NumassStorage.java b/numass-storage/src/main/java/inr/numass/storage/NumassStorage.java index bb8c1429..a3feefed 100644 --- a/numass-storage/src/main/java/inr/numass/storage/NumassStorage.java +++ b/numass-storage/src/main/java/inr/numass/storage/NumassStorage.java @@ -22,8 +22,6 @@ import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaBuilder; import hep.dataforge.storage.filestorage.FileStorage; import hep.dataforge.storage.filestorage.VFSUtils; -import inr.numass.data.NMFile; -import inr.numass.data.NumassData; import java.io.File; import java.io.IOException; import java.io.InputStream; diff --git a/numass-main/src/main/java/inr/numass/data/RawNMFile.java b/numass-storage/src/main/java/inr/numass/storage/RawNMFile.java similarity index 93% rename from numass-main/src/main/java/inr/numass/data/RawNMFile.java rename to numass-storage/src/main/java/inr/numass/storage/RawNMFile.java index b443adb4..1119dad1 100644 --- a/numass-main/src/main/java/inr/numass/data/RawNMFile.java +++ b/numass-storage/src/main/java/inr/numass/storage/RawNMFile.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; +package inr.numass.storage; import hep.dataforge.names.NamedMetaHolder; import java.io.BufferedOutputStream; @@ -109,7 +109,7 @@ public class RawNMFile extends NamedMetaHolder { return points; } - void putEvent(double U, short chanel, double time) { + public void putEvent(double U, short chanel, double time) { for (RawNMPoint point : this.getData()) { if (U == point.getUread()) { point.putEvent(new NMEvent(chanel, time)); diff --git a/numass-main/src/main/java/inr/numass/data/RawNMPoint.java b/numass-storage/src/main/java/inr/numass/storage/RawNMPoint.java similarity index 95% rename from numass-main/src/main/java/inr/numass/data/RawNMPoint.java rename to numass-storage/src/main/java/inr/numass/storage/RawNMPoint.java index 5fb619a1..4d46aec2 100644 --- a/numass-main/src/main/java/inr/numass/data/RawNMPoint.java +++ b/numass-storage/src/main/java/inr/numass/storage/RawNMPoint.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package inr.numass.data; +package inr.numass.storage; import java.time.Instant; import java.util.ArrayList; diff --git a/numass-viewer/build.gradle b/numass-viewer/build.gradle index 0b45e7da..5433ef39 100644 --- a/numass-viewer/build.gradle +++ b/numass-viewer/build.gradle @@ -11,8 +11,6 @@ description = "The viewer for numass data" dependencies { compile project(':numass-main') -// compile project(':dataforge-storage') - compile project(':numass-storage') compile project(':dataforge-fx') compile 'com.jcraft:jsch:0.1.53' } diff --git a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java index cfa08df4..01388eb0 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java @@ -21,7 +21,7 @@ import hep.dataforge.context.ProcessManager; import hep.dataforge.exceptions.StorageException; import hep.dataforge.fx.ConsoleFragment; import hep.dataforge.fx.ProcessManagerFragment; -import inr.numass.data.NumassData; +import inr.numass.storage.NumassData; import inr.numass.storage.NumassStorage; import java.io.File; import java.net.URL; diff --git a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java index 1b88f770..7b53f871 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java @@ -19,7 +19,7 @@ import hep.dataforge.context.ProcessManager; import hep.dataforge.exceptions.StorageException; import hep.dataforge.storage.api.Loader; import hep.dataforge.storage.api.Storage; -import inr.numass.data.NumassData; +import inr.numass.storage.NumassData; import inr.numass.storage.NumassStorage; import java.time.Instant; import java.util.ArrayList; diff --git a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java index b6f1d7c4..70e98eb3 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java @@ -37,8 +37,8 @@ import hep.dataforge.tables.ListTable; import hep.dataforge.tables.MapPoint; import hep.dataforge.tables.Table; import hep.dataforge.tables.XYAdapter; -import inr.numass.data.NMPoint; -import inr.numass.data.NumassData; +import inr.numass.storage.NMPoint; +import inr.numass.storage.NumassData; import java.io.File; import java.io.IOException; import java.net.URL;