source: trunk/source/global/management/src/G4PhysicsVector.cc @ 924

Last change on this file since 924 was 921, checked in by garnier, 15 years ago

en test de gl2ps. Problemes de libraries

File size: 8.0 KB
RevLine 
[833]1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
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.                             *
10// *                                                                  *
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.         *
17// *                                                                  *
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// ********************************************************************
25//
26//
[921]27// $Id: G4PhysicsVector.cc,v 1.27 2008/10/16 12:14:36 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-02-cand-01 $
[833]29//
30//
31// --------------------------------------------------------------
32//      GEANT 4 class implementation file
33//
34//  G4PhysicsVector.cc
35//
36//  History:
37//    02 Dec. 1995, G.Cosmo : Structure created based on object model
38//    03 Mar. 1996, K.Amako : Implemented the 1st version
39//    01 Jul. 1996, K.Amako : Hidden bin from the user introduced
40//    12 Nov. 1998, K.Amako : A bug in GetVectorLength() fixed
41//    11 Nov. 2000, H.Kurashige : use STL vector for dataVector and binVector
42//    18 Jan. 2001, H.Kurashige : removed ptrNextTable
43//    09 Mar. 2001, H.Kurashige : added G4PhysicsVector type
[850]44//    05 Sep. 2008, V.Ivanchenko : added protections for zero-length vector
[833]45// --------------------------------------------------------------
46
47#include "G4PhysicsVector.hh"
48#include <iomanip>
49
[850]50// --------------------------------------------------------------
51
52G4PhysicsVector::G4PhysicsVector(G4bool spline)
[833]53 : type(T_G4PhysicsVector),
[921]54   edgeMin(0.), edgeMax(0.), numberOfBin(0),
[850]55   lastEnergy(0.), lastValue(0.), lastBin(0), 
56   secDerivative(0), useSpline(spline)
[921]57{}
[833]58
[850]59// --------------------------------------------------------------
60
[833]61G4PhysicsVector::~G4PhysicsVector() 
62{
[921]63  DeleteData();
[833]64}
65
[850]66// --------------------------------------------------------------
67
[833]68G4PhysicsVector::G4PhysicsVector(const G4PhysicsVector& right)
69{
[921]70  CopyData(right);
[833]71}
72
[850]73// --------------------------------------------------------------
74
[833]75G4PhysicsVector& G4PhysicsVector::operator=(const G4PhysicsVector& right)
76{
77  if (&right==this)  { return *this; }
78  if (type != right.type)  { return *this; }
79
[921]80  DeleteData();
81  CopyData(right);
82
[833]83  return *this;
84}
85
[850]86// --------------------------------------------------------------
87
[833]88G4int G4PhysicsVector::operator==(const G4PhysicsVector &right) const
89{
90  return (this == &right);
91}
92
[850]93// --------------------------------------------------------------
94
[833]95G4int G4PhysicsVector::operator!=(const G4PhysicsVector &right) const
96{
97  return (this != &right);
98}
99
[850]100// --------------------------------------------------------------
101
[921]102void G4PhysicsVector::DeleteData()
103{
104  delete [] secDerivative;
105  secDerivative = 0;
106}
107
108// --------------------------------------------------------------
109
110void G4PhysicsVector::CopyData(const G4PhysicsVector& vec)
111{
112  type = vec.type;
113  edgeMin = vec.edgeMin;
114  edgeMax = vec.edgeMax;
115  numberOfBin = vec.numberOfBin;
116  lastEnergy = vec.lastEnergy;
117  lastValue = vec.lastValue;
118  lastBin = vec.lastBin;
119  dataVector = vec.dataVector;
120  binVector = vec.binVector;
121  useSpline = vec.useSpline;
122  comment = vec.comment;
123  if (vec.secDerivative)
124  {
125    secDerivative = new G4double [numberOfBin];
126    for (size_t i=0; i<numberOfBin; i++)
127    {
128       secDerivative[i] = vec.secDerivative[i];
129    }
130  }
131  else
132  {
133    secDerivative = 0;
134  }
135}
136
137// --------------------------------------------------------------
138
[833]139G4double G4PhysicsVector::GetLowEdgeEnergy(size_t binNumber) const
140{
141  return binVector[binNumber];
142}
143
[850]144// --------------------------------------------------------------
145
[833]146G4bool G4PhysicsVector::Store(std::ofstream& fOut, G4bool ascii)
147{
148  // Ascii mode
149  if (ascii)
150  {
151    fOut << *this;
152    return true;
153  } 
154  // Binary Mode
155
156  // binning
157  fOut.write((char*)(&edgeMin), sizeof edgeMin);
158  fOut.write((char*)(&edgeMax), sizeof edgeMax);
159  fOut.write((char*)(&numberOfBin), sizeof numberOfBin);
160
161  // contents
162  size_t size = dataVector.size(); 
163  fOut.write((char*)(&size), sizeof size);
164
165  G4double* value = new G4double[2*size];
166  for(size_t i = 0; i < size; i++)
167  {
168    value[2*i]  =  binVector[i];
169    value[2*i+1]=  dataVector[i];
170  }
171  fOut.write((char*)(value), 2*size*(sizeof (G4double)));
172  delete [] value;
173
174  return true;
175}
176
[850]177// --------------------------------------------------------------
178
[833]179G4bool G4PhysicsVector::Retrieve(std::ifstream& fIn, G4bool ascii)
180{
181  // clear properties;
182  lastEnergy=0.;
183  lastValue =0.;
184  lastBin   =0;
185  dataVector.clear();
186  binVector.clear();
187  comment = "";
188
189  // retrieve in ascii mode
190  if (ascii)
191  {
192    // binning
193    fIn >> edgeMin >> edgeMax >> numberOfBin; 
194    if (fIn.fail())  { return false; }
195    // contents
196    size_t size=0;
197    fIn >> size;
198    if (fIn.fail())  { return false; }
199
200    binVector.reserve(size);
201    dataVector.reserve(size);
[850]202    G4double vBin, vData;
203
[833]204    for(size_t i = 0; i < size ; i++)
205    {
[850]206      vBin = 0.;
207      vData= 0.;
[833]208      fIn >> vBin >> vData;
209      if (fIn.fail())  { return false; }
210      binVector.push_back(vBin);
211      dataVector.push_back(vData);
212    }
213    return true ;
214  }
215
216  // retrieve in binary mode
217  // binning
218  fIn.read((char*)(&edgeMin), sizeof edgeMin);
219  fIn.read((char*)(&edgeMax), sizeof edgeMax);
220  fIn.read((char*)(&numberOfBin), sizeof numberOfBin ); 
221 
222  // contents
223  size_t size;
224  fIn.read((char*)(&size), sizeof size); 
225 
226  G4double* value = new G4double[2*size];
227  fIn.read((char*)(value),  2*size*(sizeof(G4double)) );
[850]228  if (G4int(fIn.gcount()) != G4int(2*size*(sizeof(G4double))) )
229  {
[833]230    delete [] value;
231    return false;
232  }
233
234  binVector.reserve(size);
235  dataVector.reserve(size);
[850]236  for(size_t i = 0; i < size; i++)
237  {
[833]238    binVector.push_back(value[2*i]);
239    dataVector.push_back(value[2*i+1]);
240  }
241  delete [] value;
242  return true;
243}
[850]244
245// --------------------------------------------------------------
246
247void G4PhysicsVector::FillSecondDerivatives()
248{ 
249  secDerivative = new G4double [numberOfBin];
250
[921]251  size_t n = numberOfBin-1;
[850]252
253  // cannot compute derivatives for less than 3 points
[921]254  if(3 > numberOfBin)
255  {
256    secDerivative[0] = 0.0;
257    secDerivative[n] = 0.0;
[850]258    return;
259  }
260
[921]261  for(size_t i=1; i<n; i++)
[850]262  {
[921]263    secDerivative[i] = 
264      3.0*((dataVector[i+1]-dataVector[i])/(binVector[i+1]-binVector[i]) -
265           (dataVector[i]-dataVector[i-1])/(binVector[i]-binVector[i-1]))
266      /(binVector[i+1]-binVector[i-1]);
[850]267  }
[921]268  secDerivative[n] = secDerivative[n-1];
269  secDerivative[0] = secDerivative[1];
[850]270}
271   
272// --------------------------------------------------------------
273
[833]274std::ostream& operator<<(std::ostream& out, const G4PhysicsVector& pv)
275{
276  // binning
277  out << std::setprecision(12) << pv.edgeMin;
278  out <<" " << pv.edgeMax <<" "  << pv.numberOfBin << G4endl; 
279
280  // contents
281  out << pv.dataVector.size() << G4endl; 
282  for(size_t i = 0; i < pv.dataVector.size(); i++)
283  {
284    out << std::setprecision(12) << pv.binVector[i] << "  "
285        << pv.dataVector[i] << G4endl;
286  }
287  return out;
288}
Note: See TracBrowser for help on using the repository browser.