Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 09:36:50

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 //==========================================================================
0011 /*
0012  * TiledLayerSegmentation.cpp
0013  *
0014  *  Created on: Mar 10, 2014
0015  *      Author: cgrefe
0016  */
0017 
0018 #include <DDSegmentation/TiledLayerSegmentation.h>
0019 
0020 // C/C++ includes
0021 #include <algorithm>
0022 #include <sstream>
0023 #include <stdexcept>
0024 
0025 namespace dd4hep {
0026 namespace DDSegmentation {
0027 
0028 using std::find;
0029 using std::runtime_error;
0030 using std::stringstream;
0031 using std::vector;
0032 
0033 TiledLayerSegmentation::TiledLayerSegmentation(const std::string& cellEncoding) :
0034         Segmentation(cellEncoding) {
0035     _type = "TiledLayerSegmentation";
0036     _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
0037 
0038     // register all necessary parameters
0039     registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
0040     registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
0041     registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
0042     registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
0043     registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
0044             SegmentationParameter::NoUnit, true);
0045     registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
0046             SegmentationParameter::NoUnit, true);
0047     registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
0048             SegmentationParameter::NoUnit, true);
0049     registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
0050             SegmentationParameter::NoUnit, true);
0051 
0052 }
0053 
0054 /// Default constructor used by derived classes passing an existing decoder
0055 TiledLayerSegmentation::TiledLayerSegmentation(const BitFieldCoder* decode) :   Segmentation(decode) {
0056     _type = "TiledLayerSegmentation";
0057     _description = "Cartesian segmentation using optimal tiling depending on the layer dimensions";
0058 
0059     // register all necessary parameters
0060     registerParameter("grid_size_x", "Default cell size in X", _gridSizeX, 1., SegmentationParameter::LengthUnit);
0061     registerParameter("grid_size_y", "Default cell size in Y", _gridSizeY, 1., SegmentationParameter::LengthUnit);
0062     registerIdentifier("identifier_x", "Cell encoding identifier for X", _identifierX, "x");
0063     registerIdentifier("identifier_y", "Cell encoding identifier for Y", _identifierY, "y");
0064     registerParameter("identifier_layer", "Cell encoding identifier for layer", _identifierLayer, std::string("layer"),
0065             SegmentationParameter::NoUnit, true);
0066     registerParameter("layer_identifiers", "List of valid layer identifiers", _layerIndices, vector<int>(),
0067             SegmentationParameter::NoUnit, true);
0068     registerParameter("x_dimensions", "List of layer x dimensions", _layerDimensionsX, vector<double>(),
0069             SegmentationParameter::NoUnit, true);
0070     registerParameter("y_dimensions", "List of layer y dimensions", _layerDimensionsY, vector<double>(),
0071             SegmentationParameter::NoUnit, true);
0072 
0073 }
0074 
0075 TiledLayerSegmentation::~TiledLayerSegmentation() {
0076 }
0077 
0078 /// access the actual grid size in X for a given layer
0079 double TiledLayerSegmentation::layerGridSizeX(int layerIndex) const {
0080     // should be cached in a map if calculateOptimalCellSize is expensive
0081     return calculateOptimalCellSize(_gridSizeX, layerDimensions(layerIndex).x);
0082 }
0083 
0084 /// access the actual grid size in Y for a given layer
0085 double TiledLayerSegmentation::layerGridSizeY(int layerIndex) const {
0086     // should be cached in a map if calculateOptimalCellSize is expensive
0087     return calculateOptimalCellSize(_gridSizeY, layerDimensions(layerIndex).y);
0088 }
0089 
0090 /// set the dimensions of the given layer
0091 void TiledLayerSegmentation::setLayerDimensions(int layerIndex, double x, double y) {
0092     // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
0093     if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
0094         throw runtime_error(
0095                 "TiledLayerSegmentation::setLayerDimensions: inconsistent size of layer parameter vectors.");
0096     }
0097     vector<int>::iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
0098     if (it == _layerIndices.end()) {
0099         _layerIndices.emplace_back(layerIndex);
0100         _layerDimensionsX.emplace_back(x);
0101         _layerDimensionsY.emplace_back(y);
0102     } else {
0103         size_t index = it - _layerIndices.begin();
0104         _layerDimensionsX[index] = x;
0105         _layerDimensionsY[index] = y;
0106     }
0107 }
0108 
0109 /// access to the dimensions of the given layer
0110 TiledLayerSegmentation::LayerDimensions TiledLayerSegmentation::layerDimensions(int layerIndex) const {
0111     // a bit clumsy since we use three vectors instead of a map<int, LayerDimensions>
0112     if (_layerIndices.size() != _layerDimensionsX.size() or _layerIndices.size() != _layerDimensionsY.size()) {
0113         throw runtime_error(
0114                 "TiledLayerSegmentation::layerDimensions: inconsistent size of layer parameter vectors.");
0115     }
0116     vector<int>::const_iterator it = find(_layerIndices.begin(), _layerIndices.end(), layerIndex);
0117     if (it == _layerIndices.end()) {
0118         stringstream message;
0119         message << "TiledLayerSegmentation::layerDimensions: invalid layer index " << layerIndex;
0120         throw runtime_error(message.str());
0121     } else {
0122         size_t index = it - _layerIndices.begin();
0123         return LayerDimensions(_layerDimensionsX[index], _layerDimensionsY[index]);
0124     }
0125 }
0126 
0127 /// determine the position based on the cell ID
0128 Vector3D TiledLayerSegmentation::position(const CellID& cID) const {
0129     int layerIndex = _decoder->get(cID,_identifierLayer);
0130     double cellSizeX = layerGridSizeX(layerIndex);
0131     double cellSizeY = layerGridSizeY(layerIndex);
0132     LayerDimensions dimensions = layerDimensions(layerIndex);
0133     double offsetX = calculateOffset(cellSizeX, dimensions.x);
0134     double offsetY = calculateOffset(cellSizeY, dimensions.y);
0135     double localX = binToPosition(_decoder->get(cID,_identifierX), cellSizeX, offsetX);
0136     double localY = binToPosition(_decoder->get(cID,_identifierY), cellSizeY, offsetY);
0137     return Vector3D(localX, localY, 0.);
0138 }
0139 /// determine the cell ID based on the position
0140   CellID TiledLayerSegmentation::cellID(const Vector3D& localPosition, const Vector3D& /* globalPosition */,
0141         const VolumeID& vID) const {
0142     CellID cID = vID ;
0143     int layerIndex = _decoder->get(cID,_identifierLayer);
0144     double cellSizeX = layerGridSizeX(layerIndex);
0145     double cellSizeY = layerGridSizeY(layerIndex);
0146     LayerDimensions dimensions = layerDimensions(layerIndex);
0147     double offsetX = calculateOffset(cellSizeX, dimensions.x);
0148     double offsetY = calculateOffset(cellSizeY, dimensions.y);
0149     _decoder->set(cID,_identifierX, positionToBin(localPosition.x(), cellSizeX, offsetX));
0150     _decoder->set(cID,_identifierY, positionToBin(localPosition.y(), cellSizeY, offsetY));
0151     return cID;
0152 }
0153 
0154 /// helper method to calculate optimal cell size based on total size
0155   double TiledLayerSegmentation::calculateOptimalCellSize(double /* nominalCellSize */, double /* totalSize */) {
0156     // TODO: implement algorithm to calculate optimal cell size
0157     return 1.;
0158 }
0159 
0160 /// helper method to calculate offset of bin 0 based on the total size
0161 double TiledLayerSegmentation::calculateOffset(double /* cellSize */, double /* totalSize */) {
0162     // TODO: implement algorithm to calculate placement of bin 0
0163     return 0.;
0164 }
0165 
0166 } /* namespace DDSegmentation */
0167 } /* namespace dd4hep */