GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TabulatedHadronTensorModelI.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  or see $GENIE/LICENSE
6 
7  Author: Steven Gardiner <gardiner \at fnal.gov>
8  Liang Liu <liangliu \at fnal.gov>
9  Fermi National Accelerator Laboratory
10 
11  For the class documentation see the corresponding header file.
12 
13 */
14 //____________________________________________________________________________
15 
16 // standard library includes
17 #include <cstdlib>
18 #include <fstream>
19 #include <string>
20 
21 // GENIE includes
28 
29 namespace {
30 
31  /// Converts a string to a genie::HadronTensorType_t value. If the string
32  /// does not correspond to a valid tensor type, kHT_Undefined
33  /// is returned, and the ok flag is set to false
34  genie::HadronTensorType_t string_to_tensor_type(const std::string& str,
35  bool& ok)
36  {
37  if (str == "MEC_FullAll") return genie::kHT_MEC_FullAll;
38  else if (str == "MEC_Fullpn")
39  return genie::kHT_MEC_Fullpn;
40  else if (str == "MEC_DeltaAll")
42  else if (str == "MEC_Deltapn")
44  else if (str == "MEC_EM")
45  return genie::kHT_MEC_EM;
46  else if (str == "MEC_EM_pn")
47  return genie::kHT_MEC_EM_pn;
48  else if (str == "MEC_EM_pp")
49  return genie::kHT_MEC_EM_pp;
50  else if (str == "MEC_EM_wImag")
52  else if (str == "QE_EM")
53  return genie::kHT_QE_EM;
54  else if (str == "QE_EM_proton")
56  else if (str == "QE_EM_neutron")
58  else if (str == "MEC_FullAll_Param")
60  else if (str == "MEC_FullAll_wImag")
62  else if (str == "QE_Full")
63  return genie::kHT_QE_Full;
64 
65  else if (str == "QE_CRPA_Low")
67  else if (str == "QE_CRPA_Medium")
69  else if (str == "QE_CRPA_High")
71 
72  else if (str == "QE_CRPA_anu_Low")
74  else if (str == "QE_CRPA_anu_Medium")
76  else if (str == "QE_CRPA_anu_High")
78 
79  else if (str == "QE_HF_Low")
80  return genie::kHT_QE_HF_Low;
81  else if (str == "QE_HF_Medium")
83  else if (str == "QE_HF_High")
84  return genie::kHT_QE_HF_High;
85 
86  else if (str == "QE_HF_anu_Low")
88  else if (str == "QE_HF_anu_Medium")
90  else if (str == "QE_HF_anu_High")
92 
93 
94  else if (str == "QE_CRPAPW_Low")
96  else if (str == "QE_CRPAPW_Medium")
98  else if (str == "QE_CRPAPW_High")
100 
101  else if (str == "QE_CRPAPW_anu_Low")
103  else if (str == "QE_CRPAPW_anu_Medium")
105  else if (str == "QE_CRPAPW_anu_High")
107 
108  else if (str == "QE_HFPW_Low")
109  return genie::kHT_QE_HFPW_Low;
110  else if (str == "QE_HFPW_Medium")
112  else if (str == "QE_HFPW_High")
114 
115  else if (str == "QE_HFPW_anu_Low")
117  else if (str == "QE_HFPW_anu_Medium")
119  else if (str == "QE_HFPW_anu_High")
121 
122  else if (str == "QE_SuSABlend")
124  else if (str == "QE_SuSABlend_anu")
126 
127  else {
128  ok = false;
129  return genie::kHT_Undefined;
130  }
131  }
132 
133  /// Converts a genie::HadronTensorType_t value to a string
134  std::string tensor_type_to_string(genie::HadronTensorType_t htt)
135  {
136  if ( htt == genie::kHT_MEC_FullAll ) return "MEC_FullAll";
137  else if ( htt == genie::kHT_MEC_Fullpn ) return "MEC_Fullpn";
138  else if ( htt == genie::kHT_MEC_DeltaAll ) return "MEC_DeltaAll";
139  else if ( htt == genie::kHT_MEC_Deltapn ) return "MEC_Deltapn";
140  else if ( htt == genie::kHT_MEC_EM ) return "MEC_EM";
141  else if ( htt == genie::kHT_MEC_EM_pn ) return "MEC_EM_pn";
142  else if ( htt == genie::kHT_MEC_EM_pp ) return "MEC_EM_pp";
143  else if ( htt == genie::kHT_MEC_EM_wImag ) return "MEC_EM_wImag";
144  else if ( htt == genie::kHT_QE_EM ) return "QE_EM";
145  else if ( htt == genie::kHT_QE_EM_proton ) return "QE_EM_proton";
146  else if ( htt == genie::kHT_QE_EM_neutron ) return "QE_EM_neutron";
147  else if ( htt == genie::kHT_MEC_FullAll_Param ) return "MEC_FullAll_Param";
148  else if ( htt == genie::kHT_MEC_FullAll_wImag ) return "MEC_FullAll_wImag";
149  else if ( htt == genie::kHT_QE_Full ) return "QE_Full";
150 
151  else if ( htt == genie::kHT_QE_CRPA_Low ) return "QE_CRPA_Low";
152  else if ( htt == genie::kHT_QE_CRPA_Medium ) return "QE_CRPA_Medium";
153  else if ( htt == genie::kHT_QE_CRPA_High ) return "QE_CRPA_High";
154 
155  else if ( htt == genie::kHT_QE_CRPA_anu_Low ) return "QE_CRPA_anu_Low";
156  else if ( htt == genie::kHT_QE_CRPA_anu_Medium ) return "QE_CRPA_anu_Medium";
157  else if ( htt == genie::kHT_QE_CRPA_anu_High ) return "QE_CRPA_anu_High";
158 
159  else if ( htt == genie::kHT_QE_HF_Low ) return "QE_HF_Low";
160  else if ( htt == genie::kHT_QE_HF_Medium ) return "QE_HF_Medium";
161  else if ( htt == genie::kHT_QE_HF_High ) return "QE_HF_High";
162 
163  else if ( htt == genie::kHT_QE_HF_anu_Low ) return "QE_HF_anu_Low";
164  else if ( htt == genie::kHT_QE_HF_anu_Medium ) return "QE_HF_anu_Medium";
165  else if ( htt == genie::kHT_QE_HF_anu_High ) return "QE_HF_anu_High";
166 
167  else if ( htt == genie::kHT_QE_CRPAPW_Low ) return "QE_CRPAPW_Low";
168  else if ( htt == genie::kHT_QE_CRPAPW_Medium ) return "QE_CRPAPW_Medium";
169  else if ( htt == genie::kHT_QE_CRPAPW_High ) return "QE_CRPAPW_High";
170 
171  else if ( htt == genie::kHT_QE_CRPAPW_anu_Low ) return "QE_CRPAPW_anu_Low";
172  else if ( htt == genie::kHT_QE_CRPAPW_anu_Medium ) return "QE_CRPAPW_anu_Medium";
173  else if ( htt == genie::kHT_QE_CRPAPW_anu_High ) return "QE_CRPAPW_anu_High";
174 
175  else if ( htt == genie::kHT_QE_HFPW_Low ) return "QE_HFPW_Low";
176  else if ( htt == genie::kHT_QE_HFPW_Medium ) return "QE_HFPW_Medium";
177  else if ( htt == genie::kHT_QE_HFPW_High ) return "QE_HFPW_High";
178 
179  else if ( htt == genie::kHT_QE_HFPW_anu_Low ) return "QE_HFPW_anu_Low";
180  else if ( htt == genie::kHT_QE_HFPW_anu_Medium ) return "QE_HFPW_anu_Medium";
181  else if ( htt == genie::kHT_QE_HFPW_anu_High ) return "QE_HFPW_anu_High";
182 
183  else if ( htt == genie::kHT_QE_SuSABlend ) return "QE_SuSABlend";
184  else if ( htt == genie::kHT_QE_SuSABlend_anu ) return "QE_SuSABlend_anu";
185 
186  else return "Undefined";
187  }
188 
189  /// Returns true if a given file exists and is accessible, or false otherwise
190  bool file_exists(const std::string& file_name) {
191  return std::ifstream(file_name.c_str()).good();
192  }
193 
194 }
195 
196 //____________________________________________________________________________
198  : genie::HadronTensorModelI()
199 {
200 
201 }
202 
203 //____________________________________________________________________________
205  : genie::HadronTensorModelI( name )
206 {
207 
208 }
209 
210 //____________________________________________________________________________
212  std::string config) : genie::HadronTensorModelI(name, config)
213 {
214 
215 }
216 
217 //____________________________________________________________________________
219 {
221  this->LoadConfig();
222 }
223 //____________________________________________________________________________
225 {
227  this->LoadConfig();
228 }
229 //____________________________________________________________________________
231 {
232  GetParamDef( "WarnIfMissing", fWarnIfMissing, true );
233 
234  // Either a data path relative to the root GENIE folder
235  // or an absolute path can be used. Find out which
236  // option was chosen.
237  std::string path_type;
238  GetParamDef( "DataPathType", path_type, std::string("relative") );
239 
240  // Right now, there can only be a single data path
241  // specified. We use a vector of paths to allow for
242  // easy expansion later.
243  std::string data_path;
244  GetParam( "DataPath", data_path );
245 
246  // Convert the relative path to an absolute one if needed
247  if ( path_type == "relative" ) {
248  data_path = std::string( gSystem->Getenv("GENIE") ) + '/' + data_path;
249  }
250 
251  fDataPaths.push_back( data_path );
252 }
253 
254 //____________________________________________________________________________
256 {
257  std::map< HadronTensorID, HadronTensorI* >::iterator it;
258  for (it = fTensors.begin(); it != fTensors.end(); ++it) {
259  HadronTensorI* t = it->second;
260  if ( t ) delete t;
261  }
262  fTensors.clear();
263 }
264 //____________________________________________________________________________
266  int tensor_pdg, genie::HadronTensorType_t type) const
267 {
268  HadronTensorID temp_id(tensor_pdg, type);
269 
270  // First check to see if the hadron tensor object already exists in memory.
271  // If it does, return the existing object.
272  if ( fTensors.count(temp_id) ) return fTensors.find(temp_id)->second;
273 
274  // If not, try to create it
275  const HadronTensorI* ht = this->BuildTensor( temp_id );
276 
277  if ( !ht && fWarnIfMissing ) {
278  LOG("TabulatedHadronTensorModelI", pWARN) << "Unable to create a hadron tensor"
279  << " for target pdg = " << temp_id.target_pdg
280  << " and hadron tensor type " << temp_id.type;
281  }
282 
283  return ht;
284 }
285 
286 //____________________________________________________________________________
288  const std::string& basename, bool& ok) const
289 {
290  for (size_t p = 0; p < fDataPaths.size(); ++p) {
291  const std::string& path = fDataPaths.at( p );
292  std::string full_name = path + '/' + basename;
293  if ( file_exists(full_name) ) return full_name;
294  }
295 
296  // A matching file could not be found
297  ok = false;
298  return std::string();
299 }
300 
301 //____________________________________________________________________________
303  const HadronTensorID& tensor_id) const
304 {
305  bool tensor_ok = true;
306 
307  std::string tensor_file_basename = this->GetTensorFileBasename( tensor_id );
308 
309  // Tensor values are represented using a 2D grid that is stored in a data
310  // file. Get the full path to the file, or an empty string if it could not
311  // be found. Also set the tensor_ok flag to false if the file could not be
312  // found.
313  std::string full_file_name = FindTensorTableFile(tensor_file_basename,
314  tensor_ok);
315 
316  if ( tensor_ok ) {
317 
318  // Create the new hadron tensor object
319  LOG("TabulatedHadronTensorModelI", pINFO) << "Loading the hadron"
320  << " tensor data file " << full_file_name;
321 
322  genie::HadronTensorI* temp_ptr = this->ParseTensorFile( full_file_name );
323 
324  // Place a pointer to it in the map of loaded tensor objects for easy
325  // retrieval.
326  /// \todo Switch to using std::unique_ptr here for easy cleanup
327  /// once C++11 features are allowed in GENIE
328  fTensors[tensor_id] = temp_ptr;
329 
330  // Return a pointer to the newly-created hadron tensor object
331  return temp_ptr;
332  }
333 
334  else {
335  // If we couldn't make the hadron tensor, store a nullptr to avoid
336  // unsuccessful repeat attempts. These can otherwise slow things down
337  // for no good reason.
338  fTensors[tensor_id] = NULL;
339 
340  if ( fWarnIfMissing ) {
341  LOG("TabulatedHadronTensorModelI", pERROR) << "The hadron tensor data file \""
342  << full_file_name << "\" requested for target pdg = "
343  << tensor_id.target_pdg << " and hadron tensor type "
344  << tensor_id.type << " could not be found.";
345  }
346  }
347 
348  // If there was a problem, return a null pointer
349  return NULL;
350 }
351 
352 //____________________________________________________________________________
354  const HadronTensorID& ht_id ) const
355 {
356 
357  std::string tgt_string;
358  std::stringstream ss;
359  ss << ht_id.target_pdg;
360  tgt_string = ss.str();
361 
362  RgKey key = tensor_type_to_string( ht_id.type ) + "@Pdg="
363  + tgt_string;
364 
365  std::string basename;
366  GetParamDef( key, basename, std::string("TENSOR_FILE_NOT_FOUND") );
367 
368  return basename;
369 }
std::string GetTensorFileBasename(const HadronTensorID &ht_id) const
std::string FindTensorTableFile(const std::string &basename, bool &ok) const
#define pERROR
Definition: Messenger.h:59
virtual void Configure(const Registry &config)
enum genie::HadronTensorType HadronTensorType_t
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
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:62
Abstract interface for an object that computes the elements a hadron tensor . Also computes the contr...
Definition: HadronTensorI.h:95
#define pINFO
Definition: Messenger.h:62
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.
const HadronTensorI * BuildTensor(const HadronTensorID &ht_id) const
Create a HadronTensorI object given a particular HadronTensorID.