Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:22

0001 #include "FWCore/Utilities/interface/Exception.h"
0002 
0003 #include "SimG4Core/Geometry/interface/DD4hep_DDG4Builder.h"
0004 #include "SimG4Core/Geometry/interface/SensitiveDetectorCatalog.h"
0005 
0006 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0007 #include "DetectorDescription/DDCMS/interface/DDDetector.h"
0008 #include <DDG4/Geant4Converter.h>
0009 #include <DDG4/Geant4GeometryInfo.h>
0010 #include <DDG4/Geant4Mapping.h>
0011 #include <DD4hep/Detector.h>
0012 #include <DD4hep/Filter.h>
0013 
0014 #include "G4LogicalVolume.hh"
0015 #include "G4LogicalVolumeStore.hh"
0016 #include "G4ReflectionFactory.hh"
0017 
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019 
0020 #include <algorithm>
0021 
0022 using namespace cms;
0023 using namespace dd4hep;
0024 using namespace dd4hep::sim;
0025 
0026 DDG4Builder::DDG4Builder(const cms::DDCompactView *cpv, dd4hep::sim::Geant4GeometryMaps::VolumeMap &lvmap, bool check)
0027     : compactView_(cpv), map_(lvmap), check_(check) {}
0028 
0029 G4VPhysicalVolume *DDG4Builder::BuildGeometry(SensitiveDetectorCatalog &catalog) {
0030   G4ReflectionFactory *refFact = G4ReflectionFactory::Instance();
0031   refFact->SetScalePrecision(100. * refFact->GetScalePrecision());
0032 
0033   const cms::DDDetector *det = compactView_->detector();
0034   DetElement world = det->description()->world();
0035   const Detector &detector = *det->description();
0036 
0037   // GET FULL G4 GEOMETRY
0038   Geant4Converter g4Geo(detector);
0039   g4Geo.debugMaterials = false;
0040   Geant4GeometryInfo *geometry = g4Geo.create(world).detach();
0041   map_ = geometry->g4Volumes;
0042 
0043   // FIND & STORE ALL G4 LOGICAL VOLUMES DEFINED AS SENSITIVE IN CMSSW XMLS
0044   std::vector<std::pair<G4LogicalVolume *, const dd4hep::SpecPar *>> dd4hepVec;
0045   const dd4hep::SpecParRegistry &specPars = det->specpars();
0046   dd4hep::SpecParRefs specs;
0047   specPars.filter(specs, "SensitiveDetector");
0048   for (auto const &it : map_) {
0049     bool foundMatch = false;  // Stop search at first occurrence
0050     for (auto const &fit : specs) {
0051       for (auto const &pit : fit.second->paths) {
0052         if (dd4hep::dd::compareEqualName(dd4hep::dd::noNamespace(dd4hep::dd::realTopName(pit)),
0053                                          dd4hep::dd::noNamespace(it.first.name()))) {
0054           dd4hepVec.emplace_back(&*it.second, &*fit.second);
0055 
0056           foundMatch = true;
0057           break;
0058         }
0059       }
0060       if (foundMatch)
0061         break;
0062     }
0063   }
0064 
0065   // ADD ALL SELECTED G4 LOGICAL VOLUMES TO SENSITIVE DETECTORS CATALOGUE
0066   for (auto const &it : dd4hepVec) {
0067     // Sensitive detector info
0068     const G4String &sensitiveDetectorG4Name = it.first->GetName();
0069     auto sClassName = it.second->strValue("SensitiveDetector");
0070     auto sROUName = it.second->strValue("ReadOutName");
0071     // Add to catalogue
0072     catalog.insert({sClassName.data(), sClassName.size()}, {sROUName.data(), sROUName.size()}, sensitiveDetectorG4Name);
0073 
0074     edm::LogVerbatim("SimG4CoreApplication") << " DDG4SensitiveConverter: Sensitive " << sensitiveDetectorG4Name
0075                                              << " Class Name " << sClassName << " ROU Name " << sROUName;
0076 
0077     // Reflected sensors also need to be added to the senstive detectors catalogue!
0078     // Similar treatment here with DD4hep, as what was done for old DD.
0079     const G4String &sensitiveDetectorG4ReflectedName = sensitiveDetectorG4Name + "_refl";
0080 
0081     const G4LogicalVolumeStore *const allG4LogicalVolumes = G4LogicalVolumeStore::GetInstance();
0082     const bool hasG4ReflectedVolume =
0083         std::find_if(
0084             allG4LogicalVolumes->begin(), allG4LogicalVolumes->end(), [&](G4LogicalVolume *const aG4LogicalVolume) {
0085               return (aG4LogicalVolume->GetName() == sensitiveDetectorG4ReflectedName);
0086             }) != allG4LogicalVolumes->end();
0087     if (hasG4ReflectedVolume) {
0088       // Add reflected sensitive detector to catalogue
0089       catalog.insert(
0090           {sClassName.data(), sClassName.size()}, {sROUName.data(), sROUName.size()}, sensitiveDetectorG4ReflectedName);
0091 
0092       edm::LogVerbatim("SimG4CoreApplication")
0093           << " DDG4SensitiveConverter: Sensitive " << sensitiveDetectorG4ReflectedName << " Class Name " << sClassName
0094           << " ROU Name " << sROUName;
0095     }
0096   }
0097 
0098   return geometry->world();
0099 }