2 // ********************************************************************
3 // * License and Disclaimer *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
27 // $Id: G4PhysicsVector.icc 98864 2016-08-15 11:53:26Z gcosmo $
30 //---------------------------------------------------------------
31 // GEANT 4 class source file
33 // G4PhysicsVector.icc
36 // A physics vector which has values of energy-loss, cross-section,
37 // and other physics values of a particle in matter in a given
38 // range of the energy, momentum, etc.
39 // This class serves as the base class for a vector having various
40 // energy scale, for example like 'log', 'linear', 'free', etc.
42 //---------------------------------------------------------------
45 G4double G4PhysicsVector::operator[](const size_t index) const
47 return dataVector[index];
50 //---------------------------------------------------------------
53 G4double G4PhysicsVector::operator()(const size_t index) const
55 return dataVector[index];
58 //---------------------------------------------------------------
61 G4double G4PhysicsVector::Energy(const size_t index) const
63 return binVector[index];
66 //---------------------------------------------------------------
69 G4double G4PhysicsVector::GetMaxEnergy() const
74 //---------------------------------------------------------------
77 size_t G4PhysicsVector::GetVectorLength() const
82 //------------------------------------------------
85 G4double G4PhysicsVector::LinearInterpolation(size_t idx, G4double e) const
87 // Linear interpolation is used to get the value. Before this method
88 // is called it is ensured that the energy is inside the bin
89 // 0 < idx < numberOfNodes-1
91 return dataVector[idx] +
92 ( dataVector[idx + 1]-dataVector[idx] ) * (e - binVector[idx])
93 /( binVector[idx + 1]-binVector[idx] );
96 //---------------------------------------------------------------
99 G4double G4PhysicsVector::SplineInterpolation(size_t idx, G4double e) const
101 // Spline interpolation is used to get the value. Before this method
102 // is called it is ensured that the energy is inside the bin
103 // 0 < idx < numberOfNodes-1
105 static const G4double onesixth = 1.0/6.0;
108 G4double x1 = binVector[idx];
109 G4double x2 = binVector[idx + 1];
110 G4double delta = x2 - x1;
112 G4double a = (x2 - e)/delta;
113 G4double b = (e - x1)/delta;
115 // Final evaluation of cubic spline polynomial for return
116 G4double y1 = dataVector[idx];
117 G4double y2 = dataVector[idx + 1];
119 G4double res = a*y1 + b*y2 +
120 ( (a*a*a - a)*secDerivative[idx] +
121 (b*b*b - b)*secDerivative[idx + 1] )*delta*delta*onesixth;
126 //---------------------------------------------------------------
129 G4double G4PhysicsVector::Interpolation(size_t idx, G4double e) const
131 return useSpline ? SplineInterpolation(idx, e) : LinearInterpolation(idx, e);
134 //---------------------------------------------------------------
137 void G4PhysicsVector::PutValue(size_t index, G4double theValue)
139 if(index >= numberOfNodes) { PrintPutValueError(index); }
140 dataVector[index] = theValue;
143 //---------------------------------------------------------------
146 G4bool G4PhysicsVector::IsFilledVectorExist() const
148 return (numberOfNodes > 0) ? true : false;
151 //---------------------------------------------------------------
154 G4PhysicsVectorType G4PhysicsVector::GetType() const
159 //---------------------------------------------------------------
161 // Flag useSpline is "true" only if second derivatives are filled
163 void G4PhysicsVector::SetSpline(G4bool val)
166 if(0 == secDerivative.size() && 0 < dataVector.size()) {
167 FillSecondDerivatives();
171 secDerivative.clear();
175 //---------------------------------------------------------------
178 void G4PhysicsVector::SetVerboseLevel(G4int value)
180 verboseLevel = value;
183 //---------------------------------------------------------------
186 G4int G4PhysicsVector::GetVerboseLevel() const
191 //---------------------------------------------------------------
194 size_t G4PhysicsVector::FindBinLocation(G4double theEnergy) const
197 if(type == T_G4PhysicsLogVector) {
198 bin = size_t(G4Log(theEnergy)/dBin - baseBin);
199 if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
200 else if(theEnergy > binVector[bin+1]) { ++bin; }
201 } else if(type == T_G4PhysicsLinearVector) {
202 bin = size_t( theEnergy/dBin - baseBin );
203 if(bin > 0 && theEnergy < binVector[bin]) { --bin; }
204 else if(theEnergy > binVector[bin+1]) { ++bin; }
206 // Bin location proposed by K.Genser (FNAL)
207 bin = std::lower_bound(binVector.begin(), binVector.end(), theEnergy)
208 - binVector.begin() - 1;
210 return std::min(bin, numberOfNodes-2);
213 //---------------------------------------------------------------
215 inline size_t G4PhysicsVector::FindBin(G4double e, size_t idx) const
218 if(e < binVector[1]) {
220 } else if(e >= binVector[numberOfNodes-2]) {
221 id = numberOfNodes - 2;
222 } else if(idx >= numberOfNodes || e < binVector[idx]
223 || e > binVector[idx+1]) {
224 id = FindBinLocation(e);
229 //---------------------------------------------------------------
232 G4double G4PhysicsVector::Value(G4double theEnergy) const
235 return Value(theEnergy, idx);
238 //---------------------------------------------------------------
241 G4double G4PhysicsVector::GetValue(G4double theEnergy, G4bool&) const
244 return Value(theEnergy, idx);
247 //---------------------------------------------------------------