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