ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XrayFluoDataSet.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file XrayFluoDataSet.cc
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 //
28 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it)
29 //
30 // History:
31 // -----------
32 // 28 Nov 2001 Elena Guardincerri Created
33 //
34 // -------------------------------------------------------------------
35 
36 #include "XrayFluoDataSet.hh"
37 #include <fstream>
38 #include "G4VDataSetAlgorithm.hh"
39 
41  G4DataVector* points,
43  const G4VDataSetAlgorithm* interpolation,
44  G4double unitE, G4double unitData)
45  :energies(points), data(values), algorithm(interpolation)
46 {
47  numberOfBins = energies->size();
48  unit1 = unitE;
49  unit2 = unitData;
50 }
51 
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
53 
55  const G4String& dataFile,
56  const G4VDataSetAlgorithm* interpolation,
57  G4double unitE, G4double unitData)
58  : algorithm(interpolation)
59 {
60  energies = new G4DataVector;
61  data = new G4DataVector;
62  unit1 = unitE;
63  unit2 = unitData;
64  LoadData(dataFile);
65  numberOfBins = energies->size();
66 }
67 
68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
69 
70 // Destructor
72 {
73  delete energies;
74  delete data;
75 }
76 
77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
78 
80 {
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 
103 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
104 
106 {
107  // Protection against call outside allowed range
108  G4double e0 = (*energies)[0];
109  if (energy < e0)
110  {
111 
112  energy = e0;
113  }
114 
115  size_t lowerBound = 0;
116  size_t upperBound = numberOfBins - 1;
117 
118  // Binary search
119  while (lowerBound <= upperBound)
120  {
121  size_t midBin = (lowerBound + upperBound)/2;
122  if ( energy < (*energies)[midBin] ) upperBound = midBin-1;
123  else lowerBound = midBin+1;
124  }
125 
126  return upperBound;
127 }
128 
129 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
130 
132 {
133  // Build the complete string identifying the file with the data set
134  G4String dirFile = "";
135 
136  char* path;
137 
138  path = std::getenv("XRAYDATA");
139  if (!path)
140  path = std::getenv("PWD");
141 
142  //G4cout << path << G4endl;
143  //G4cout << fileName << G4endl;
144 
145 
146  G4String pathString(path);
147  pathString += "\0";
148  dirFile = pathString + "/" + fileName + ".dat";
149 
150  std::ifstream file(dirFile);
151  std::filebuf* lsdp = file.rdbuf();
152 
153  if (! (lsdp->is_open()) )
154  {
156  execp << "XrayFluoDataSet - data file: " + dirFile + " not found"<<G4endl;
157  G4Exception("XrayFluoDataSet::LoadData()","example-xray_fluorescence01",
158  FatalException, execp);
159  }
160  G4double a = 0;
161  G4int k = 1;
162 
163  do
164  {
165  file >> a;
166  G4int nColumns = 2;
167  // The file is organized into two columns:
168  // 1st column is the energy
169  // 2nd column is the corresponding value
170  // The file terminates with the pattern: -1 -1
171  // -2 -2
172  if (a == -1 || a == -2)
173  {
174 
175  }
176  else
177  {
178  if (k%nColumns != 0)
179  {
180  G4double e = a * unit1;
181  energies->push_back(e);
182 
183  k++;
184 
185  }
186  else if (k%nColumns == 0)
187  {
188  G4double value = a * unit2;
189  data->push_back(value);
190 
191  k = 1;
192  }
193  }
194 
195  } while (a != -2); // end of file
196 
197  file.close();
198  return true;
199 
200 }
201 
202 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
203 
205 {
206  size_t size = numberOfBins;
207  for (size_t i=0; i<size; i++)
208  {
209  G4double e = (*energies)[i] / unit1;
210  G4double sigma = (*data)[i] / unit2 ;
211  G4cout << "Point: "
212  << e
213  << " - Data value : "
214  << sigma
215  << G4endl;
216  }
217 }
218 
219 
220