ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHGeomUtility.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHGeomUtility.cc
1 #include "PHGeomUtility.h"
2 
3 #include "PHGeomIOTGeo.h"
4 #include "PHGeomTGeo.h"
5 
7 
9 #include <phool/PHDataNode.h>
10 #include <phool/PHIODataNode.h>
11 #include <phool/PHNodeIterator.h>
12 #include <phool/PHObject.h>
13 #include <phool/getClass.h>
14 #include <phool/recoConsts.h>
15 
16 #include <TGeoManager.h>
17 
18 #include <uuid/uuid.h>
19 
20 #include <unistd.h> // for generate unique local file
21 #include <cassert>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <stdexcept>
28 
29 using namespace std;
30 
32 TGeoManager *
34 {
35  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode, true);
36  if (!dst_geom)
37  {
38  cout << __PRETTY_FUNCTION__
39  << " - Error - Can NOT construct geometry node." << endl;
40  exit(1);
41  return nullptr;
42  }
43 
44  if (not dst_geom->isValid())
45  {
46  // try to construct the geometry node
47  dst_geom = LoadFromIONode(topNode);
48  }
49 
50  UpdateIONode(topNode);
51 
52  return dst_geom->GetGeometry();
53 }
54 
56  const std::string &geometry_file)
57 {
58  TGeoManager *tgeo = GetTGeoManager(topNode);
59 
60  assert(tgeo);
61 
62  tgeo->Export(geometry_file.c_str());
63 }
64 
66  const std::string &geometry_file)
67 {
68  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode);
69  assert(dst_geom);
70 
71  dst_geom->Reset();
72 
73  TGeoManager::SetVerboseLevel(GetVerbosity());
74  dst_geom->SetGeometry(TGeoManager::Import(geometry_file.c_str()));
75 
76  if (dst_geom->GetGeometry() == nullptr)
77  {
78  cout << __PRETTY_FUNCTION__ << "failed to import " << geometry_file
79  << endl;
81  }
82 
83  UpdateIONode(topNode);
84 
86 }
87 
89 {
90  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode);
91  assert(dst_geom);
92 
93  if (dst_geom->GetGeometry() == gGeoManager)
94  return Fun4AllReturnCodes::EVENT_OK; // noting to be done
95 
96  assert(not dst_geom->isValid()); // check that it is uninitialized
97  dst_geom->SetGeometry(gGeoManager);
98  TGeoManager::SetVerboseLevel(GetVerbosity());
99 
101 }
102 
104 PHGeomTGeo *
106 {
107  PHNodeIterator iter(topNode);
108 
109  // Looking for the RUN node
110  PHCompositeNode *parNode = static_cast<PHCompositeNode *>(iter.findFirst(
111  "PHCompositeNode", "PAR"));
112  if (!parNode)
113  {
114  ostringstream serr;
115  serr << __PRETTY_FUNCTION__ << ": PAR Node missing, request aborting.";
116  cout << serr.str() << endl;
117 
118  throw runtime_error(serr.str());
119 
120  return nullptr;
121  }
122 
123  PHGeomTGeo *dst_geom = findNode::getClass<PHGeomTGeo>(parNode,
124  GetDSTNodeName());
125  if (!dst_geom and build_new)
126  {
127  dst_geom = new PHGeomTGeo();
128  PHDataNode<PHObject> *GeomNode = new PHDataNode<PHObject>(dst_geom,
129  GetDSTNodeName(), "PHObject");
130  parNode->addNode(GeomNode);
131  }
132 
133  return dst_geom;
134 }
135 
137 PHGeomIOTGeo *
139 {
140  PHNodeIterator iter(topNode);
141 
142  // Looking for the RUN node
143  PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst(
144  "PHCompositeNode", "RUN"));
145  if (!runNode)
146  {
147  ostringstream serr;
148  serr << __PRETTY_FUNCTION__ << ": RUN Node missing, request aborting.";
149  cout << serr.str() << endl;
150 
151  throw runtime_error(serr.str());
152 
153  return nullptr;
154  }
155 
156  PHGeomIOTGeo *dst_geom = findNode::getClass<PHGeomIOTGeo>(runNode,
157  GetDSTIONodeName());
158  if (!dst_geom and build_new)
159  {
160  dst_geom = new PHGeomIOTGeo();
161  PHIODataNode<PHObject> *GeomNode = new PHIODataNode<PHObject>(dst_geom,
162  GetDSTIONodeName(), "PHObject");
163  runNode->addNode(GeomNode);
164  }
165 
166  return dst_geom;
167 }
168 
169 std::string
170 PHGeomUtility::GenerateGeometryFileName(const std::string &filename_extension)
171 {
172  ostringstream file;
173 
174  uuid_t uu;
175  uuid_generate(uu);
176  char uuid[50];
177  uuid_unparse(uu, uuid);
178 
179  file << mg_GenerateGeometryFileNameBase << "/"
180  << "PHGeomUtility_geom_file_" << uuid << "."
181  << filename_extension;
182 
183  return file.str();
184 }
185 
187 
189 bool PHGeomUtility::RemoveGeometryFile(const std::string &file_name)
190 {
191  fstream ifile(file_name, ios_base::in);
192 
193  if (ifile)
194  {
195  ifile.close();
196  if (remove(file_name.c_str()) != 0)
197  {
198  cout << __PRETTY_FUNCTION__ << " - Error - can not remove file "
199  << file_name << endl;
200  return false;
201  }
202  else
203  return true;
204  }
205  else
206  return true; // file do not exist
207 }
208 
211 PHGeomIOTGeo *
213 {
214  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode, false);
215 
216  if (not dst_geom)
217  {
218  cout << __PRETTY_FUNCTION__
219  << " - ERROR - failed to update PHGeomIOTGeo node RUN/GEOMETRY_IO due to missing PHGeomTGeo node at RUN/GEOMETRY"
220  << endl;
221  return nullptr;
222  }
223  if (not dst_geom->isValid())
224  {
225  cout << __PRETTY_FUNCTION__
226  << " - ERROR - failed to update PHGeomIOTGeo node RUN/GEOMETRY_IO due to invalid PHGeomTGeo node at RUN/GEOMETRY"
227  << endl;
228  return nullptr;
229  }
230 
231  PHGeomIOTGeo *dst_geom_io = GetGeomIOTGeoNode(topNode, true);
232  assert(dst_geom_io);
233 
234  dst_geom_io->SetGeometry(dst_geom->GetGeometry()->GetTopVolume());
235 
236  return dst_geom_io;
237 }
238 
241 PHGeomTGeo *
243 {
244  PHGeomIOTGeo *dst_geom_io = GetGeomIOTGeoNode(topNode, false);
245 
246  if (not dst_geom_io)
247  {
248  cout << __PRETTY_FUNCTION__
249  << " - ERROR - failed to update PHGeomTGeo node RUN/GEOMETRY due to missing PHGeomIOTGeo node at RUN/GEOMETRY_IO"
250  << endl;
251  return nullptr;
252  }
253  if (not dst_geom_io->isValid())
254  {
255  cout << __PRETTY_FUNCTION__
256  << " - ERROR - failed to update PHGeomTGeo node RUN/GEOMETRY due to invalid PHGeomIOTGeo node at RUN/GEOMETRY_IO"
257  << endl;
258  return nullptr;
259  }
260 
261  // build new TGeoManager
262  TGeoManager::SetVerboseLevel(GetVerbosity());
263  TGeoManager *tgeo = dst_geom_io->ConstructTGeoManager();
264  tgeo->CloseGeometry();
265 
266  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode, true);
267  assert(dst_geom);
268  dst_geom->SetGeometry(tgeo);
269 
270  return dst_geom;
271 }
272 
275 {
277  rc->set_IntFlag("PHGEOMETRY_VERBOSITY", v);
278 }
279 
282 {
284  if (rc->FlagExist("PHGEOMETRY_VERBOSITY"))
285  return rc->get_IntFlag("PHGEOMETRY_VERBOSITY");
286  else
287  return 0;
288 }