DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TiledLayerSegmentation.cpp
Go to the documentation of this file.
1 /*
2  * TiledLayerSegmentation.cpp
3  *
4  * Created on: Mar 10, 2014
5  * Author: cgrefe
6  */
7 
9 
10 // C/C++ includes
11 #include <algorithm>
12 #include <sstream>
13 #include <stdexcept>
14 
15 namespace DD4hep {
16 namespace DDSegmentation {
17 
18 using std::find;
19 using std::runtime_error;
20 using std::stringstream;
21 using std::vector;
22 
23 TiledLayerSegmentation::TiledLayerSegmentation(const std::string& cellEncoding) :
24  Segmentation(cellEncoding) {
25  _type = "TiledLayerSegmentation";
26  _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
27 
28  // register all necessary parameters
29  registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
30  registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
31  registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
32  registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
33  registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
35  registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
37  registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
39  registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
41 
42 }
43 
46  _type = "TiledLayerSegmentation";
47  _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
48 
49  // register all necessary parameters
50  registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
51  registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
52  registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
53  registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
54  registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
56  registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
58  registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
60  registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
62 
63 }
64 
66 }
67 
69 double TiledLayerSegmentation::layerGridSizeX(int layerIndex) const {
70  // should be cached in a map if calculateOptimalCellSize is expensive
72 }
73 
75 double TiledLayerSegmentation::layerGridSizeY(int layerIndex) const {
76  // should be cached in a map if calculateOptimalCellSize is expensive
78 }
79 
81 void TiledLayerSegmentation::setLayerDimensions(int layerIndex, double x, double y) {
82  // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
83  if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
84  throw runtime_error(
85  "TiledLayerSegmentation::setLayerDimensions: inconsistent size of layer parameter vectors.");
86  }
87  vector<int>::iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
88  if (it == _layerIndices.end()) {
89  _layerIndices.push_back(layerIndex);
90  _layerDimensionsX.push_back(x);
91  _layerDimensionsY.push_back(y);
92  } else {
93  size_t index = it - _layerIndices.begin();
94  _layerDimensionsX[index] = x;
95  _layerDimensionsY[index] = y;
96  }
97 }
98 
101  // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
102  if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
103  throw runtime_error(
104  "TiledLayerSegmentation::layerDimensions: inconsistent size of layer parameter vectors.");
105  }
106  vector<int>::const_iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
107  if (it == _layerIndices.end()) {
108  stringstream message;
109  message << "TiledLayerSegmentation::layerDimensions: invalid layer index " << layerIndex;
110  throw runtime_error(message.str());
111  } else {
112  size_t index = it - _layerIndices.begin();
113  return LayerDimensions(_layerDimensionsX[index], _layerDimensionsY[index]);
114  }
115 }
116 
119  _decoder->setValue(cID);
120  int layerIndex = (*_decoder)[_identifierLayer];
121  double cellSizeX = layerGridSizeX(layerIndex);
122  double cellSizeY = layerGridSizeY(layerIndex);
123  LayerDimensions dimensions = layerDimensions(layerIndex);
124  double offsetX = calculateOffset(cellSizeX, dimensions.x);
125  double offsetY = calculateOffset(cellSizeY, dimensions.y);
126  double localX = binToPosition((*_decoder)[_identifierX], cellSizeX, offsetX);
127  double localY = binToPosition((*_decoder)[_identifierY], cellSizeY, offsetY);
128  return Vector3D(localX, localY, 0.);
129 }
131  CellID TiledLayerSegmentation::cellID(const Vector3D& localPosition, const Vector3D& /* globalPosition */,
132  const VolumeID& vID) const {
133  _decoder->setValue(vID);
134  int layerIndex = (*_decoder)[_identifierLayer];
135  double cellSizeX = layerGridSizeX(layerIndex);
136  double cellSizeY = layerGridSizeY(layerIndex);
137  LayerDimensions dimensions = layerDimensions(layerIndex);
138  double offsetX = calculateOffset(cellSizeX, dimensions.x);
139  double offsetY = calculateOffset(cellSizeY, dimensions.y);
140  (*_decoder)[_identifierX] = positionToBin(localPosition.x(), cellSizeX, offsetX);
141  (*_decoder)[_identifierY] = positionToBin(localPosition.y(), cellSizeY, offsetY);
142  return _decoder->getValue();
143 }
144 
146  double TiledLayerSegmentation::calculateOptimalCellSize(double /* nominalCellSize */, double /* totalSize */) {
147  // TODO: implement algorithm to calculate optimal cell size
148  return 1.;
149 }
150 
152 double TiledLayerSegmentation::calculateOffset(double /* cellSize */, double /* totalSize */) {
153  // TODO: implement algorithm to calculate placement of bin 0
154  return 0.;
155 }
156 
157 } /* namespace DDSegmentation */
158 } /* namespace DD4hep */
std::string _identifierY
encoding field used for X
std::vector< double > _layerDimensionsX
list of valid layer identifiers
Helper class to store x and y dimensions of a layer.
long long int CellID
Useful typedefs to differentiate cell IDs and volume IDs.
Definition: Primitives.h:32
std::vector< int > _layerIndices
encoding field used for the layer
static double calculateOptimalCellSize(double nominalCellSize, double totalSize)
list of layer y dimensions
double x() const
Access to x value (required for use with ROOT GenVector)
Definition: Segmentation.h:53
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...
LayerDimensions layerDimensions(int layerIndex) const
access to the dimensions of the given layer
BitField64 * _decoder
The cell ID encoder and decoder.
Definition: Segmentation.h:161
std::string _identifierX
default grid size in Y
double y() const
Access to y value (required for use with ROOT GenVector)
Definition: Segmentation.h:57
std::vector< double > _layerDimensionsY
list of layer x dimensions
static double calculateOffset(double cellSize, double totalSize)
helper method to calculate offset of bin 0 based on the total size
double layerGridSizeX(int layerIndex) const
access the actual grid size in X for a given layer
Simple container for a physics vector.
Definition: Segmentation.h:41
static T::const_iterator find(const T &c, const string &s)
Definition: View.cpp:34
std::string _description
The description of the segmentation.
Definition: Segmentation.h:155
TiledLayerSegmentation(const std::string &cellEncoding="")
Default constructor passing the encoding string.
Base class for all segmentations.
Definition: Segmentation.h:68
static int positionToBin(double position, double cellSize, double offset=0.)
Helper method to convert a 1D position to a cell ID.
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:131
virtual Vector3D position(const CellID &cellID) const
determine the position based on the cell ID
double layerGridSizeY(int layerIndex) const
access the actual grid size in Y for a given layer
std::string _type
The segmentation type.
Definition: Segmentation.h:153
long long int VolumeID
Definition: Primitives.h:35
void setLayerDimensions(int layerIndex, double x, double y)
set the dimensions of the given layer
virtual CellID cellID(const Vector3D &localPosition, const Vector3D &globalPosition, const VolumeID &volumeID) const
determine the cell ID based on the position
static double binToPosition(CellID bin, double cellSize, double offset=0.)
Helper method to convert a bin number to a 1D position.
std::string _identifierLayer
encoding field used for Y