forked from sPHENIX-Collaboration/coresoftware
/
PHG4SpacalDetector.cc
387 lines (331 loc) · 13.3 KB
/
PHG4SpacalDetector.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*!
* \file ${file_name}
* \brief
* \author Jin Huang <jhuang@bnl.gov>
* \version $$Revision: 1.7 $$
* \date $$Date: 2015/02/10 15:39:26 $$
*/
#include "PHG4SpacalDetector.h"
#include "PHG4CylinderGeomContainer.h"
#include "PHG4SpacalDisplayAction.h"
#include <g4main/PHG4Detector.h> // for PHG4Detector
#include <g4main/PHG4DisplayAction.h> // for PHG4DisplayAction
#include <g4main/PHG4Subsystem.h>
#include <phool/PHCompositeNode.h>
#include <phool/PHIODataNode.h>
#include <phool/PHNode.h> // for PHNode
#include <phool/PHNodeIterator.h> // for PHNodeIterator
#include <phool/PHObject.h> // for PHObject
#include <phool/getClass.h>
#include <phool/recoConsts.h>
#include <g4gdml/PHG4GDMLConfig.hh>
#include <g4gdml/PHG4GDMLUtility.hh>
#include <TSystem.h>
#include <Geant4/G4LogicalVolume.hh>
#include <Geant4/G4Material.hh>
#include <Geant4/G4PVPlacement.hh>
#include <Geant4/G4PhysicalConstants.hh>
#include <Geant4/G4String.hh> // for G4String
#include <Geant4/G4SystemOfUnits.hh>
#include <Geant4/G4ThreeVector.hh> // for G4ThreeVector
#include <Geant4/G4Transform3D.hh> // for G4Transform3D, G4RotateZ3D
#include <Geant4/G4Tubs.hh>
#include <Geant4/G4Types.hh> // for G4double
#include <Geant4/G4UserLimits.hh>
#include <cassert>
#include <cstdlib> // for exit
#include <iostream> // for operator<<, basic_ostream
#include <sstream>
class PHG4CylinderGeom;
//_______________________________________________________________
//note this inactive thickness is ~1.5% of a radiation length
PHG4SpacalDetector::PHG4SpacalDetector(PHG4Subsystem *subsys,
PHCompositeNode *Node,
const std::string &dnam,
PHParameters *parameters,
const int lyr,
bool init_geom)
: PHG4Detector(subsys, Node, dnam)
, m_DisplayAction(dynamic_cast<PHG4SpacalDisplayAction *>(subsys->GetDisplayAction()))
, layer(lyr)
{
if (init_geom)
{
_geom = new SpacalGeom_t();
if (_geom == nullptr)
{
std::cout << "PHG4SpacalDetector::Constructor - Fatal Error - invalid geometry object!" << std::endl;
gSystem->Exit(1);
exit(1);
}
assert(parameters);
_geom->ImportParameters(*parameters);
// _geom->Print();
}
gdml_config = PHG4GDMLUtility::GetOrMakeConfigNode(Node);
assert(gdml_config);
}
PHG4SpacalDetector::~PHG4SpacalDetector(void)
{
// deleting nullptr pointers is allowed (results in NOOP)
// so checking for not null before deleting is not needed
delete fiber_core_step_limits;
delete _geom;
}
//_______________________________________________________________
int PHG4SpacalDetector::IsInCylinderActive(const G4VPhysicalVolume *volume)
{
// std::cout << "checking detector" << std::endl;
if (active && fiber_core_vol.find(volume) != fiber_core_vol.end())
{
// return fiber_core_vol.find(volume)->second;
return FIBER_CORE;
}
if (absorberactive)
{
if (fiber_vol.find(volume) != fiber_vol.end())
{
return FIBER_CLADING;
}
if (block_vol.find(volume) != block_vol.end())
{
return ABSORBER;
}
if (calo_vol.find(volume) != calo_vol.end())
{
return SUPPORT;
}
}
return INACTIVE;
}
//_______________________________________________________________
void PHG4SpacalDetector::ConstructMe(G4LogicalVolume *logicWorld)
{
assert(_geom);
fiber_core_step_limits = new G4UserLimits(_geom->get_fiber_core_step_size() * cm);
Verbosity(_geom->get_construction_verbose());
if ((Verbosity() > 0))
{
std::cout << "PHG4SpacalDetector::Construct::" << GetName()
<< " - Start. Print Geometry:" << std::endl;
Print();
}
if ((_geom->get_zmin() * cm + _geom->get_zmax() * cm) / 2 != _geom->get_zpos() * cm)
{
std::cout << "PHG4SpacalDetector::Construct - ERROR - not yet support unsymmetric system. Let me know if you need it. - Jin" << std::endl;
_geom->Print();
gSystem->Exit(-1);
}
if (_geom->get_zmin() * cm >= _geom->get_zmax() * cm)
{
std::cout << "PHG4SpacalDetector::Construct - ERROR - zmin >= zmax!" << std::endl;
_geom->Print();
gSystem->Exit(-1);
}
G4Tubs *cylinder_solid = new G4Tubs(G4String(GetName()),
_geom->get_radius() * cm, _geom->get_max_radius() * cm,
_geom->get_length() * cm / 2.0, 0, twopi);
recoConsts *rc = recoConsts::instance();
G4Material *cylinder_mat = GetDetectorMaterial(rc->get_StringFlag("WorldMaterial"));
assert(cylinder_mat);
G4LogicalVolume *cylinder_logic = new G4LogicalVolume(cylinder_solid, cylinder_mat, GetName(), 0, 0, 0);
GetDisplayAction()->AddVolume(cylinder_logic, "SpacalCylinder");
if (!m_CosmicSetupFlag)
{
new G4PVPlacement(0, G4ThreeVector(_geom->get_xpos() * cm, _geom->get_ypos() * cm, _geom->get_zpos() * cm),
cylinder_logic, GetName(),
logicWorld, false, 0, OverlapCheck());
}
// install sectors
if (_geom->get_sector_map().size() == 0)
{
_geom->init_default_sector_map();
}
if ((Verbosity() > 0))
{
std::cout << "PHG4SpacalDetector::Construct::" << GetName()
<< " - start constructing " << _geom->get_sector_map().size() << " sectors in total. " << std::endl;
Print();
}
std::pair<G4LogicalVolume *, G4Transform3D> psec = Construct_AzimuthalSeg();
G4LogicalVolume *sec_logic = psec.first;
const G4Transform3D &sec_trans = psec.second;
for (const SpacalGeom_t::sector_map_t::value_type &val : _geom->get_sector_map())
{
const int sec = val.first;
const double rot = val.second;
G4Transform3D sec_place = G4RotateZ3D(rot) * sec_trans;
std::ostringstream name;
name << GetName() << "_sec" << sec;
G4PVPlacement *calo_phys = nullptr;
if (m_CosmicSetupFlag)
{
calo_phys = new G4PVPlacement(0, G4ThreeVector(0, -(_geom->get_radius()) * cm, 0), sec_logic,
G4String(name.str()), logicWorld, false, sec,
OverlapCheck());
}
else
{
calo_phys = new G4PVPlacement(sec_place, sec_logic,
G4String(name.str()), cylinder_logic, false, sec,
OverlapCheck());
}
calo_vol[calo_phys] = sec;
assert(gdml_config);
gdml_config->exclude_physical_vol(calo_phys);
}
_geom->set_nscint(_geom->get_nscint() * _geom->get_sector_map().size());
if (active)
{
std::ostringstream geonode;
if (superdetector != "NONE")
{
geonode << "CYLINDERGEOM_" << superdetector;
}
else
{
geonode << "CYLINDERGEOM_" << detector_type << "_" << layer;
}
PHG4CylinderGeomContainer *geo = findNode::getClass<PHG4CylinderGeomContainer>(topNode(), geonode.str());
if (!geo)
{
geo = new PHG4CylinderGeomContainer();
PHNodeIterator iter(topNode());
PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(geo,
geonode.str(), "PHObject");
runNode->addNode(newNode);
}
// here in the detector class we have internal units, convert to cm
// before putting into the geom object
PHG4CylinderGeom *mygeom = clone_geom();
geo->AddLayerGeom(layer, mygeom);
// geo->identify();
}
if (absorberactive)
{
std::ostringstream geonode;
if (superdetector != "NONE")
{
geonode << "CYLINDERGEOM_ABSORBER_" << superdetector;
}
else
{
geonode << "CYLINDERGEOM_ABSORBER_" << detector_type << "_" << layer;
}
PHG4CylinderGeomContainer *geo = findNode::getClass<PHG4CylinderGeomContainer>(topNode(), geonode.str());
if (!geo)
{
geo = new PHG4CylinderGeomContainer();
PHNodeIterator iter(topNode());
PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(geo,
geonode.str(), "PHObject");
runNode->addNode(newNode);
}
// here in the detector class we have internal units, convert to cm
// before putting into the geom object
PHG4CylinderGeom *mygeom = clone_geom();
geo->AddLayerGeom(layer, mygeom);
// geo->identify();
}
if ((Verbosity() > 0))
{
std::cout << "PHG4SpacalDetector::Construct::" << GetName()
<< " - Completed. Print Geometry:" << std::endl;
Print();
}
}
std::pair<G4LogicalVolume *, G4Transform3D>
PHG4SpacalDetector::Construct_AzimuthalSeg()
{
G4Tubs *sec_solid = new G4Tubs(G4String(GetName() + std::string("_sec")),
_geom->get_radius() * cm, _geom->get_max_radius() * cm,
_geom->get_length() * cm / 2.0, 0, twopi / _geom->get_azimuthal_n_sec());
G4Material *cylinder_mat = GetDetectorMaterial(_geom->get_absorber_mat());
assert(cylinder_mat);
G4LogicalVolume *sec_logic = new G4LogicalVolume(sec_solid, cylinder_mat,
G4String(G4String(GetName() + std::string("_sec"))), 0, 0, nullptr);
GetDisplayAction()->AddVolume(sec_logic, "AzimuthSegment");
const double fiber_length = _geom->get_thickness() * cm - 2 * _geom->get_fiber_outer_r() * cm;
G4LogicalVolume *fiber_logic = Construct_Fiber(fiber_length, std::string(""));
int fiber_count = 0;
// double z_step = _geom->get_fiber_distance() * cm * sqrt(3) / 2.;
double z_step = _geom->get_z_distance() * cm;
G4double z = _geom->get_zmin() * cm - _geom->get_zpos() * cm + z_step;
while (z < _geom->get_zmax() * cm - _geom->get_zpos() * cm - z_step)
{
const double rot = twopi / _geom->get_azimuthal_n_sec() * ((fiber_count % 2 == 0) ? 1. / 4. : 3. / 4.);
G4Transform3D fiber_place(G4RotateZ3D(rot) * G4TranslateZ3D(z) * G4TranslateX3D(_geom->get_half_radius() * cm) * G4RotateY3D(halfpi));
std::ostringstream name;
name << GetName() << "_fiber_" << fiber_count;
G4PVPlacement *fiber_physi = new G4PVPlacement(fiber_place, fiber_logic,
G4String(name.str()), sec_logic, false, fiber_count,
OverlapCheck());
fiber_vol[fiber_physi] = fiber_count;
assert(gdml_config);
gdml_config->exclude_physical_vol(fiber_physi);
z += z_step;
fiber_count++;
}
_geom->set_nscint(fiber_count);
if (Verbosity() > 0)
{
std::cout << "PHG4SpacalDetector::Construct_AzimuthalSeg::" << GetName()
<< " - constructed " << fiber_count << " fibers" << std::endl;
std::cout << "\t"
<< "_geom->get_fiber_distance() = " << _geom->get_fiber_distance()
<< std::endl;
std::cout << "\t"
<< "fiber_length = " << fiber_length / cm << std::endl;
std::cout << "\t"
<< "z_step = " << z_step << std::endl;
std::cout << "\t"
<< "_geom->get_azimuthal_bin() = " << _geom->get_azimuthal_n_sec()
<< std::endl;
std::cout << "\t"
<< "_geom->get_azimuthal_distance() = "
<< _geom->get_azimuthal_distance() << std::endl;
std::cout << "\t"
<< "_geom->is_virualize_fiber() = " << _geom->is_virualize_fiber()
<< std::endl;
}
return std::make_pair(sec_logic, G4Transform3D::Identity);
}
G4LogicalVolume *
PHG4SpacalDetector::Construct_Fiber(const G4double length, const std::string &id)
{
G4Tubs *fiber_solid = new G4Tubs(G4String(GetName() + std::string("_fiber") + id),
0, _geom->get_fiber_outer_r() * cm, length / 2.0, 0, twopi);
G4Material *clading_mat = GetDetectorMaterial(_geom->get_fiber_clading_mat());
assert(clading_mat);
G4LogicalVolume *fiber_logic = new G4LogicalVolume(fiber_solid, clading_mat,
G4String(G4String(GetName() + std::string("_fiber") + id)), 0, 0,
nullptr);
{
GetDisplayAction()->AddVolume(fiber_logic, "Fiber");
}
G4Tubs *core_solid = new G4Tubs(
G4String(GetName() + std::string("_fiber_core") + id), 0,
_geom->get_fiber_core_diameter() * cm / 2, length / 2.0, 0, twopi);
G4Material *core_mat = GetDetectorMaterial(_geom->get_fiber_core_mat());
assert(core_mat);
G4LogicalVolume *core_logic = new G4LogicalVolume(core_solid, core_mat,
G4String(G4String(GetName() + std::string("_fiber_core") + id)), 0, 0,
fiber_core_step_limits);
{
GetDisplayAction()->AddVolume(core_logic, "FiberCore");
}
const bool overlapcheck_fiber = OverlapCheck() and (Verbosity() >= 3);
G4PVPlacement *core_physi = new G4PVPlacement(0, G4ThreeVector(), core_logic,
G4String(G4String(GetName() + std::string("_fiber_core") + id)), fiber_logic,
false, 0, overlapcheck_fiber);
fiber_core_vol[core_physi] = 0;
return fiber_logic;
}
void PHG4SpacalDetector::Print(const std::string & /*what*/) const
{
std::cout << "PHG4SpacalDetector::Print::" << GetName() << " - Print Geometry:" << std::endl;
_geom->Print();
return;
}