FermiMomentumTablePool.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 
7  Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
8  University of Liverpool & STFC Rutherford Appleton Laboratory
9 
10  For the class documentation see the corresponding header file.
11 
12  Important revisions after version 2.0.0 :
13  @ Jun 18, 2008 - CA
14  Fix small memory leak - xmlFree the input XML doc
15  @ Aug 25, 2009 - RH
16  Use the GetXMLFilePath() to search the potential XML config file locations
17  and return the first actual file that can be found. Adapt code to use the
18  utils::xml namespace.
19 */
20 //____________________________________________________________________________
21 
22 #include <string>
23 
24 #include "libxml/parser.h"
25 #include "libxml/xmlmemory.h"
26 
27 #include <TSystem.h>
28 
34 
35 using std::string;
36 
37 using namespace genie;
38 
39 //____________________________________________________________________________
41 //____________________________________________________________________________
43 {
44  if( ! this->LoadTables() ) {
45  LOG("FermiP", pERROR) << "FermiMomentumTablePool initialization failed!";
46  }
47  fInstance = 0;
48 }
49 //____________________________________________________________________________
51 {
53  for(titer = fKFSets.begin(); titer != fKFSets.end(); ++titer) {
54  FermiMomentumTable * t = titer->second;
55  if(t) delete t;
56  t=0;
57  }
58  fKFSets.clear();
59  fInstance = 0;
60 }
61 //____________________________________________________________________________
63 {
64  if(fInstance == 0) {
65 
66  LOG("FermiP", pINFO) << "FermiMomentumTablePool late initialization";
67 
68  static FermiMomentumTablePool::Cleaner cleaner;
70 
72  }
73  return fInstance;
74 }
75 //____________________________________________________________________________
77 {
78  string defopt = "Default"; // default option
79 
81  FermiMomentumTable * table = 0;
82 
83  if(fKFSets.count(name) == 1) {
84  table_iter = fKFSets.find(name);
85  table = table_iter->second;
86  if(table) return table;
87  }
88  if(fKFSets.count(defopt) == 1) {
89  LOG("FermiP", pWARN)
90  << "Fermi momentum table: [" << name << "] was not found! "
91  << "Switching to table: [" << defopt << "]";
92  table_iter = fKFSets.find(defopt);
93  table = table_iter->second;
94  if(table) return table;
95  }
96  LOG("FermiP", pERROR)
97  << "Not even the default Fermi momentum table was not found!";
98  return 0;
99 }
100 //____________________________________________________________________________
102 {
103  bool loaded = true;
104 
105  //-- Fermi momenta sets XML file using GXMLPATH + default locations
106  string filename = utils::xml::GetXMLFilePath("FermiMomentumTables.xml");
107 
108  LOG("FermiP", pINFO) << "Loading Fermi momenta from file: " << filename;
109 
110  bool is_accessible = ! (gSystem->AccessPathName( filename.c_str() ));
111 
112  if(is_accessible) {
113  XmlParserStatus_t status = this->ParseXMLTables(filename);
114  if(status != kXmlOK) {
115  LOG("FermiP", pWARN)
116  << "XML parser status: " << XmlParserStatus::AsString(status)
117  << " - Couldn't read file: " << filename;
118  loaded = false;
119  }
120  } else {
121  LOG("FermiP", pWARN) << "Not accessible file: " << filename;
122  loaded = false;
123  }
124  return loaded;
125 };
126 //____________________________________________________________________________
128 {
129  LOG("FermiP", pDEBUG) << "Reading XML file: " << filename;
130 
131  xmlDocPtr xml_doc = xmlParseFile(filename.c_str());
132  if(xml_doc == NULL) return kXmlNotParsed;
133 
134  xmlNodePtr xml_root = xmlDocGetRootElement(xml_doc);
135  if(xml_root==NULL)
136  {
137  xmlFreeDoc(xml_doc);
138  return kXmlEmpty;
139  }
140 
141  const xmlChar * xml_root_name = (const xmlChar *)"fermi_momentum_const";
142  if( xmlStrcmp(xml_root->name, xml_root_name) )
143  {
144  xmlFreeDoc(xml_doc);
145  return kXmlInvalidRoot;
146  }
147 
148  xmlNodePtr xml_kft = xml_root->xmlChildrenNode; // <kf_table>'s
149 
150  // loop over <kf_table> nodes
151  while (xml_kft != NULL) {
152  if( (!xmlStrcmp(xml_kft->name, (const xmlChar *) "kf_table")) ) {
153 
154  string name = utils::str::TrimSpaces(
155  utils::xml::GetAttribute(xml_kft, "name"));
156 
157  LOG("FermiP", pDEBUG) << "Reading Fermi momenta table: " << name;
158 
159  FermiMomentumTable * kftable = new FermiMomentumTable; // new table
160 
161  // loop over <kf> nodes
162  xmlNodePtr xml_kf = xml_kft->xmlChildrenNode; // <kf_table> children
163  while (xml_kf != NULL) {
164  if( (!xmlStrcmp(xml_kf->name, (const xmlChar *) "kf")) ) {
165 
166  string spdgc = utils::str::TrimSpaces(
167  utils::xml::GetAttribute(xml_kf, "nucleus_pdgc"));
168  int pdgc = atoi (spdgc.c_str());
169 
170  xmlNodePtr xml_cur = xml_kf->xmlChildrenNode; // <kf> children
171  const xmlChar * ntag = (const xmlChar *)"n";
172  const xmlChar * ptag = (const xmlChar *)"p";
173  double kfp=0, kfn=0, kf=0;
174  // loop over <kf> children
175  while (xml_cur != NULL) {
176  bool isp = !xmlStrcmp(xml_cur->name, ptag);
177  bool isn = !xmlStrcmp(xml_cur->name, ntag);
178  if(isn || isp) {
179  string skf = utils::xml::TrimSpaces(
180  xmlNodeListGetString(xml_doc, xml_cur->xmlChildrenNode, 1));
181  kf = atof(skf.c_str());
182  }
183  if(isp) kfp = kf;
184  if(isn) kfn = kf;
185  xml_cur = xml_cur->next;
186  }
187  xmlFreeNode(xml_cur);
188 
189  KF_t kft;
190  kft.p = kfp;
191  kft.n = kfn;
192 
193  LOG("FermiP", pDEBUG)
194  << "Add KF table entry: PDGC = " << pdgc
195  << " --> " << "kf(p) = " << kft.p << ", kf(n) = " << kft.n;
196  kftable->AddTableEntry(pdgc,kft);
197  } //<x> == <kf>
198  xml_kf = xml_kf->next;
199  } //<kf> loop
200  xmlFreeNode(xml_kf);
201 
202  fKFSets.insert(
203  map<string, FermiMomentumTable *>::value_type(name,kftable));
204 
205  } //<x> == <kf_table>
206  xml_kft = xml_kft->next;
207  } //<kf_table> loop
208  xmlFreeNode(xml_kft);
209  xmlFreeDoc(xml_doc);
210 
211  return kXmlOK;
212 }
213 //____________________________________________________________________________
214 
static QCString name
Definition: declinfo.cpp:673
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 TrimSpaces(xmlChar *xmls)
static FermiMomentumTablePool * Instance(void)
static FermiMomentumTablePool * fInstance
A table of Fermi momentum constants.
intermediate_table::const_iterator const_iterator
string filename
Definition: train.py:213
map< string, FermiMomentumTable * > fKFSets
Singleton class to load & serve tables of Fermi momentum constants.
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
const FermiMomentumTable * GetTable(string name)
string GetXMLFilePath(string basename)
#define pINFO
Definition: Messenger.h:62
void AddTableEntry(int target_pdgc, KF_t kf)
#define pWARN
Definition: Messenger.h:60
string TrimSpaces(string input)
Definition: StringUtils.cxx:18
static const char * AsString(XmlParserStatus_t status)
XmlParserStatus_t ParseXMLTables(string filename)
enum genie::EXmlParseStatus XmlParserStatus_t
string GetAttribute(xmlNodePtr xml_cur, string attr_name)
#define pDEBUG
Definition: Messenger.h:63