Warning, /tutorial-geometry-development-using-dd4hep/_episodes/01-defining-geometry.md is written in an unsupported language. File is not indexed.
0001 ---
0002 title: "Geometry Definition"
0003 teaching: 15
0004 exercises: 10
0005 questions:
0006 - "How do we define geometry using DD4hep?"
0007 objectives:
0008 - "Know where standard geometries as stored in `eic-shell`."
0009 - "Understand the structure of a geometry description file."
0010 keypoints:
0011 - "Compact XML files are used to store parameters which are used by compiled plugins."
0012 ---
0013
0014 ## Introduction
0015
0016 The [ePIC geometry](https://github.com/eic/epic) is described using the [DD4hep toolkit](https://dd4hep.web.cern.ch/dd4hep/).
0017
0018 DD4hep (Detector Description for High Energy Physics) is a toolkit which acts as a single, central source of detector information for simulation and reconstruction of simulated and experimental data. The core DD4hep geometry description is built around the [ROOT geometry package](https://root.cern/manual/geometry/) while providing automatic conversions to other geometrical representaions, such as exporting CAD models, surfaces for acts reconstruction, material maps but most importany, [Geant4](https://geant4.web.cern.ch/docs/) for carrying out simulations.
0019
0020 DD4hep additionally provides a much simplified wrapper around running Geant4 simulations, providing standardized output from sensitive detectors. In the case of the ePIC simulation, simulated particles and tracker/calorimeter hits are saved as collections in [EDM4hep format](https://github.com/key4hep/EDM4hep) (Event Data Model for High Energy Physics) built on [podio](https://github.com/AIDASoft/podio/blob/master/doc/doc.md) (plain-old-data I/O).
0021
0022 # Lesson
0023
0024 We start the discussion of the geometry definition with an overview of the locations of geometry files, and what is included in these files.
0025
0026 ## Location of standard geometries in `eic-shell`
0027
0028 Several standard geometry versions are included in `eic-shell` under the `/opt/detector/` location. This includes (currently) at least the following:
0029 ```console
0030 $ ls -1 /opt/detector/
0031 calibrations
0032 dummyOutput.root
0033 epic-23.10.0
0034 epic-23.11.0
0035 epic-23.12.0
0036 epic-24.02.0
0037 epic-24.02.1
0038 epic-24.03.0
0039 epic-24.03.1
0040 epic-24.04.0
0041 epic-main
0042 fieldmaps
0043 gdml
0044 lib
0045 share
0046 ```
0047
0048 > Note: `ls -1` lists the files with 1 file per line, i.e. in 1 column.
0049 {: .callout}
0050
0051 The versions avaliable in eic-shell are updated when tagged releases for the geometry are made each month or an update to dependancies installed in eic-shell removes back compatibility with older versions.
0052
0053 The `epic-main` directory contains the current 'nightly build' of the ePIC geometry, built from the [epic repositories main branch](https://github.com/eic/epic/) every day.
0054 ```console
0055 $ ls -1 /opt/detector/epic-main/
0056 bin
0057 lib
0058 share
0059 $ ls -1 /opt/detector/epic-main/bin
0060 thisepic.sh
0061 ```
0062
0063 You can load a geometry by 'sourcing' the `bin/thisepic.sh` file.
0064 ```console
0065 $ source /opt/detector/epic-main/bin/thisepic.sh
0066 ```
0067 Both commands should have the same effect:
0068 - your prompt should have changed to `main> ` to indicate the geometry that is loaded,
0069 - your shell environment will have the necessary variables loaded to work with the `epic-main` geometry.
0070
0071 You can verify the latter by investigating the values of several environment variables:
0072 - `DETECTOR` is the name of the detector geometry that is loaded (`epic`),
0073 - `DETECTOR_VERSION` is the version (i.e. GitHub branch or tag) that is loaded (`main`),
0074 - `DETECTOR_CONFIG` is the detector configuration to use (i.e. whether to include MRICH or PFRICH, SciGlass or imaging ECAL),
0075 - `DETECTOR_PATH` is the location that points to the geometry resources (`/opt/detector/epic-main/share/epic`).
0076
0077 > Note: When working on the geometry in your own git branch you will need to source the setup.sh present in the local install directory.
0078 {: .callout}
0079
0080 > Exercise:
0081 > - Load the standard ePIC geometry and verify (with e.g. `echo $DETECTOR`) that the environment variables are set.
0082 > - Load another geometry and verify that the environment variables are indeed different.
0083 {: .challenge}
0084
0085 ## What is stored at those locations?
0086
0087 We will now take a look in the directory pointed to with the environment variable `$DETECTOR_PATH`, the location of the geometry resources:
0088 ```console
0089 $ ls $DETECTOR_PATH
0090 calibrations epic_craterlake_tracking_only.xml epic_lfhcal_with_insert.xml
0091 compact epic_craterlake.xml epic_mrich_only.xml
0092 dummyOutput.root epic_dirc_only.xml epic_pfrich_only.xml
0093 epic_bhcal.xml epic_drich_only.xml epic_pid_only.xml
0094 epic_calorimeters.xml epic_forward_detectors_with_inserts.xml epic_tof_endcap_only.xml
0095 epic_craterlake_10x100.xml epic_forward_detectors.xml epic_tof_only.xml
0096 epic_craterlake_10x275.xml epic_full.xml epic_vertex_only.xml
0097 epic_craterlake_18x110_Au.xml epic_imaging_only.xml epic.xml
0098 epic_craterlake_18x275.xml epic_inner_detector.xml epic_zdc_lyso_sipm.xml
0099 epic_craterlake_5x41.xml epic_ip6_extended.xml epic_zdc_sipm_on_tile_only.xml
0100 epic_craterlake_material_map.xml epic_ip6.xml fieldmaps
0101 epic_craterlake_no_bhcal.xml epic_lfhcal_only.xml gdml
0102 ```
0103 You will see many xml files, all of which are entry points to the geometry in certain configurations. For example, `epic_drich_only.xml` includes the geometry that has only the dual RICH or dRICH. `epic_ip6.xml` includes the beampipe geometry and the auxillary far-forward and backward detectors but no components of the central detector. The default configuration, `epic.xml`, is typically the configuration you will want to use, this is the value that `DETECTOR_CONFIG` will be set to by default.
0104
0105 > Note: The current nightly eic-shell build contains only configurations for the craterlake detector setup.
0106 {: .callout}
0107
0108 Let's take a look in *the default entry point file*, pointed at by the `DETECTOR_CONFIG` environment variable. This is the file `epic.xml`:
0109 ```console
0110 $ less $DETECTOR_PATH/$DETECTOR_CONFIG.xml
0111 ```
0112 (Note: Use `q` to exit `less`, or use any editor you prefer.)
0113
0114 The xml file includes several blocks, but look in particular for the following lines:
0115 - `<include ref="${DETECTOR_PATH}/compact/definitions.xml"/>`: This line includes the overall detector parametrization file (think of this as a detector parameter table similar to what the EIC Menagerie provides).
0116 - `<include ref="${DETECTOR_PATH}/compact/tracking/vertex_barrel.xml"/>`: This line includes one of the tracker subsystems; there are other include lines that load other tracking subsystems, or even other types of subsystems.
0117 - `<include ref="${DETECTOR_PATH}/compact/far_forward/default.xml"/>`: This line includes the far forward subsystems.
0118
0119 These included files (e.g. `far_forward/default.xml`) can include further nested inclusion of even more files (e.g. `far_forward/ZDC.xml`).
0120
0121 > Note: The xml files are parsed in order, so the `definitions.xml` file needs to be included before any file which needs to access the defined parameters.
0122 {: .callout}
0123
0124 Let's now take a look at *a particular detector subsystem end point file* (which does not include any more files), namely `tracking/vertex_barrel.xml`.
0125 ```console
0126 $ less $DETECTOR_PATH/compact/tracking/vertex_barrel.xml
0127 ```
0128 You will notice that the detector is described by parameters in a `define` block, such as (abridged):
0129 ```xml
0130 <define>
0131 <constant name="VertexBarrelMod_length" value="VertexBarrel_length"/>
0132 <constant name="VertexBarrelMod_rmin" value="VertexBarrel_rmin"/>
0133
0134 <constant name="SiVertexSensor_thickness" value="40*um"/>
0135 </define>
0136 ```
0137 which can either use another parameter defined previously in the included files, or which can be defined in this file itself. A best practices is to define detailed parameters of each subsystem in the end point file, but to defer to the central definitions in the `definitions.xml` for quantities such as the overal size and location of the subsystem, or interfaces with other subsystems.
0138
0139 The parameters are then used in the `detector` block to define the detector itself (much abridged):
0140 ```xml
0141 <detectors>
0142 <detector
0143 id="VertexBarrel_0_ID"
0144 name="VertexBarrel"
0145 type="epic_VertexBarrel"
0146 readout="VertexBarrelHits"
0147 insideTrackingVolume="true">
0148 <dimensions
0149 rmin="VertexBarrelLayer1_rmin"
0150 rmax="VertexBarrelLayer3_rmax"
0151 length="VertexBarrelEnvelope_length" />
0152 </detector>
0153 </detectors>
0154 ```
0155
0156 The parametrization of the entire detector, down to the subsystems, is defined in these xml files. But where are the volumes created? The key here is the `type` field, which points to the detector type *plugin* that interprets the parametrization (here the type is `epic_VertexBarrel`). A well-written detector plugin can support many different detector configurations and parametrizations without the need to ever touch a line of code.
0157
0158 > Exercise:
0159 > - Identify which subsystem or detector you are interested in.
0160 > - Take a look in the `epic.xml` file and locate where this detector is included.
0161 > - Locate the end point file that defines the parameters that describe this file.
0162 > - Identify the detector plugin that is used for this detector.
0163 {: .challenge}