ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RDShellEMDataSet.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RDShellEMDataSet.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: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch)
29 //
30 // History:
31 // -----------
32 // 1 Aug 2001 MGP Created
33 // 09.10.01 V.Ivanchenko Add case z=0
34 // 9 Mar 2008 MGP Cleaned up unreadable code modified by former developer
35 // (Further clean-up needed)
36 //
37 // -------------------------------------------------------------------
38 
39 #include "G4RDShellEMDataSet.hh"
40 #include "G4RDEMDataSet.hh"
41 #include "G4RDVDataSetAlgorithm.hh"
42 #include <fstream>
43 #include <sstream>
44 
45 
47  G4double eUnit,
48  G4double dataUnit)
49  :
50  z(zeta),
51  algorithm(algo),
52  unitEnergies(eUnit),
53  unitData(dataUnit)
54 {
55  if (algorithm == 0)
56  G4Exception("G4RDShellEMDataSet::G4RDShellEMDataSet()", "InvalidSetup",
57  FatalException, "Interpolation == 0!");
58 }
59 
60 
62 {
64  if (algorithm) delete algorithm;
65 }
66 
67 
69 {
70  // Returns the sum over the shells corresponding to e
71  G4double value = 0.;
72 
73  std::vector<G4RDVEMDataSet *>::const_iterator i(components.begin());
74  std::vector<G4RDVEMDataSet *>::const_iterator end(components.end());
75 
76  while (i != end)
77  {
78  value += (*i)->FindValue(energy);
79  i++;
80  }
81 
82  return value;
83 }
84 
85 
87 {
88  const size_t n = NumberOfComponents();
89 
90  G4cout << "The data set has " << n << " components" << G4endl;
91  G4cout << G4endl;
92 
93  size_t i = 0;
94 
95  while (i < n)
96  {
97  G4cout << "--- Component " << i << " ---" << G4endl;
98  GetComponent(i)->PrintData();
99  i++;
100  }
101 }
102 
103 
105  G4DataVector* data,
106  G4int componentId)
107 {
108  G4RDVEMDataSet* component = components[componentId];
109 
110  if (component)
111  {
112  component->SetEnergiesData(energies, data, 0);
113  return;
114  }
115 
116  std::ostringstream message;
117  message << "Component " << componentId << " not found";
118 
119  G4Exception("G4RDShellEMDataSet::SetEnergiesData()", "DataNotFound",
120  FatalException, message.str().c_str());
121 }
122 
123 
125 {
127 
128  G4String fullFileName = FullFileName(file);
129  std::ifstream in(fullFileName);
130 
131  if (!in.is_open())
132  {
133  G4String message("Data file \"");
134  message += fullFileName;
135  message += "\" not found";
136  G4Exception("G4RDShellEMDataSet::LoadData()", "DataNotFound",
137  FatalException, message);
138  }
139 
140  G4DataVector* energies = 0;
141  G4DataVector* data = 0;
142 
143  G4double a = 0.;
144  G4int shellIndex = 0;
145  bool energyColumn = true;
146 
147  do
148  {
149  in >> a;
150 
151  if (a == -1)
152  {
153  if (energyColumn && energies!=0)
154  {
155  AddComponent(new G4RDEMDataSet(shellIndex, energies, data, algorithm->Clone(), unitEnergies, unitData));
156  energies = 0;
157  data = 0;
158  }
159 
160  energyColumn = (!energyColumn);
161  }
162  else if (a != -2)
163  {
164  if (energies == 0)
165  {
166  energies = new G4DataVector;
167  data = new G4DataVector;
168  }
169 
170  if (energyColumn)
171  energies->push_back(a * unitEnergies);
172  else
173  data->push_back(a * unitData);
174 
175  energyColumn = (!energyColumn);
176  }
177  }
178  while (a != -2);
179 
180  return true;
181 }
182 
183 
185 {
186  G4String fullFileName = FullFileName(file);
187  std::ofstream out(fullFileName);
188 
189  if (!out.is_open())
190  {
191  G4String message("Cannot open \"");
192  message += fullFileName;
193  message += "\"";
194  G4Exception("G4RDEMDataSet::SaveData()", "CannotOpenFile",
195  FatalException, message);
196  }
197 
198  const size_t n = NumberOfComponents();
199  size_t k = 0;
200 
201  while (k < n)
202  {
203  const G4RDVEMDataSet* component = GetComponent(k);
204 
205  if (component)
206  {
207  const G4DataVector& energies = component->GetEnergies(0);
208  const G4DataVector& data = component->GetData(0);
209 
210  G4DataVector::const_iterator i = energies.begin();
211  G4DataVector::const_iterator endI = energies.end();
212  G4DataVector::const_iterator j = data.begin();
213 
214  while (i != endI)
215  {
216  out.precision(10);
217  out.width(15);
218  out.setf(std::ofstream::left);
219  out << ((*i)/unitEnergies) << ' ';
220 
221  out.precision(10);
222  out.width(15);
223  out.setf(std::ofstream::left);
224  out << ((*j)/unitData) << std::endl;
225  i++;
226  j++;
227  }
228  }
229 
230  out.precision(10);
231  out.width(15);
232  out.setf(std::ofstream::left);
233  out << -1.f << ' ';
234 
235  out.precision(10);
236  out.width(15);
237  out.setf(std::ofstream::left);
238  out << -1.f << std::endl;
239 
240  k++;
241  }
242 
243  out.precision(10);
244  out.width(15);
245  out.setf(std::ofstream::left);
246  out << -2.f << ' ';
247 
248  out.precision(10);
249  out.width(15);
250  out.setf(std::ofstream::left);
251  out << -2.f << std::endl;
252 
253  return true;
254 }
255 
256 
258 {
259  while (!components.empty())
260  {
261  if (components.back()) delete components.back();
262  components.pop_back();
263  }
264 }
265 
266 
268 {
269  char* path = std::getenv("G4LEDATA");
270  if (!path)
271  G4Exception("G4RDShellEMDataSet::FullFileName()", "InvalidSetup",
272  FatalException, "G4LEDATA environment variable not set!");
273 
274  std::ostringstream fullFileName;
275 
276  fullFileName << path << '/' << fileName << z << ".dat";
277 
278  return G4String(fullFileName.str().c_str());
279 }