Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:14:26

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 /*
0013  * Segmentation.h
0014  *
0015  *  Created on: Jun 27, 2013
0016  *      Author: Christian Grefe, CERN
0017  */
0018 
0019 #ifndef DDSEGMENTATION_SEGMENTATION_H
0020 #define DDSEGMENTATION_SEGMENTATION_H
0021 
0022 #include <DD4hep/detail/SegmentationsInterna.h>
0023 #include <DDSegmentation/BitFieldCoder.h>
0024 #include <DDSegmentation/SegmentationParameter.h>
0025 
0026 #include <map>
0027 #include <set>
0028 #include <string>
0029 #include <vector>
0030 
0031 namespace dd4hep {
0032   namespace DDSegmentation {
0033 
0034     typedef SegmentationParameter* Parameter;
0035     typedef std::vector<Parameter> Parameters;
0036     typedef TypedSegmentationParameter<int>* IntParameter;
0037     typedef TypedSegmentationParameter<float>* FloatParameter;
0038     typedef TypedSegmentationParameter<double>* DoubleParameter;
0039     typedef TypedSegmentationParameter<std::string>* StringParameter;
0040     typedef TypedSegmentationParameter<std::vector<int> >* IntVecParameter;
0041     typedef TypedSegmentationParameter<std::vector<float> >* FloatVecParameter;
0042     typedef TypedSegmentationParameter<std::vector<double> >* DoubleVecParameter;
0043     typedef TypedSegmentationParameter<std::vector<std::string> >* StringVecParameter;
0044     typedef SegmentationParameter::UnitType UnitType;
0045 
0046     /// Simple container for a physics vector
0047     struct Vector3D {
0048       /// Default constructor
0049       Vector3D(double x_val = 0., double y_val = 0., double z_val = 0.) :
0050         X(x_val), Y(y_val), Z(z_val) {
0051       }
0052       /// Constructor using a foreign vector class. Requires methods x(), y() and z()
0053       template<typename T> Vector3D(const T& v) {
0054         X = v.x();
0055         Y = v.y();
0056         Z = v.Z();
0057       }
0058       /// Access to x value (required for use with ROOT GenVector)
0059       double x() const {
0060         return X;
0061       }
0062       /// Access to y value (required for use with ROOT GenVector)
0063       double y() const {
0064         return Y;
0065       }
0066       /// Access to z value (required for use with ROOT GenVector)
0067       double z() const {
0068         return Z;
0069       }
0070       double X, Y, Z;
0071     };
0072 
0073     /// Base class for all segmentations
0074     class Segmentation {
0075     public:
0076       /// Destructor
0077       virtual ~Segmentation();
0078 
0079       /// Add subsegmentation. Call only valid for Multi-segmentations. Default implementation throws an exception
0080       virtual void addSubsegmentation(long key_min, long key_max, Segmentation* entry);
0081       /// Determine the local position based on the cell ID
0082       virtual Vector3D position(const CellID& cellID) const = 0;
0083       /// Determine the cell ID based on the position
0084       virtual CellID cellID(const Vector3D& localPosition, const Vector3D& globalPosition,
0085                             const VolumeID& volumeID) const = 0;
0086       /// Determine the volume ID from the full cell ID by removing all local fields
0087       virtual VolumeID volumeID(const CellID& cellID) const;
0088       /// Calculates the neighbours of the given cell ID and adds them to the list of neighbours
0089       virtual void neighbours(const CellID& cellID, std::set<CellID>& neighbours) const;
0090       /// Access the encoding string
0091       virtual std::string fieldDescription() const {
0092         return _decoder->fieldDescription();
0093       }
0094       /// Access the segmentation name
0095       virtual const std::string& name() const {
0096         return _name;
0097       }
0098       /// Set the segmentation name
0099       virtual void setName(const std::string& value) {
0100         _name = value;
0101       }
0102       /// Access the segmentation type
0103       virtual const std::string& type() const {
0104         return _type;
0105       }
0106       /// Access the description of the segmentation
0107       virtual const std::string& description() const {
0108         return _description;
0109       }
0110       /// Access the underlying decoder
0111       virtual const BitFieldCoder* decoder()  const {
0112         return _decoder;
0113       }
0114       /// Set the underlying decoder
0115       virtual void setDecoder(const BitFieldCoder* decoder);
0116       /// Access to parameter by name
0117       virtual Parameter parameter(const std::string& parameterName) const;
0118       /// Access to all parameters
0119       virtual Parameters parameters() const;
0120       /// Set all parameters from an existing set of parameters
0121       virtual void setParameters(const Parameters& parameters);
0122       /** \brief Returns a vector<double> of the cellDimensions of the given cell ID
0123           in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi
0124 
0125           \param cellID cellID of the cell for which parameters are returned
0126           \return vector<double> in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi
0127       */
0128       virtual std::vector<double> cellDimensions(const CellID& cellID) const;
0129       /// Return true if this segmentation can have cells that span multiple
0130       /// volumes.  That is, points from multiple distinct volumes may
0131       /// be assigned to the same cell.
0132       /// In that case, a working volumeID() implementation is required.
0133       virtual bool cellsSpanVolumes() const
0134       {
0135         return false;
0136       }
0137 
0138     protected:
0139       /// Default constructor used by derived classes passing the encoding string
0140       Segmentation(const std::string& cellEncoding = "");
0141       /// Default constructor used by derived classes passing an existing decoder
0142       Segmentation(const BitFieldCoder* decoder);
0143 
0144       /// Add a parameter to this segmentation. Used by derived classes to define their parameters
0145       template<typename TYPE> void registerParameter(const std::string& nam, const std::string& desc,
0146                                                      TYPE& param, const TYPE& defaultVal, UnitType unitTyp = SegmentationParameter::NoUnit,
0147                                                      bool isOpt = false) {
0148         _parameters[nam] = new TypedSegmentationParameter<TYPE>(nam,desc,param,defaultVal,unitTyp,isOpt);
0149       }
0150       /// Add a cell identifier to this segmentation. Used by derived classes to define their required identifiers
0151       void registerIdentifier(const std::string& nam, const std::string& desc, std::string& ident,
0152                               const std::string& defaultVal);
0153 
0154       /// Helper method to convert a bin number to a 1D position
0155       static double binToPosition(FieldID bin, double cellSize, double offset = 0.);
0156       /// Helper method to convert a 1D position to a cell ID
0157       static int positionToBin(double position, double cellSize, double offset = 0.);
0158 
0159       /// Helper method to convert a bin number to a 1D position given a vector of binBoundaries
0160       static double binToPosition(FieldID bin, std::vector<double> const& cellBoundaries, double offset = 0.);
0161       /// Helper method to convert a 1D position to a cell ID given a vector of binBoundaries
0162       static int positionToBin(double position, std::vector<double> const& cellBoundaries, double offset = 0.);
0163 
0164       /// The segmentation name
0165       std::string _name;
0166       /// The segmentation type
0167       std::string _type;
0168       /// The description of the segmentation
0169       std::string _description;
0170       /// The parameters for this segmentation
0171       std::map<std::string, Parameter> _parameters;   //! No ROOT persistency
0172       /// The indices used for the encoding
0173       std::map<std::string, StringParameter> _indexIdentifiers;   //! No ROOT persistency
0174       /// The cell ID encoder and decoder
0175       const BitFieldCoder* _decoder = 0;
0176       /// Keeps track of the decoder ownership
0177       bool _ownsDecoder = false;
0178     private:
0179       /// No copy constructor allowed
0180       Segmentation(const Segmentation&);
0181     };
0182 
0183   } /* namespace DDSegmentation */
0184 } /* namespace dd4hep */
0185 #endif // DDSEGMENTATION_SEGMENTATION_H