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