CMS 3D CMS Logo

CMSG4CheckOverlap.cc
Go to the documentation of this file.
4 
6 
7 #include "G4GDMLParser.hh"
8 #include "G4GeomTestVolume.hh"
9 #include "G4RunManagerKernel.hh"
10 #include "G4LogicalVolume.hh"
11 #include "G4LogicalVolumeStore.hh"
12 #include "G4PhysicalVolumeStore.hh"
13 #include "G4GeometryManager.hh"
14 #include "G4Region.hh"
15 #include "G4RegionStore.hh"
16 #include "G4Element.hh"
17 #include "G4ElementTable.hh"
18 #include "G4Material.hh"
19 #include "G4MaterialTable.hh"
20 #include "G4ProductionCutsTable.hh"
21 #include "G4MaterialCutsCouple.hh"
22 #include "G4SystemOfUnits.hh"
23 #include "G4VPhysicalVolume.hh"
24 #include "G4UnitsTable.hh"
25 #include "G4ios.hh"
26 #include "globals.hh"
27 
28 #include <string>
29 #include <vector>
30 #include <iostream>
31 #include <iomanip>
32 
34  std::string& regFile,
35  CustomUIsession* session,
36  G4VPhysicalVolume* world) {
37  bool mat = p.getParameter<bool>("MaterialFlag");
38  const std::string& ss = p.getParameter<std::string>("OutputBaseName");
39  if (ss.empty()) {
40  edm::LogWarning("SimG4CoreGeometry")
41  << "CMSG4CheckOverlap: OutputFileBaseName is not provided - no check is performed";
42  return;
43  }
44  if (mat) {
45  const std::string sss = "Materials_" + ss + ".txt";
46  std::ofstream fout(sss.c_str(), std::ios::out);
47  if (fout.fail()) {
48  edm::LogWarning("SimG4CoreGeometry")
49  << "CMSG4CheckOverlap: file <" << sss << "> is not opened - no report provided";
50  } else {
51  edm::LogVerbatim("SimG4CoreGeometry") << "CMSG4CheckOverlap: output file <" << sss << "> is opened";
53  fout.close();
54  }
55  }
56 
57  bool reg = p.getParameter<bool>("RegionFlag");
58  if (reg) {
59  const std::string qqq = (regFile.empty()) ? ss : regFile;
60  const std::string sss = "Regions_" + qqq + ".txt";
62  rrep.ReportRegions(sss);
63  }
64 
65  bool geom = p.getParameter<bool>("GeomFlag");
66  if (geom) {
67  const std::string sss = "Geometry_" + ss + ".txt";
68  std::ofstream fout(sss.c_str(), std::ios::out);
69  if (fout.fail()) {
70  edm::LogWarning("SimG4CoreGeometry")
71  << "CMSG4CheckOverlap: file <" << sss << "> is not opened - no report provided";
72  } else {
73  edm::LogVerbatim("SimG4CoreGeometry") << "CMSG4CheckOverlap: output file <" << sss << "> is opened";
74  session->sendToFile(&fout);
76  session->stopSendToFile();
77  fout.close();
78  }
79  }
80 
81  bool gdmlFlag = p.getParameter<bool>("gdmlFlag");
82  if (gdmlFlag) {
83  std::string PVname = p.getParameter<std::string>("PVname");
84  if (PVname.empty() || PVname == "world" || PVname == "World") {
85  G4GDMLParser gdml = nullptr;
86  gdml.SetRegionExport(true);
87  gdml.SetEnergyCutsExport(true);
88  gdml.SetSDExport(true);
89  gdml.Write(ss + ".gdml", world, true);
90  }
91  }
92 
93  bool oFlag = p.getParameter<bool>("OverlapFlag");
94  if (oFlag) {
95  const std::string sss = "Overlaps_" + ss + ".txt";
96  std::ofstream fout(sss.c_str(), std::ios::out);
97  if (fout.fail()) {
98  edm::LogWarning("SimG4CoreGeometry")
99  << "CMSG4CheckOverlap: file <" << sss << "> is not opened - no report provided";
100  } else {
101  edm::LogVerbatim("SimG4CoreGeometry") << "CMSG4CheckOverlap: output file <" << ss << "> is opened";
102  session->sendToFile(&fout);
103  makeReportForOverlaps(fout, p, world);
104  session->stopSendToFile();
105  fout.close();
106  }
107  }
108 }
109 
111  int nelm = G4Element::GetNumberOfElements();
112  int nmat = G4Material::GetNumberOfMaterials();
113  G4ProductionCutsTable* theCoupleTable = G4ProductionCutsTable::GetProductionCutsTable();
114  int ncouples = theCoupleTable->GetTableSize();
115  fout << "====================================================================="
116  << "\n";
117  fout << "NumberOfElements " << nelm << "\n";
118  fout << "NumberOfMaterials " << nmat << "\n";
119  fout << "NumberOfCouples " << ncouples << "\n";
120  fout << "====================================================================="
121  << "\n";
122  fout << "ElementsDump:"
123  << "\n";
124  G4ElementTable* elmtab = G4Element::GetElementTable();
125  fout << *elmtab;
126  fout << "====================================================================="
127  << "\n";
128  G4MaterialTable* mattab = G4Material::GetMaterialTable();
129  fout << "MaterialsDump:"
130  << "\n";
131  //fout << *mattab << "\n";
132  for (int i = 0; i < nmat; ++i) {
133  fout << "### Material " << i << " " << ((*mattab)[i])->GetNumberOfElements() << " elements\n";
134  fout << (*mattab)[i] << "\n";
135  }
136  fout << "====================================================================="
137  << "\n";
138  fout << "MaterialsCutsCoupleDump:"
139  << "\n";
140  const std::vector<G4double>* gcut = theCoupleTable->GetEnergyCutsVector(0);
141  const std::vector<G4double>* ecut = theCoupleTable->GetEnergyCutsVector(1);
142  const std::vector<G4double>* pcut = theCoupleTable->GetEnergyCutsVector(2);
143  const std::vector<G4double>* icut = theCoupleTable->GetEnergyCutsVector(3);
144  for (int i = 0; i < ncouples; ++i) {
145  const G4MaterialCutsCouple* couple = theCoupleTable->GetMaterialCutsCouple(i);
146  const G4ProductionCuts* aCut = couple->GetProductionCuts();
147  fout << "Index : " << i << " used in the geometry : ";
148  if (couple->IsUsed()) {
149  fout << "Yes\n";
150  } else {
151  fout << "No \n";
152  }
153  fout << " Material : " << couple->GetMaterial()->GetName() << "\n";
154  fout << " Range cuts : "
155  << " gamma " << G4BestUnit(aCut->GetProductionCut(0), "Length") << " e- "
156  << G4BestUnit(aCut->GetProductionCut(1), "Length") << " e+ "
157  << G4BestUnit(aCut->GetProductionCut(2), "Length") << " proton "
158  << G4BestUnit(aCut->GetProductionCut(3), "Length") << "\n";
159  fout << " Energy thresholds : ";
160  fout << " gamma " << G4BestUnit((*gcut)[i], "Energy") << " e- " << G4BestUnit((*ecut)[i], "Energy")
161  << " e+ " << G4BestUnit((*pcut)[i], "Energy") << " proton " << G4BestUnit((*icut)[i], "Energy") << "\n";
162  }
163  fout << "======================================================================"
164  << "\n";
165 }
166 
167 void CMSG4CheckOverlap::makeReportForGeometry(std::ofstream& fout, G4VPhysicalVolume* world) {
168  const G4RegionStore* regs = G4RegionStore::GetInstance();
169  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
170  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
171  int numPV = pvs->size();
172  int numLV = lvs->size();
173  int nreg = regs->size();
174  fout << "====================================================================="
175  << "\n";
176  fout << "NumberOfRegions " << nreg << "\n";
177  fout << "NumberOfLogicalVolumes " << numLV << "\n";
178  fout << "NumberOfPhysicalVolumes " << numPV << "\n";
179  fout << "====================================================================="
180  << "\n";
181  G4GeometryManager::GetInstance()->CloseGeometry(true, true, world);
182  fout << "====================================================================="
183  << "\n";
184 }
185 
187  const edm::ParameterSet& p,
188  G4VPhysicalVolume* world) {
189  std::vector<std::string> nodeNames = p.getParameter<std::vector<std::string>>("NodeNames");
190  std::string PVname = p.getParameter<std::string>("PVname");
191  std::string LVname = p.getParameter<std::string>("LVname");
192  double tolerance = p.getParameter<double>("Tolerance") * CLHEP::mm;
193  int nPoints = p.getParameter<int>("Resolution");
194  bool verbose = p.getParameter<bool>("Verbose");
195  bool regionFlag = p.getParameter<bool>("RegionFlag");
196  bool gdmlFlag = p.getParameter<bool>("gdmlFlag");
197  int nPrints = p.getParameter<int>("ErrorThreshold");
198 
199  const G4RegionStore* regStore = G4RegionStore::GetInstance();
200 
201  G4LogicalVolume* lv;
202  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
203  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
204  unsigned int numPV = pvs->size();
205  unsigned int numLV = lvs->size();
206  unsigned int nn = nodeNames.size();
207 
208  std::vector<G4String> savedgdml;
209 
210  fout << "====================================================================="
211  << "\n";
212  fout << "CMSG4OverlapCheck is initialised with " << nodeNames.size() << " nodes; "
213  << " nPoints= " << nPoints << "; tolerance= " << tolerance / mm << " mm; verbose: " << verbose << "\n"
214  << " RegionFlag: " << regionFlag << " PVname: " << PVname << " LVname: " << LVname << "\n"
215  << " Nlv= " << numLV << " Npv= " << numPV << "\n";
216  fout << "====================================================================="
217  << "\n";
218 
219  G4GDMLParser* gdml = nullptr;
220  if (gdmlFlag) {
221  gdml = new G4GDMLParser();
222  gdml->SetRegionExport(true);
223  gdml->SetEnergyCutsExport(true);
224  gdml->SetSDExport(true);
225  }
226  if (0 < nn) {
227  for (unsigned int ii = 0; ii < nn; ++ii) {
228  if (nodeNames[ii].empty() || "world" == nodeNames[ii] || "World" == nodeNames[ii]) {
229  nodeNames[ii] = world->GetName();
230  fout << "### Check overlaps for World "
231  << "\n";
232  G4GeomTestVolume test(world, tolerance, nPoints, verbose);
233  test.SetErrorsThreshold(nPrints);
234  test.TestOverlapInTree();
235  } else if (regionFlag) {
236  fout << "---------------------------------------------------------------"
237  << "\n";
238  fout << "### Check overlaps for G4Region Node[" << ii << "] : " << nodeNames[ii] << "\n";
239  G4Region* reg = regStore->GetRegion((G4String)nodeNames[ii]);
240  if (!reg) {
241  fout << "### NO G4Region found - EXIT"
242  << "\n";
243  return;
244  }
245  std::vector<G4LogicalVolume*>::iterator rootLVItr = reg->GetRootLogicalVolumeIterator();
246  unsigned int numRootLV = reg->GetNumberOfRootVolumes();
247  fout << " " << numRootLV << " Root Logical Volumes in this region"
248  << "\n";
249 
250  for (unsigned int iLV = 0; iLV < numRootLV; ++iLV, ++rootLVItr) {
251  // Cover each root logical volume in this region
252  lv = *rootLVItr;
253  fout << "### Check overlaps for G4LogicalVolume " << lv->GetName() << "\n";
254  for (unsigned int i = 0; i < numPV; ++i) {
255  if (((*pvs)[i])->GetLogicalVolume() == lv) {
256  G4String pvname = ((*pvs)[i])->GetName();
257  G4bool isNew = true;
258  for (unsigned int k = 0; k < savedgdml.size(); ++k) {
259  if (pvname == savedgdml[k]) {
260  isNew = false;
261  break;
262  }
263  }
264  if (!isNew) {
265  fout << "### Check overlaps for PhysVolume " << pvname << " is skipted because was already done"
266  << "\n";
267  continue;
268  }
269  savedgdml.push_back(pvname);
270  fout << "### Check overlaps for PhysVolume " << pvname << "\n";
271  // gdml dump only for 1 volume
272  if (gdmlFlag) {
273  gdml->Write(pvname + ".gdml", (*pvs)[i], true);
274  }
275  G4GeomTestVolume test(((*pvs)[i]), tolerance, nPoints, verbose);
276  test.SetErrorsThreshold(nPrints);
277  test.TestOverlapInTree();
278  }
279  }
280  }
281  } else {
282  fout << "### Check overlaps for PhysVolume Node[" << ii << "] : " << nodeNames[ii] << "\n";
283  G4VPhysicalVolume* pv = pvs->GetVolume((G4String)nodeNames[ii]);
284  G4GeomTestVolume test(pv, tolerance, nPoints, verbose);
285  test.SetErrorsThreshold(nPrints);
286  test.TestOverlapInTree();
287  }
288  }
289  }
290  if (!PVname.empty()) {
291  fout << "----------- List of PhysVolumes by name -----------------"
292  << "\n";
293  for (unsigned int i = 0; i < numPV; ++i) {
294  if (PVname == (*pvs)[i]->GetName()) {
295  fout << " ##### PhysVolume " << PVname << " [" << ((*pvs)[i])->GetCopyNo()
296  << "] LV: " << ((*pvs)[i])->GetLogicalVolume()->GetName()
297  << " Mother LV: " << ((*pvs)[i])->GetMotherLogical()->GetName()
298  << " Region: " << ((*pvs)[i])->GetLogicalVolume()->GetRegion()->GetName() << "\n";
299  fout << " Translation: " << ((*pvs)[i])->GetObjectTranslation() << "\n";
300  fout << " Rotation: " << ((*pvs)[i])->GetObjectRotationValue() << "\n";
301  if (gdmlFlag) {
302  gdml->Write(PVname + ".gdml", (*pvs)[i], true);
303  }
304  }
305  }
306  }
307  if (!LVname.empty()) {
308  fout << "---------- List of Logical Volumes by name ------------------"
309  << "\n";
310  for (unsigned int i = 0; i < numLV; ++i) {
311  if (LVname == ((*lvs)[i])->GetName()) {
312  G4int np = ((*lvs)[i])->GetNoDaughters();
313  fout << " ##### LogVolume " << LVname << " " << np << " daughters"
314  << " Region: " << ((*lvs)[i])->GetRegion()->GetName() << "\n";
315  fout << *(((*lvs)[i])->GetSolid()) << "\n";
316  for (G4int j = 0; j < np; ++j) {
317  G4VPhysicalVolume* pv = ((*lvs)[i])->GetDaughter(j);
318  if (pv) {
319  fout << " PV: " << pv->GetName() << " [" << pv->GetCopyNo() << "]"
320  << " type: " << pv->VolumeType() << " multiplicity: " << pv->GetMultiplicity()
321  << " LV: " << pv->GetLogicalVolume()->GetName() << "\n";
322  fout << " Translation: " << pv->GetObjectTranslation() << "\n";
323  fout << " Rotation: " << pv->GetObjectRotationValue() << "\n";
324  fout << *(pv->GetLogicalVolume()->GetSolid()) << "\n";
325  }
326  }
327  }
328  }
329  }
330  fout << "---------------- End of overlap checks ---------------------"
331  << "\n";
332  delete gdml;
333 }
void makeReportForGeometry(std::ofstream &fout, G4VPhysicalVolume *world)
Log< level::Info, true > LogVerbatim
bool verbose
const double tolerance
void sendToFile(std::ofstream *)
void makeReportForMaterials(std::ofstream &fout)
int np
Definition: AMPTWrapper.h:43
ii
Definition: cuy.py:589
void ReportRegions(const std::string &ss)
void makeReportForOverlaps(std::ofstream &fout, const edm::ParameterSet &p, G4VPhysicalVolume *world)
Log< level::Warning, false > LogWarning
CMSG4CheckOverlap(edm::ParameterSet const &p, std::string &regFile, CustomUIsession *, G4VPhysicalVolume *world)