CMS 3D CMS Logo

PrintG4Solids.cc
Go to the documentation of this file.
4 
8 
9 #include "G4Box.hh"
10 #include "G4Cons.hh"
11 #include "G4ExtrudedSolid.hh"
12 #include "G4Polycone.hh"
13 #include "G4Polyhedra.hh"
14 #include "G4Trap.hh"
15 #include "G4Trd.hh"
16 #include "G4Tubs.hh"
17 #include "G4LogicalVolumeStore.hh"
18 #include "G4LogicalVolume.hh"
19 #include "G4VSolid.hh"
20 #include "G4NavigationHistory.hh"
21 #include "G4PhysicalVolumeStore.hh"
22 #include "G4TransportationManager.hh"
23 #include "G4VPhysicalVolume.hh"
24 
25 #include <algorithm>
26 #include <map>
27 #include <set>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
33 
34 class PrintG4Solids : public SimWatcher, public Observer<const BeginOfRun *> {
35 public:
37  ~PrintG4Solids() override = default;
38 
39 private:
40  void update(const BeginOfRun *run) override;
41  void dumpSummary(std::ostream &out = G4cout);
42  G4VPhysicalVolume *getTopPV();
43  bool select(const std::string &name, const std::string &shape) const;
45 
46 private:
47  const bool dd4hep_;
48  const std::vector<std::string> solids_;
49  const std::vector<std::string> types_;
50  G4VPhysicalVolume *theTopPV_;
51 };
52 
54  : dd4hep_(p.getUntrackedParameter<bool>("dd4hep")),
55  solids_(p.getUntrackedParameter<std::vector<std::string> >("dumpVolumes")),
56  types_(p.getUntrackedParameter<std::vector<std::string> >("dumpShapes")) {
57  G4cout << "PrintG4Solids:: initialised for printing information about G4VSolids for version dd4heP:" << dd4hep_
58  << G4endl;
59 }
60 
62  //Now take action
63  theTopPV_ = getTopPV();
64 
66 }
67 
68 void PrintG4Solids::dumpSummary(std::ostream &out) {
69  //---------- Dump number of objects of each class
70  out << " @@@@@@@@@@@@@@@@@@ Dumping G4 geometry objects Summary " << G4endl;
71  if (theTopPV_ == nullptr) {
72  out << " No volume created " << G4endl;
73  return;
74  }
75  out << " @@@ Geometry built inside world volume: " << theTopPV_->GetName() << G4endl;
76  // Get number of solids (< # LV if several LV share a solid)
77  const G4LogicalVolumeStore *lvs = G4LogicalVolumeStore::GetInstance();
78  std::vector<G4LogicalVolume *>::const_iterator lvcite;
79  std::set<G4VSolid *> theSolids;
80  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
81  G4VSolid *solid = (*lvcite)->GetSolid();
82  std::string name = static_cast<std::string>(solid->GetName());
83  if (dd4hep_)
85  std::string type = static_cast<std::string>(solid->GetEntityType());
86  if (select(name, type))
87  theSolids.insert(solid);
88  }
89  out << " Number of G4VSolid's: " << theSolids.size() << G4endl;
90  std::set<G4VSolid *>::const_iterator solid;
91  for (solid = theSolids.begin(); solid != theSolids.end(); solid++) {
92  G4String type = (*solid)->GetEntityType();
93  std::string name = static_cast<std::string>((*solid)->GetName());
94  if (dd4hep_)
96  out << name << ":" << type << " Volume " << (*solid)->GetCubicVolume();
97  if (type == "G4Box") {
98  const G4Box *box = static_cast<const G4Box *>(*solid);
99  out << " dx:dy:dz " << box->GetXHalfLength() << ":" << box->GetYHalfLength() << ":" << box->GetZHalfLength();
100  } else if (type == "G4Tubs") {
101  const G4Tubs *tube = static_cast<const G4Tubs *>(*solid);
102  out << " rin:rout:dz:phistart:dphi " << tube->GetInnerRadius() << ":" << tube->GetOuterRadius() << ":"
103  << tube->GetZHalfLength() << ":" << convertRadToDeg(tube->GetStartPhiAngle()) << ":"
104  << convertRadToDeg(tube->GetDeltaPhiAngle());
105  } else if (type == "G4Cons") {
106  const G4Cons *cone = static_cast<const G4Cons *>(*solid);
107  out << " rinminus:routminus:rinplus:routplus:dz:phistart:dphi " << cone->GetInnerRadiusMinusZ() << ":"
108  << cone->GetOuterRadiusMinusZ() << ":" << cone->GetInnerRadiusPlusZ() << ":" << cone->GetOuterRadiusPlusZ()
109  << ":" << cone->GetZHalfLength() << ":" << convertRadToDeg(cone->GetStartPhiAngle()) << ":"
110  << convertRadToDeg(cone->GetDeltaPhiAngle());
111  } else if (type == "G4Trap") {
112  const G4Trap *trap = static_cast<const G4Trap *>(*solid);
113  out << " zhalf:yl1:xl11:xl12:tana1:yl2:xl21:xl22:tana2 " << trap->GetZHalfLength() << ":"
114  << trap->GetYHalfLength1() << ":" << trap->GetXHalfLength1() << ":" << trap->GetXHalfLength2() << ":"
115  << trap->GetTanAlpha1() << ":" << trap->GetYHalfLength2() << ":" << trap->GetXHalfLength3() << ":"
116  << trap->GetXHalfLength4() << ":" << trap->GetTanAlpha2();
117  } else if (type == "G4Trd") {
118  const G4Trd *trd = static_cast<const G4Trd *>(*solid);
119  out << " xl1:xl2:yl1:yl2:zhalf " << trd->GetXHalfLength1() << ":" << trd->GetXHalfLength2() << ":"
120  << trd->GetYHalfLength1() << ":" << trd->GetYHalfLength2() << ":" << trd->GetZHalfLength();
121  } else if (type == "G4Polycone") {
122  const G4Polycone *cone = static_cast<const G4Polycone *>(*solid);
123  const auto hist = cone->GetOriginalParameters();
124  int num = hist->Num_z_planes;
125  out << " angle " << convertRadToDeg(hist->Start_angle) << ":" << convertRadToDeg(hist->Opening_angle) << " with "
126  << num << " planes:";
127  for (int k = 0; k < num; ++k)
128  out << " [" << k << "] " << hist->Z_values[k] << ":" << hist->Rmin[k] << ":" << hist->Rmax[k];
129  } else if (type == "G4Polyhedra") {
130  const G4Polyhedra *pgon = static_cast<const G4Polyhedra *>(*solid);
131  const auto hist = pgon->GetOriginalParameters();
132  int num = hist->Num_z_planes;
133  out << " angle " << convertRadToDeg(hist->Start_angle) << ":" << convertRadToDeg(hist->Opening_angle) << " with "
134  << hist->numSide << " sides and " << num << " planes:";
135  for (int k = 0; k < num; ++k)
136  out << " [" << k << "] " << hist->Z_values[k] << ":" << hist->Rmin[k] << ":" << hist->Rmax[k];
137  } else if (type == "G4ExtrudedSolid") {
138  const G4ExtrudedSolid *pgon = static_cast<const G4ExtrudedSolid *>(*solid);
139  int vert = pgon->GetNofVertices();
140  int numz = pgon->GetNofZSections();
141  out << " " << vert << " vertices:";
142  for (int k = 0; k < vert; ++k)
143  out << " [" << k << "] " << pgon->GetVertex(k);
144  out << "; and " << numz << " z-sections:";
145  for (int k = 0; k < numz; ++k) {
146  const auto &zsec = pgon->GetZSection(k);
147  out << " [" << k << "] " << zsec.fZ << ":" << zsec.fScale << ":" << zsec.fOffset;
148  }
149  }
150  out << G4endl;
151  }
152 }
153 
154 G4VPhysicalVolume *PrintG4Solids::getTopPV() {
155  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
156 }
157 
159  std::string nam(name);
160  uint32_t first = ((name.find(':') == std::string::npos) ? 0 : (name.find(':') + 1));
161  uint32_t last(name.size() + 1);
162  uint32_t loc(first);
163  while (true) {
164  if (name.find('_', loc) == std::string::npos)
165  break;
166  if (((loc + 5) < name.size()) && (name.substr(loc, 5) == "shape")) {
167  last = loc;
168  break;
169  }
170  loc = name.find('_', loc) + 1;
171  if (loc > name.size())
172  break;
173  }
174  nam = name.substr(first, last - first - 1);
175  if ((last < name.size()) && (name.substr(name.size() - 5, 5) == "_refl"))
176  nam += "_refl";
177  return nam;
178 }
179 
181  bool flag(true);
182  if (!solids_.empty())
183  flag = (flag && (std::find(solids_.begin(), solids_.end(), name) != solids_.end()));
184  if (!types_.empty())
185  flag = (flag && (std::find(types_.begin(), types_.end(), type) != types_.end()));
186  return flag;
187 }
188 
191 
#define DEFINE_SIMWATCHER(type)
void dumpSummary(std::ostream &out=G4cout)
const std::vector< std::string > types_
std::string reducedName(const std::string &name)
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
G4VPhysicalVolume * theTopPV_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
~PrintG4Solids() override=default
const bool dd4hep_
bool select(const std::string &name, const std::string &shape) const
PrintG4Solids(edm::ParameterSet const &p)
const std::vector< std::string > solids_
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
G4VPhysicalVolume * getTopPV()