# Design of Geometry in HERDOS
## Local Coordinate System
### Introduction
In the construction of MC detectors, it is important to use a universal local coordinate system definition rule for minimum detector units, or cells, to avoid inconsistencies between different geometry versions. With the recording of coordinates of MC hits in Geant4 simulation in local coordinate system for the minimun of unit, we could simplify the process of digitization and reconstruction.
The principle of setting up a local coordinate system is to set up the coordinate system based on proximity to the readout end.
- The local X axis along the direction of readout, e.g. strips in silicon sensors, or SiPMs of PSD & FIT
- The local Y axis along the long edge of the detector cell
- The local Z axis of outer detectors always towrds the outer direction of HERD detector
- The original point of local coordinate system always at the center of the unit
There are five sectors in each outer detector , top sector and four side sector. In the following figures, side sector means sector 1 (in +X direction). The other sectors can be conducted by rotate sector 1 along z axis direction.

### CALO
The local coordinate system for CALO is relatively simple. The local coordinate origin is set at the center of the crystal, and its orientation is aligned with the global coordinate system.

### PSD
PSD has two types, which are:
- bar PSD
- tile PSD
#### bar PSD
PSD bar is readout from both ends. To simplify the process, the top and side sector use the same coordinate system definition.

As shown in the figure, all unit in the same column(horizontal or vertical) share same local coordinate direction definition.
#### tile PSD
The local coordinate system definition of all unit in same sector is the same.
Similar to bar PSD, the oritation in each side sector are slightly different.
### SCD
The readout are located at outer end of SCD ladders, therefore the local coordinate system of most ladders are different in the same column. See the following figure for details.
### FIT
The readouts in the top sector are on the right side and the upper side. The readout in the side are on the left side and the lower side.
## Detector Cell Numbering
A unified numbering system is designed in order to ease the identifying of detector geometry concerning the sensitive detector cells which corresponds to detector readout units in actual detector.
This system defines, a specified `systemid` for each type of sub-detector, and for each detector cell, a serial of numbers, and a combined number called `cellcode` for easier human reading and storage. The serial of numbers are designed taking into account the actual detector hierarchical structure, e.g. sector, layer, ladder, sensor structure for silicon detector, therefore a sensor would have 4 numbers as its ID. Finally these ID numbers have to be combined into a single number for storage. For easier human reading the combining is decimal, so an id of `1,2,3,4` would become `cellcode 1020304`. This way one can easily split the number by eye and knows the number of each part.
Note that `cellcode` alone should only be used inside a sub detector. That's because different sub detector have different hierarchical structure leading to different length of `cellcode` variable, it's not practical to also include also the `systemid` inside it. Also the data are stored separately for different sub-detectors, so no need to make `cellcode` identical for cells in different sub-detectors.
Another part of the numbering system is to define the numbering rule for each level of structure, e.g. layer, ladder, etc. Due to different structure of each sub-detector, the rule can be different. But the convention shall be the same.
- Sectors of outer detectors are numbered the same way as mentioned in the above [section](#Local-Coordinate-System) , i.e. top sector is 0, side sector at +X is 1, etc.
- Layers of outer detectors are numbered starting from the outside of HERD detector. Therefore the outer most layer of each sub-detector is layer `0`
- Other structures, e.g. ladders, mats, sensor, are numbered in a way that larger numbers corresponds to larger values of global coordinate. Numbering of CALO crystal also follows this rule.
### CALO
Each crystal has three numbers: ix, iy and iz starting from 0, which represents the layer number of the corresponding direction. Since the crystal arrangement, some combinations of ix,iy,iz do not exist. The number of crystals increase in the direction of increasing global coordinates. And there is a global number: $\rm{cellcode=iz\times10000+iy\times100+ix}$ which is easy to read and suitable for general use,and a global index$$\rm{index=iz\times23^2+iy\times23+ix}$$
which is compact, can be used for array indexing and bidirectional conversion function is provided.

### FIT
FIT is numbered accrocding to the level of sector, layer, mat:
The sector number of the top FIT is 0. In the global coordinate system, the sector number of the side FIT increase counterclockwise along z-axis from 1 to 4, and the sector number of the FIT in the positive direction of the x-axis is 1.
The layer number increases sequentially from outside to inside from 0.
The mat number increase in the direction of increasing global coordinates from 0.
$\rm{cellcode=10000\times sector+100\times layer+mat}$
for example 00103 means sector 0, layer 1, mat 3.

### PSD
#### short bar PSD
short bar PSD is numbered according to the level of sector, layer, col, row:
The sector number of the top PSD is 0. In the global coordinate system, the sector number of the side PSD increase counterclockwise along z-axis from 1 to 4, and the sector number of the PSD in the positive direction of the x-axis is 1.
The layer number increases sequentially from outside to inside from 0.
In each layer , column is defined along long edge of bars, row is defined along the other direction.The col number and row number increase in the direction of increasing global coordinates from 0.
$\rm{cellcode=10^8\times type+10^6\times sector+10^4\times layer+10^2\times col+row}$
type represents the geometric type of PSD(0:long bar, 1:short bar, 2:tile)

#### long bar PSD
long bar PSD is numbered similarly as the short bar PSD :
The overlapping layers in the same direction are numbered alternately, bar0 is on the layer with smaller layer number(outside).See the following figure.
The long bar PSD has the same cellcode formular with short bar PSD, col=bar, row=0.

#### tile PSD
Tile PSD has the similar coding rules with short bar PSD. In the following figure, the first number is column and the second one is row.

### SCD
SCD is numbered according to the level of sector, layer, ladder, sensor:
The sector number of the top SCD is 0. In the global coordinate system, the sector number of the side SCD increase counterclockwise along z-axis from 1 to 4, and the sector number of the SCD in the positive direction of the x-axis is 1.
The layer number increases sequentially from outside to inside from 0.
There are one or two ladders in each column.The ladder number increase in the direction of increasing global coordinates.
The sensor number of each ladder increase in the direction of increasing global coordinates.
$\rm{cellcode=10^6\times sector+10^4\times layer+10^2\times ladder+ sensor}$

If there are one ladder in each column , see in the following figure.
