Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:59

0001 // $Id: $
0002 
0003 /*!
0004  * \file PHGeomIOTGeo.cc
0005  * \brief 
0006  * \author Jin Huang <jhuang@bnl.gov>
0007  * \version $Revision:   $
0008  * \date $Date: $
0009  */
0010 
0011 #include "PHGeomIOTGeo.h"
0012 
0013 #include <TGeoManager.h>
0014 #include <TGeoVolume.h>
0015 #include <TMemFile.h>
0016 #include <TObject.h>      // for TObject
0017 
0018 #include <cassert>
0019 #include <iostream>
0020 #include <sstream>
0021 #include <string>
0022 
0023 using namespace std;
0024 
0025 PHGeomIOTGeo::PHGeomIOTGeo()
0026   : Data(0)
0027 {
0028 }
0029 
0030 PHGeomIOTGeo::~PHGeomIOTGeo()
0031 {
0032     Data.resize(0);
0033 }
0034 
0035 void PHGeomIOTGeo::SetGeometry(const TGeoVolume* g)
0036 {
0037   if (!g)
0038   {
0039     cout << __PRETTY_FUNCTION__ << " - Error - Invalid input" << endl;
0040     return;
0041   }
0042 
0043   // Stream TGeoVolume into binary stream with its streamer using TFIle utility
0044   TMemFile f1("mem", "CREATE");
0045   g->Write("TOP");
0046   f1.Close();
0047 
0048   const Long64_t n = f1.GetSize();
0049 
0050   Data.resize(n);
0051   Long64_t n1 = f1.CopyTo(Data.data(), n);
0052   assert(n1 == n);
0053 }
0054 
0055 TGeoVolume*
0056 PHGeomIOTGeo::GetGeometryCopy()
0057 {
0058   if (not isValid()) return nullptr;
0059 
0060   TMemFile f2("mem2", Data.data(), Data.size(), "READ");
0061   TGeoVolume* vol = dynamic_cast<TGeoVolume*>(f2.Get("TOP"));
0062   assert(vol);
0063   f2.Close();
0064 
0065   return vol;
0066 }
0067 
0068 TGeoManager*
0069 PHGeomIOTGeo::
0070     ConstructTGeoManager()
0071 {
0072   if (not isValid()) return nullptr;
0073 
0074   // force TGeoManager to use the Fun4All unit of cm
0075 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,23,2)
0076   TGeoManager::LockDefaultUnits(kFALSE);
0077   TGeoManager::SetDefaultUnits( TGeoManager::kRootUnits );
0078   TGeoManager::LockDefaultUnits(kTRUE);
0079 #else
0080   TGeoManager::SetDefaultRootUnits();
0081 #endif
0082 
0083   // build new TGeoManager
0084   TGeoManager* tgeo = new TGeoManager("PHGeometry", "");
0085   assert(tgeo);
0086 
0087   TGeoVolume* vol = GetGeometryCopy();
0088   vol->RegisterYourself();
0089 
0090   tgeo->SetTopVolume(vol);
0091   //  tgeo->CloseGeometry();
0092 
0093   ostringstream stitle;
0094   stitle
0095       << "TGeoManager built by PHGeomUtility::LoadFromIONode based on RUN/GEOMETRY_IO node with name ("
0096       << vol->GetName() << ") and title ("
0097       << vol->GetTitle() << ")";
0098 
0099   tgeo->SetTitle(stitle.str().c_str());
0100 
0101   return tgeo;
0102 }
0103 
0104 /** identify Function from PHObject
0105  @param os Output Stream
0106  */
0107 void PHGeomIOTGeo::identify(std::ostream& os) const
0108 {
0109   os << "PHGeomIOTGeo - ";
0110   if (isValid())
0111     os << " with geometry data " << Data.size() << "Byte";
0112   else
0113     os << "Empty";
0114   os << endl;
0115 }
0116 
0117 /// Clear Event
0118 void PHGeomIOTGeo::Reset()
0119 {
0120   Data.resize(0);
0121 }
0122 
0123 /// isValid returns non zero if object contains vailid data
0124 int PHGeomIOTGeo::isValid() const
0125 {
0126   return Data.size();
0127 }