DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4RegexSensitivesConstruction.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // \author Markus Frank
11 // \date 2015-11-09
12 //
13 //==========================================================================
14 
15 // Framework include files
17 
18 // C/C++ include files
19 #include <set>
20 #include <regex>
21 
23 namespace dd4hep {
24 
26  namespace sim {
27 
29 
40  public:
41  std::string detector_name;
42  std::vector<std::string> regex_values;
43  std::size_t collect_volumes(std::set<Volume>& volumes,
44  PlacedVolume pv,
45  const std::string& path,
46  const std::vector<std::regex>& matches);
47  public:
49  Geant4RegexSensitivesConstruction(Geant4Context* ctxt, const std::string& nam);
54  };
55  } // End namespace sim
56 } // End namespace dd4hep
57 
58 
59 // Framework include files
60 #include <DD4hep/InstanceCount.h>
61 #include <DD4hep/Printout.h>
62 #include <DD4hep/Plugins.h>
63 #include <DD4hep/Detector.h>
64 #include <DD4hep/DetectorTools.h>
65 
66 #include <DDG4/Geant4Mapping.h>
67 #include <DDG4/Geant4Kernel.h>
68 #include <DDG4/Factories.h>
69 
70 // ROOT include files
71 #include <TTimeStamp.h>
72 #include <TGeoManager.h>
73 // Geant4 include files
74 #include <G4PVPlacement.hh>
75 #include <G4VSensitiveDetector.hh>
76 
77 using namespace dd4hep::sim;
78 
80 
84 {
85  declareProperty("Detector", detector_name);
86  declareProperty("Match", regex_values);
88 }
89 
93 }
94 
95 std::size_t
97  PlacedVolume pv,
98  const std::string& path,
99  const std::vector<std::regex>& matches)
100 {
101  std::size_t count = 0;
102  // Try to minimize a bit the number of regex matches.
103  if ( volumes.find(pv.volume()) == volumes.end() ) {
104  if( !path.empty() ) {
105  for( const auto& match : matches ) {
106  std::smatch sm;
107  bool stat = std::regex_search(path, sm, match);
108  if( stat ) {
109  volumes.insert(pv.volume());
110  ++count;
111  break;
112  }
113  }
114  }
115  // Now recurse down the daughters
116  for( int i=0, num = pv->GetNdaughters(); i < num; ++i ) {
117  PlacedVolume daughter = pv->GetDaughter(i);
118  std::string daughter_path = path + "/" + daughter.name();
119  count += this->collect_volumes(volumes, daughter, daughter_path, matches);
120  }
121  }
122  return count;
123 }
124 
128  const Geant4Kernel& kernel = context()->kernel();
129  const auto& types = kernel.sensitiveDetectorTypes();
130  const std::string& dflt = kernel.defaultSensitiveDetectorType();
131  const char* det = detector_name.c_str();
132 
134  if( !de.isValid() ) {
135  except("Failed to locate subdetector DetElement %s to manage Geant4 energy deposits.", det);
136  }
138  if( !sd.isValid() ) {
139  except("Failed to locate sensitive detector %s to manage Geant4 energy deposits.", det);
140  }
141  std::string nam = sd.name();
142  auto iter = types.find(nam);
143  std::string typ = (iter != types.end()) ? (*iter).second : dflt;
144  G4VSensitiveDetector* g4sd = this->createSensitiveDetector(typ, nam);
145 
146  std::set<Volume> volumes;
147  int flags = std::regex_constants::icase | std::regex_constants::ECMAScript;
148  std::vector<std::regex> expressions;
149  for( const auto& val : regex_values ) {
150  std::regex e(val, (std::regex_constants::syntax_option_type)flags);
151  expressions.emplace_back(e);
152  }
153  TTimeStamp start;
154  info("%s Starting to scan volume....", det);
155  std::size_t num_nodes = this->collect_volumes(volumes, de.placement(), de.placementPath(), expressions);
156  for( const auto& vol : volumes ) {
157  G4LogicalVolume* g4vol = g4info->g4Volumes[vol];
158  if( !g4vol ) {
159  except("+++ Failed to access G4LogicalVolume for SD %s of type %s", nam.c_str(), typ.c_str());
160  }
161  debug("%s Assign sensitive detector [%s] to volume: %s.",
162  nam.c_str(), typ.c_str(), vol.name());
163  ctxt->setSensitiveDetector(g4vol, g4sd);
164  }
165  TTimeStamp stop;
166  info("%s Handled %ld nodes with %ld sensitive volume type(s). Total of %7.3f seconds.",
167  det, num_nodes, volumes.size(), stop.AsDouble()-start.AsDouble() );
168 }
Geant4DetectorConstruction.h
Geant4Mapping.h
dd4hep::sim::Geant4RegexSensitivesConstruction::~Geant4RegexSensitivesConstruction
virtual ~Geant4RegexSensitivesConstruction()
Default destructor.
Definition: Geant4RegexSensitivesConstruction.cpp:91
dd4hep::SensitiveDetector
Handle class to hold the information of a sensitive detector.
Definition: DetElement.h:44
Detector.h
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:163
dd4hep::sim::Geant4RegexSensitivesConstruction
Class to create Geant4 detector geometry from TGeo representation in memory.
Definition: Geant4RegexSensitivesConstruction.cpp:39
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
dd4hep::sim::Geant4Kernel
Class, which allows all Geant4Action derivatives to access the DDG4 kernel structures.
Definition: Geant4Kernel.h:64
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4Mapping::instance
static Geant4Mapping & instance()
Possibility to define a singleton instance.
Definition: Geant4Mapping.cpp:35
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::detail::tools::findElement
DetElement findElement(const Detector &description, const std::string &path)
Find DetElement as child of the top level volume by its absolute path.
Definition: DetectorTools.cpp:214
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
dd4hep::sim::Geant4DetectorConstructionContext
Geant4 detector construction context definition.
Definition: Geant4DetectorConstruction.h:61
dd4hep::sim::Geant4Kernel::sensitiveDetectorTypes
const std::map< std::string, std::string > & sensitiveDetectorTypes() const
Property access: Names with specialized factories to create G4VSensitiveDetector instances.
Definition: Geant4Kernel.h:223
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::sim::Geant4DetectorConstructionContext::description
Detector & description
Reference to geometry object.
Definition: Geant4DetectorConstruction.h:67
dd4hep::sim::Geant4RegexSensitivesConstruction::collect_volumes
std::size_t collect_volumes(std::set< Volume > &volumes, PlacedVolume pv, const std::string &path, const std::vector< std::regex > &matches)
Definition: Geant4RegexSensitivesConstruction.cpp:96
dd4hep::sim::Geant4Mapping::ptr
Geant4GeometryInfo * ptr() const
Access to the data pointer.
Definition: Geant4Mapping.h:63
G4VSensitiveDetector
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:59
DetectorTools.h
dd4hep::Detector::sensitiveDetector
virtual SensitiveDetector sensitiveDetector(const std::string &name) const =0
Retrieve a sensitive detector by its name from the detector description.
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4Kernel::defaultSensitiveDetectorType
const std::string defaultSensitiveDetectorType() const
Property access: Name of the default factory to create G4VSensitiveDetector instances.
Definition: Geant4Kernel.h:219
dd4hep::sim::Geant4RegexSensitivesConstruction::constructSensitives
void constructSensitives(Geant4DetectorConstructionContext *ctxt)
Sensitives construction callback. Called at "ConstructSDandField()".
Definition: Geant4RegexSensitivesConstruction.cpp:126
Plugins.h
dd4hep::DetElement::placementPath
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: DetElement.cpp:85
Factories.h
dd4hep::sim::Geant4RegexSensitivesConstruction::detector_name
std::string detector_name
Definition: Geant4RegexSensitivesConstruction.cpp:41
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4Context::kernel
Geant4Kernel & kernel() const
Access to the kernel object.
Definition: Geant4Context.h:233
dd4hep::sim::Geant4GeometryInfo
Concreate class holding the relation information between geant4 objects and dd4hep objects.
Definition: Geant4GeometryInfo.h:93
dd4hep::sim::Geant4DetectorConstructionContext::setSensitiveDetector
void setSensitiveDetector(G4LogicalVolume *vol, G4VSensitiveDetector *sd)
Helper: Assign sensitive detector to logical volume.
Definition: Geant4DetectorConstruction.cpp:27
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::PlacedVolume::volume
Volume volume() const
Logical volume of this placement.
Definition: Volumes.cpp:468
dd4hep::sim::Geant4Action::debug
void debug(const char *fmt,...) const
Support of debug messages.
Definition: Geant4Action.cpp:207
Geant4Kernel.h
InstanceCount.h
dd4hep::sim::Geant4DetectorConstruction
Basic implementation of the Geant4 detector construction action.
Definition: Geant4DetectorConstruction.h:102
dd4hep::sim::Geant4RegexSensitivesConstruction::regex_values
std::vector< std::string > regex_values
Definition: Geant4RegexSensitivesConstruction.cpp:42
Printout.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4RegexSensitivesConstruction::Geant4RegexSensitivesConstruction
Geant4RegexSensitivesConstruction(Geant4Context *ctxt, const std::string &nam)
Initializing constructor for DDG4.
Definition: Geant4RegexSensitivesConstruction.cpp:82
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4GeometryInfo::g4Volumes
Geant4GeometryMaps::VolumeMap g4Volumes
Definition: Geant4GeometryInfo.h:116