Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:20:20

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