DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
TiledLayerSegmentation.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 //==========================================================================
11 /*
12  * TiledLayerSegmentation.cpp
13  *
14  * Created on: Mar 10, 2014
15  * Author: cgrefe
16  */
17 
19 
20 // C/C++ includes
21 #include <algorithm>
22 #include <sstream>
23 #include <stdexcept>
24 
25 namespace dd4hep {
26 namespace DDSegmentation {
27 
28 using std::find;
29 using std::runtime_error;
30 using std::stringstream;
31 using std::vector;
32 
33 TiledLayerSegmentation::TiledLayerSegmentation(const std::string& cellEncoding) :
34  Segmentation(cellEncoding) {
35  _type = "TiledLayerSegmentation";
36  _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
37 
38  // register all necessary parameters
39  registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
40  registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
41  registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
42  registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
43  registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
45  registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
47  registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
49  registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
51 
52 }
53 
56  _type = "TiledLayerSegmentation";
57  _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
58 
59  // register all necessary parameters
60  registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
61  registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
62  registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
63  registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
64  registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
66  registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
68  registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
70  registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
72 
73 }
74 
76 }
77 
79 double TiledLayerSegmentation::layerGridSizeX(int layerIndex) const {
80  // should be cached in a map if calculateOptimalCellSize is expensive
82 }
83 
85 double TiledLayerSegmentation::layerGridSizeY(int layerIndex) const {
86  // should be cached in a map if calculateOptimalCellSize is expensive
88 }
89 
91 void TiledLayerSegmentation::setLayerDimensions(int layerIndex, double x, double y) {
92  // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
93  if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
94  throw runtime_error(
95  "TiledLayerSegmentation::setLayerDimensions: inconsistent size of layer parameter vectors.");
96  }
97  vector<int>::iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
98  if (it == _layerIndices.end()) {
99  _layerIndices.emplace_back(layerIndex);
100  _layerDimensionsX.emplace_back(x);
101  _layerDimensionsY.emplace_back(y);
102  } else {
103  size_t index = it - _layerIndices.begin();
104  _layerDimensionsX[index] = x;
105  _layerDimensionsY[index] = y;
106  }
107 }
108 
111  // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
112  if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
113  throw runtime_error(
114  "TiledLayerSegmentation::layerDimensions: inconsistent size of layer parameter vectors.");
115  }
116  vector<int>::const_iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
117  if (it == _layerIndices.end()) {
118  stringstream message;
119  message << "TiledLayerSegmentation::layerDimensions: invalid layer index " << layerIndex;
120  throw runtime_error(message.str());
121  } else {
122  size_t index = it - _layerIndices.begin();
123  return LayerDimensions(_layerDimensionsX[index], _layerDimensionsY[index]);
124  }
125 }
126 
129  int layerIndex = _decoder->get(cID,_identifierLayer);
130  double cellSizeX = layerGridSizeX(layerIndex);
131  double cellSizeY = layerGridSizeY(layerIndex);
133  double offsetX = calculateOffset(cellSizeX, dimensions.x);
134  double offsetY = calculateOffset(cellSizeY, dimensions.y);
135  double localX = binToPosition(_decoder->get(cID,_identifierX), cellSizeX, offsetX);
136  double localY = binToPosition(_decoder->get(cID,_identifierY), cellSizeY, offsetY);
137  return Vector3D(localX, localY, 0.);
138 }
140  CellID TiledLayerSegmentation::cellID(const Vector3D& localPosition, const Vector3D& /* globalPosition */,
141  const VolumeID& vID) const {
142  CellID cID = vID ;
143  int layerIndex = _decoder->get(cID,_identifierLayer);
144  double cellSizeX = layerGridSizeX(layerIndex);
145  double cellSizeY = layerGridSizeY(layerIndex);
147  double offsetX = calculateOffset(cellSizeX, dimensions.x);
148  double offsetY = calculateOffset(cellSizeY, dimensions.y);
149  _decoder->set(cID,_identifierX, positionToBin(localPosition.x(), cellSizeX, offsetX));
150  _decoder->set(cID,_identifierY, positionToBin(localPosition.y(), cellSizeY, offsetY));
151  return cID;
152 }
153 
155  double TiledLayerSegmentation::calculateOptimalCellSize(double /* nominalCellSize */, double /* totalSize */) {
156  // TODO: implement algorithm to calculate optimal cell size
157  return 1.;
158 }
159 
161 double TiledLayerSegmentation::calculateOffset(double /* cellSize */, double /* totalSize */) {
162  // TODO: implement algorithm to calculate placement of bin 0
163  return 0.;
164 }
165 
166 } /* namespace DDSegmentation */
167 } /* namespace dd4hep */
dd4hep::DDSegmentation::VolumeID
uint64_t VolumeID
Definition: BitFieldCoder.h:27
dd4hep::DDSegmentation::Segmentation::_decoder
const BitFieldCoder * _decoder
The cell ID encoder and decoder.
Definition: Segmentation.h:168
dd4hep::DDSegmentation::TiledLayerSegmentation::calculateOffset
static double calculateOffset(double cellSize, double totalSize)
helper method to calculate offset of bin 0 based on the total size
Definition: TiledLayerSegmentation.cpp:161
dd4hep::DDSegmentation::Vector3D
Simple container for a physics vector.
Definition: Segmentation.h:48
TiledLayerSegmentation.h
dd4hep::DDSegmentation::TiledLayerSegmentation::_gridSizeY
double _gridSizeY
default grid size in X
Definition: TiledLayerSegmentation.h:113
dd4hep::DDSegmentation::TiledLayerSegmentation::_identifierY
std::string _identifierY
encoding field used for X
Definition: TiledLayerSegmentation.h:115
dd4hep::DDSegmentation::SegmentationParameter::LengthUnit
@ LengthUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::BitFieldCoder
Helper class for decoding and encoding a bit field of 64bits for convenient declaration.
Definition: BitFieldCoder.h:114
dd4hep::dimensions
std::vector< double > dimensions(const Handle< TGeoShape > &solid)
Access Shape dimension parameters (As in TGeo, but angles in radians rather than degrees)
Definition: ShapeUtilities.cpp:467
dd4hep::DDSegmentation::TiledLayerSegmentation::~TiledLayerSegmentation
virtual ~TiledLayerSegmentation()
destructor
Definition: TiledLayerSegmentation.cpp:75
dd4hep::DDSegmentation::BitFieldCoder::get
FieldID get(CellID bitfield, size_t idx) const
Definition: BitFieldCoder.h:165
dd4hep::DDSegmentation::TiledLayerSegmentation::LayerDimensions
Helper class to store x and y dimensions of a layer.
Definition: TiledLayerSegmentation.h:35
dd4hep::DDSegmentation::TiledLayerSegmentation::layerGridSizeX
double layerGridSizeX(int layerIndex) const
access the actual grid size in X for a given layer
Definition: TiledLayerSegmentation.cpp:79
dd4hep::DDSegmentation::TiledLayerSegmentation::setLayerDimensions
void setLayerDimensions(int layerIndex, double x, double y)
set the dimensions of the given layer
Definition: TiledLayerSegmentation.cpp:91
dd4hep::DDSegmentation::TiledLayerSegmentation::cellID
virtual CellID cellID(const Vector3D &localPosition, const Vector3D &globalPosition, const VolumeID &volumeID) const
determine the cell ID based on the position
Definition: TiledLayerSegmentation.cpp:140
dd4hep::DDSegmentation::SegmentationParameter::NoUnit
@ NoUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::TiledLayerSegmentation::_gridSizeX
double _gridSizeX
Definition: TiledLayerSegmentation.h:112
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::TiledLayerSegmentation::layerDimensions
LayerDimensions layerDimensions(int layerIndex) const
access to the dimensions of the given layer
Definition: TiledLayerSegmentation.cpp:110
dd4hep::DDSegmentation::TiledLayerSegmentation::layerGridSizeY
double layerGridSizeY(int layerIndex) const
access the actual grid size in Y for a given layer
Definition: TiledLayerSegmentation.cpp:85
dd4hep::DDSegmentation::BitFieldCoder::set
void set(CellID &bitfield, size_t idx, FieldID value) const
Definition: BitFieldCoder.h:177
dd4hep::DDSegmentation::CellID
uint64_t CellID
Definition: BitFieldCoder.h:26
dd4hep::DDSegmentation::TiledLayerSegmentation::_identifierLayer
std::string _identifierLayer
encoding field used for Y
Definition: TiledLayerSegmentation.h:116
dd4hep::DDSegmentation::TiledLayerSegmentation::_identifierX
std::string _identifierX
default grid size in Y
Definition: TiledLayerSegmentation.h:114
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:135
dd4hep::DDSegmentation::TiledLayerSegmentation::_layerDimensionsX
std::vector< double > _layerDimensionsX
list of valid layer identifiers
Definition: TiledLayerSegmentation.h:118
dd4hep::DDSegmentation::Segmentation::_description
std::string _description
The description of the segmentation.
Definition: Segmentation.h:162
dd4hep::DDSegmentation::Segmentation::positionToBin
static int positionToBin(double position, double cellSize, double offset=0.)
Helper method to convert a 1D position to a cell ID.
Definition: Segmentation.cpp:150
dd4hep::DDSegmentation::TiledLayerSegmentation::calculateOptimalCellSize
static double calculateOptimalCellSize(double nominalCellSize, double totalSize)
list of layer y dimensions
Definition: TiledLayerSegmentation.cpp:155
dd4hep::DDSegmentation::TiledLayerSegmentation::position
virtual Vector3D position(const CellID &cellID) const
determine the position based on the cell ID
Definition: TiledLayerSegmentation.cpp:128
dd4hep::DDSegmentation::Segmentation::binToPosition
static double binToPosition(FieldID bin, double cellSize, double offset=0.)
Helper method to convert a bin number to a 1D position.
Definition: Segmentation.cpp:145
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::Vector3D::y
double y() const
Access to y value (required for use with ROOT GenVector)
Definition: Segmentation.h:64
dd4hep::DDSegmentation::TiledLayerSegmentation::_layerIndices
std::vector< int > _layerIndices
encoding field used for the layer
Definition: TiledLayerSegmentation.h:117
dd4hep::DDSegmentation::Segmentation
Base class for all segmentations.
Definition: Segmentation.h:75
dd4hep::DDSegmentation::Vector3D::x
double x() const
Access to x value (required for use with ROOT GenVector)
Definition: Segmentation.h:60
dd4hep::DDSegmentation::TiledLayerSegmentation::TiledLayerSegmentation
TiledLayerSegmentation(const std::string &cellEncoding="")
Default constructor passing the encoding string.
Definition: TiledLayerSegmentation.cpp:33
dd4hep::DDSegmentation::TiledLayerSegmentation::_layerDimensionsY
std::vector< double > _layerDimensionsY
list of layer x dimensions
Definition: TiledLayerSegmentation.h:119