OMSim
Geant4 for IceCube optical module studies
OMSimInputData.hh
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "OMSimLogger.hh"
9 #include "OMSimOpBoundaryProcess.hh"
10 #include <G4SystemOfUnits.hh>
11 #include <boost/property_tree/json_parser.hpp>
12 
13 namespace pt = boost::property_tree;
14 
39 {
40 public:
41  ParameterTable(){};
42 
51  template <typename T>
52  T getValue(G4String pKey, G4String pParameter)
53  {
54  log_trace("Fetching parameter {} in key {}", pParameter, pKey);
55  try
56  {
57  const T lValue = m_table.at(pKey).get<T>(pParameter);
58  return lValue;
59  }
60 
61  catch (const std::exception &e)
62  {
63  log_error("Fetching parameter {} in key {} from table failed!", pParameter, pKey);
64  throw std::runtime_error("Fetching parameter failed");
65  }
66  }
67 
68  G4bool checkIfTreeNameInTable(G4String pKey);
69  G4bool checkIfKeyInTree(G4String p_treeName, G4String p_key);
70  G4double getValueWithUnit(G4String pKey, G4String pParameter);
71  pt::ptree appendAndReturnTree(G4String pFileName);
72  pt::ptree getJSONTree(G4String pKey);
73 
85  template <typename T>
86  void parseKeyContentToVector(std::vector<T> &pVector, pt::ptree pTree,
87  std::basic_string<char> pKey, G4double pScaling,
88  bool pInverse)
89  {
90  log_trace("Parsing content in key {} to a vector", pKey);
91  for (pt::ptree::value_type &ridx : pTree.get_child(
92  pKey))
93  { // get array from element with key "pKey" of the json
94  if (pInverse)
95  { // if we need 1/x
96  pVector.push_back(pScaling / ridx.second.get_value<T>());
97  }
98  else
99  { // otherwise we only by scaling factor
100  pVector.push_back(ridx.second.get_value<T>() * pScaling);
101  }
102  }
103  };
104 
118  template <typename T>
119  void parseKeyContentToVector(std::vector<T> &pVector,
120  std::basic_string<char> p_MapKey,
121  std::basic_string<char> pKey, G4double pScaling,
122  bool pInverse)
123  {
124  log_trace("Parsing content in key {} to a vector", pKey);
125 
126  // Get the JSON tree
127  boost::property_tree::ptree lTree = getJSONTree(p_MapKey);
128 
129  // Check if the tree is empty
130  if (lTree.empty())
131  {
132  log_warning("JSON tree is empty for key {}", p_MapKey);
133  return;
134  }
135 
136  // Access the child node with the key "pKey"
137  auto lChildNode = lTree.get_child_optional(pKey);
138 
139  // Check if the child node exists
140  if (!lChildNode)
141  {
142  log_warning("Child node with key {} not found in JSON tree", pKey);
143  return;
144  }
145 
146  // Iterate over the child nodes and parse the content
147  for (const auto &ridx : *lChildNode)
148  {
149  if (pInverse)
150  {
151  // if we need 1/x
152  pVector.push_back(pScaling / ridx.second.get_value<T>());
153  }
154  else
155  {
156  // otherwise we only multiply by scaling factor
157  pVector.push_back(ridx.second.get_value<T>() * pScaling);
158  }
159  }
160  };
161 
162 private:
163  std::map<G4String, boost::property_tree::ptree> m_table;
164  std::map<G4String, G4String> m_keyToFileName;
165 };
166 
189 {
190 public:
191  static void init();
192  static void shutdown();
193  static OMSimInputData& getInstance();
194  G4Material *getMaterial(G4String pName);
195  G4OpticalSurface *getOpticalSurface(G4String pName);
196  void searchFolders();
197  std::map<G4String, G4OpticalSurface *> m_opticalSurfaceMap;
198 
199 private:
200  enum class FileType {
201  IceCubeICE,
202  Scintillator,
203  Extra,
204  Table,
205  Surface,
206  Material
207  };
208 
209  static const std::unordered_map<std::string, FileType> fileTypePrefixes;
210  FileType getFileType(const std::string& fileName) const;
211 
212  OMSimInputData() = default;
213  ~OMSimInputData() = default;
214  OMSimInputData(const OMSimInputData&) = delete;
215  OMSimInputData& operator=(const OMSimInputData&) = delete;
216  void scannDataDirectory();
217  void processFile(const std::string &filePath, const std::string &fileName);
218  G4String m_dataDirectory;
219 };
220 
221 inline OMSimInputData* g_OMSimInputData = nullptr;
222 
Manages the input data, including parsing and storing material properties.
Definition: OMSimInputData.hh:189
G4Material * getMaterial(G4String pName)
Retrieves a G4Material based on the given name.
Definition: OMSimInputData.cc:190
std::map< G4String, G4OpticalSurface * > m_opticalSurfaceMap
Map that links names with optical surfaces.
Definition: OMSimInputData.hh:197
static OMSimInputData & getInstance()
Definition: OMSimInputData.cc:169
void searchFolders()
Searches through predefined folders for input data files.
Definition: OMSimInputData.cc:267
void processFile(const std::string &filePath, const std::string &fileName)
Processes a data file based on its name prefix.
Definition: OMSimInputData.cc:294
static void shutdown()
Deletes the global instance of OMSimInputData.
Definition: OMSimInputData.cc:159
G4OpticalSurface * getOpticalSurface(G4String pName)
Definition: OMSimInputData.cc:236
G4String m_dataDirectory
The current directory being scanned for data.
Definition: OMSimInputData.hh:218
static void init()
Initializes the global instance of OMSimInputData and calls OMSimInputData::searchFolders to load dat...
Definition: OMSimInputData.cc:145
A utility class for managing JSON-based data tables.
Definition: OMSimInputData.hh:39
pt::ptree getJSONTree(G4String pKey)
Definition: OMSimInputData.cc:87
pt::ptree appendAndReturnTree(G4String pFileName)
Definition: OMSimInputData.cc:21
void parseKeyContentToVector(std::vector< T > &pVector, pt::ptree pTree, std::basic_string< char > pKey, G4double pScaling, bool pInverse)
Parses the content of a JSON subtree into a vector, scaling values if necessary.
Definition: OMSimInputData.hh:86
G4bool checkIfTreeNameInTable(G4String pKey)
Checks if a key exists within the table.
Definition: OMSimInputData.cc:102
G4bool checkIfKeyInTree(G4String p_treeName, G4String p_key)
Checks if a specific key exists in a given JSON tree.
Definition: OMSimInputData.cc:119
void parseKeyContentToVector(std::vector< T > &pVector, std::basic_string< char > p_MapKey, std::basic_string< char > pKey, G4double pScaling, bool pInverse)
Parses the content of a JSON subtree into a vector, scaling values if necessary.
Definition: OMSimInputData.hh:119
G4double getValueWithUnit(G4String pKey, G4String pParameter)
Fetches the value associated with a given key and parameter.
Definition: OMSimInputData.cc:39
std::map< G4String, G4String > m_keyToFileName
A table mapping keys to original file name.
Definition: OMSimInputData.hh:164
T getValue(G4String pKey, G4String pParameter)
Fetches a value from the table based on a key and parameter.
Definition: OMSimInputData.hh:52
std::map< G4String, boost::property_tree::ptree > m_table
A table mapping keys to property trees.
Definition: OMSimInputData.hh:160