source: trunk/source/processes/electromagnetic/lowenergy/test/fluoTest/src/XrayFluoDataSet.cc @ 1199

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

nvx fichiers dans CVS

File size: 5.2 KB
Line 
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 <strstream>
41#include "G4VDataSetAlgorithm.hh"
42
43XrayFluoDataSet::XrayFluoDataSet(G4int Z,
44                         G4DataVector* points, 
45                         G4DataVector* values,
46                         const G4VDataSetAlgorithm* interpolation,
47                         G4double unitE, G4double unitData)
48  :z(Z), energies(points), data(values), algorithm(interpolation)
49{
50  numberOfBins = energies->size();
51  unit1 = unitE;
52  unit2 = unitData;
53}
54
55XrayFluoDataSet:: XrayFluoDataSet(G4int Z, 
56                          const G4String& dataFile,
57                          const G4VDataSetAlgorithm* interpolation,
58                          G4double unitE, G4double unitData)
59  :z(Z), algorithm(interpolation)
60{
61  energies = new G4DataVector;
62  data = new G4DataVector;
63  unit1 = unitE;
64  unit2 = unitData; 
65  LoadData(dataFile);
66  numberOfBins = energies->size();
67}
68
69
70// Destructor
71
72XrayFluoDataSet::~XrayFluoDataSet()
73{ 
74  delete energies;
75  delete data;
76}
77
78
79G4double XrayFluoDataSet::FindValue(G4double e, G4int id) const
80{
81  G4double value;
82  G4double e0 = (*energies)[0];
83  // Protections
84  size_t bin = FindBinLocation(e);
85  if (bin == numberOfBins)
86    {
87     
88      value = (*data)[bin];
89    }
90  else if (e <= e0)
91    {
92   
93      value = (*data)[0];
94    }
95  else
96    {
97      value = algorithm->Calculate(e,bin,*energies,*data);
98    }
99 
100  return value;
101}
102
103G4int XrayFluoDataSet::FindBinLocation(G4double energy) const
104{
105  // Protection against call outside allowed range
106  G4double e0 = (*energies)[0];
107  if (energy < e0)
108    {
109   
110      energy = e0;
111    }
112
113  size_t lowerBound = 0;
114  size_t upperBound = numberOfBins - 1;
115 
116  // Binary search
117  while (lowerBound <= upperBound) 
118    {
119      size_t midBin = (lowerBound + upperBound)/2;
120      if ( energy < (*energies)[midBin] ) upperBound = midBin-1;
121      else lowerBound = midBin+1;
122  }
123 
124  return upperBound;
125}
126
127
128void XrayFluoDataSet::LoadData(const G4String& fileName)
129{
130  // Build the complete string identifying the file with the data set
131 
132  char nameChar[100] = {""};
133  std::ostrstream ost(nameChar, 100, std::ios::out);
134 
135  ost << fileName <<".dat";
136 
137  G4String name(nameChar);
138 
139  char* path = getenv("G4INSTALL");
140 
141  G4String pathString(path);
142  G4String dirFile = pathString + "/" + name;
143  std::ifstream file(dirFile);
144  std::filebuf* lsdp = file.rdbuf();
145 
146  if (! (lsdp->is_open()) )
147        {
148          G4String excep = "XrayFluoDataSet - data file: " + dirFile + " not found";
149          G4Exception(excep);
150        }
151  G4double a = 0;
152  G4int k = 1;
153
154  do
155    {
156      file >> a;
157      G4int nColumns = 2;
158      // The file is organized into two columns:
159      // 1st column is the energy
160      // 2nd column is the corresponding value
161      // The file terminates with the pattern: -1   -1
162      //                                       -2   -2
163      if (a == -1 || a == -2)
164        {
165         
166        }
167      else
168        {
169          if (k%nColumns != 0)
170            {   
171              G4double e = a * unit1;
172              energies->push_back(e);
173           
174              k++;
175
176            }
177          else if (k%nColumns == 0)
178            {
179              G4double value = a * unit2;
180              data->push_back(value);
181             
182              k = 1;
183            }
184        }
185     
186    } while (a != -2); // end of file
187 
188  file.close();
189}
190void XrayFluoDataSet::PrintData() const
191{
192  size_t size = numberOfBins;
193  for (size_t i=0; i<size; i++)
194    {
195      G4double e = (*energies)[i]  / unit1;
196      G4double sigma = (*data)[i] / unit2 ;
197      G4cout << "Point: "
198             << e
199             << " - Data value : "
200             << sigma
201             << G4endl; 
202    }
203}
204
205
206
207
208
Note: See TracBrowser for help on using the repository browser.