1 package org.lcsim.detector.converter.compact;
2
3 import static java.lang.Math.cos;
4 import static java.lang.Math.sin;
5
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9
10 import org.jdom.DataConversionException;
11 import org.jdom.Element;
12 import org.lcsim.detector.IDetectorElement;
13 import org.lcsim.detector.ILogicalVolume;
14 import org.lcsim.detector.IPhysicalVolume;
15 import org.lcsim.detector.IPhysicalVolumePath;
16 import org.lcsim.detector.IRotation3D;
17 import org.lcsim.detector.ITranslation3D;
18 import org.lcsim.detector.LogicalVolume;
19 import org.lcsim.detector.PhysicalVolume;
20 import org.lcsim.detector.RotationPassiveXYZ;
21 import org.lcsim.detector.Transform3D;
22 import org.lcsim.detector.Translation3D;
23 import org.lcsim.detector.DetectorIdentifierHelper.SystemMap;
24 import org.lcsim.detector.RotationGeant;
25 import org.lcsim.detector.identifier.ExpandedIdentifier;
26 import org.lcsim.detector.identifier.IIdentifier;
27 import org.lcsim.detector.identifier.IIdentifierDictionary;
28 import org.lcsim.detector.identifier.IIdentifierHelper;
29 import org.lcsim.detector.identifier.IdentifierDictionaryManager;
30 import org.lcsim.detector.identifier.IdentifierUtil;
31 import org.lcsim.detector.material.IMaterial;
32 import org.lcsim.detector.material.MaterialStore;
33 import org.lcsim.detector.solids.Box;
34 import org.lcsim.detector.solids.ISolid;
35 import org.lcsim.detector.solids.Trd;
36 import org.lcsim.detector.solids.Tube;
37 import org.lcsim.detector.tracker.silicon.SiSensor;
38 import org.lcsim.detector.tracker.silicon.SiTrackerIdentifierHelper;
39 import org.lcsim.detector.tracker.silicon.SiTrackerLayer;
40 import org.lcsim.detector.tracker.silicon.SiTrackerModule;
41 import org.lcsim.geometry.compact.Detector;
42 import org.lcsim.geometry.compact.Subdetector;
43 import org.lcsim.geometry.subdetector.SiTrackerBarrel;
44
45 public class SiTrackerFixedTarget2ConverterBarrel extends AbstractSubdetectorConverter implements ISubdetectorConverter
46 {
47
48 public IIdentifierHelper makeIdentifierHelper( Subdetector subdetector, SystemMap systemMap )
49 {
50 return new SiTrackerIdentifierHelper( subdetector.getDetectorElement(),
51 makeIdentifierDictionary( subdetector ),
52 systemMap );
53 }
54
55 public void convert( Subdetector subdet, Detector detector )
56 {
57 Map< String, ILogicalVolume > modules = buildModules( subdet );
58
59 try
60 {
61 buildLayers( detector, subdet, modules );
62 }
63 catch ( DataConversionException x )
64 {
65 throw new RuntimeException( x );
66 }
67
68 setupSensorDetectorElements( subdet );
69 }
70
71 private Map< String, ILogicalVolume > buildModules( Subdetector subdet )
72 {
73 Map< String, ILogicalVolume > modules = new HashMap< String, ILogicalVolume >();
74
75 Element subdetElement = subdet.getNode();
76
77 for ( Iterator i = subdetElement.getChildren( "module" ).iterator(); i.hasNext(); )
78 {
79 Element module = ( Element ) i.next();
80 String module_name = module.getAttributeValue( "name" );
81 ILogicalVolume module_envelope;
82 try
83 {
84 module_envelope = buildModule( subdetElement, module_name );
85 modules.put( module_name, module_envelope );
86 }
87 catch ( Exception x )
88 {
89 throw new RuntimeException( x );
90 }
91 }
92
93 return modules;
94 }
95
96 private ILogicalVolume buildModule( Element subdetElement, String module_name ) throws Exception
97 {
98 String subdetName = subdetElement.getAttributeValue( "name" );
99 ILogicalVolume moduleLogVol = null;
100
101 Element moduleElement = null;
102 for ( Iterator i = subdetElement.getChildren( "module" ).iterator(); i.hasNext(); )
103 {
104 Element moduleCheck = ( Element ) i.next();
105 if ( moduleCheck.getAttributeValue( "name" ).compareTo( module_name ) == 0 )
106 {
107 moduleElement = moduleCheck;
108 }
109 }
110 if ( moduleElement == null )
111 {
112 throw new RuntimeException( "module <" + module_name + " was not found" );
113 }
114
115 Element moduleEnvelopeElement = moduleElement.getChild( "module_envelope" );
116
117
118 double moduleLength = moduleEnvelopeElement.getAttribute( "length" ).getDoubleValue();
119 double moduleWidth = moduleEnvelopeElement.getAttribute( "width" ).getDoubleValue();
120 double moduleThickness = moduleEnvelopeElement.getAttribute( "thickness" ).getDoubleValue();
121 ISolid moduleBox = new Box( module_name + "_box", moduleWidth / 2, moduleLength / 2, moduleThickness / 2 );
122
123
124 IMaterial air = MaterialStore.getInstance().get( "Air" );
125 moduleLogVol = new LogicalVolume( module_name, moduleBox, air );
126
127 int componentNumber = 0;
128 for ( Iterator j = moduleElement.getChildren( "module_component" ).iterator(); j.hasNext(); ++componentNumber )
129 {
130 Element componentElement = ( Element ) j.next();
131
132 boolean sensitive = ( ( componentElement.getAttribute( "sensitive" ) == null ) ? false : componentElement
133 .getAttribute( "sensitive" ).getBooleanValue() );
134
135 String componentName = module_name + "_component" + componentNumber;
136
137
138 double componentLength = componentElement.getAttribute( "length" ).getDoubleValue();
139 double componentWidth = componentElement.getAttribute( "width" ).getDoubleValue();
140 double componentThickness = componentElement.getAttribute( "thickness" ).getDoubleValue();
141 ISolid componentBox = new Box( componentName,
142 componentWidth / 2,
143 componentLength / 2,
144 componentThickness / 2 );
145
146 IMaterial componentMaterial = MaterialStore.getInstance().get(
147 componentElement.getAttributeValue( "material" ) );
148
149
150 ILogicalVolume componentLogVol = new LogicalVolume( componentName, componentBox, componentMaterial );
151
152
153 double px = 0, py = 0, pz = 0;
154
155 if ( componentElement.getChild( "position" ) != null )
156 {
157 Element pos_elem = componentElement.getChild( "position" );
158
159 if ( pos_elem.getAttribute( "x" ) != null )
160 {
161 px = pos_elem.getAttribute( "x" ).getDoubleValue();
162 }
163
164 if ( pos_elem.getAttribute( "y" ) != null )
165 {
166 py = pos_elem.getAttribute( "y" ).getDoubleValue();
167 }
168
169 if ( pos_elem.getAttribute( "z" ) != null )
170 {
171 pz = pos_elem.getAttribute( "z" ).getDoubleValue();
172 }
173 }
174
175 ITranslation3D pos = new Translation3D( px, py, pz );
176
177
178 double rx = 0, ry = 0, rz = 0;
179
180 if ( componentElement.getChild( "rotation" ) != null )
181 {
182 Element rot_elem = componentElement.getChild( "rotation" );
183
184 if ( rot_elem.getAttribute( "x" ) != null )
185 {
186 rx = rot_elem.getAttribute( "x" ).getDoubleValue();
187 }
188
189 if ( rot_elem.getAttribute( "y" ) != null )
190 {
191 ry = rot_elem.getAttribute( "y" ).getDoubleValue();
192 }
193
194 if ( rot_elem.getAttribute( "z" ) != null )
195 {
196 rz = rot_elem.getAttribute( "z" ).getDoubleValue();
197 }
198 }
199
200 IRotation3D rot = new RotationPassiveXYZ( rx, ry, rz );
201
202
203 Transform3D componentTransform = new Transform3D( pos, rot );
204
205 PhysicalVolume componentPhysVol = new PhysicalVolume( componentTransform,
206 componentName,
207 componentLogVol,
208 moduleLogVol,
209 componentNumber );
210
211 if ( sensitive )
212 {
213 componentPhysVol.setSensitive( true );
214 }
215
216 ++componentNumber;
217 }
218
219 return moduleLogVol;
220 }
221
222 private void buildLayers( Detector detector, Subdetector subdet, Map< String, ILogicalVolume > modules ) throws DataConversionException
223 {
224 Element node = subdet.getNode();
225 String detector_name = subdet.getName();
226
227
228
229 for ( Iterator i = node.getChildren( "layer" ).iterator(); i.hasNext(); )
230 {
231
232 Element layer_element = ( Element ) i.next();
233
234 int layern = layer_element.getAttribute( "id" ).getIntValue();
235
236
237 String module_name = layer_element.getAttributeValue( "module" );
238
239
240 ILogicalVolume moduleEnvelope = modules.get( module_name );
241
242
243 Element barrel_envelope = layer_element.getChild( "barrel_envelope" );
244
245
246 double ir = barrel_envelope.getAttribute( "inner_x" ).getDoubleValue();
247
248
249 double or = barrel_envelope.getAttribute( "outer_x" ).getDoubleValue();
250
251
252 double oz = barrel_envelope.getAttribute( "z_length" ).getDoubleValue();
253 double oy = barrel_envelope.getAttribute( "y_length" ).getDoubleValue();
254
255
256 String layer_name = detector_name + "_layer" + layern;
257
258
259
260 double dx1, dx2;
261 dx1 = dx2 = ( or - ir ) / 2;
262 Trd layer_trd = new Trd( layer_name + "_trd", dx1, dx2, oy, oy, oz );
263 Translation3D p = new Translation3D( ir, 0, 0 );
264 RotationGeant rot = new RotationGeant( 0, 0, -Math.PI / 2 );
265 Transform3D trans = new Transform3D( p, rot );
266 Tube layer_tube = new Tube( layer_name + "_tube", ir, or, oz / 2 );
267
268
269 IMaterial air = MaterialStore.getInstance().get( "Air" );
270 ILogicalVolume layer_volume = new LogicalVolume( layer_name, layer_trd, air );
271
272
273 IPhysicalVolume layer_envelope_physvol = new PhysicalVolume( trans, layer_name, layer_volume, detector
274 .getTrackingVolume().getLogicalVolume(), layern );
275
276
277 String layerPath = "/tracking_region/" + layer_name;
278 IDetectorElement layerDE = new SiTrackerLayer( layer_name, subdet.getDetectorElement(), layerPath, layern );
279
280
281 Element layout = layer_element.getChild( "layout" );
282
283
284 double xTilt = layout.getAttribute( "xTilt" ).getDoubleValue();
285
286
287 int ny = layout.getAttribute( "ny" ).getIntValue();
288 assert ( ny > 0 );
289
290 int nz = layout.getAttribute( "nz" ).getIntValue();
291 assert ( nz > 0 );
292
293
294 double xCent = layout.getAttribute( "xCent" ).getDoubleValue();
295
296
297 double zGap = layout.getAttribute( "zgap" ).getDoubleValue();
298
299
300 double dx = 0.0;
301 if ( layout.getAttribute( "dx" ) != null )
302 {
303 dx = layout.getAttribute( "dx" ).getDoubleValue();
304 }
305
306
307
308
309
310 double phic = 0;
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 int moduleId = 0;
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 for ( int zcount = 0; zcount < nz; zcount++ )
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 }
428 }
429 }
430
431 public Class getSubdetectorType()
432 {
433 return SiTrackerFixedTarget2ConverterBarrel.class;
434 }
435
436 private void setupSensorDetectorElements( Subdetector subdet )
437 {
438 int moduleId = 0;
439 for ( IDetectorElement layer : subdet.getDetectorElement().getChildren() )
440 {
441 for ( IDetectorElement module : layer.getChildren() )
442 {
443 IPhysicalVolumePath modulePath = module.getGeometry().getPath();
444
445 IPhysicalVolume modulePhysVol = modulePath.getLeafVolume();
446
447 int sensorId = 0;
448 for ( IPhysicalVolume pv : modulePhysVol.getLogicalVolume().getDaughters() )
449 {
450
451 if ( pv.isSensitive() )
452 {
453 IIdentifierDictionary iddict = IdentifierDictionaryManager.getInstance()
454 .getIdentifierDictionary( subdet.getReadout().getName() );
455
456 ExpandedIdentifier expId = new ExpandedIdentifier( iddict.getNumberOfFields() );
457
458
459 expId.setValue( iddict.getFieldIndex( "system" ), subdet.getSystemID() );
460
461
462 expId.setValue( iddict.getFieldIndex( "barrel" ), 0 );
463
464
465 expId.setValue( iddict.getFieldIndex( "layer" ), layer.getGeometry().getPath().getLeafVolume()
466 .getCopyNumber() );
467
468
469 expId.setValue( iddict.getFieldIndex( "module" ), ( ( SiTrackerModule ) module ).getModuleId() );
470
471
472 expId.setValue( iddict.getFieldIndex( "sensor" ), sensorId );
473
474
475
476 IIdentifier id = iddict.pack( expId );
477
478
479
480
481 String sensorPath = modulePath.toString() + "/" + pv.getName();
482
483
484
485 String sensorName = subdet.getName() + "_layer" + layer.getGeometry().getPhysicalVolume()
486 .getCopyNumber() + "_module" + ( ( SiTrackerModule ) module ).getModuleId() + "_sensor" + sensorId;
487
488 SiSensor sensor = new SiSensor( sensorId, sensorName, module, sensorPath, id );
489 sensor.setIdentifier( id );
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 ++sensorId;
559
560
561
562
563
564
565
566
567
568
569
570
571 }
572 }
573
574 ++moduleId;
575 }
576 }
577 }
578 }