TabulatedHadronTensorModelI.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: Steven Gardiner <gardiner \at fnal.gov>
8  Fermi National Accelerator Laboratory
9 
10  For the class documentation see the corresponding header file.
11 
12 */
13 //____________________________________________________________________________
14 
15 // standard library includes
16 #include <cstdlib>
17 #include <fstream>
18 #include <string>
19 
20 // GENIE includes
27 
28 namespace {
29 
30  /// Converts a string to a genie::HadronTensorType_t value. If the string
31  /// does not correspond to a valid tensor type, kHT_Undefined
32  /// is returned, and the ok flag is set to false
33  genie::HadronTensorType_t string_to_tensor_type(const std::string& str,
34  bool& ok)
35  {
36  if (str == "MEC_FullAll") return genie::kHT_MEC_FullAll;
37  else if (str == "MEC_Fullpn")
38  return genie::kHT_MEC_Fullpn;
39  else if (str == "MEC_DeltaAll")
41  else if (str == "MEC_Deltapn")
43  else if (str == "MEC_EM")
44  return genie::kHT_MEC_EM;
45  else if (str == "MEC_EM_wImag")
47  else if (str == "QE_EM")
48  return genie::kHT_QE_EM;
49  else if (str == "MEC_FullAll_Param")
51  else if (str == "MEC_FullAll_wImag")
53  else if (str == "QE_Full")
54  return genie::kHT_QE_Full;
55  else {
56  ok = false;
57  return genie::kHT_Undefined;
58  }
59  }
60 
61  /// Converts a genie::HadronTensorType_t value to a string
62  std::string tensor_type_to_string(genie::HadronTensorType_t htt)
63  {
64  if ( htt == genie::kHT_MEC_FullAll ) return "MEC_FullAll";
65  else if ( htt == genie::kHT_MEC_Fullpn ) return "MEC_Fullpn";
66  else if ( htt == genie::kHT_MEC_DeltaAll ) return "MEC_DeltaAll";
67  else if ( htt == genie::kHT_MEC_Deltapn ) return "MEC_Deltapn";
68  else if ( htt == genie::kHT_MEC_EM ) return "MEC_EM";
69  else if ( htt == genie::kHT_MEC_EM_wImag ) return "MEC_EM_wImag";
70  else if ( htt == genie::kHT_QE_EM ) return "QE_EM";
71  else if ( htt == genie::kHT_MEC_FullAll_Param ) return "MEC_FullAll_Param";
72  else if ( htt == genie::kHT_MEC_FullAll_wImag ) return "MEC_FullAll_wImag";
73  else if ( htt == genie::kHT_QE_Full ) return "QE_Full";
74  else return "Undefined";
75  }
76 
77  /// Returns true if a given file exists and is accessible, or false otherwise
78  bool file_exists(const std::string& file_name) {
79  return std::ifstream(file_name.c_str()).good();
80  }
81 
82 }
83 
84 //____________________________________________________________________________
87 {
88 
89 }
90 
91 //____________________________________________________________________________
93  : genie::HadronTensorModelI( name )
94 {
95 
96 }
97 
98 //____________________________________________________________________________
100  std::string config) : genie::HadronTensorModelI(name, config)
101 {
102 
103 }
104 
105 //____________________________________________________________________________
107 {
109  this->LoadConfig();
110 }
111 //____________________________________________________________________________
113 {
115  this->LoadConfig();
116 }
117 //____________________________________________________________________________
119 {
120  GetParamDef( "WarnIfMissing", fWarnIfMissing, true );
121 
122  // Either a data path relative to the root GENIE folder
123  // or an absolute path can be used. Find out which
124  // option was chosen.
125  std::string path_type;
126  GetParamDef( "DataPathType", path_type, std::string("relative") );
127 
128  // Right now, there can only be a single data path
129  // specified. We use a vector of paths to allow for
130  // easy expansion later.
131  std::string data_path;
132  GetParam( "DataPath", data_path );
133 
134  // Convert the relative path to an absolute one if needed
135  if ( path_type == "relative" ) {
136  data_path = std::string( gSystem->Getenv("GENIE") ) + '/' + data_path;
137  }
138 
139  fDataPaths.push_back( data_path );
140 }
141 
142 //____________________________________________________________________________
144 {
146  for (it = fTensors.begin(); it != fTensors.end(); ++it) {
147  HadronTensorI* t = it->second;
148  if ( t ) delete t;
149  }
150  fTensors.clear();
151 }
152 //____________________________________________________________________________
154  int tensor_pdg, genie::HadronTensorType_t type) const
155 {
156  HadronTensorID temp_id(tensor_pdg, type);
157 
158  // First check to see if the hadron tensor object already exists in memory.
159  // If it does, return the existing object.
160  if ( fTensors.count(temp_id) ) return fTensors.find(temp_id)->second;
161 
162  // If not, try to create it
163  const HadronTensorI* ht = this->BuildTensor( temp_id );
164 
165  if ( !ht && fWarnIfMissing ) {
166  LOG("TabulatedHadronTensorModelI", pWARN) << "Unable to create a hadron tensor"
167  << " for target pdg = " << temp_id.target_pdg
168  << " and hadron tensor type " << temp_id.type;
169  }
170 
171  return ht;
172 }
173 
174 //____________________________________________________________________________
176  const std::string& basename, bool& ok) const
177 {
178  for (size_t p = 0; p < fDataPaths.size(); ++p) {
179  const std::string& path = fDataPaths.at( p );
180  std::string full_name = path + '/' + basename;
181  if ( file_exists(full_name) ) return full_name;
182  }
183 
184  // A matching file could not be found
185  ok = false;
186  return std::string();
187 }
188 
189 //____________________________________________________________________________
191  const HadronTensorID& tensor_id) const
192 {
193  bool tensor_ok = true;
194 
195  std::string tensor_file_basename = this->GetTensorFileBasename( tensor_id );
196 
197  // Tensor values are represented using a 2D grid that is stored in a data
198  // file. Get the full path to the file, or an empty string if it could not
199  // be found. Also set the tensor_ok flag to false if the file could not be
200  // found.
201  std::string full_file_name = FindTensorTableFile(tensor_file_basename,
202  tensor_ok);
203 
204  if ( tensor_ok ) {
205 
206  // Create the new hadron tensor object
207  LOG("TabulatedHadronTensorModelI", pINFO) << "Loading the hadron"
208  << " tensor data file " << full_file_name;
209 
210  genie::HadronTensorI* temp_ptr = this->ParseTensorFile( full_file_name );
211 
212  // Place a pointer to it in the map of loaded tensor objects for easy
213  // retrieval.
214  /// \todo Switch to using std::unique_ptr here for easy cleanup
215  /// once C++11 features are allowed in GENIE
216  fTensors[tensor_id] = temp_ptr;
217 
218  // Return a pointer to the newly-created hadron tensor object
219  return temp_ptr;
220  }
221 
222  else {
223  // If we couldn't make the hadron tensor, store a nullptr to avoid
224  // unsuccessful repeat attempts. These can otherwise slow things down
225  // for no good reason.
226  fTensors[tensor_id] = NULL;
227 
228  if ( fWarnIfMissing ) {
229  LOG("TabulatedHadronTensorModelI", pERROR) << "The hadron tensor data file \""
230  << full_file_name << "\" requested for target pdg = "
231  << tensor_id.target_pdg << " and hadron tensor type "
232  << tensor_id.type << " could not be found.";
233  }
234  }
235 
236  // If there was a problem, return a null pointer
237  return NULL;
238 }
239 
240 //____________________________________________________________________________
242  const HadronTensorID& ht_id ) const
243 {
244 
245  std::string tgt_string;
246  std::stringstream ss;
247  ss << ht_id.target_pdg;
248  tgt_string = ss.str();
249 
250  RgKey key = tensor_type_to_string( ht_id.type ) + "@Pdg="
251  + tgt_string;
252 
254  GetParamDef( key, basename, std::string("TENSOR_FILE_NOT_FOUND") );
255 
256  return basename;
257 }
static QCString name
Definition: declinfo.cpp:673
std::string GetTensorFileBasename(const HadronTensorID &ht_id) const
intermediate_table::iterator iterator
std::string FindTensorTableFile(const std::string &basename, bool &ok) const
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
#define pERROR
Definition: Messenger.h:59
std::string string
Definition: nybbler.cc:12
virtual void Configure(const Registry &config)
enum genie::HadronTensorType HadronTensorType_t
QCString file_name
virtual const HadronTensorI * GetTensor(int tensor_pdg, HadronTensorType_t type) const
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
def key(type, name=None)
Definition: graph.py:13
static Config * config
Definition: config.cpp:1054
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:62
p
Definition: test.py:223
Abstract interface for an object that computes the elements a hadron tensor . Also computes the contr...
Definition: HadronTensorI.h:53
#define pINFO
Definition: Messenger.h:62
std::vector< std::string > fDataPaths
Paths to check when searching for hadron tensor data files.
Struct used to provide a unique ID for each tensor object.
#define pWARN
Definition: Messenger.h:60
void LoadConfig()
Saves some basic XML config parameters to data members.
string RgKey
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
Creates hadron tensor objects for use in cross section calculations.
bool file_exists(std::string const &qualified_filename)
Definition: filesystem.cc:14
const HadronTensorI * BuildTensor(const HadronTensorID &ht_id) const
Create a HadronTensorI object given a particular HadronTensorID.
bool GetParamDef(const RgKey &name, T &p, const T &def) const
bool GetParam(const RgKey &name, T &p, bool is_top_call=true) const
virtual HadronTensorI * ParseTensorFile(const std::string &full_file_name) const =0
std::map< HadronTensorID, HadronTensorI * > fTensors
static QCString str