NaturalIsotopes.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5  or see $GENIE/LICENSE
6 
7  Author: Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
8  University of Liverpool & STFC Rutherford Appleton Lab
9 
10  Jim Dobson <j.dobson07@imperial.ac.uk>
11  Imperial College London
12 
13  For the class documentation see the corresponding header file.
14 
15  Important revisions after version 2.0.0 :
16  @ May 30, 2008 - CA, JD
17  This class was first added in version 2.3.1
18  @ May 01, 2012 - CA, JD
19  Pick data from new location ($GENIE/data/evgen/catalogues/iso/)
20 */
21 //____________________________________________________________________________
22 
23 #include <fstream>
24 #include <string>
25 
26 #include <TSystem.h>
27 
30 
31 using std::string;
32 using std::cout;
33 using std::endl;
34 
35 using namespace genie;
36 
37 //____________________________________________________________________________
39 //____________________________________________________________________________
41 {
42  if( ! this->LoadTable() ) {
43  LOG("NatIsotop", pERROR) << "NaturalIsotopes initialization failed!";
44  }
45  fInstance = 0;
46 }
47 //____________________________________________________________________________
49 {
50  cout << "NaturalIsotopes singleton dtor: "
51  << "Deleting natural isotope data tables" << endl;
52 
53  map<int, vector<NaturalIsotopeElementData*> >::iterator miter;
54  vector<NaturalIsotopeElementData*>::iterator viter;
55 
56  for(miter = fNaturalIsotopesTable.begin();
57  miter != fNaturalIsotopesTable.end(); ++miter) {
58  vector<NaturalIsotopeElementData*> vec = miter->second;
59  for(viter = vec.begin(); viter != vec.end(); ++viter) {
60  NaturalIsotopeElementData * element_data = *viter;
61  if(element_data) {
62  delete element_data;
63  element_data = 0;
64  }
65  }
66  vec.clear();
67  }
68  fNaturalIsotopesTable.clear();
69  fInstance = 0;
70 }
71 //____________________________________________________________________________
73 {
74  if(fInstance == 0) {
75  LOG("NatIsotop", pINFO) << "NaturalIsotopes late initialization";
76 
77  static NaturalIsotopes::Cleaner cleaner;
79 
81  }
82  return fInstance;
83 }
84 //____________________________________________________________________________
86 {
87  map<int, vector<NaturalIsotopeElementData*> >::const_iterator miter;
88 
89  if( (miter=fNaturalIsotopesTable.find(Z)) == fNaturalIsotopesTable.end()) {
90  LOG("NatIsotop", pWARN)
91  << "Table has no elements for natural isotope Z = " << Z;
92  return 0;
93  }
94  vector<NaturalIsotopeElementData*> vec = miter->second;
95  return vec.size();
96 }
97 //____________________________________________________________________________
99  NaturalIsotopes::ElementData(int Z, int ielement) const
100 {
101  map<int, vector<NaturalIsotopeElementData*> >::const_iterator miter;
102 
103  if( (miter=fNaturalIsotopesTable.find(Z)) == fNaturalIsotopesTable.end()) {
104  LOG("NatIsotop", pWARN)
105  << "Table has no elements for natural isotope Z = " << Z;
106  return 0;
107  }
108  vector<NaturalIsotopeElementData*> vec = miter->second;
109  if(ielement >= (int)vec.size() || ielement < 0) {
110  LOG("NatIsotop", pWARN)
111  << "Natural isotope Z = " << Z << " has " << vec.size() << " elements"
112  << " (element = " << ielement << " was requested)";
113  return 0;
114  }
115  return vec[ielement];
116 }
117 //____________________________________________________________________________
119 {
120  // get the natural isotopes table filename
121  string filename = string(gSystem->Getenv("GENIE")) +
122  string("/data/evgen/catalogues/iso/natural-isotopes.data");
123 
124  LOG("NatIsotop", pINFO)
125  << "Loading natural occurring isotope table from file: " << filename;
126 
127  bool is_accessible = ! (gSystem->AccessPathName( filename.c_str() ));
128  if (!is_accessible) {
129  LOG("NatIsotop", pWARN) << "Can not read file: " << filename;
130  return false;
131  }
132 
133  // load the natural isotopes .txt file
134  string input_buf;
135  std::ifstream input(filename.c_str());
136  if (input.is_open()){
137 
138  //skip first 8 lines (comments)
139  for(int i=0; i<8; i++){
140  string buffer;
141  getline(input, buffer);
142  }
143 
144  int Z = -1, Z_previous = -1, nelements = 0, pdgcode = 0;
145  double atomicmass = 0, abundance = 0;
146  string elementname, subelementname;
147 
148  while( !input.eof() ) {
149 
150  //read in naturally occuring element info
151  input >> Z;
152  input >> elementname;
153  input >> nelements;
154 
155  vector<NaturalIsotopeElementData *> vec;
157 
158  // check not re-reading same element
159  if(Z!=Z_previous){
160  LOG("NatIsotop", pDEBUG) << "Reading entry for Z = " << Z;
161  for(int n=0 ; n < nelements; n++){
162  input >> subelementname;
163  input >> pdgcode;
164  input >> atomicmass;
165  input >> abundance;
166  LOG("NatIsotop", pDEBUG)
167  << " - Element: " << n << ", pdg = " << pdgcode
168  << ", A = " << atomicmass << ", abundance = " << abundance;
169  data = new NaturalIsotopeElementData(pdgcode, abundance);
170  vec.push_back(data);
171  }
172  fNaturalIsotopesTable.insert(
173  map<int,vector<NaturalIsotopeElementData*> >::value_type(Z,vec));
174  }
175  Z_previous = Z;
176  } //!eof
177 
178  } else {
179  return false;
180  } //open?
181 
182  return true;
183 }
184 //____________________________________________________________________________
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
#define pERROR
Definition: Messenger.h:60
string filename
Definition: shutoffs.py:106
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
const XML_Char const XML_Char * data
Definition: expat.h:268
Float_t Z
Definition: plot.C:38
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
int NElements(int Z) const
static NaturalIsotopes * Instance(void)
map< int, vector< NaturalIsotopeElementData * > > fNaturalIsotopesTable
#define pINFO
Definition: Messenger.h:63
Eigen::VectorXd vec
static NaturalIsotopes * fInstance
#define pWARN
Definition: Messenger.h:61
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
Singleton class to load & serve tables of natural occurring isotopes.
const NaturalIsotopeElementData * ElementData(int Z, int ielement) const
#define pDEBUG
Definition: Messenger.h:64