G4PhysicsOrderedFreeVector.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00030 // PhysicsOrderedFreeVector Class Implementation
00032 //
00033 // File:        G4PhysicsOrderedFreeVector.cc
00034 // Version:     2.0
00035 // Created:     1996-08-13
00036 // Author:      Juliet Armstrong
00037 // Updated:     1997-03-25 by Peter Gumplinger
00038 //              > cosmetics (only)
00039 //              1998-11-11 by Peter Gumplinger
00040 //              > initialize all data members of the base class in 
00041 //                derived class constructors
00042 //              2000-11-11 by H.Kurashige
00043 //              > use STL vector for dataVector and binVector
00044 //              19 Jun. 2009-06-19 by V.Ivanchenko 
00045 //              > removed hidden bin 
00046 //
00047 // mail:        gum@triumf.ca
00048 //
00050 
00051 #include "G4PhysicsOrderedFreeVector.hh"
00052 
00054 // Class Implementation
00056 
00058         // Constructors
00060 
00061 G4PhysicsOrderedFreeVector::G4PhysicsOrderedFreeVector(G4double *Energies,
00062                                                        G4double *Values,
00063                                                        size_t VectorLength)
00064   : G4PhysicsVector()
00065 {
00066   type = T_G4PhysicsOrderedFreeVector;
00067 
00068   for (size_t i = 0 ; i < VectorLength ; i++)
00069     {
00070       InsertValues(Energies[i], Values[i]);
00071     }
00072 }
00073 
00074 G4PhysicsOrderedFreeVector::G4PhysicsOrderedFreeVector()
00075   : G4PhysicsVector()
00076 {
00077   type = T_G4PhysicsOrderedFreeVector;
00078 }
00079 
00081         // Destructors
00083 
00084 G4PhysicsOrderedFreeVector::~G4PhysicsOrderedFreeVector() {}
00085 
00087         // Methods
00089   
00090 void G4PhysicsOrderedFreeVector::InsertValues(G4double energy, G4double value)
00091 {
00092         std::vector<G4double>::iterator binLoc =
00093                  std::lower_bound(binVector.begin(), binVector.end(), energy);
00094 
00095         size_t binIdx = binLoc - binVector.begin();     // Iterator difference!
00096 
00097         std::vector<G4double>::iterator dataLoc = dataVector.begin() + binIdx;
00098 
00099         binVector.insert(binLoc, energy);
00100         dataVector.insert(dataLoc, value);
00101 
00102         numberOfNodes++;
00103         edgeMin = binVector.front();
00104         edgeMax = binVector.back();
00105 }
00106 
00107 G4double  G4PhysicsOrderedFreeVector::GetLowEdgeEnergy(size_t binNumber) const
00108 {
00109         return binVector[binNumber];
00110 } 
00111 
00112 G4double  G4PhysicsOrderedFreeVector::GetEnergy(G4double aValue)
00113 {
00114 
00115         if (aValue <= GetMinValue()) {
00116                 return GetMinLowEdgeEnergy();
00117         } else if (aValue >= GetMaxValue()) {
00118                 return GetMaxLowEdgeEnergy();
00119         } else { 
00120         size_t closestBin = FindValueBinLocation(aValue);
00121         G4double theEnergy = LinearInterpolationOfEnergy(aValue, closestBin);
00122 
00123         return theEnergy;
00124         }
00125 }
00126 
00127 size_t G4PhysicsOrderedFreeVector::FindValueBinLocation(G4double aValue)
00128 {
00129    G4int n1 = 0;
00130    G4int n2 = numberOfNodes/2;
00131    G4int n3 = numberOfNodes - 1;
00132    while (n1 != n3 - 1) {
00133       if (aValue > dataVector[n2])
00134          { n1 = n2; }
00135       else
00136          { n3 = n2; }
00137       n2 = n1 + (n3 - n1 + 1)/2;
00138    }
00139    return (size_t)n1;
00140 }
00141 
00142 G4double G4PhysicsOrderedFreeVector::LinearInterpolationOfEnergy(G4double aValue,
00143                                                                  size_t theLocBin)
00144 {
00145   G4double intplFactor = (aValue-dataVector[theLocBin])
00146      / (dataVector[theLocBin+1]-dataVector[theLocBin]); // Interpolation factor
00147 
00148   return binVector[theLocBin] +
00149          ( binVector[theLocBin+1]-binVector[theLocBin] ) * intplFactor;
00150 }
00151 
00152 
00153 size_t G4PhysicsOrderedFreeVector::FindBinLocation(G4double theEnergy) const
00154 {
00155    G4int n1 = 0;
00156    G4int n2 = numberOfNodes/2;
00157    G4int n3 = numberOfNodes - 1;
00158    while (n1 != n3 - 1)
00159    {
00160       if (theEnergy > binVector[n2])
00161          { n1 = n2; }
00162       else
00163          { n3 = n2; }
00164       n2 = n1 + (n3 - n1 + 1)/2;
00165    }
00166    return (size_t)n1;
00167 }

Generated on Mon May 27 17:49:19 2013 for Geant4 by  doxygen 1.4.7