numass-framework/numass-main/src/main/java/inr/numass/models/VariableLossSpectrum.java

149 lines
5.0 KiB
Java
Raw Normal View History

2015-12-18 16:20:47 +03:00
/*
* Copyright 2015 Alexander Nozik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package inr.numass.models;
import hep.dataforge.exceptions.NotDefinedException;
2016-07-24 21:44:57 +03:00
import hep.dataforge.fitting.parametric.AbstractParametricFunction;
import hep.dataforge.fitting.parametric.ParametricFunction;
2015-12-18 16:20:47 +03:00
import hep.dataforge.maths.integration.UnivariateIntegrator;
2016-05-31 20:05:15 +03:00
import hep.dataforge.values.NamedValueSet;
import hep.dataforge.values.ValueProvider;
2015-12-18 16:20:47 +03:00
import inr.numass.NumassContext;
import java.util.List;
import org.apache.commons.math3.analysis.BivariateFunction;
import org.apache.commons.math3.analysis.UnivariateFunction;
/**
*
* @author Darksnake
*/
public class VariableLossSpectrum extends AbstractParametricFunction {
public static String[] names = {"X", "shift", "exPos", "ionPos", "exW", "ionW", "exIonRatio"};
public static VariableLossSpectrum withGun(double eMax) {
return new VariableLossSpectrum(new GunSpectrum(), eMax);
}
public static VariableLossSpectrum withData(final UnivariateFunction transmission, double eMax) {
return new VariableLossSpectrum(new AbstractParametricFunction(new String[0]) {
@Override
2016-05-31 20:05:15 +03:00
public double derivValue(String parName, double x, NamedValueSet set) {
2015-12-18 16:20:47 +03:00
throw new NotDefinedException();
}
@Override
public boolean providesDeriv(String name) {
return false;
}
@Override
2016-05-31 20:05:15 +03:00
public double value(double x, NamedValueSet set) {
2015-12-18 16:20:47 +03:00
return transmission.value(x);
}
}, eMax);
}
private final ParametricFunction transmission;
private UnivariateFunction backgroundFunction;
private final double eMax;
protected VariableLossSpectrum(ParametricFunction transmission, double eMax) {
super(names);
this.transmission = transmission;
this.eMax = eMax;
}
@Override
2016-05-31 20:05:15 +03:00
public double derivValue(String parName, double x, NamedValueSet set) {
2015-12-18 16:20:47 +03:00
throw new NotDefinedException();
}
@Override
2016-05-31 20:05:15 +03:00
public double value(double U, NamedValueSet set) {
2015-12-18 16:20:47 +03:00
if (U >= eMax) {
return 0;
}
2016-05-31 20:05:15 +03:00
double X = set.getDouble("X");
final double shift = set.getDouble("shift");
2015-12-18 16:20:47 +03:00
final LossCalculator loss = LossCalculator.instance();
final List<Double> probs = loss.getGunLossProbabilities(X);
final double noLossProb = probs.get(0);
UnivariateFunction scatter = singleScatterFunction(set);
final BivariateFunction lossFunction = (Ei, Ef) -> {
if (probs.size() == 1) {
return 0;
}
double sum = probs.get(1) * scatter.value(Ei - Ef);
for (int i = 2; i < probs.size(); i++) {
sum += probs.get(i) * loss.getLossValue(i, Ei, Ef);
}
return sum;
};
UnivariateFunction integrand = (double x) -> transmission.value(x, set) * lossFunction.value(x, U - shift);
UnivariateIntegrator integrator;
if (eMax - U > 150) {
integrator = NumassContext.highDensityIntegrator;
} else {
integrator = NumassContext.defaultIntegrator;
}
return noLossProb * transmission.value(U - shift, set) + integrator.integrate(integrand, U, eMax);
}
2016-05-31 20:05:15 +03:00
public UnivariateFunction singleScatterFunction(ValueProvider set) {
2015-12-18 16:20:47 +03:00
2016-05-31 20:05:15 +03:00
final double exPos = set.getDouble("exPos");
final double ionPos = set.getDouble("ionPos");
final double exW = set.getDouble("exW");
final double ionW = set.getDouble("ionW");
final double exIonRatio = set.getDouble("exIonRatio");
2015-12-18 16:20:47 +03:00
return singleScatterFunction(exPos, ionPos, exW, ionW, exIonRatio);
}
public UnivariateFunction singleScatterFunction(
final double exPos,
final double ionPos,
final double exW,
final double ionW,
final double exIonRatio) {
return LossCalculator.getSingleScatterFunction(exPos, ionPos, exW, ionW, exIonRatio);
}
@Override
public boolean providesDeriv(String name) {
return false;
}
@Override
2016-07-24 21:44:57 +03:00
protected double getDefaultParameter(String name) {
2015-12-18 16:20:47 +03:00
switch (name) {
case "shift":
return 0;
case "X":
return 0;
default:
2016-07-24 21:44:57 +03:00
return super.getDefaultParameter(name);
2015-12-18 16:20:47 +03:00
}
}
}