DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Segmentation.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  * Segmentation.cpp
13  *
14  * Created on: Jun 27, 2013
15  * Author: Christian Grefe, CERN
16  */
17 
19 
20 #include <iostream>
21 #include <sstream>
22 #include <stdexcept>
23 #include <cmath>
24 #include <algorithm>
25 #include <iomanip>
26 
27 namespace dd4hep {
28 
29  namespace DDSegmentation {
30 
31  using std::cerr;
32  using std::endl;
33  using std::map;
34  using std::runtime_error;
35  using std::stringstream;
36  using std::vector;
37 
39  Segmentation::Segmentation(const std::string& cellEncoding) :
40  _name("Segmentation"), _type("Segmentation"), _decoder(new BitFieldCoder(cellEncoding)), _ownsDecoder(true) {
41 
42  }
43 
46  _name("Segmentation"), _type("Segmentation"), _decoder(newDecoder), _ownsDecoder(false) {
47  }
48 
51  if (_ownsDecoder and _decoder != 0) {
52  delete _decoder;
53  }
54  for (auto& p : _parameters) {
55  if ( p.second ) delete p.second;
56  }
57  _parameters.clear();
58  }
59 
61  void Segmentation::addSubsegmentation(long /* key_min */, long /* key_max */, Segmentation* /* entry */) {
62  throw std::runtime_error("This segmentation type:"+_type+" does not support sub-segmentations.");
63  }
64 
67  map<std::string, StringParameter>::const_iterator it;
68  VolumeID vID = cID ;
69  for (it = _indexIdentifiers.begin(); it != _indexIdentifiers.end(); ++it) {
70  std::string identifier = it->second->typedValue();
71  _decoder->set(vID,identifier,0);
72  }
73  return vID;
74  }
75 
77  void Segmentation::neighbours(const CellID& cID, std::set<CellID>& cellNeighbours) const {
78  map<std::string, StringParameter>::const_iterator it;
79  for (it = _indexIdentifiers.begin(); it != _indexIdentifiers.end(); ++it) {
80  const std::string& identifier = it->second->typedValue();
81  CellID nID = cID ;
82  int currentValue = _decoder->get(cID,identifier);
83  // add both neighbouring cell IDs, don't add out of bound indices
84  try {
85  _decoder->set(nID,identifier,currentValue - 1);
86  cellNeighbours.insert(nID);
87  } catch (runtime_error& e) {
88  // nothing to do
89  }
90  try {
91  _decoder->set(nID,identifier,currentValue + 1);
92  cellNeighbours.insert(nID);
93  } catch (runtime_error& e) {
94  // nothing to do
95  }
96  }
97  }
98 
100  void Segmentation::setDecoder(const BitFieldCoder* newDecoder) {
101  if ( _decoder == newDecoder )
102  return; //self assignment
103  else if (_ownsDecoder)
104  delete _decoder;
105  _decoder = newDecoder;
106  _ownsDecoder = false;
107  }
108 
110  Parameter Segmentation::parameter(const std::string& parameterName) const {
111  map<std::string, Parameter>::const_iterator it = _parameters.find(parameterName);
112  if (it != _parameters.end()) {
113  return it->second;
114  }
115  stringstream s;
116  s << "Unknown parameter " << parameterName << " for segmentation type " << _type;
117  throw std::runtime_error(s.str());
118  }
119 
122  Parameters pars;
123  for ( const auto& it : _parameters )
124  pars.emplace_back(it.second);
125  return pars;
126  }
127 
130  for ( const auto* p : pars )
131  parameter(p->name())->value() = p->value();
132  }
133 
135  void Segmentation::registerIdentifier(const std::string& idName, const std::string& idDescription, std::string& identifier,
136  const std::string& defaultValue) {
137  StringParameter idParameter =
138  new TypedSegmentationParameter<std::string>(idName, idDescription, identifier, defaultValue,
140  _parameters[idName] = idParameter;
141  _indexIdentifiers[idName] = idParameter;
142  }
143 
145  double Segmentation::binToPosition(FieldID bin, double cellSize, double offset) {
146  return bin * cellSize + offset;
147  }
148 
150  int Segmentation::positionToBin(double position, double cellSize, double offset) {
151  if (cellSize <= 1e-10) {
152  throw runtime_error("Invalid cell size: 0.0");
153  }
154  return int(floor((position + 0.5 * cellSize - offset) / cellSize));
155  }
156 
158  double Segmentation::binToPosition(FieldID bin, std::vector<double> const& cellBoundaries, double offset) {
159  return (cellBoundaries[bin+1] + cellBoundaries[bin])*0.5 + offset;
160  }
162  int Segmentation::positionToBin(double position, std::vector<double> const& cellBoundaries, double offset) {
163 
164  // include the lower edge to the segmentation, deal with numerical issues
165  if(fabs(position/cellBoundaries.front()-1.0) < 3e-12) return 0;
166 
167  // include the upper edge of the last bin to the segmentation, deal with numerical issues
168  if(fabs(position/cellBoundaries.back()-1.0) < 3e-12) return int(cellBoundaries.size()-2);
169 
170  // hits outside cannot be treated
171  if(position < cellBoundaries.front()) {
172  std::stringstream err;
173  err << std::setprecision(20) << std::scientific;
174  err << "Hit Position (" << position << ") is below the acceptance"
175  << " (" << cellBoundaries.front() << ") "
176  << "of the segmentation";
177  throw std::runtime_error(err.str());
178  }
179  if(position > cellBoundaries.back() ) {
180  std::stringstream err;
181  err << std::setprecision(20) << std::scientific;
182  err << "Hit Position (" << position << ") is above the acceptance"
183  << " (" << cellBoundaries.back() << ") "
184  << "of the segmentation";
185  throw std::runtime_error(err.str());
186  }
187  std::vector<double>::const_iterator bin = std::upper_bound(cellBoundaries.begin(),
188  cellBoundaries.end(),
189  position-offset);
190  // need to reduce found bin by one, because upper_bound works that way, lower_bound would not help
191  return bin - cellBoundaries.begin() - 1 ;
192 
193  }
194 
195  std::vector<double> Segmentation::cellDimensions(const CellID&) const {
196  std::stringstream errorMessage;
197  errorMessage << __func__ << " is not implemented for " << _type;
198  throw std::logic_error(errorMessage.str());
199  }
200 
201  } /* namespace DDSegmentation */
202 } /* namespace dd4hep */
dd4hep::DDSegmentation::VolumeID
uint64_t VolumeID
Definition: BitFieldCoder.h:27
dd4hep::DDSegmentation::Segmentation::_parameters
std::map< std::string, Parameter > _parameters
The parameters for this segmentation.
Definition: Segmentation.h:164
dd4hep::DDSegmentation::Segmentation::_decoder
const BitFieldCoder * _decoder
The cell ID encoder and decoder.
Definition: Segmentation.h:168
dd4hep::DDSegmentation::BitFieldCoder
Helper class for decoding and encoding a bit field of 64bits for convenient declaration.
Definition: BitFieldCoder.h:114
dd4hep::DDSegmentation::Segmentation::~Segmentation
virtual ~Segmentation()
Destructor.
Definition: Segmentation.cpp:50
dd4hep::DDSegmentation::BitFieldCoder::get
FieldID get(CellID bitfield, size_t idx) const
Definition: BitFieldCoder.h:165
dd4hep::DDSegmentation::SegmentationParameter
Class to hold a segmentation parameter with its description.
Definition: SegmentationParameter.h:107
dd4hep::DDSegmentation::FieldID
int64_t FieldID
Definition: BitFieldCoder.h:25
dd4hep::DDSegmentation::Segmentation::_ownsDecoder
bool _ownsDecoder
Keeps track of the decoder ownership.
Definition: Segmentation.h:170
dd4hep::DDSegmentation::SegmentationParameter::NoUnit
@ NoUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::Segmentation::parameters
virtual Parameters parameters() const
Access to all parameters.
Definition: Segmentation.cpp:121
dd4hep::DDSegmentation::Segmentation::parameter
virtual Parameter parameter(const std::string &parameterName) const
Access to parameter by name.
Definition: Segmentation.cpp:110
dd4hep::DDSegmentation::Parameters
std::vector< Parameter > Parameters
Definition: SegmentationsInterna.h:35
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::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::Segmentation::setDecoder
virtual void setDecoder(const BitFieldCoder *decoder)
Set the underlying decoder.
Definition: Segmentation.cpp:100
dd4hep::DDSegmentation::Segmentation::volumeID
virtual VolumeID volumeID(const CellID &cellID) const
Determine the volume ID from the full cell ID by removing all local fields.
Definition: Segmentation.cpp:66
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::Segmentation::Segmentation
Segmentation(const std::string &cellEncoding="")
Default constructor used by derived classes passing the encoding string.
Definition: Segmentation.cpp:39
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::TypedSegmentationParameter
Concrete class to hold a segmentation parameter of a given type with its description.
Definition: SegmentationParameter.h:166
dd4hep::DDSegmentation::Segmentation::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: Segmentation.cpp:195
dd4hep::DDSegmentation::Segmentation::_type
std::string _type
The segmentation type.
Definition: Segmentation.h:160
dd4hep::DDSegmentation::Segmentation::neighbours
virtual void neighbours(const CellID &cellID, std::set< CellID > &neighbours) const
Calculates the neighbours of the given cell ID and adds them to the list of neighbours.
Definition: Segmentation.cpp:77
dd4hep::DDSegmentation::Segmentation::_indexIdentifiers
std::map< std::string, StringParameter > _indexIdentifiers
The indices used for the encoding.
Definition: Segmentation.h:166
dd4hep::DDSegmentation::Segmentation::addSubsegmentation
virtual void addSubsegmentation(long key_min, long key_max, Segmentation *entry)
Add subsegmentation. Call only valid for Multi-segmentations. Default implementation throws an except...
Definition: Segmentation.cpp:61
Segmentation.h
dd4hep::DDSegmentation::Segmentation::setParameters
virtual void setParameters(const Parameters &parameters)
Set all parameters from an existing set of parameters.
Definition: Segmentation.cpp:129
dd4hep::DDSegmentation::Segmentation
Base class for all segmentations.
Definition: Segmentation.h:75
dd4hep::DDSegmentation::Segmentation::position
virtual Vector3D position(const CellID &cellID) const =0
Determine the local position based on the cell ID.
dd4hep::DDSegmentation::SegmentationParameter::value
virtual std::string value() const =0
Access to the parameter value in string representation.