Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XrayFluoDataSet.cc
Go to the documentation of this file.
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 //
27 // $Id: XrayFluoDAtaSet.cc
28 // GEANT4 tag $Name: xray_fluo-V03-02-00
29 //
30 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it)
31 //
32 // History:
33 // -----------
34 // 28 Nov 2001 Elena Guardincerri Created
35 //
36 // -------------------------------------------------------------------
37 
38 #include "XrayFluoDataSet.hh"
39 #include <fstream>
40 #include "G4VDataSetAlgorithm.hh"
41 
43  G4DataVector* points,
44  G4DataVector* values,
45  const G4VDataSetAlgorithm* interpolation,
46  G4double unitE, G4double unitData)
47  :energies(points), data(values), algorithm(interpolation)
48 {
49  numberOfBins = energies->size();
50  unit1 = unitE;
51  unit2 = unitData;
52 }
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
55 
57  const G4String& dataFile,
58  const G4VDataSetAlgorithm* interpolation,
59  G4double unitE, G4double unitData)
60  : algorithm(interpolation)
61 {
62  energies = new G4DataVector;
63  data = new G4DataVector;
64  unit1 = unitE;
65  unit2 = unitData;
66  LoadData(dataFile);
67  numberOfBins = energies->size();
68 }
69 
70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
71 
72 // Destructor
74 {
75  delete energies;
76  delete data;
77 }
78 
79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
80 
82 {
84  G4double e0 = (*energies)[0];
85  // Protections
86  size_t bin = FindBinLocation(e);
87  if (bin == numberOfBins)
88  {
89 
90  value = (*data)[bin];
91  }
92  else if (e <= e0)
93  {
94 
95  value = (*data)[0];
96  }
97  else
98  {
99  value = algorithm->Calculate(e,bin,*energies,*data);
100  }
101 
102  return value;
103 }
104 
105 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
106 
107 G4int XrayFluoDataSet::FindBinLocation(G4double energy) const
108 {
109  // Protection against call outside allowed range
110  G4double e0 = (*energies)[0];
111  if (energy < e0)
112  {
113 
114  energy = e0;
115  }
116 
117  size_t lowerBound = 0;
118  size_t upperBound = numberOfBins - 1;
119 
120  // Binary search
121  while (lowerBound <= upperBound)
122  {
123  size_t midBin = (lowerBound + upperBound)/2;
124  if ( energy < (*energies)[midBin] ) upperBound = midBin-1;
125  else lowerBound = midBin+1;
126  }
127 
128  return upperBound;
129 }
130 
131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
132 
133 G4bool XrayFluoDataSet::LoadData(const G4String& fileName)
134 {
135  // Build the complete string identifying the file with the data set
136  G4String dirFile = "";
137 
138  char* path;
139 
140  path = getenv("XRAYDATA");
141  if (!path)
142  path = getenv("PWD");
143 
144  //G4cout << path << G4endl;
145  //G4cout << fileName << G4endl;
146 
147 
148  G4String pathString(path);
149  pathString += "\0";
150  dirFile = pathString + "/" + fileName + ".dat";
151 
152  std::ifstream file(dirFile);
153  std::filebuf* lsdp = file.rdbuf();
154 
155  if (! (lsdp->is_open()) )
156  {
158  execp << "XrayFluoDataSet - data file: " + dirFile + " not found"<<G4endl;
159  G4Exception("XrayFluoDataSet::LoadData()","example-xray_fluorescence01",
160  FatalException, execp);
161  }
162  G4double a = 0;
163  G4int k = 1;
164 
165  do
166  {
167  file >> a;
168  G4int nColumns = 2;
169  // The file is organized into two columns:
170  // 1st column is the energy
171  // 2nd column is the corresponding value
172  // The file terminates with the pattern: -1 -1
173  // -2 -2
174  if (a == -1 || a == -2)
175  {
176 
177  }
178  else
179  {
180  if (k%nColumns != 0)
181  {
182  G4double e = a * unit1;
183  energies->push_back(e);
184 
185  k++;
186 
187  }
188  else if (k%nColumns == 0)
189  {
190  G4double value = a * unit2;
191  data->push_back(value);
192 
193  k = 1;
194  }
195  }
196 
197  } while (a != -2); // end of file
198 
199  file.close();
200  return true;
201 
202 }
203 
204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
205 
207 {
208  size_t size = numberOfBins;
209  for (size_t i=0; i<size; i++)
210  {
211  G4double e = (*energies)[i] / unit1;
212  G4double sigma = (*data)[i] / unit2 ;
213  G4cout << "Point: "
214  << e
215  << " - Data value : "
216  << sigma
217  << G4endl;
218  }
219 }
220 
221 
222 
XrayFluoDataSet(G4int Z, G4DataVector *points, G4DataVector *values, const G4VDataSetAlgorithm *interpolation, G4double unitE=CLHEP::MeV, G4double unitData=CLHEP::barn)
tuple bin
Definition: plottest35.py:22
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
int G4int
Definition: G4Types.hh:78
virtual G4double Calculate(G4double point, G4int bin, const G4DataVector &energies, const G4DataVector &data) const =0
const XML_Char const XML_Char * data
Definition: expat.h:268
void PrintData() const
G4GLOB_DLL std::ostream G4cout
const XML_Char int const XML_Char * value
Definition: expat.h:331
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double energy(const ThreeVector &p, const G4double m)
G4double FindValue(G4double e, G4int) const
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76