Import Geant4 10.3.1 source tree
This commit is contained in:
@@ -37,21 +37,24 @@
|
||||
|
||||
#include "G4PolynomialPDF.hh"
|
||||
#include "Randomize.hh"
|
||||
//#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
G4PolynomialPDF::G4PolynomialPDF(size_t n, const G4double* coeffs,
|
||||
G4double x1, G4double x2) :
|
||||
fX1(x1), fX2(x2), fChanged(true), fTolerance(1.e-8)
|
||||
fX1(x1), fX2(x2), fChanged(true), fTolerance(1.e-8), fVerbose(0)
|
||||
{
|
||||
if(coeffs != NULL) SetCoefficients(n, coeffs);
|
||||
if(coeffs != nullptr) SetCoefficients(n, coeffs);
|
||||
else if(n > 0) SetNCoefficients(n);
|
||||
}
|
||||
|
||||
G4PolynomialPDF::~G4PolynomialPDF()
|
||||
{}
|
||||
|
||||
void G4PolynomialPDF::SetCoefficient(size_t i, G4double value)
|
||||
{
|
||||
while(i >= fCoefficients.size()) fCoefficients.push_back(0); /* Loop checking, 30-Oct-2015, G.Folger */
|
||||
while(i >= fCoefficients.size()) fCoefficients.push_back(0);
|
||||
/* Loop checking, 30-Oct-2015, G.Folger */
|
||||
fCoefficients[i] = value;
|
||||
fChanged = true;
|
||||
}
|
||||
@@ -69,8 +72,10 @@ void G4PolynomialPDF::SetCoefficients(size_t nCoeffs,
|
||||
void G4PolynomialPDF::SetDomain(G4double x1, G4double x2)
|
||||
{
|
||||
if(x2 <= x1) {
|
||||
G4cout << "G4PolynomialPDF::SetDomain(): Invalide domain! "
|
||||
<< "(x1 = " << x1 << ", x2 = " << x2 << ")." << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::SetDomain() WARNING: Invalide domain! "
|
||||
<< "(x1 = " << x1 << ", x2 = " << x2 << ")." << G4endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
fX1 = x1;
|
||||
@@ -95,9 +100,11 @@ void G4PolynomialPDF::Normalize()
|
||||
x2N*=fX2;
|
||||
}
|
||||
if(sum <= 0) {
|
||||
G4cout << "G4PolynomialPDF::Normalize() PDF has non-positive area: "
|
||||
<< sum << G4endl;
|
||||
Dump();
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::Normalize() WARNING: PDF has non-positive area: "
|
||||
<< sum << G4endl;
|
||||
Dump();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,14 +121,16 @@ G4double G4PolynomialPDF::Evaluate(G4double x, G4int ddxPower)
|
||||
/// ddxPower = 1: f = (d/dx) PDF
|
||||
/// ddxPower = 2: f = (d2/dx2) PDF
|
||||
if(ddxPower < -1 || ddxPower > 2) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): ddxPower " << ddxPower
|
||||
<< " not implemented" << G4endl;
|
||||
return 0;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: ddxPower " << ddxPower
|
||||
<< " not implemented" << G4endl;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double f = 0; // return value
|
||||
double xN = 1; // x to the power N
|
||||
double x1N = 1; // endpoint x1 to the power N; only used by CDF
|
||||
double f = 0.; // return value
|
||||
double xN = 1.; // x to the power N
|
||||
double x1N = 1.; // endpoint x1 to the power N; only used by CDF
|
||||
for(size_t i=0; i<=GetNCoefficients(); ++i) {
|
||||
if(ddxPower == -1) { // CDF
|
||||
if(i>0) f += GetCoefficient(i-1)*(xN - x1N)/i;
|
||||
@@ -145,8 +154,10 @@ G4bool G4PolynomialPDF::HasNegativeMinimum(G4double x1, G4double x2)
|
||||
// p': 2ax + b = 0 -> = 0 at min: x_extreme = -b/2a
|
||||
|
||||
if(x1 < fX1 || x2 > fX2 || x2 < x1) {
|
||||
G4cout << "G4PolynomialPDF::HasNegativeMinimum(): Invalid range "
|
||||
<< x1 << " - " << x2 << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::HasNegativeMinimum() WARNING: Invalid range "
|
||||
<< x1 << " - " << x2 << G4endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -183,8 +194,11 @@ G4double G4PolynomialPDF::GetRandomX()
|
||||
if(fChanged) {
|
||||
Normalize();
|
||||
if(HasNegativeMinimum(fX1, fX2)) {
|
||||
G4cout << "G4PolynomialPDF::GetRandomX(): PDF has negative values, returning 0..." << G4endl;
|
||||
return 0;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetRandomX() WARNING: PDF has negative values, returning 0..."
|
||||
<< G4endl;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
fChanged = false;
|
||||
}
|
||||
@@ -203,23 +217,31 @@ G4double G4PolynomialPDF::GetX(G4double p, G4double x1, G4double x2,
|
||||
|
||||
// input range checking
|
||||
if(GetNCoefficients() == 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): no PDF defined!" << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: no PDF defined!" << G4endl;
|
||||
}
|
||||
return x2;
|
||||
}
|
||||
if(ddxPower < -1 || ddxPower > 1) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): ddxPower " << ddxPower
|
||||
<< " not implemented" << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: ddxPower " << ddxPower
|
||||
<< " not implemented" << G4endl;
|
||||
}
|
||||
return x2;
|
||||
}
|
||||
if(ddxPower == -1 && (p<0 || p>1)) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): p is out of range" << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: p is out of range" << G4endl;
|
||||
}
|
||||
return fX2;
|
||||
}
|
||||
|
||||
// check limits
|
||||
if(x2 <= x1 || x1 < fX1 || x2 > fX2) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): domain must have fX1 <= x1 < x2 <= fX2. "
|
||||
<< "You sent x1 = " << x1 << ", x2 = " << x2 << "." << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: domain must have fX1 <= x1 < x2 <= fX2. "
|
||||
<< "You sent x1 = " << x1 << ", x2 = " << x2 << "." << G4endl;
|
||||
}
|
||||
return x2;
|
||||
}
|
||||
|
||||
@@ -298,8 +320,10 @@ G4double G4PolynomialPDF::GetX(G4double p, G4double x1, G4double x2,
|
||||
}
|
||||
if(f == 0) return guess;
|
||||
if(dfdx == 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): got f != 0 but slope = 0 for ddxPower = "
|
||||
<< ddxPower << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: got f != 0 but slope = 0 for ddxPower = "
|
||||
<< ddxPower << G4endl;
|
||||
}
|
||||
return x2;
|
||||
}
|
||||
lastChange = - f/dfdx;
|
||||
@@ -316,13 +340,18 @@ G4double G4PolynomialPDF::GetX(G4double p, G4double x1, G4double x2,
|
||||
++iterations;
|
||||
if(iterations > 50) {
|
||||
if(p!=0) {
|
||||
G4cout << "G4PolynomialPDF::GetX(): got stuck searching for " << p
|
||||
<< " between " << x1 << " and " << x2 << " with ddxPower = "
|
||||
<< ddxPower
|
||||
<< ". Last guess was " << guess << "." << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: got stuck searching for " << p
|
||||
<< " between " << x1 << " and " << x2 << " with ddxPower = "
|
||||
<< ddxPower
|
||||
<< ". Last guess was " << guess << "." << G4endl;
|
||||
}
|
||||
}
|
||||
if(ddxPower==-1 && bisect) {
|
||||
G4cout << "Bisceting and trying again..." << G4endl;
|
||||
if(fVerbose > 0) {
|
||||
G4cout << "G4PolynomialPDF::GetX() WARNING: Bisceting and trying again..."
|
||||
<< G4endl;
|
||||
}
|
||||
return Bisect(p, x1, x2);
|
||||
}
|
||||
else return guess;
|
||||
|
||||
Reference in New Issue
Block a user