Analysis Software
Documentation for sPHENIX simulation 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  if (TGeoManager::GetDefaultUnits() != TGeoManager::kRootUnits )
51  {
52  cout << __PRETTY_FUNCTION__ << " TGeoManager was not constructed with RootUnits, which potentially leads to unit mismatch with Fun4All. This is considered a fatal error."
53  <<endl;
54 
55  exit(1);
56  return nullptr;
57  }
58 
59  UpdateIONode(topNode);
60 
61  return dst_geom->GetGeometry();
62 }
63 
65  const std::string &geometry_file)
66 {
67  TGeoManager *tgeo = GetTGeoManager(topNode);
68 
69  assert(tgeo);
70 
71  tgeo->Export(geometry_file.c_str());
72 }
73 
75  const std::string &geometry_file)
76 {
77  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode);
78  assert(dst_geom);
79 
80  dst_geom->Reset();
81 
82  TGeoManager::SetVerboseLevel(GetVerbosity());
83 
84  // force TGeoManager to use the Fun4All unit of cm
85 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,23,2)
86  TGeoManager::LockDefaultUnits(kFALSE);
87  TGeoManager::SetDefaultUnits( TGeoManager::kRootUnits );
88  TGeoManager::LockDefaultUnits(kTRUE);
89 #else
90  TGeoManager::SetDefaultRootUnits();
91 #endif
92 
93  dst_geom->SetGeometry(TGeoManager::Import(geometry_file.c_str()));
94 
95  if (dst_geom->GetGeometry() == nullptr)
96  {
97  cout << __PRETTY_FUNCTION__ << "failed to import " << geometry_file
98  << endl;
100  }
101 
102  UpdateIONode(topNode);
103 
105 }
106 
108 {
109  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode);
110  assert(dst_geom);
111 
112  if (dst_geom->GetGeometry() == gGeoManager)
113  return Fun4AllReturnCodes::EVENT_OK; // noting to be done
114 
115  assert(not dst_geom->isValid()); // check that it is uninitialized
116  dst_geom->SetGeometry(gGeoManager);
117  TGeoManager::SetVerboseLevel(GetVerbosity());
118 
120 }
121 
123 PHGeomTGeo *
125 {
126  PHNodeIterator iter(topNode);
127 
128  // Looking for the RUN node
129  PHCompositeNode *parNode = static_cast<PHCompositeNode *>(iter.findFirst(
130  "PHCompositeNode", "PAR"));
131  if (!parNode)
132  {
133  ostringstream serr;
134  serr << __PRETTY_FUNCTION__ << ": PAR Node missing, request aborting.";
135  cout << serr.str() << endl;
136 
137  throw runtime_error(serr.str());
138 
139  return nullptr;
140  }
141 
142  PHGeomTGeo *dst_geom = findNode::getClass<PHGeomTGeo>(parNode,
143  GetDSTNodeName());
144  if (!dst_geom and build_new)
145  {
146  dst_geom = new PHGeomTGeo();
147  PHDataNode<PHObject> *GeomNode = new PHDataNode<PHObject>(dst_geom,
148  GetDSTNodeName(), "PHObject");
149  parNode->addNode(GeomNode);
150  }
151 
152  return dst_geom;
153 }
154 
156 PHGeomIOTGeo *
158 {
159  PHNodeIterator iter(topNode);
160 
161  // Looking for the RUN node
162  PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst(
163  "PHCompositeNode", "RUN"));
164  if (!runNode)
165  {
166  ostringstream serr;
167  serr << __PRETTY_FUNCTION__ << ": RUN Node missing, request aborting.";
168  cout << serr.str() << endl;
169 
170  throw runtime_error(serr.str());
171 
172  return nullptr;
173  }
174 
175  PHGeomIOTGeo *dst_geom = findNode::getClass<PHGeomIOTGeo>(runNode,
176  GetDSTIONodeName());
177  if (!dst_geom and build_new)
178  {
179  dst_geom = new PHGeomIOTGeo();
180  PHIODataNode<PHObject> *GeomNode = new PHIODataNode<PHObject>(dst_geom,
181  GetDSTIONodeName(), "PHObject");
182  runNode->addNode(GeomNode);
183  }
184 
185  return dst_geom;
186 }
187 
190 {
191  ostringstream file;
192 
193  uuid_t uu;
194  uuid_generate(uu);
195  char uuid[50];
196  uuid_unparse(uu, uuid);
197 
198  file << mg_GenerateGeometryFileNameBase << "/"
199  << "PHGeomUtility_geom_file_" << uuid << "."
200  << filename_extension;
201 
202  return file.str();
203 }
204 
206 
209 {
210  fstream ifile(file_name, ios_base::in);
211 
212  if (ifile)
213  {
214  ifile.close();
215  if (remove(file_name.c_str()) != 0)
216  {
217  cout << __PRETTY_FUNCTION__ << " - Error - can not remove file "
218  << file_name << endl;
219  return false;
220  }
221  else
222  return true;
223  }
224  else
225  return true; // file do not exist
226 }
227 
230 PHGeomIOTGeo *
232 {
233  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode, false);
234 
235  if (not dst_geom)
236  {
237  cout << __PRETTY_FUNCTION__
238  << " - ERROR - failed to update PHGeomIOTGeo node RUN/GEOMETRY_IO due to missing PHGeomTGeo node at RUN/GEOMETRY"
239  << endl;
240  return nullptr;
241  }
242  if (not dst_geom->isValid())
243  {
244  cout << __PRETTY_FUNCTION__
245  << " - ERROR - failed to update PHGeomIOTGeo node RUN/GEOMETRY_IO due to invalid PHGeomTGeo node at RUN/GEOMETRY"
246  << endl;
247  return nullptr;
248  }
249 
250  PHGeomIOTGeo *dst_geom_io = GetGeomIOTGeoNode(topNode, true);
251  assert(dst_geom_io);
252 
253  dst_geom_io->SetGeometry(dst_geom->GetGeometry()->GetTopVolume());
254 
255  return dst_geom_io;
256 }
257 
260 PHGeomTGeo *
262 {
263  PHGeomIOTGeo *dst_geom_io = GetGeomIOTGeoNode(topNode, false);
264 
265  if (not dst_geom_io)
266  {
267  cout << __PRETTY_FUNCTION__
268  << " - ERROR - failed to update PHGeomTGeo node RUN/GEOMETRY due to missing PHGeomIOTGeo node at RUN/GEOMETRY_IO"
269  << endl;
270  return nullptr;
271  }
272  if (not dst_geom_io->isValid())
273  {
274  cout << __PRETTY_FUNCTION__
275  << " - ERROR - failed to update PHGeomTGeo node RUN/GEOMETRY due to invalid PHGeomIOTGeo node at RUN/GEOMETRY_IO"
276  << endl;
277  return nullptr;
278  }
279 
280  // build new TGeoManager
281  TGeoManager::SetVerboseLevel(GetVerbosity());
282  TGeoManager *tgeo = dst_geom_io->ConstructTGeoManager();
283  tgeo->CloseGeometry();
284 
285  PHGeomTGeo *dst_geom = GetGeomTGeoNode(topNode, true);
286  assert(dst_geom);
287  dst_geom->SetGeometry(tgeo);
288 
289  return dst_geom;
290 }
291 
294 {
296  rc->set_IntFlag("PHGEOMETRY_VERBOSITY", v);
297 }
298 
301 {
303  if (rc->FlagExist("PHGEOMETRY_VERBOSITY"))
304  return rc->get_IntFlag("PHGEOMETRY_VERBOSITY");
305  else
306  return 0;
307 }