Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:17:48

0001 // $Id: $
0002 
0003 /*!
0004  * \file PHG4GDMLDetector.cc
0005  * \brief
0006  * \author Jin Huang <jhuang@bnl.gov>
0007  * \version $Revision:   $
0008  * \date $Date: $
0009  */
0010 
0011 #include "PHG4GDMLDetector.h"
0012 
0013 #include <g4main/PHG4Detector.h>  // for PHG4Detector
0014 #include <g4main/PHG4Subsystem.h>
0015 #include <g4main/PHG4Utils.h>
0016 
0017 #include <phparameter/PHParameters.h>
0018 
0019 #include <g4gdml/PHG4GDMLConfig.hh>
0020 #include <g4gdml/PHG4GDMLUtility.hh>
0021 
0022 #include <Geant4/G4AssemblyVolume.hh>
0023 #include <Geant4/G4LogicalVolume.hh>
0024 #include <Geant4/G4Material.hh>
0025 #include <Geant4/G4PVPlacement.hh>
0026 #include <Geant4/G4RotationMatrix.hh>  // for G4RotationMatrix
0027 #include <Geant4/G4String.hh>          // for G4String
0028 #include <Geant4/G4SystemOfUnits.hh>
0029 #include <Geant4/G4ThreeVector.hh>      // for G4ThreeVector
0030 #include <Geant4/G4VPhysicalVolume.hh>  // for G4VPhysicalVolume
0031 #include <Geant4/G4VisAttributes.hh>
0032 
0033 // Xerces has shadowed variables
0034 #pragma GCC diagnostic push
0035 #pragma GCC diagnostic ignored "-Wshadow"
0036 #include <Geant4/G4GDMLParser.hh>
0037 #include <Geant4/G4GDMLReadStructure.hh>  // for G4GDMLReadStructure
0038 #pragma GCC diagnostic pop
0039 
0040 #include <CLHEP/Units/SystemOfUnits.h>  // for cm, degree
0041 
0042 #include <cassert>
0043 #include <cstdlib>   // for exit
0044 #include <iostream>  // for operator<<, basic_ostream
0045 #include <memory>
0046 #include <vector>  // for vector, vector<>::iterator
0047 
0048 PHG4GDMLDetector::PHG4GDMLDetector(PHG4Subsystem* subsys, PHCompositeNode* Node, const std::string& dnam, PHParameters* parameters)
0049   : PHG4Detector(subsys, Node, dnam)
0050   , m_GDMPath(parameters->get_string_param("GDMPath"))
0051   , m_TopVolName(parameters->get_string_param("TopVolName"))
0052   , m_placeX(parameters->get_double_param("place_x") * cm)
0053   , m_placeY(parameters->get_double_param("place_y") * cm)
0054   , m_placeZ(parameters->get_double_param("place_z") * cm)
0055   , m_rotationX(parameters->get_double_param("rot_x") * degree)
0056   , m_rotationY(parameters->get_double_param("rot_y") * degree)
0057   , m_rotationZ(parameters->get_double_param("rot_z") * degree)
0058   , m_skipDSTGeometryExport(parameters->get_int_param("skip_DST_geometry_export"))
0059 {
0060   if (m_skipDSTGeometryExport)
0061   {
0062     gdml_config = PHG4GDMLUtility::GetOrMakeConfigNode(Node);
0063     assert(gdml_config);
0064   }
0065 }
0066 
0067 PHG4GDMLDetector::~PHG4GDMLDetector() = default;
0068 
0069 void
0070 
0071 PHG4GDMLDetector::Print(const std::string& /*what*/) const
0072 {
0073   std::cout << "PHG4GDMLDetector::" << GetName() << " - import " << m_TopVolName << " from " << m_GDMPath << " with shift "
0074             << m_placeX << ","
0075             << m_placeY << ","
0076             << m_placeZ << "cm and rotation "
0077             << m_rotationX << ","
0078             << m_rotationY << ","
0079             << m_rotationZ << "rad" << std::endl;
0080 }
0081 
0082 void PHG4GDMLDetector::ConstructMe(G4LogicalVolume* logicWorld)
0083 {
0084   if (Verbosity() > 0)
0085   {
0086     std::cout << " PHG4MapsDetector::Construct:";
0087     Print();
0088     //      std::cout << std::endl;
0089   }
0090 
0091   //===================================
0092   // Import the stave physical volume here
0093   //===================================
0094 
0095   // import the staves from the gemetry file
0096   std::unique_ptr<G4GDMLReadStructure> reader(new G4GDMLReadStructure());
0097   G4GDMLParser gdmlParser(reader.get());
0098   gdmlParser.SetOverlapCheck(OverlapCheck());
0099   //  gdmlParser.Read(m_GDMPath, false);
0100   gdmlParser.Read(m_GDMPath, OverlapCheck());
0101 
0102   //  G4AssemblyVolume* av_ITSUStave = reader->GetAssembly(assemblyname);
0103 
0104   G4LogicalVolume* vol = reader->GetVolume(m_TopVolName);
0105 
0106   if (not vol)
0107   {
0108     std::cout << "PHG4GDMLDetector::Construct - Fatal Error - failed to find G4LogicalVolume " << m_TopVolName << " - Print: ";
0109     Print();
0110     exit(121);
0111   }
0112   PHG4Subsystem* mysys = GetMySubsystem();
0113   mysys->SetLogicalVolume(vol);
0114 
0115   G4RotationMatrix* rotm = new G4RotationMatrix();
0116   rotm->rotateX(m_rotationX);
0117   rotm->rotateY(m_rotationY);
0118   rotm->rotateZ(m_rotationZ);
0119   G4ThreeVector placeVec(m_placeX, m_placeY, m_placeZ);
0120 
0121   //  av_ITSUStave->MakeImprint(trackerenvelope, Tr, 0, OverlapCheck());
0122 
0123   G4PVPlacement* gdml_phys =
0124       new G4PVPlacement(rotm, placeVec,
0125                         vol,
0126                         G4String(GetName()),
0127                         logicWorld, false, 0, OverlapCheck());
0128   SetDisplayProperty(vol);
0129 
0130   if (m_skipDSTGeometryExport)
0131   {
0132     assert(gdml_config);
0133     gdml_config->exclude_physical_vol(gdml_phys);
0134   }
0135 }
0136 
0137 void PHG4GDMLDetector::SetDisplayProperty(G4AssemblyVolume* av)
0138 {
0139   //  std::cout <<"SetDisplayProperty - G4AssemblyVolume w/ TotalImprintedVolumes "<<av->TotalImprintedVolumes()
0140   //   <<"/"<<av->GetImprintsCount()<<std::endl;
0141 
0142   std::vector<G4VPhysicalVolume*>::iterator it = av->GetVolumesIterator();
0143 
0144   int nDaughters = av->TotalImprintedVolumes();
0145   for (int i = 0; i < nDaughters; ++i, ++it)
0146   {
0147     //  std::cout <<"SetDisplayProperty - AV["<<i<<"] = "<<(*it)->GetName()<<std::endl;
0148     G4VPhysicalVolume* pv = (*it);
0149 
0150     G4LogicalVolume* worldLogical = pv->GetLogicalVolume();
0151     SetDisplayProperty(worldLogical);
0152   }
0153 }
0154 
0155 // NOLINTNEXTLINE(misc-no-recursion)
0156 void PHG4GDMLDetector::SetDisplayProperty(G4LogicalVolume* lv)
0157 {
0158   std::string material_name(
0159       lv->GetMaterial()->GetName());
0160 
0161   if (Verbosity() >= 5)
0162   {
0163     std::cout << "SetDisplayProperty - LV " << lv->GetName() << " built with "
0164               << material_name << std::endl;
0165   }
0166 
0167   G4VisAttributes* matVis = new G4VisAttributes();
0168   if (material_name.find("SI") != std::string::npos)
0169   {
0170     PHG4Utils::SetColour(matVis, "G4_Si");
0171     matVis->SetVisibility(true);
0172     matVis->SetForceSolid(true);
0173     if (Verbosity() >= 5)
0174     {
0175       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_Si" << std::endl;
0176     }
0177   }
0178   else if (material_name.find("KAPTON") != std::string::npos)
0179   {
0180     PHG4Utils::SetColour(matVis, "G4_KAPTON");
0181     matVis->SetVisibility(true);
0182     matVis->SetForceSolid(true);
0183     if (Verbosity() >= 5)
0184     {
0185       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_KAPTON" << std::endl;
0186     }
0187   }
0188   else if (material_name.find("ALUMINUM") != std::string::npos)
0189   {
0190     PHG4Utils::SetColour(matVis, "G4_Al");
0191     matVis->SetVisibility(true);
0192     matVis->SetForceSolid(true);
0193     if (Verbosity() >= 5)
0194     {
0195       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_Al" << std::endl;
0196     }
0197   }
0198   else if (material_name.find("Carbon") != std::string::npos)
0199   {
0200     matVis->SetColour(0.5, 0.5, 0.5, .25);
0201     matVis->SetVisibility(true);
0202     matVis->SetForceSolid(true);
0203     if (Verbosity() >= 5)
0204     {
0205       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with Gray" << std::endl;
0206     }
0207   }
0208   else if (material_name.find("M60J3K") != std::string::npos)
0209   {
0210     matVis->SetColour(0.25, 0.25, 0.25, .25);
0211     matVis->SetVisibility(true);
0212     matVis->SetForceSolid(true);
0213     if (Verbosity() >= 5)
0214     {
0215       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with Gray" << std::endl;
0216     }
0217   }
0218   else if (material_name.find("WATER") != std::string::npos)
0219   {
0220     matVis->SetColour(0.0, 0.5, 0.0, .25);
0221     matVis->SetVisibility(true);
0222     matVis->SetForceSolid(true);
0223     if (Verbosity() >= 5)
0224     {
0225       std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with WATER" << std::endl;
0226     }
0227   }
0228   else
0229   {
0230     matVis->SetColour(.2, .2, .7, .25);
0231     matVis->SetVisibility(true);
0232     matVis->SetForceSolid(true);
0233   }
0234   lv->SetVisAttributes(matVis);
0235 
0236   int nDaughters = lv->GetNoDaughters();
0237   for (int i = 0; i < nDaughters; ++i)
0238   {
0239     G4VPhysicalVolume* pv = lv->GetDaughter(i);
0240 
0241     // std::cout <<"SetDisplayProperty - PV["<<i<<"] = "<<pv->GetName()<<std::endl;
0242 
0243     G4LogicalVolume* worldLogical = pv->GetLogicalVolume();
0244     SetDisplayProperty(worldLogical);
0245   }
0246 }