Newer
Older
Markus Frank
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// \author Markus Frank
// \date 2015-11-09
//
//==========================================================================
// Framework include files
#include <DDG4/Geant4DetectorConstruction.h>
// C/C++ include files
#include <set>
#include <regex>
/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
/// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
namespace sim {
/// Class to create Geant4 detector geometry from TGeo representation in memory
/**
* On demand the sensitive detectors are created and attached to all sensitive
* volumes. The relevant callback is executed when the call to
* ConstructSDandField() of the corresponding G4VUserDetectorConstruction
* instance is called. The call is thread-local!
*
* \author M.Frank
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class Geant4RegexSensitivesConstruction : public Geant4DetectorConstruction {
public:
std::string detector_name;
std::string regex_value;
std::size_t collect_volumes(std::set<Volume>& volumes,
PlacedVolume pv,
const std::string& path,
std::regex& match);
public:
/// Initializing constructor for DDG4
Geant4RegexSensitivesConstruction(Geant4Context* ctxt, const std::string& nam);
/// Default destructor
virtual ~Geant4RegexSensitivesConstruction();
/// Sensitives construction callback. Called at "ConstructSDandField()"
void constructSensitives(Geant4DetectorConstructionContext* ctxt);
};
} // End namespace sim
} // End namespace dd4hep
// Framework include files
#include <DD4hep/InstanceCount.h>
#include <DD4hep/Printout.h>
#include <DD4hep/Plugins.h>
#include <DD4hep/Detector.h>
#include <DD4hep/DetectorTools.h>
#include <DDG4/Geant4Mapping.h>
#include <DDG4/Geant4Kernel.h>
#include <DDG4/Factories.h>
// ROOT include files
#include <TTimeStamp.h>
#include <TGeoManager.h>
// Geant4 include files
#include <G4PVPlacement.hh>
#include <G4VSensitiveDetector.hh>
using namespace dd4hep::sim;
DECLARE_GEANT4ACTION(Geant4RegexSensitivesConstruction)
/// Initializing constructor for other clients
Geant4RegexSensitivesConstruction::Geant4RegexSensitivesConstruction(Geant4Context* ctxt, const std::string& nam)
: Geant4DetectorConstruction(ctxt,nam)
{
declareProperty("Detector", detector_name);
declareProperty("Regex", regex_value);
InstanceCount::increment(this);
}
/// Default destructor
Geant4RegexSensitivesConstruction::~Geant4RegexSensitivesConstruction() {
InstanceCount::decrement(this);
}
std::size_t
Geant4RegexSensitivesConstruction::collect_volumes(std::set<Volume>& volumes,
PlacedVolume pv,
const std::string& path,
std::regex& match)
{
std::size_t count = 0;
// Try to minimize a bit the number of regex matches.
if ( volumes.find(pv.volume()) == volumes.end() ) {
if( !path.empty() ) {
std::smatch sm;
bool stat = std::regex_match(path, sm, match);
if( stat ) {
volumes.insert(pv.volume());
}
++count;
}
// Now recurse down the daughters
for( int i=0, num = pv->GetNdaughters(); i < num; ++i ) {
PlacedVolume daughter = pv->GetDaughter(i);
std::string daughter_path = path + "/" + daughter.name();
count += this->collect_volumes(volumes, daughter, daughter_path, match);
}
}
return count;
}
/// Sensitive detector construction callback. Called at "ConstructSDandField()"
void Geant4RegexSensitivesConstruction::constructSensitives(Geant4DetectorConstructionContext* ctxt) {
Geant4GeometryInfo* g4info = Geant4Mapping::instance().ptr();
const Geant4Kernel& kernel = context()->kernel();
const auto& types = kernel.sensitiveDetectorTypes();
const std::string& dflt = kernel.defaultSensitiveDetectorType();
const char* det = detector_name.c_str();
DetElement de = detail::tools::findElement(ctxt->description, detector_name);
if( !de.isValid() ) {
except("Failed to locate subdetector DetElement %s to manage Geant4 energy deposits.", det);
}
SensitiveDetector sd = ctxt->description.sensitiveDetector(detector_name);
if( !sd.isValid() ) {
except("Failed to locate sensitive detector %s to manage Geant4 energy deposits.", det);
}
std::string nam = sd.name();
auto iter = types.find(nam);
std::string typ = (iter != types.end()) ? (*iter).second : dflt;
G4VSensitiveDetector* g4sd = this->createSensitiveDetector(typ, nam);
std::set<Volume> volumes;
int flags = std::regex_constants::icase | std::regex_constants::ECMAScript;
std::regex expression(regex_value, (std::regex_constants::syntax_option_type)flags);
TTimeStamp start;
print("+++ Detector: %s Starting to scan volume....", det);
std::size_t num_nodes = this->collect_volumes(volumes, de.placement(), de.placementPath(), expression);
for( const auto& vol : volumes ) {
G4LogicalVolume* g4vol = g4info->g4Volumes[vol];
if( !g4vol ) {
except("+++ Failed to access G4LogicalVolume for SD %s of type %s",
nam.c_str(), typ.c_str());
}
print("+++ Detector: %s Assign sensitive detector [%s] to volume: %s.",
nam.c_str(), typ.c_str(), vol.name());
ctxt->setSensitiveDetector(g4vol, g4sd);
}
TTimeStamp stop;
print("+++ Detector: %s Handled %ld nodes with %ld sensitive volume type. Total of %7.3f seconds.",
det, num_nodes, volumes.size(), stop.AsDouble()-start.AsDouble() );
}