Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHGeomIOTGeo.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHGeomIOTGeo.cc
1 // $Id: $
2 
11 #include "PHGeomIOTGeo.h"
12 
13 #include <TGeoManager.h>
14 #include <TGeoVolume.h>
15 #include <TMemFile.h>
16 #include <TObject.h> // for TObject
17 
18 #include <cassert>
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 
23 using namespace std;
24 
26  : Data(0)
27 {
28 }
29 
31 {
32  Data.resize(0);
33 }
34 
35 void PHGeomIOTGeo::SetGeometry(const TGeoVolume* g)
36 {
37  if (!g)
38  {
39  cout << __PRETTY_FUNCTION__ << " - Error - Invalid input" << endl;
40  return;
41  }
42 
43  // Stream TGeoVolume into binary stream with its streamer using TFIle utility
44  TMemFile f1("mem", "CREATE");
45  g->Write("TOP");
46  f1.Close();
47 
48  const Long64_t n = f1.GetSize();
49 
50  Data.resize(n);
51  Long64_t n1 = f1.CopyTo(Data.data(), n);
52  assert(n1 == n);
53 }
54 
55 TGeoVolume*
57 {
58  if (not isValid()) return nullptr;
59 
60  TMemFile f2("mem2", Data.data(), Data.size(), "READ");
61  TGeoVolume* vol = dynamic_cast<TGeoVolume*>(f2.Get("TOP"));
62  assert(vol);
63  f2.Close();
64 
65  return vol;
66 }
67 
68 TGeoManager*
71 {
72  if (not isValid()) return nullptr;
73 
74  // force TGeoManager to use the Fun4All unit of cm
75 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,23,2)
76  TGeoManager::LockDefaultUnits(kFALSE);
77  TGeoManager::SetDefaultUnits( TGeoManager::kRootUnits );
78  TGeoManager::LockDefaultUnits(kTRUE);
79 #else
80  TGeoManager::SetDefaultRootUnits();
81 #endif
82 
83  // build new TGeoManager
84  TGeoManager* tgeo = new TGeoManager("PHGeometry", "");
85  assert(tgeo);
86 
87  TGeoVolume* vol = GetGeometryCopy();
88  vol->RegisterYourself();
89 
90  tgeo->SetTopVolume(vol);
91  // tgeo->CloseGeometry();
92 
93  ostringstream stitle;
94  stitle
95  << "TGeoManager built by PHGeomUtility::LoadFromIONode based on RUN/GEOMETRY_IO node with name ("
96  << vol->GetName() << ") and title ("
97  << vol->GetTitle() << ")";
98 
99  tgeo->SetTitle(stitle.str().c_str());
100 
101  return tgeo;
102 }
103 
107 void PHGeomIOTGeo::identify(std::ostream& os) const
108 {
109  os << "PHGeomIOTGeo - ";
110  if (isValid())
111  os << " with geometry data " << Data.size() << "Byte";
112  else
113  os << "Empty";
114  os << endl;
115 }
116 
119 {
120  Data.resize(0);
121 }
122 
125 {
126  return Data.size();
127 }