GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NaturalIsotopes.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
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;
45  vector<NaturalIsotopeElementData*>::iterator viter;
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  NaturalIsotopes::ElementDataPdg(int Z, int pdgcode) const
111 {
112  map<int, vector<NaturalIsotopeElementData*> >::const_iterator miter;
113 
114  if( (miter=fNaturalIsotopesTable.find(Z)) == fNaturalIsotopesTable.end()) {
115  LOG("NatIsotop", pWARN)
116  << "Table has no elements for natural isotope Z = " << Z;
117  return 0;
118  }
119 
120  vector<NaturalIsotopeElementData*> vec = miter->second;
121  for (int i; i<vec.size(); i++) {
122  if (vec[i]->PdgCode()==pdgcode) return vec[i];
123  }
124 
125  LOG("NatIsotop", pWARN)
126  << "Natural isotope Z = " << Z << " has " << vec.size() << " elements"
127  << " (pdgcode = " << pdgcode << " was requested)";
128  return 0;
129 
130 }
131 //____________________________________________________________________________
133 {
134  // get the natural isotopes table filename
135  string filename = string(gSystem->Getenv("GENIE")) +
136  string("/data/evgen/catalogues/iso/natural-isotopes.data");
137 
138  LOG("NatIsotop", pINFO)
139  << "Loading natural occurring isotope table from file: " << filename;
140 
141  bool is_accessible = ! (gSystem->AccessPathName( filename.c_str() ));
142  if (!is_accessible) {
143  LOG("NatIsotop", pWARN) << "Can not read file: " << filename;
144  return false;
145  }
146 
147  // load the natural isotopes .txt file
148  string input_buf;
149  std::ifstream input(filename.c_str());
150  if (input.is_open()){
151 
152  //skip first 8 lines (comments)
153  for(int i=0; i<8; i++){
154  string buffer;
155  getline(input, buffer);
156  }
157 
158  int Z = -1, Z_previous = -1, nelements = 0, pdgcode = 0;
159  double atomicmass = 0, abundance = 0;
160  string elementname, subelementname;
161 
162  while( !input.eof() ) {
163 
164  //read in naturally occuring element info
165  input >> Z;
166  input >> elementname;
167  input >> nelements;
168 
169  vector<NaturalIsotopeElementData *> vec;
170  NaturalIsotopeElementData * data = 0;
171 
172  // check not re-reading same element
173  if(Z!=Z_previous){
174  LOG("NatIsotop", pDEBUG) << "Reading entry for Z = " << Z;
175  for(int n=0 ; n < nelements; n++){
176  input >> subelementname;
177  input >> pdgcode;
178  input >> atomicmass;
179  input >> abundance;
180  LOG("NatIsotop", pDEBUG)
181  << " - Element: " << n << ", pdg = " << pdgcode
182  << ", A = " << atomicmass << ", abundance = " << abundance;
183  data = new NaturalIsotopeElementData(pdgcode, abundance,atomicmass);
184  vec.push_back(data);
185  }
186  fNaturalIsotopesTable.insert(
187  map<int,vector<NaturalIsotopeElementData*> >::value_type(Z,vec));
188  }
189  Z_previous = Z;
190  } //!eof
191 
192  } else {
193  return false;
194  } //open?
195 
196  return true;
197 }
198 //____________________________________________________________________________
#define pERROR
Definition: Messenger.h:59
int PdgCode(Resonance_t res, int Q)
(resonance id, charge) -&gt; PDG code
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
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 &amp; serve tables of natural occurring isotopes.
const NaturalIsotopeElementData * ElementData(int Z, int ielement) const
const NaturalIsotopeElementData * ElementDataPdg(int Z, int pdgcode) const
#define pDEBUG
Definition: Messenger.h:63