CMS 3D CMS Logo

PrintG4Touch.cc
Go to the documentation of this file.
5 
10 
11 #include "G4LogicalVolumeStore.hh"
12 #include "G4LogicalVolume.hh"
13 #include "G4VSolid.hh"
14 #include "G4Material.hh"
15 #include "G4NavigationHistory.hh"
16 #include "G4PhysicalVolumeStore.hh"
17 #include "G4Region.hh"
18 #include "G4RegionStore.hh"
19 #include "G4Run.hh"
20 #include "G4Track.hh"
21 #include "G4TransportationManager.hh"
22 #include "G4UserLimits.hh"
23 #include "G4VisAttributes.hh"
24 #include "G4VPhysicalVolume.hh"
25 
26 #include <algorithm>
27 #include <fstream>
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
34 
35 class PrintG4Touch : public SimWatcher, public Observer<const BeginOfRun *> {
36 public:
38  ~PrintG4Touch() override = default;
39 
40 private:
41  void update(const BeginOfRun *run) override;
42  void dumpSummary(std::ostream &out = G4cout);
43  int countNoTouchables();
44  void add1touchable(G4LogicalVolume *lv, int &nTouch);
45  void dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, std::ostream &out = G4cout);
46  void getTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, unsigned int copym, std::vector<std::string> &touches);
47  G4VPhysicalVolume *getTopPV();
48  G4LogicalVolume *getTopLV();
49 
50 private:
52  G4VPhysicalVolume *theTopPV_;
53  G4NavigationHistory fHistory_;
54 };
55 
57  dd4hep_ = p.getUntrackedParameter<bool>("dd4hep", false);
58  verbosity_ = p.getUntrackedParameter<bool>("verbosity", false);
59  G4cout << "PrintG4Touch:: initialised for dd4hep " << dd4hep_ << " with verbosity levels:" << verbosity_ << G4endl;
60 }
61 
63  //Now take action
64  theTopPV_ = getTopPV();
65 
67 
68  std::vector<std::string> touches;
69  getTouch(theTopPV_, 0, 1, touches);
70  std::sort(touches.begin(), touches.end());
71  for (const auto &touch : touches)
72  G4cout << touch << G4endl;
73 
74  //---------- Dump LV and PV information
75  if (verbosity_)
77 }
78 
79 void PrintG4Touch::dumpSummary(std::ostream &out) {
80  //---------- Dump number of objects of each class
81  out << " @@@@@@@@@@@@@@@@@@ Dumping G4 geometry objects Summary " << G4endl;
82  if (theTopPV_ == nullptr) {
83  out << " No volume created " << G4endl;
84  return;
85  }
86  out << " @@@ Geometry built inside world volume: "
87  << DD4hep2DDDName::namePV(static_cast<std::string>(theTopPV_->GetName()), dd4hep_) << G4endl;
88  // Get number of solids (< # LV if several LV share a solid)
89  const G4LogicalVolumeStore *lvs = G4LogicalVolumeStore::GetInstance();
90  std::vector<G4LogicalVolume *>::const_iterator lvcite;
91  std::set<G4VSolid *> theSolids;
92  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++)
93  theSolids.insert((*lvcite)->GetSolid());
94  out << " Number of G4VSolid's: " << theSolids.size() << G4endl;
95  out << " Number of G4LogicalVolume's: " << lvs->size() << G4endl;
96  const G4PhysicalVolumeStore *pvs = G4PhysicalVolumeStore::GetInstance();
97  out << " Number of G4VPhysicalVolume's: " << pvs->size() << G4endl;
98  out << " Number of Touchable's: " << countNoTouchables() << G4endl;
99  const G4MaterialTable *matTab = G4Material::GetMaterialTable();
100  out << " Number of G4Material's: " << matTab->size() << G4endl;
101  const G4RegionStore *regs = G4RegionStore::GetInstance();
102  out << " Number of G4Region's: " << regs->size() << G4endl;
103 }
104 
106  int nTouch = 0;
107  G4LogicalVolume *lv = getTopLV();
108  add1touchable(lv, nTouch);
109  return nTouch;
110 }
111 
112 void PrintG4Touch::add1touchable(G4LogicalVolume *lv, int &nTouch) {
113  int siz = lv->GetNoDaughters();
114  for (int ii = 0; ii < siz; ii++)
115  add1touchable(lv->GetDaughter(ii)->GetLogicalVolume(), ++nTouch);
116 }
117 
118 void PrintG4Touch::dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, std::ostream &out) {
119  if (leafDepth == 0)
120  fHistory_.SetFirstEntry(pv);
121  else
122  fHistory_.NewLevel(pv, kNormal, pv->GetCopyNo());
123 
124  G4LogicalVolume *lv = pv->GetLogicalVolume();
125 
126  G4ThreeVector globalpoint = fHistory_.GetTopTransform().Inverse().TransformPoint(G4ThreeVector(0, 0, 0));
127  std::string mother = (pv->GetMotherLogical())
129  static_cast<std::string>(pv->GetMotherLogical()->GetSolid()->GetName()), dd4hep_))
130  : "World";
131  std::string lvname = DD4hep2DDDName::nameSolid(static_cast<std::string>(lv->GetName()), dd4hep_);
132  out << leafDepth << "### VOLUME = " << lvname << " Copy No " << pv->GetCopyNo() << " in " << mother
133  << " global position of centre " << globalpoint << " (r = " << globalpoint.perp()
134  << ", phi = " << convertRadToDeg(globalpoint.phi()) << ")" << G4endl;
135 
136  int NoDaughters = lv->GetNoDaughters();
137  while ((NoDaughters--) > 0) {
138  G4VPhysicalVolume *pvD = lv->GetDaughter(NoDaughters);
139  if (!pvD->IsReplicated())
140  dumpTouch(pvD, leafDepth + 1, out);
141  }
142 
143  if (leafDepth > 0)
144  fHistory_.BackLevel();
145 }
146 
147 void PrintG4Touch::getTouch(G4VPhysicalVolume *pv,
148  unsigned int leafDepth,
149  unsigned int copym,
150  std::vector<std::string> &touches) {
151  if (leafDepth == 0)
152  fHistory_.SetFirstEntry(pv);
153  else
154  fHistory_.NewLevel(pv, kNormal, pv->GetCopyNo());
155 
156  std::string mother = (pv->GetMotherLogical())
158  static_cast<std::string>(pv->GetMotherLogical()->GetSolid()->GetName()), dd4hep_))
159  : "World";
160 
161  G4LogicalVolume *lv = pv->GetLogicalVolume();
162  std::string lvname = DD4hep2DDDName::nameSolid(static_cast<std::string>(lv->GetSolid()->GetName()), dd4hep_);
163  unsigned int copy = static_cast<unsigned int>(pv->GetCopyNo());
164 
165  std::string type = static_cast<std::string>(lv->GetSolid()->GetEntityType());
166 
167  std::string name = lvname + " " + std::to_string(copy) + " " + mother + " " + std::to_string(copym) + " " + type;
168  touches.emplace_back(name);
169 
170  int NoDaughters = lv->GetNoDaughters();
171  while ((NoDaughters--) > 0) {
172  G4VPhysicalVolume *pvD = lv->GetDaughter(NoDaughters);
173  if (!pvD->IsReplicated())
174  getTouch(pvD, leafDepth + 1, copy, touches);
175  }
176 
177  if (leafDepth > 0)
178  fHistory_.BackLevel();
179 }
180 
181 G4VPhysicalVolume *PrintG4Touch::getTopPV() {
182  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
183 }
184 
185 G4LogicalVolume *PrintG4Touch::getTopLV() { return theTopPV_->GetLogicalVolume(); }
186 
189 
G4VPhysicalVolume * theTopPV_
Definition: PrintG4Touch.cc:52
#define DEFINE_SIMWATCHER(type)
G4LogicalVolume * getTopLV()
void getTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, unsigned int copym, std::vector< std::string > &touches)
void add1touchable(G4LogicalVolume *lv, int &nTouch)
int countNoTouchables()
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
void dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, std::ostream &out=G4cout)
static std::string to_string(const XMLCh *ch)
~PrintG4Touch() override=default
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: PrintG4Touch.cc:62
ii
Definition: cuy.py:589
void dumpSummary(std::ostream &out=G4cout)
Definition: PrintG4Touch.cc:79
std::string namePV(const std::string &name, bool dd4hep)
G4VPhysicalVolume * getTopPV()
G4NavigationHistory fHistory_
Definition: PrintG4Touch.cc:53
std::string nameSolid(const std::string &name, bool dd4hep)
PrintG4Touch(edm::ParameterSet const &p)
Definition: PrintG4Touch.cc:56