ECCE @ EIC 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  // build new TGeoManager
75  TGeoManager* tgeo = new TGeoManager("PHGeometry", "");
76  assert(tgeo);
77 
78  TGeoVolume* vol = GetGeometryCopy();
79  vol->RegisterYourself();
80 
81  tgeo->SetTopVolume(vol);
82  // tgeo->CloseGeometry();
83 
84  ostringstream stitle;
85  stitle
86  << "TGeoManager built by PHGeomUtility::LoadFromIONode based on RUN/GEOMETRY_IO node with name ("
87  << vol->GetName() << ") and title ("
88  << vol->GetTitle() << ")";
89 
90  tgeo->SetTitle(stitle.str().c_str());
91 
92  return tgeo;
93 }
94 
98 void PHGeomIOTGeo::identify(std::ostream& os) const
99 {
100  os << "PHGeomIOTGeo - ";
101  if (isValid())
102  os << " with geometry data " << Data.size() << "Byte";
103  else
104  os << "Empty";
105  os << endl;
106 }
107 
110 {
111  Data.resize(0);
112 }
113 
116 {
117  return Data.size();
118 }