CMS 3D CMS Logo

PrintGeomSummary.cc
Go to the documentation of this file.
4 
16 
17 #include "G4Run.hh"
18 #include "G4PhysicalVolumeStore.hh"
19 #include "G4LogicalVolumeStore.hh"
20 #include "G4VPhysicalVolume.hh"
21 #include "G4LogicalVolume.hh"
22 #include "G4VSolid.hh"
23 #include "G4Material.hh"
24 #include "G4NavigationHistory.hh"
25 #include "G4Track.hh"
26 #include "G4VisAttributes.hh"
27 #include "G4UserLimits.hh"
28 #include "G4TransportationManager.hh"
29 
30 #include <algorithm>
31 #include <iostream>
32 #include <vector>
33 #include <map>
34 #include <set>
35 #include <string>
36 
37 class PrintGeomSummary : public SimWatcher, public Observer<const BeginOfRun*> {
38 public:
40  ~PrintGeomSummary() override = default;
41 
43  void beginRun(edm::EventSetup const&) override;
44 
45 private:
46  void update(const BeginOfRun* run) override;
47  void addSolid(const DDLogicalPart& part);
48  void fillLV(G4LogicalVolume* lv);
49  void fillPV(G4VPhysicalVolume* pv);
50  void dumpSummary(std::ostream& out, std::string name);
51  G4VPhysicalVolume* getTopPV();
52  void addName(std::string name);
53  void printSummary(std::ostream& out);
54 
55 private:
57  std::vector<std::string> nodeNames_;
58  std::map<DDSolidShape, std::string> solidShape_;
59  std::map<std::string, DDSolidShape> solidMap_;
60  G4VPhysicalVolume* theTopPV_;
61  std::vector<G4LogicalVolume*> lvs_, touch_;
62  std::vector<G4VSolid*> sls_;
63  std::vector<G4VPhysicalVolume*> pvs_;
64  std::map<DDSolidShape, std::pair<int, int>> kount_;
65 };
66 
68  std::vector<std::string> defNames;
69  nodeNames_ = p.getUntrackedParameter<std::vector<std::string>>("NodeNames", defNames);
70  G4cout << "PrintGeomSummary:: initialised for " << nodeNames_.size() << " nodes:" << G4endl;
71  for (unsigned int ii = 0; ii < nodeNames_.size(); ii++)
72  G4cout << "Node[" << ii << "] : " << nodeNames_[ii] << G4endl;
73 
76  solidShape_[DDSolidShape::ddtrap] = "Trapezoid";
79  solidShape_[DDSolidShape::ddpolyhedra_rz] = "Polyhedra_rz";
80  solidShape_[DDSolidShape::ddpolycone_rrz] = "Polycone_rrz";
81  solidShape_[DDSolidShape::ddpolyhedra_rrz] = "Polyhedra_rrz";
83  solidShape_[DDSolidShape::ddunion] = "UnionSolid";
84  solidShape_[DDSolidShape::ddsubtraction] = "SubtractionSolid";
85  solidShape_[DDSolidShape::ddintersection] = "IntersectionSolid";
86  solidShape_[DDSolidShape::ddshapeless] = "ShapelessSolid";
87  solidShape_[DDSolidShape::ddpseudotrap] = "PseudoTrapezoid";
88  solidShape_[DDSolidShape::ddtrunctubs] = "TruncatedTube";
90  solidShape_[DDSolidShape::ddellipticaltube] = "EllipticalTube";
92  solidShape_[DDSolidShape::ddextrudedpolygon] = "ExtrudedPolygon";
94 }
95 
98  G4cout << "PrintGeomSummary::Initialize ESGetToken for DDCompactView" << G4endl;
99 }
100 
102  const DDCompactView* cpv = &es.getData(ddcompToken_);
103 
104  const auto& gra = cpv->graph();
105 
106  using Graph = DDCompactView::Graph;
108 
109  solidMap_.clear();
110  for (adjl_iterator git = gra.begin(); git != gra.end(); ++git) {
111  const DDLogicalPart& ddLP = gra.nodeData(git);
112  addSolid(ddLP);
113  if (!git->empty()) {
114  // ask for children of ddLP
115  for (Graph::edge_list::const_iterator cit = git->begin(); cit != git->end(); ++cit) {
116  const DDLogicalPart& ddcurLP = gra.nodeData(cit->first);
117  addSolid(ddcurLP);
118  }
119  }
120  }
121  G4cout << "Finds " << solidMap_.size() << " different solids in the tree" << G4endl;
122 }
123 
125  theTopPV_ = getTopPV();
126  if (theTopPV_) {
127  lvs_.clear();
128  sls_.clear();
129  touch_.clear();
130  fillLV(theTopPV_->GetLogicalVolume());
131  std::string name = theTopPV_->GetName();
133 
134  pvs_.clear();
135  fillPV(theTopPV_);
136  G4cout << " Number of G4VPhysicalVolume's for " << name << ": " << pvs_.size() << G4endl;
137 
138  for (unsigned int k = 0; k < nodeNames_.size(); ++k) {
139  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
140  std::vector<G4LogicalVolume*>::const_iterator lvcite;
141  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
142  if ((*lvcite)->GetName() == (G4String)(nodeNames_[k])) {
143  lvs_.clear();
144  sls_.clear();
145  touch_.clear();
146  fillLV(*lvcite);
148  }
149  }
150  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
151  std::vector<G4VPhysicalVolume*>::const_iterator pvcite;
152  for (pvcite = pvs->begin(); pvcite != pvs->end(); pvcite++) {
153  if ((*pvcite)->GetName() == (G4String)(nodeNames_[k])) {
154  pvs_.clear();
155  fillPV(*pvcite);
156  G4cout << " Number of G4VPhysicalVolume's for " << nodeNames_[k] << ": " << pvs_.size() << G4endl;
157  }
158  }
159  }
160  }
161 }
162 
164  const DDSolid& solid = part.solid();
165  std::map<DDSolidShape, std::string>::iterator it = solidShape_.find(solid.shape());
166  std::string name = solid.name().name();
167  if (it == solidShape_.end())
169  else
170  solidMap_[name] = it->first;
171  //G4cout << "Solid " << name << " is of shape " << solidMap_[name] << G4endl;
172 }
173 
174 void PrintGeomSummary::fillLV(G4LogicalVolume* lv) {
175  if (std::find(lvs_.begin(), lvs_.end(), lv) == lvs_.end())
176  lvs_.push_back(lv);
177  G4VSolid* sl = lv->GetSolid();
178  if (std::find(sls_.begin(), sls_.end(), sl) == sls_.end())
179  sls_.push_back(sl);
180  touch_.push_back(lv);
181  for (int ii = 0; ii < (int)(lv->GetNoDaughters()); ii++)
182  fillLV(lv->GetDaughter(ii)->GetLogicalVolume());
183 }
184 
185 void PrintGeomSummary::fillPV(G4VPhysicalVolume* pv) {
186  if (std::find(pvs_.begin(), pvs_.end(), pv) == pvs_.end())
187  pvs_.push_back(pv);
188  for (int ii = 0; ii < (int)(pv->GetLogicalVolume()->GetNoDaughters()); ii++)
189  fillPV(pv->GetLogicalVolume()->GetDaughter(ii));
190 }
191 
193  //---------- Dump number of objects of each class
194  out << G4endl << G4endl << "@@@@@@@@@@@@@@@@@@ Dumping Summary For Node " << name << G4endl;
195  out << " Number of G4VSolid's: " << sls_.size() << G4endl;
196  out << " Number of G4LogicalVolume's: " << lvs_.size() << G4endl;
197  out << " Number of Touchable's: " << touch_.size() << G4endl;
198  //First the solids
199  out << G4endl << "Occurence of each type of shape among Solids" << G4endl;
200  kount_.clear();
201  for (std::vector<G4VSolid*>::iterator it = sls_.begin(); it != sls_.end(); ++it) {
202  std::string name = (*it)->GetName();
203  addName(name);
204  }
205  printSummary(out);
206  //Then the logical volumes
207  out << G4endl << "Occurence of each type of shape among Logical Volumes" << G4endl;
208  kount_.clear();
209  for (std::vector<G4LogicalVolume*>::iterator it = lvs_.begin(); it != lvs_.end(); ++it) {
210  std::string name = ((*it)->GetSolid())->GetName();
211  addName(name);
212  }
213  printSummary(out);
214  //Finally the touchables
215  out << G4endl << "Occurence of each type of shape among Touchables" << G4endl;
216  kount_.clear();
217  for (std::vector<G4LogicalVolume*>::iterator it = touch_.begin(); it != touch_.end(); ++it) {
218  std::string name = ((*it)->GetSolid())->GetName();
219  addName(name);
220  }
221  printSummary(out);
222 }
223 
224 G4VPhysicalVolume* PrintGeomSummary::getTopPV() {
225  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
226 }
227 
229  bool refl(false);
230  if (name.find("_refl") < name.size()) {
231  refl = true;
232  name = name.substr(0, (name.find("_refl")));
233  }
234  std::map<std::string, DDSolidShape>::const_iterator jt = solidMap_.find(name);
235  DDSolidShape shape = (jt == solidMap_.end()) ? DDSolidShape::dd_not_init : jt->second;
236  std::map<DDSolidShape, std::pair<int, int>>::iterator itr = kount_.find(shape);
237  if (itr == kount_.end()) {
238  kount_[shape] = (refl) ? std::pair<int, int>(0, 1) : std::pair<int, int>(1, 0);
239  } else {
240  kount_[shape] = (refl) ? std::pair<int, int>(((itr->second).first), ++((itr->second).second))
241  : std::pair<int, int>(++((itr->second).first), ((itr->second).second));
242  }
243 }
244 
245 void PrintGeomSummary::printSummary(std::ostream& out) {
246  int k(0);
247  for (std::map<DDSolidShape, std::pair<int, int>>::iterator itr = kount_.begin(); itr != kount_.end(); ++itr, ++k) {
248  std::string shape = solidShape_[itr->first];
249  out << "Shape [" << k << "] " << shape << " # " << (itr->second).first << " : " << (itr->second).second << G4endl;
250  }
251 }
252 
255 
math::Graph< DDLogicalPart, DDPosData * > Graph
Definition: DDCompactView.h:83
#define DEFINE_SIMWATCHER(type)
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
void addName(std::string name)
void addSolid(const DDLogicalPart &part)
void fillPV(G4VPhysicalVolume *pv)
void printSummary(std::ostream &out)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
std::map< DDSolidShape, std::string > solidShape_
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
G4VPhysicalVolume * getTopPV()
G4VPhysicalVolume * theTopPV_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
A DDSolid represents the shape of a part.
Definition: DDSolid.h:39
void beginRun(edm::EventSetup const &) override
U second(std::pair< T, U > const &p)
void registerConsumes(edm::ConsumesCollector) override
const std::string & name() const
Returns the name.
Definition: DDName.cc:41
std::vector< G4LogicalVolume * > touch_
~PrintGeomSummary() override=default
PrintGeomSummary(edm::ParameterSet const &p)
def pv(vc)
Definition: MetAnalyzer.py:7
A DDLogicalPart aggregates information concerning material, solid and sensitveness ...
Definition: DDLogicalPart.h:93
Graph::const_adj_iterator adjl_iterator
edm::ESGetToken< DDCompactView, IdealGeometryRecord > ddcompToken_
std::vector< std::string > nodeNames_
void fillLV(G4LogicalVolume *lv)
DDSolidShape shape(void) const
The type of the solid.
Definition: DDSolid.cc:123
ii
Definition: cuy.py:589
const N & name() const
Definition: DDBase.h:59
part
Definition: HCALResponse.h:20
std::vector< G4VSolid * > sls_
DDSolidShape
Definition: DDSolidShapes.h:6
void dumpSummary(std::ostream &out, std::string name)
std::vector< G4VPhysicalVolume * > pvs_
std::vector< G4LogicalVolume * > lvs_
Pt3D refl(const Pt3D &p)
adj_list::const_iterator const_adj_iterator
Definition: Graph.h:105
const Graph & graph() const
Provides read-only access to the data structure of the compact-view.
std::map< std::string, DDSolidShape > solidMap_
std::map< DDSolidShape, std::pair< int, int > > kount_