DD4hep  1.28.0
Detector Description Toolkit for High Energy Physics
HexGrid.cpp
Go to the documentation of this file.
1 /*
2  * AIDA Detector description implementation
3  *
4  * Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5  * All rights reserved.
6  *
7  * For the licensing terms see $DD4hepINSTALL/LICENSE.
8  * For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9  *
10  * HexGrid.cpp
11  *
12  * Created on: August 9, 2023
13  * Author: Sebouh J. Paul, UC Riverside
14  */
15 #include "DD4hep/Factories.h"
16 #include "DDSegmentation/HexGrid.h"
17 
18 namespace dd4hep {
19  namespace DDSegmentation {
20 
22  HexGrid::HexGrid(const std::string& cellEncoding) :
23  Segmentation(cellEncoding) {
24  _type = "HexGridXY";
25  _description = "Hexagonal segmentation in the local XY-plane";
26 
27  // register all necessary parameters
28  registerParameter("stagger", "stagger mode", _stagger, 1);
29  registerParameter("side_length", "Cell size", _sideLength, 1., SegmentationParameter::LengthUnit);
30  registerParameter("offset_x", "Cell offset in X", _offsetX, 0., SegmentationParameter::LengthUnit, true);
31  registerParameter("offset_y", "Cell offset in Y", _offsetY, 0., SegmentationParameter::LengthUnit, true);
32  registerIdentifier("identifier_x", "Cell ID identifier for X", _xId, "x");
33  registerIdentifier("identifier_y", "Cell ID identifier for Y", _yId, "y");
34  registerParameter("stagger_keyword", "Volume ID identifier used for determining which volumes to stagger", _staggerKeyword, (std::string)"layer", SegmentationParameter::NoUnit, true);
35  }
36 
38  HexGrid::HexGrid(const BitFieldCoder* decode) : Segmentation(decode) {
39  // define type and description
40  _type = "HexGridXY";
41  _description = "Hexagonal segmentation in the local XY-plane";
42 
43  // register all necessary parameters
44  registerParameter("stagger", "stagger mode", _stagger, 1);
45  registerParameter("side_length", "Cell size", _sideLength, 1., SegmentationParameter::LengthUnit);
46  registerParameter("offset_x", "Cell offset in X", _offsetX, 0., SegmentationParameter::LengthUnit, true);
47  registerParameter("offset_y", "Cell offset in Y", _offsetY, 0., SegmentationParameter::LengthUnit, true);
48  registerIdentifier("identifier_x", "Cell ID identifier for X", _xId, "x");
49  registerIdentifier("identifier_y", "Cell ID identifier for Y", _yId, "y");
50  registerParameter("stagger_keyword", "Volume ID identifier used for determining which volumes to stagger", _staggerKeyword, (std::string)"layer", SegmentationParameter::NoUnit, true);
51 
52  }
53 
56  }
57 
59  Vector3D HexGrid::position(const CellID& cID) const {
60  int layer=0;
61  if (_stagger) layer= _decoder->get(cID,_staggerKeyword);
62 
63  Vector3D cellPosition;
64  cellPosition.X = _decoder->get(cID,_xId )*1.5*_sideLength+_offsetX+_sideLength/2.;
65  cellPosition.Y = _decoder->get(cID,_yId )*std::sqrt(3)/2.*_sideLength+ _offsetY+_sideLength*std::sqrt(3)/2.;
66  if (_stagger==0)
67  cellPosition.X+=_sideLength;
68  else if (_stagger==1)
69  cellPosition.X+=(layer%3)*_sideLength;
70  else if (_stagger==2){
71  switch (layer%4){
72  case 0:
73  cellPosition.X-=0.75*_sideLength;
74  break;
75  case 1:
76  cellPosition.Y+=std::sqrt(3)/4*_sideLength;
77  break;
78  case 2:
79  cellPosition.Y-=std::sqrt(3)/4*_sideLength;
80  break;
81  case 3:
82  cellPosition.X+=0.75*_sideLength;
83  break;
84  }
85  }
86  return cellPosition;
87  }
88 
89  inline double positive_modulo(double i, double n) {
90  return std::fmod(std::fmod(i,n) + n,n);
91  }
92 
93 
95  CellID HexGrid::cellID(const Vector3D& localPosition, const Vector3D& /* globalPosition */, const VolumeID& vID) const {
96  CellID cID = vID ;
97  int layer=0;
98  if (_stagger) layer= _decoder->get(cID,_staggerKeyword);
99 
100  double x=localPosition.X-_offsetX;
101  double y=localPosition.Y-_offsetY;
102  if (_stagger==0)
103  x-=_sideLength;
104  else if (_stagger==1)
105  x-=(layer%3)*_sideLength;
106  else if (_stagger==2){
107  switch (layer%4){
108  case 0:
109  x+=0.75*_sideLength;
110  break;
111  case 1:
112  y-=std::sqrt(3)/4*_sideLength;
113  break;
114  case 2:
115  y+=std::sqrt(3)/4*_sideLength;
116  break;
117  case 3:
118  x-=0.75*_sideLength;
119  break;
120  }
121  }
122 
123  double a=positive_modulo(y/(std::sqrt(3)*_sideLength),1);
124  double b=positive_modulo(x/(3*_sideLength),1);
125  int ix = std::floor(x/(3*_sideLength/2.))+
126  (b<0.5)*(-std::abs(a-.5)<(b-.5)*3)+(b>0.5)*(std::abs(a-.5)-.5<(b-1)*3);
127  int iy=std::floor(y/(std::sqrt(3)*_sideLength/2.));
128  iy-=(ix+iy)&1;
129 
130  _decoder->set( cID,_xId, ix );
131  _decoder->set( cID,_yId, iy );
132  return cID ;
133  }
134 
135  std::vector<double> HexGrid::cellDimensions(const CellID&) const {
136 #if __cplusplus >= 201103L
137  return {2*_sideLength, std::sqrt(3)*_sideLength};
138 #else
139  std::vector<double> cellDims(2,0.0);
140  cellDims[0] = 2*_sideLength;
141  cellDims[1] = std::sqrt(3)*_sideLength;
142  return cellDims;
143 #endif
144 }
145 
146  } /* namespace DDSegmentation */
147 } /* namespace dd4hep */
dd4hep::DDSegmentation::VolumeID
uint64_t VolumeID
Definition: BitFieldCoder.h:27
dd4hep::DDSegmentation::HexGrid::_staggerKeyword
std::string _staggerKeyword
the keyword used to determine which volumes to stagger
Definition: HexGrid.h:126
dd4hep::DDSegmentation::HexGrid::_yId
std::string _yId
the field name used for Y
Definition: HexGrid.h:124
dd4hep::DDSegmentation::positive_modulo
double positive_modulo(double i, double n)
Definition: HexGrid.cpp:89
dd4hep::DDSegmentation::Segmentation::_decoder
const BitFieldCoder * _decoder
The cell ID encoder and decoder.
Definition: Segmentation.h:168
dd4hep::DDSegmentation::Vector3D
Simple container for a physics vector.
Definition: Segmentation.h:48
dd4hep::DDSegmentation::SegmentationParameter::LengthUnit
@ LengthUnit
Definition: SegmentationParameter.h:112
dd4hep::DDSegmentation::BitFieldCoder
Helper class for decoding and encoding a bit field of 64bits for convenient declaration.
Definition: BitFieldCoder.h:114
dd4hep::DDSegmentation::BitFieldCoder::get
FieldID get(CellID bitfield, size_t idx) const
Definition: BitFieldCoder.h:165
dd4hep::DDSegmentation::HexGrid::position
virtual Vector3D position(const CellID &cellID) const
determine the position based on the cell ID
Definition: HexGrid.cpp:59
Factories.h
dd4hep::DDSegmentation::HexGrid::HexGrid
HexGrid(const std::string &cellEncoding="")
Default constructor used by derived classes passing the encoding string.
Definition: HexGrid.cpp:22
dd4hep::DDSegmentation::SegmentationParameter::NoUnit
@ NoUnit
Definition: SegmentationParameter.h:112
HexGrid.h
dd4hep::DDSegmentation::Segmentation::registerParameter
void registerParameter(const std::string &nam, const std::string &desc, TYPE &param, const TYPE &defaultVal, UnitType unitTyp=SegmentationParameter::NoUnit, bool isOpt=false)
Add a parameter to this segmentation. Used by derived classes to define their parameters.
Definition: Segmentation.h:138
dd4hep::DDSegmentation::HexGrid::_stagger
int _stagger
the stagger mode: 0=off ; 1=cycle through 3 different offsets (H3)
Definition: HexGrid.h:114
dd4hep::DDSegmentation::HexGrid::_xId
std::string _xId
the field name used for X
Definition: HexGrid.h:122
dd4hep::DDSegmentation::Vector3D::Y
double Y
Definition: Segmentation.h:71
dd4hep::DDSegmentation::BitFieldCoder::set
void set(CellID &bitfield, size_t idx, FieldID value) const
Definition: BitFieldCoder.h:177
dd4hep::DDSegmentation::HexGrid::cellID
virtual CellID cellID(const Vector3D &localPosition, const Vector3D &globalPosition, const VolumeID &volumeID) const
determine the cell ID based on the position
Definition: HexGrid.cpp:95
dd4hep::DDSegmentation::CellID
uint64_t CellID
Definition: BitFieldCoder.h:26
dd4hep::DDSegmentation::Segmentation::registerIdentifier
void registerIdentifier(const std::string &nam, const std::string &desc, std::string &ident, const std::string &defaultVal)
Add a cell identifier to this segmentation. Used by derived classes to define their required identifi...
Definition: Segmentation.cpp:125
dd4hep::DDSegmentation::Segmentation::_description
std::string _description
The description of the segmentation.
Definition: Segmentation.h:162
dd4hep::DDSegmentation::HexGrid::cellDimensions
virtual std::vector< double > cellDimensions(const CellID &cellID) const
Returns a vector<double> of the cellDimensions of the given cell ID in natural order of dimensions,...
Definition: HexGrid.cpp:135
dd4hep::DDSegmentation::HexGrid::_sideLength
double _sideLength
the length of one side of a hexagon
Definition: HexGrid.h:116
dd4hep::DDSegmentation::Vector3D::X
double X
Definition: Segmentation.h:71
dd4hep::DDSegmentation::HexGrid::~HexGrid
virtual ~HexGrid()
Destructor.
Definition: HexGrid.cpp:55
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DDSegmentation::Segmentation::_type
std::string _type
The segmentation type.
Definition: Segmentation.h:160
dd4hep::DDSegmentation::HexGrid::_offsetY
double _offsetY
the coordinate offset in Y
Definition: HexGrid.h:120
dd4hep::DDSegmentation::Segmentation
Base class for all segmentations.
Definition: Segmentation.h:75
dd4hep::DDSegmentation::HexGrid::_offsetX
double _offsetX
the coordinate offset in X
Definition: HexGrid.h:118