File indexing completed on 2023-03-17 11:27:00
0001 #include "Validation/CheckOverlap/interface/CheckOverlap.h"
0002
0003 #include "SimG4Core/Notification/interface/BeginOfRun.h"
0004
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/ESHandle.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0009
0010 #include "G4Run.hh"
0011 #include "G4LogicalVolumeStore.hh"
0012 #include "G4PVPlacement.hh"
0013 #include "G4PVParameterised.hh"
0014 #include "G4LogicalVolume.hh"
0015 #include "G4Material.hh"
0016 #include "G4TransportationManager.hh"
0017
0018 #include <set>
0019
0020 CheckOverlap::CheckOverlap(const edm::ParameterSet& p) : topLV(0) {
0021 std::vector<std::string> defNames;
0022 nodeNames = p.getUntrackedParameter<std::vector<std::string> >("NodeNames", defNames);
0023 nPoints = p.getUntrackedParameter<int>("Resolution", 1000);
0024 G4cout << "CheckOverlap:: initialised with " << nodeNames.size() << " Node Names and Resolution " << nPoints
0025 << " the names are:" << G4endl;
0026 for (unsigned int ii = 0; ii < nodeNames.size(); ii++)
0027 G4cout << "CheckOverlap:: Node[" << ii << "] : " << nodeNames[ii] << G4endl;
0028 }
0029
0030 CheckOverlap::~CheckOverlap() {}
0031
0032 void CheckOverlap::update(const BeginOfRun* run) {
0033 if (!nodeNames.empty()) {
0034 const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
0035 G4cout << "CheckOverlap::update nLV= " << lvs->size() << G4endl;
0036 std::vector<G4LogicalVolume*>::const_iterator lvcite;
0037 int i = 0;
0038 for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
0039 for (unsigned int ii = 0; ii < nodeNames.size(); ii++) {
0040 if ((*lvcite)->GetName() == (G4String)(nodeNames[ii])) {
0041 topLV.push_back((*lvcite));
0042 break;
0043 }
0044 }
0045 G4cout << "Name of node " << (++i) << " : " << (*lvcite)->GetName() << G4endl;
0046 if (topLV.size() == nodeNames.size())
0047 break;
0048 }
0049 } else {
0050 G4VPhysicalVolume* theTopPV = getTopPV();
0051 topLV.push_back(theTopPV->GetLogicalVolume());
0052 }
0053
0054 if (topLV.empty()) {
0055 G4cout << "No Top LV Found" << G4endl;
0056 } else {
0057 for (unsigned int ii = 0; ii < topLV.size(); ++ii) {
0058 G4cout << "Top LV Name " << topLV[ii]->GetName() << G4endl;
0059 checkHierarchyLeafPVLV(topLV[ii], 0);
0060 }
0061 }
0062 }
0063
0064 void CheckOverlap::checkHierarchyLeafPVLV(G4LogicalVolume* lv, unsigned int leafDepth) {
0065
0066 mmlvpv lvpvDaughters;
0067 std::set<G4LogicalVolume*> lvDaughters;
0068 int NoDaughters = lv->GetNoDaughters();
0069 while ((NoDaughters--) > 0) {
0070 G4VPhysicalVolume* pvD = lv->GetDaughter(NoDaughters);
0071 lvpvDaughters.insert(mmlvpv::value_type(pvD->GetLogicalVolume(), pvD));
0072 lvDaughters.insert(pvD->GetLogicalVolume());
0073 }
0074
0075 std::set<G4LogicalVolume*>::const_iterator scite;
0076 mmlvpv::const_iterator mmcite;
0077
0078
0079 for (scite = lvDaughters.begin(); scite != lvDaughters.end(); scite++) {
0080 std::pair<mmlvpv::iterator, mmlvpv::iterator> mmER = lvpvDaughters.equal_range(*scite);
0081
0082 for (mmcite = mmER.first; mmcite != mmER.second; mmcite++)
0083 checkPV((*mmcite).second, leafDepth + 1);
0084
0085 checkHierarchyLeafPVLV(*scite, leafDepth + 1);
0086 }
0087 }
0088
0089 void CheckOverlap::checkPV(G4VPhysicalVolume* pv, unsigned int leafDepth) {
0090
0091 #ifndef G4V7
0092 std::string mother = "DDDWorld";
0093 if (pv->GetMotherLogical())
0094 mother = pv->GetMotherLogical()->GetName();
0095 if (!pv->IsReplicated()) {
0096 G4PVPlacement* pvplace = dynamic_cast<G4PVPlacement*>(pv);
0097 G4bool ok = pvplace->CheckOverlaps(nPoints);
0098 G4cout << "Placed PV " << pvplace->GetName() << " Number " << pvplace->GetCopyNo() << " in mother " << mother
0099 << " at depth " << leafDepth << " Status " << ok << G4endl;
0100 if (ok) {
0101 if (pv->GetRotation() == nullptr) {
0102 G4cout << "Translation " << pv->GetTranslation() << " and with no rotation" << G4endl;
0103 } else {
0104 G4cout << "Translation " << pv->GetTranslation() << " and with rotation " << *(pv->GetRotation()) << G4endl;
0105 }
0106 G4LogicalVolume* lv = pv->GetLogicalVolume();
0107 dumpLV(lv, "Self");
0108 if (pv->GetMotherLogical()) {
0109 lv = pv->GetMotherLogical();
0110 dumpLV(lv, "Mother");
0111 }
0112 }
0113 } else {
0114 if (pv->GetParameterisation() != nullptr) {
0115 G4PVParameterised* pvparam = dynamic_cast<G4PVParameterised*>(pv);
0116 G4bool ok = pvparam->CheckOverlaps(nPoints);
0117 G4cout << "Parametrized PV " << pvparam->GetName() << " in mother " << mother << " at depth " << leafDepth
0118 << " Status " << ok << G4endl;
0119 }
0120 }
0121 #endif
0122 }
0123
0124 G4VPhysicalVolume* CheckOverlap::getTopPV() {
0125 return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
0126 }
0127
0128 void CheckOverlap::dumpLV(G4LogicalVolume* lv, std::string str) {
0129 G4cout << "Dump of " << str << " Logical Volume " << lv->GetName() << " Solid: " << lv->GetSolid()->GetName()
0130 << " Material: " << lv->GetMaterial()->GetName() << G4endl;
0131 G4cout << *(lv->GetSolid()) << G4endl;
0132 }