Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4GDMLDetector.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4GDMLDetector.cc
1 // $Id: $
2 
11 #include "PHG4GDMLDetector.h"
12 
13 #include <g4main/PHG4Detector.h> // for PHG4Detector
14 #include <g4main/PHG4Subsystem.h>
15 #include <g4main/PHG4Utils.h>
16 
17 #include <phparameter/PHParameters.h>
18 
19 #include <g4gdml/PHG4GDMLConfig.hh>
21 
22 #include <Geant4/G4AssemblyVolume.hh>
23 #include <Geant4/G4LogicalVolume.hh>
24 #include <Geant4/G4Material.hh>
25 #include <Geant4/G4PVPlacement.hh>
26 #include <Geant4/G4RotationMatrix.hh> // for G4RotationMatrix
27 #include <Geant4/G4String.hh> // for G4String
28 #include <Geant4/G4SystemOfUnits.hh>
29 #include <Geant4/G4ThreeVector.hh> // for G4ThreeVector
30 #include <Geant4/G4VPhysicalVolume.hh> // for G4VPhysicalVolume
31 #include <Geant4/G4VisAttributes.hh>
32 
33 // Xerces has shadowed variables
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wshadow"
36 #include <Geant4/G4GDMLParser.hh>
37 #include <Geant4/G4GDMLReadStructure.hh> // for G4GDMLReadStructure
38 #pragma GCC diagnostic pop
39 
40 #include <CLHEP/Units/SystemOfUnits.h> // for cm, degree
41 
42 #include <cassert>
43 #include <cstdlib> // for exit
44 #include <iostream> // for operator<<, basic_ostream
45 #include <memory>
46 #include <vector> // for vector, vector<>::iterator
47 
49  : PHG4Detector(subsys, Node, dnam)
50  , m_GDMPath(parameters->get_string_param("GDMPath"))
51  , m_TopVolName(parameters->get_string_param("TopVolName"))
52  , m_placeX(parameters->get_double_param("place_x") * cm)
53  , m_placeY(parameters->get_double_param("place_y") * cm)
54  , m_placeZ(parameters->get_double_param("place_z") * cm)
55  , m_rotationX(parameters->get_double_param("rot_x") * degree)
56  , m_rotationY(parameters->get_double_param("rot_y") * degree)
57  , m_rotationZ(parameters->get_double_param("rot_z") * degree)
58  , m_skipDSTGeometryExport(parameters->get_int_param("skip_DST_geometry_export"))
59 {
61  {
64  }
65 }
66 
68 
69 void
70 
71 PHG4GDMLDetector::Print(const std::string& /*what*/) const
72 {
73  std::cout << "PHG4GDMLDetector::" << GetName() << " - import " << m_TopVolName << " from " << m_GDMPath << " with shift "
74  << m_placeX << ","
75  << m_placeY << ","
76  << m_placeZ << "cm and rotation "
77  << m_rotationX << ","
78  << m_rotationY << ","
79  << m_rotationZ << "rad" << std::endl;
80 }
81 
82 void PHG4GDMLDetector::ConstructMe(G4LogicalVolume* logicWorld)
83 {
84  if (Verbosity() > 0)
85  {
86  std::cout << " PHG4MapsDetector::Construct:";
87  Print();
88  // std::cout << std::endl;
89  }
90 
91  //===================================
92  // Import the stave physical volume here
93  //===================================
94 
95  // import the staves from the gemetry file
96  std::unique_ptr<G4GDMLReadStructure> reader(new G4GDMLReadStructure());
97  G4GDMLParser gdmlParser(reader.get());
98  gdmlParser.SetOverlapCheck(OverlapCheck());
99  // gdmlParser.Read(m_GDMPath, false);
100  gdmlParser.Read(m_GDMPath, OverlapCheck());
101 
102  // G4AssemblyVolume* av_ITSUStave = reader->GetAssembly(assemblyname);
103 
104  G4LogicalVolume* vol = reader->GetVolume(m_TopVolName);
105 
106  if (not vol)
107  {
108  std::cout << "PHG4GDMLDetector::Construct - Fatal Error - failed to find G4LogicalVolume " << m_TopVolName << " - Print: ";
109  Print();
110  exit(121);
111  }
112  PHG4Subsystem* mysys = GetMySubsystem();
113  mysys->SetLogicalVolume(vol);
114 
115  G4RotationMatrix* rotm = new G4RotationMatrix();
116  rotm->rotateX(m_rotationX);
117  rotm->rotateY(m_rotationY);
118  rotm->rotateZ(m_rotationZ);
119  G4ThreeVector placeVec(m_placeX, m_placeY, m_placeZ);
120 
121  // av_ITSUStave->MakeImprint(trackerenvelope, Tr, 0, OverlapCheck());
122 
123  G4PVPlacement* gdml_phys =
124  new G4PVPlacement(rotm, placeVec,
125  vol,
126  G4String(GetName()),
127  logicWorld, false, 0, OverlapCheck());
128  SetDisplayProperty(vol);
129 
131  {
133  gdml_config->exclude_physical_vol(gdml_phys);
134  }
135 }
136 
137 void PHG4GDMLDetector::SetDisplayProperty(G4AssemblyVolume* av)
138 {
139  // std::cout <<"SetDisplayProperty - G4AssemblyVolume w/ TotalImprintedVolumes "<<av->TotalImprintedVolumes()
140  // <<"/"<<av->GetImprintsCount()<<std::endl;
141 
142  std::vector<G4VPhysicalVolume*>::iterator it = av->GetVolumesIterator();
143 
144  int nDaughters = av->TotalImprintedVolumes();
145  for (int i = 0; i < nDaughters; ++i, ++it)
146  {
147  // std::cout <<"SetDisplayProperty - AV["<<i<<"] = "<<(*it)->GetName()<<std::endl;
148  G4VPhysicalVolume* pv = (*it);
149 
150  G4LogicalVolume* worldLogical = pv->GetLogicalVolume();
151  SetDisplayProperty(worldLogical);
152  }
153 }
154 
155 void PHG4GDMLDetector::SetDisplayProperty(G4LogicalVolume* lv)
156 {
157  std::string material_name(
158  lv->GetMaterial()->GetName());
159 
160  if (Verbosity() >= 5)
161  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " built with "
162  << material_name << std::endl;
163 
164  G4VisAttributes* matVis = new G4VisAttributes();
165  if (material_name.find("SI") != std::string::npos)
166  {
167  PHG4Utils::SetColour(matVis, "G4_Si");
168  matVis->SetVisibility(true);
169  matVis->SetForceSolid(true);
170  if (Verbosity() >= 5)
171  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_Si" << std::endl;
172  }
173  else if (material_name.find("KAPTON") != std::string::npos)
174  {
175  PHG4Utils::SetColour(matVis, "G4_KAPTON");
176  matVis->SetVisibility(true);
177  matVis->SetForceSolid(true);
178  if (Verbosity() >= 5)
179  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_KAPTON" << std::endl;
180  }
181  else if (material_name.find("ALUMINUM") != std::string::npos)
182  {
183  PHG4Utils::SetColour(matVis, "G4_Al");
184  matVis->SetVisibility(true);
185  matVis->SetForceSolid(true);
186  if (Verbosity() >= 5)
187  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with G4_Al" << std::endl;
188  }
189  else if (material_name.find("Carbon") != std::string::npos)
190  {
191  matVis->SetColour(0.5, 0.5, 0.5, .25);
192  matVis->SetVisibility(true);
193  matVis->SetForceSolid(true);
194  if (Verbosity() >= 5)
195  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with Gray" << std::endl;
196  }
197  else if (material_name.find("M60J3K") != std::string::npos)
198  {
199  matVis->SetColour(0.25, 0.25, 0.25, .25);
200  matVis->SetVisibility(true);
201  matVis->SetForceSolid(true);
202  if (Verbosity() >= 5)
203  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with Gray" << std::endl;
204  }
205  else if (material_name.find("WATER") != std::string::npos)
206  {
207  matVis->SetColour(0.0, 0.5, 0.0, .25);
208  matVis->SetVisibility(true);
209  matVis->SetForceSolid(true);
210  if (Verbosity() >= 5)
211  std::cout << "SetDisplayProperty - LV " << lv->GetName() << " display with WATER" << std::endl;
212  }
213  else
214  {
215  matVis->SetColour(.2, .2, .7, .25);
216  matVis->SetVisibility(true);
217  matVis->SetForceSolid(true);
218  }
219  lv->SetVisAttributes(matVis);
220 
221  int nDaughters = lv->GetNoDaughters();
222  for (int i = 0; i < nDaughters; ++i)
223  {
224  G4VPhysicalVolume* pv = lv->GetDaughter(i);
225 
226  // std::cout <<"SetDisplayProperty - PV["<<i<<"] = "<<pv->GetName()<<std::endl;
227 
228  G4LogicalVolume* worldLogical = pv->GetLogicalVolume();
229  SetDisplayProperty(worldLogical);
230  }
231 }