G4DataSet Class Reference

#include <G4DataSet.hh>

Inheritance diagram for G4DataSet:

G4IDataSet

Public Member Functions

 G4DataSet (G4int argZ, G4IInterpolator *algo, G4double xUnit=CLHEP::MeV, G4double yUnit=CLHEP::barn, G4bool random=false)
 G4DataSet (G4int argZ, G4DataVector *xData, G4DataVector *data, G4IInterpolator *algo, G4double xUnit=CLHEP::MeV, G4double yUnit=CLHEP::barn, G4bool random=false)
virtual ~G4DataSet ()
virtual G4double FindValue (G4double x, G4int componentId=0) const
virtual void PrintData (void) const
virtual const G4IDataSetGetComponent (G4int) const
virtual void AddComponent (G4IDataSet *)
virtual size_t NumberOfComponents (void) const
virtual const G4DataVectorGetEnergies (G4int) const
virtual const G4DataVectorGetData (G4int) const
virtual void SetEnergiesData (G4DataVector *xData, G4DataVector *data, G4int componentId)
virtual G4bool LoadData (const G4String &fileName)
virtual G4bool SaveData (const G4String &fileName) const
virtual G4double RandomSelect (G4int componentId=0) const

Detailed Description

Definition at line 54 of file G4DataSet.hh.


Constructor & Destructor Documentation

G4DataSet::G4DataSet ( G4int  argZ,
G4IInterpolator algo,
G4double  xUnit = CLHEP::MeV,
G4double  yUnit = CLHEP::barn,
G4bool  random = false 
)

Definition at line 47 of file G4DataSet.cc.

References FatalException, G4DataSet(), and G4Exception().

Referenced by G4DataSet().

00051                                    : 
00052   z(Z),
00053   energies(0),
00054   data(0),
00055   algorithm(algo),
00056   unitEnergies(xUnit),
00057   unitData(yUnit),
00058   pdf(0),
00059   randomSet(random)
00060 {
00061   if (algorithm == 0) G4Exception("G4DataSet::G4DataSet",
00062                                   "pii00000101",
00063                                   FatalException,
00064                                   "Interpolation == 0");
00065   if (randomSet) BuildPdf();
00066 }

G4DataSet::G4DataSet ( G4int  argZ,
G4DataVector xData,
G4DataVector data,
G4IInterpolator algo,
G4double  xUnit = CLHEP::MeV,
G4double  yUnit = CLHEP::barn,
G4bool  random = false 
)

Definition at line 68 of file G4DataSet.cc.

References FatalException, G4DataSet(), and G4Exception().

00074                                    :
00075   z(argZ),
00076   energies(dataX),
00077   data(dataY),
00078   algorithm(algo),
00079   unitEnergies(xUnit),
00080   unitData(yUnit),
00081   pdf(0),
00082   randomSet(random)
00083 {
00084   if (algorithm == 0) G4Exception("G4DataSet::G4DataSet",
00085                                   "pii00000110",
00086                                   FatalException,
00087                                   "Interpolation == 0");
00088 
00089   if ((energies == 0) ^ (data == 0))
00090     G4Exception("G4DataSet::G4DataSet",
00091                 "pii00000111-",
00092                 FatalException,  
00093                 "different size for energies and data (zero case)");
00094 
00095   if (energies == 0) return;
00096   
00097   if (energies->size() != data->size()) 
00098     G4Exception("G4DataSet::G4DataSet",
00099                 "pii00000112",
00100                 FatalException, 
00101                 "different size for energies and data");
00102 
00103   if (randomSet) BuildPdf();
00104 }

G4DataSet::~G4DataSet (  )  [virtual]

Definition at line 106 of file G4DataSet.cc.

00107 { 
00108   delete algorithm;
00109   if (energies) delete energies;
00110   if (data) delete data;
00111   if (pdf) delete pdf;
00112 }


Member Function Documentation

virtual void G4DataSet::AddComponent ( G4IDataSet  )  [inline, virtual]

Implements G4IDataSet.

Definition at line 79 of file G4DataSet.hh.

00079 {}

G4double G4DataSet::FindValue ( G4double  x,
G4int  componentId = 0 
) const [virtual]

Implements G4IDataSet.

Definition at line 114 of file G4DataSet.cc.

References G4IInterpolator::Calculate(), FatalException, and G4Exception().

00115 {
00116   if (!energies) G4Exception("G4DataSet::FindValue",
00117                              "pii00000120",
00118                              FatalException,  
00119                              "energies == 0");
00120   if (energies->empty()) return 0;
00121   if (energy <= (*energies)[0]) return (*data)[0];
00122 
00123   size_t i = energies->size()-1;
00124   if (energy >= (*energies)[i]) return (*data)[i];
00125 
00126   G4double interpolated = algorithm->Calculate(energy,FindLowerBound(energy),*energies,*data);
00127   return interpolated;
00128 }

virtual const G4IDataSet* G4DataSet::GetComponent ( G4int   )  const [inline, virtual]

Implements G4IDataSet.

Definition at line 77 of file G4DataSet.hh.

00077 { return 0; }

virtual const G4DataVector& G4DataSet::GetData ( G4int   )  const [inline, virtual]

Implements G4IDataSet.

Definition at line 84 of file G4DataSet.hh.

00084 { return *data; }

virtual const G4DataVector& G4DataSet::GetEnergies ( G4int   )  const [inline, virtual]

Implements G4IDataSet.

Definition at line 83 of file G4DataSet.hh.

00083 { return *energies; }

G4bool G4DataSet::LoadData ( const G4String fileName  )  [virtual]

Implements G4IDataSet.

Definition at line 176 of file G4DataSet.cc.

References FatalException, G4Exception(), and SetEnergiesData().

00177 {
00178   // The file is organized into two columns:
00179   // 1st column is the energy
00180   // 2nd column is the corresponding value
00181   // The file terminates with the pattern: -1   -1
00182   //                                       -2   -2
00183  
00184   G4String fullFileName(FullFileName(fileName));
00185   std::ifstream in(fullFileName);
00186 
00187   if (!in.is_open())
00188     {
00189 
00190       std::ostringstream message;
00191       message << "G4DataSet::LoadData - data file " << fullFileName << " not found";
00192 
00193       G4Exception("G4CompositeDataSet::LoadData",
00194                   "pii00000140",
00195                   FatalException,
00196                   message.str().c_str());
00197     }
00198 
00199   G4DataVector* argEnergies=new G4DataVector;
00200   G4DataVector* argData=new G4DataVector;
00201 
00202   G4double a;
00203   bool energyColumn(true);
00204 
00205   do
00206     {
00207       in >> a;
00208   
00209       if (a!=-1 && a!=-2)
00210         {
00211           if (energyColumn)
00212             {
00213               // std::cout << fullFileName << ", a = " << a <<std::endl;
00214               argEnergies->push_back(a*unitEnergies);
00215             }
00216           else
00217             argData->push_back(a*unitData);
00218           energyColumn=(!energyColumn);
00219         }
00220     }
00221   while (a != -2);
00222  
00223   SetEnergiesData(argEnergies, argData, 0);
00224   if (randomSet) BuildPdf();
00225  
00226   return true;
00227 }

virtual size_t G4DataSet::NumberOfComponents ( void   )  const [inline, virtual]

Implements G4IDataSet.

Definition at line 81 of file G4DataSet.hh.

00081 { return 0; }

void G4DataSet::PrintData ( void   )  const [virtual]

Implements G4IDataSet.

Definition at line 131 of file G4DataSet.cc.

References G4cout, and G4endl.

00132 {
00133   if (!energies)
00134     {
00135       G4cout << "Data not available." << G4endl;
00136     }
00137   else
00138     {
00139       size_t size = energies->size();
00140       for (size_t i(0); i<size; i++)
00141         {
00142           G4cout << "Point: " << ((*energies)[i]/unitEnergies)
00143                  << " - Data value: " << ((*data)[i]/unitData);
00144           if (pdf != 0) G4cout << " - PDF : " << (*pdf)[i];
00145           G4cout << G4endl; 
00146         }
00147     }
00148 }

G4double G4DataSet::RandomSelect ( G4int  componentId = 0  )  const [virtual]

Implements G4IDataSet.

Definition at line 383 of file G4DataSet.cc.

References G4IInterpolator::Calculate(), G4LinInterpolation::Calculate(), FatalException, G4Exception(), and G4UniformRand.

00384 {
00385   // Random select a X value according to the cumulative probability distribution
00386   // derived from the data
00387 
00388   if (!pdf) G4Exception("G4DataSet::RandomSelect",
00389                         "pii00000170",
00390                         FatalException, 
00391                         "PDF has not been created for this data set");
00392 
00393   G4double value = 0.;
00394   G4double x = G4UniformRand();
00395 
00396   // Locate the random value in the X vector based on the PDF
00397   size_t bin = FindLowerBound(x,pdf);
00398 
00399   // Interpolate the PDF to calculate the X value: 
00400   // linear interpolation in the first bin (to avoid problem with 0),
00401   // interpolation with associated data set algorithm in other bins
00402 
00403   G4LinInterpolation linearAlgo;
00404   if (bin == 0) value = linearAlgo.Calculate(x, bin, *pdf, *energies);
00405   else value = algorithm->Calculate(x, bin, *pdf, *energies);
00406 
00407   //  G4cout << x << " random bin "<< bin << " - " << value << G4endl;
00408   return value;
00409 }

G4bool G4DataSet::SaveData ( const G4String fileName  )  const [virtual]

Implements G4IDataSet.

Definition at line 229 of file G4DataSet.cc.

References FatalException, and G4Exception().

00230 {
00231   // The file is organized into two columns:
00232   // 1st column is the energy
00233   // 2nd column is the corresponding value
00234   // The file terminates with the pattern: -1   -1
00235   //                                       -2   -2
00236  
00237   G4String fullFileName(FullFileName(name));
00238   std::ofstream out(fullFileName);
00239 
00240   if (!out.is_open())
00241     {
00242  
00243       std::ostringstream message;
00244       message << "G4DataSet:: SaveData - cannot open " << fullFileName;
00245 
00246       G4Exception("G4CompositeDataSet::SaveData",
00247                   "pii00000150",
00248                   FatalException,
00249                   message.str().c_str());
00250 
00251     }
00252  
00253   out.precision(10);
00254   out.width(15);
00255   out.setf(std::ofstream::left);
00256   
00257   if (energies!=0 && data!=0)
00258     {
00259       G4DataVector::const_iterator i(energies->begin());
00260       G4DataVector::const_iterator endI(energies->end());
00261       G4DataVector::const_iterator j(data->begin());
00262   
00263       while (i!=endI)
00264         {
00265           out.precision(10);
00266           out.width(15);
00267           out.setf(std::ofstream::left);
00268           out << ((*i)/unitEnergies) << ' ';
00269 
00270           out.precision(10);
00271           out.width(15);
00272           out.setf(std::ofstream::left);
00273           out << ((*j)/unitData) << std::endl;
00274 
00275           i++;
00276           j++;
00277         }
00278     }
00279  
00280   out.precision(10);
00281   out.width(15);
00282   out.setf(std::ofstream::left);
00283   out << -1.f << ' ';
00284 
00285   out.precision(10);
00286   out.width(15);
00287   out.setf(std::ofstream::left);
00288   out << -1.f << std::endl;
00289 
00290   out.precision(10);
00291   out.width(15);
00292   out.setf(std::ofstream::left);
00293   out << -2.f << ' ';
00294 
00295   out.precision(10);
00296   out.width(15);
00297   out.setf(std::ofstream::left);
00298   out << -2.f << std::endl;
00299 
00300   return true;
00301 }

void G4DataSet::SetEnergiesData ( G4DataVector xData,
G4DataVector data,
G4int  componentId 
) [virtual]

Implements G4IDataSet.

Definition at line 151 of file G4DataSet.cc.

References FatalException, and G4Exception().

Referenced by LoadData().

00154 {
00155   if (energies) delete energies;
00156   energies = dataX;
00157 
00158   if (data) delete data;
00159   data = dataY;
00160  
00161   if ((energies == 0) ^ (data==0)) 
00162     G4Exception("G4DataSet::SetEnergiesData",
00163                 "pii00000130",
00164                 FatalException, 
00165                 "different size for energies and data (zero case)");
00166 
00167   if (energies == 0) return;
00168   
00169   if (energies->size() != data->size()) 
00170     G4Exception("G4DataSet::SetEnergiesData",
00171                 "pii00000131",
00172                 FatalException, 
00173                 "different size for energies and data");
00174 }


The documentation for this class was generated from the following files:
Generated on Mon May 27 17:51:44 2013 for Geant4 by  doxygen 1.4.7