CMS 3D CMS Logo

DD4hep_DDG4Builder.cc
Go to the documentation of this file.
2 
5 
8 #include <DDG4/Geant4Converter.h>
9 #include <DDG4/Geant4GeometryInfo.h>
10 #include <DDG4/Geant4Mapping.h>
11 #include <DD4hep/Detector.h>
12 #include <DD4hep/Filter.h>
13 
14 #include "G4LogicalVolume.hh"
15 #include "G4LogicalVolumeStore.hh"
16 #include "G4ReflectionFactory.hh"
17 
19 
20 #include <algorithm>
21 
22 using namespace cms;
23 using namespace dd4hep;
24 using namespace dd4hep::sim;
25 
26 DDG4Builder::DDG4Builder(const cms::DDCompactView *cpv, dd4hep::sim::Geant4GeometryMaps::VolumeMap &lvmap, bool check)
27  : compactView_(cpv), map_(lvmap), check_(check) {}
28 
30  G4ReflectionFactory *refFact = G4ReflectionFactory::Instance();
31  refFact->SetScalePrecision(100. * refFact->GetScalePrecision());
32 
33  const cms::DDDetector *det = compactView_->detector();
34  DetElement world = det->description()->world();
35  const Detector &detector = *det->description();
36 
37  // GET FULL G4 GEOMETRY
38  Geant4Converter g4Geo(detector);
39  g4Geo.debugMaterials = false;
40  Geant4GeometryInfo *geometry = g4Geo.create(world).detach();
41  map_ = geometry->g4Volumes;
42 
43  // FIND & STORE ALL G4 LOGICAL VOLUMES DEFINED AS SENSITIVE IN CMSSW XMLS
44  std::vector<std::pair<G4LogicalVolume *, const dd4hep::SpecPar *>> dd4hepVec;
45  const dd4hep::SpecParRegistry &specPars = det->specpars();
46  dd4hep::SpecParRefs specs;
47  specPars.filter(specs, "SensitiveDetector");
48  for (auto const &it : map_) {
49  bool foundMatch = false; // Stop search at first occurrence
50  for (auto const &fit : specs) {
51  for (auto const &pit : fit.second->paths) {
52  if (dd4hep::dd::compareEqualName(dd4hep::dd::noNamespace(dd4hep::dd::realTopName(pit)),
53  dd4hep::dd::noNamespace(it.first.name()))) {
54  dd4hepVec.emplace_back(&*it.second, &*fit.second);
55 
56  foundMatch = true;
57  break;
58  }
59  }
60  if (foundMatch)
61  break;
62  }
63  }
64 
65  // ADD ALL SELECTED G4 LOGICAL VOLUMES TO SENSITIVE DETECTORS CATALOGUE
66  for (auto const &it : dd4hepVec) {
67  // Sensitive detector info
68  const G4String &sensitiveDetectorG4Name = it.first->GetName();
69  auto sClassName = it.second->strValue("SensitiveDetector");
70  auto sROUName = it.second->strValue("ReadOutName");
71  // Add to catalogue
72  catalog.insert({sClassName.data(), sClassName.size()}, {sROUName.data(), sROUName.size()}, sensitiveDetectorG4Name);
73 
74  edm::LogVerbatim("SimG4CoreApplication") << " DDG4SensitiveConverter: Sensitive " << sensitiveDetectorG4Name
75  << " Class Name " << sClassName << " ROU Name " << sROUName;
76 
77  // Reflected sensors also need to be added to the senstive detectors catalogue!
78  // Similar treatment here with DD4hep, as what was done for old DD.
79  const G4String &sensitiveDetectorG4ReflectedName = sensitiveDetectorG4Name + "_refl";
80 
81  const G4LogicalVolumeStore *const allG4LogicalVolumes = G4LogicalVolumeStore::GetInstance();
82  const bool hasG4ReflectedVolume =
83  std::find_if(
84  allG4LogicalVolumes->begin(), allG4LogicalVolumes->end(), [&](G4LogicalVolume *const aG4LogicalVolume) {
85  return (aG4LogicalVolume->GetName() == sensitiveDetectorG4ReflectedName);
86  }) != allG4LogicalVolumes->end();
87  if (hasG4ReflectedVolume) {
88  // Add reflected sensitive detector to catalogue
89  catalog.insert(
90  {sClassName.data(), sClassName.size()}, {sROUName.data(), sROUName.size()}, sensitiveDetectorG4ReflectedName);
91 
92  edm::LogVerbatim("SimG4CoreApplication")
93  << " DDG4SensitiveConverter: Sensitive " << sensitiveDetectorG4ReflectedName << " Class Name " << sClassName
94  << " ROU Name " << sROUName;
95  }
96  }
97 
98  return geometry->world();
99 }
Log< level::Info, true > LogVerbatim
dd4hep::Detector const * description() const
Definition: DDDetector.h:35
const cms::DDDetector * detector() const
Definition: DDCompactView.h:34
dd4hep::sim::Geant4GeometryMaps::VolumeMap & map_
DDG4Builder(const cms::DDCompactView *, dd4hep::sim::Geant4GeometryMaps::VolumeMap &, bool check)
G4VPhysicalVolume * BuildGeometry(SensitiveDetectorCatalog &)
Namespace of DDCMS conversion namespace.
const cms::DDCompactView * compactView_
dd4hep::SpecParRegistry const & specpars() const
Definition: DDDetector.h:21