File indexing completed on 2024-04-06 12:29:53
0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0003 #include "SimG4CMS/Calo/interface/HcalDumpGeometry.h"
0004
0005 #include <iostream>
0006 #include <memory>
0007
0008 HcalDumpGeometry::HcalDumpGeometry(const std::vector<std::string_view>& names,
0009 const HcalNumberingFromDDD* hcn,
0010 bool test,
0011 bool flag)
0012 : numberingFromDDD_(hcn), flag_(flag) {
0013 if (test)
0014 numberingScheme_.reset(dynamic_cast<HcalNumberingScheme*>(new HcalTestNumberingScheme(false)));
0015 else
0016 numberingScheme_ = std::make_unique<HcalNumberingScheme>();
0017 std::stringstream ss;
0018 for (const auto& lvname : names)
0019 ss << " " << lvname;
0020 G4cout << " Testmode: " << test << " with " << names.size() << " LVs: " << ss.str() << G4endl;
0021 const std::vector<std::string> namg = {"HBS", "HES", "HTS", "HVQ"};
0022 for (const auto& name : names) {
0023 std::string namex = (getNameNoNS(static_cast<std::string>(name))).substr(0, 3);
0024 if (std::find(namg.begin(), namg.end(), namex) != namg.end()) {
0025 if (std::find(names_.begin(), names_.end(), namex) == names_.end())
0026 names_.emplace_back(namex);
0027 }
0028 }
0029 G4cout << "HcalDumpGeometry:: dump geometry information for Hcal with " << names_.size() << " elements:" << G4endl;
0030 for (unsigned int k = 0; k < names_.size(); ++k)
0031 G4cout << "[" << k << "] : " << names_[k] << G4endl;
0032 }
0033
0034 void HcalDumpGeometry::update() {
0035 G4VPhysicalVolume* theTopPV =
0036 G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
0037 G4cout << "HcalDumpGeometry entered with entry of top PV at " << theTopPV << G4endl;
0038
0039 dumpTouch(theTopPV, 0);
0040 fHistory_.SetFirstEntry(theTopPV);
0041 G4cout << "HcalDumpGeometry finds " << infoVec_.size() << " touchables" << G4endl;
0042 sort(infoVec_.begin(), infoVec_.end(), CaloDetInfoLess());
0043 unsigned int k(0);
0044 for (const auto& info : infoVec_) {
0045 G4cout << "[" << k << "] " << info << G4endl;
0046 if (info.flag() && (info.solid() != nullptr)) {
0047 info.solid()->DumpInfo();
0048 G4cout << G4endl;
0049 }
0050 ++k;
0051 }
0052 }
0053
0054 void HcalDumpGeometry::dumpTouch(G4VPhysicalVolume* pv, unsigned int leafDepth) {
0055 if (leafDepth == 0)
0056 fHistory_.SetFirstEntry(pv);
0057 else
0058 fHistory_.NewLevel(pv, kNormal, pv->GetCopyNo());
0059
0060 G4ThreeVector globalpoint = fHistory_.GetTopTransform().Inverse().TransformPoint(G4ThreeVector(0, 0, 0));
0061 G4LogicalVolume* lv = pv->GetLogicalVolume();
0062
0063 const std::string& lvname = lv->GetName();
0064 std::string namex = (getNameNoNS(lvname)).substr(0, 3);
0065 for (unsigned int k = 0; k < names_.size(); ++k) {
0066 if (namex == names_[k]) {
0067 int theSize = fHistory_.GetDepth();
0068
0069 if (theSize > 5) {
0070 int depth = (fHistory_.GetVolume(theSize)->GetCopyNo()) % 10 + 1;
0071 int lay = (fHistory_.GetVolume(theSize)->GetCopyNo() / 10) % 100 + 1;
0072 int det = (fHistory_.GetVolume(theSize - 1)->GetCopyNo()) / 1000;
0073 HcalNumberingFromDDD::HcalID tmp = numberingFromDDD_->unitID(
0074 det, math::XYZVectorD(globalpoint.x(), globalpoint.y(), globalpoint.z()), depth, lay);
0075 uint32_t id = numberingScheme_->getUnitID(tmp);
0076 G4cout << "Det " << det << " Layer " << lay << ":" << depth << " Volume "
0077 << fHistory_.GetVolume(theSize)->GetName() << ":" << fHistory_.GetVolume(theSize - 1)->GetName()
0078 << " ID " << std::hex << id << std::dec << G4endl;
0079
0080 G4VSolid* solid = lv->GetSolid();
0081 infoVec_.emplace_back(CaloDetInfo(id, 0, 0, getNameNoNS(lvname), globalpoint, solid, flag_));
0082 }
0083 break;
0084 }
0085 }
0086
0087 int NoDaughters = lv->GetNoDaughters();
0088 while ((NoDaughters--) > 0) {
0089 G4VPhysicalVolume* pvD = lv->GetDaughter(NoDaughters);
0090 if (!pvD->IsReplicated())
0091 dumpTouch(pvD, leafDepth + 1);
0092 }
0093
0094 if (leafDepth > 0)
0095 fHistory_.BackLevel();
0096 }
0097
0098 std::string HcalDumpGeometry::getNameNoNS(const std::string& name) {
0099 if (name.find(':') == std::string::npos) {
0100 return name;
0101 } else {
0102 auto n1 = name.find(':') + 1;
0103 return name.substr(n1, (name.size() - n1));
0104 }
0105 }