diff --git a/DDCore/include/DD4hep/Segmentations.h b/DDCore/include/DD4hep/Segmentations.h index c95e1ff966744e9c4ee5a10210bcd6300573a91b..ccc86f11093118118adb4563cdec8488ceb9d020 100644 --- a/DDCore/include/DD4hep/Segmentations.h +++ b/DDCore/include/DD4hep/Segmentations.h @@ -14,6 +14,7 @@ #include "DD4hep/Handle.h" #include "DD4hep/IDDescriptor.h" #include "DDSegmentation/Segmentation.h" +#include "DDSegmentation/SegmentationFactory.h" // C/C++ include files #include <cmath> @@ -39,6 +40,8 @@ namespace DD4hep { struct Segmentation: public Handle<DDSegmentation::Segmentation> { public: typedef DDSegmentation::Segmentation BaseSegmentation; + typedef DDSegmentation::Parameter Parameter; + typedef DDSegmentation::Parameters Parameters; /** @class Segmentation::Object Segmentations.h DD4hep/Segmentations.h * @@ -50,8 +53,6 @@ namespace DD4hep { unsigned long magic; /// Flag to use segmentation for hit positioning unsigned char useForHitPosition; - /// Reference to base segmentation - BaseSegmentation* segmentation; /// determine the local position based on the cell ID DDSegmentation::Position position(const long64& cellID) const; /// determine the cell ID based on the local position @@ -60,6 +61,28 @@ namespace DD4hep { Object(BaseSegmentation* s = 0); /// Default destructor virtual ~Object(); + /// Access the encoding string + std::string fieldDescription() const; + /// Access the segmentation name + const std::string& name() const; + /// Set the segmentation name + void setName(const std::string& value); + /// Access the segmentation type + const std::string& type() const; + /// Access the description of the segmentation + const std::string& description() const; + /// Access the underlying decoder + BitField64* decoder(); + /// Set the underlying decoder + void setDecoder(BitField64* decoder); + /// Access to parameter by name + Parameter parameter(const std::string& parameterName) const; + /// Access to all parameters + Parameters parameters() const; + /// Set all parameters from an existing set of parameters + void setParameters(const Parameters& parameters); + /// Reference to base segmentation + BaseSegmentation* segmentation; }; public: @@ -67,21 +90,12 @@ namespace DD4hep { Segmentation() : Handle<Implementation>() { } - /// Initializing constructor creating new object - template <typename T> Segmentation(T* o, const std::string& nam, const std::string& typ) - : Handle<Implementation>() { - o->segmentation = o; - assign(o, nam, typ); - } + /// Initializing constructor creating a new object of the given DDSegmentation type + Segmentation(const std::string& type, const std::string& name); /// Constructor to be used when reading the already parsed object template <typename Q> Segmentation(const Handle<Q>& e) : Handle<Implementation>(e) { } - /// Constructor to used when creating a new object - Segmentation(BaseSegmentation* s, const std::string& nam, const std::string& typ) : - Handle<Implementation>() { - assign(new Object(s), nam, typ); - } /// Access flag for hit positioning bool useForHitPosition() const; /// Accessor: Segmentation type @@ -91,7 +105,7 @@ namespace DD4hep { /// Access segmentation object BaseSegmentation* segmentation() const; /// Access to the parameters - DDSegmentation::Parameters parameters() const; + Parameters parameters() const; /// determine the local position based on the cell ID Position position(const long64& cellID) const; /// determine the cell ID based on the local position diff --git a/DDCore/src/Handle.cpp b/DDCore/src/Handle.cpp index 410a294ead54fc1bc9dd817636902b87e778451b..32125d4a2379e8f4d5504b1ea955c2846a932783 100644 --- a/DDCore/src/Handle.cpp +++ b/DDCore/src/Handle.cpp @@ -327,7 +327,6 @@ namespace DD4hep { namespace Geometry { template <> void Handle<_Segmentation>::assign(_Segmentation* s, const std::string& n, const std::string& t) { this->m_element = s; - s->setType(t); s->setName(n); } template <> const char* Handle<_Segmentation>::name() const { diff --git a/DDCore/src/Readout.cpp b/DDCore/src/Readout.cpp index 3c5b1cba50493d1e369f014c0a8f3b4da9273e2e..dc68bf8b835e2a1f653818dbf93511868325f06b 100644 --- a/DDCore/src/Readout.cpp +++ b/DDCore/src/Readout.cpp @@ -46,7 +46,7 @@ void Readout::setIDDescriptor(const Ref_t& new_descriptor) const { return; } } - throw runtime_error("DD4hep: Readout::setSegmentation: Cannot assign ID descriptor [Invalid Handle]"); + throw runtime_error("DD4hep: Readout::setIDDescriptor: Cannot assign ID descriptor [Invalid Handle]"); } /// Access IDDescription structure diff --git a/DDCore/src/Segementations.cpp b/DDCore/src/Segementations.cpp index 87bdc2815f6c7cc22000337052bd7dace0f562e4..de158701595ee88fe0ed919da75ade6d7e7d19c4 100644 --- a/DDCore/src/Segementations.cpp +++ b/DDCore/src/Segementations.cpp @@ -15,54 +15,104 @@ using namespace std; using namespace DD4hep; using namespace DD4hep::Geometry; +using DD4hep::DDSegmentation::Parameter; +using DD4hep::DDSegmentation::Parameters; +using DD4hep::DDSegmentation::SegmentationParameter; /// Standard constructor Segmentation::Object::Object(BaseSegmentation* s) : magic(magic_word()), useForHitPosition(0), segmentation(s) { + cout << "Creating Object " << this << ", segmentation " << s << endl; InstanceCount::increment(this); } /// Default destructor Segmentation::Object::~Object() { InstanceCount::decrement(this); + if (segmentation) { + delete segmentation; + } } /// determine the local position based on the cell ID DDSegmentation::Position Segmentation::Object::position(const long64& cellID) const { - return segmentation->position(cellID); + return segmentation->position(cellID); } /// determine the cell ID based on the local position long64 Segmentation::Object::cellID(const DDSegmentation::Position& localPosition, const DDSegmentation::Position& globalPosition, const long64& volumeID) const { - return segmentation->cellID(localPosition,globalPosition, volumeID); + return segmentation->cellID(localPosition, globalPosition, volumeID); +} + +/// Access the encoding string +string Segmentation::Object::fieldDescription() const { + return segmentation->fieldDescription(); +} +/// Access the segmentation name +const string& Segmentation::Object::name() const { + return segmentation->name(); +} +/// Set the segmentation name +void Segmentation::Object::setName(const string& value) { + segmentation->setName(value); +} +/// Access the segmentation type +const string& Segmentation::Object::type() const { + return segmentation->type(); +} +/// Access the description of the segmentation +const string& Segmentation::Object::description() const { + return segmentation->description(); +} +/// Access the underlying decoder +BitField64* Segmentation::Object::decoder() { + return segmentation->decoder(); +} +/// Set the underlying decoder +void Segmentation::Object::setDecoder(BitField64* decoder) { + segmentation->setDecoder(decoder); +} +/// Access to parameter by name +Parameter Segmentation::Object::parameter(const string& parameterName) const { + return segmentation->parameter(parameterName); +} +/// Access to all parameters +Parameters Segmentation::Object::parameters() const { + return segmentation->parameters(); +} +/// Set all parameters from an existing set of parameters +void Segmentation::Object::setParameters(const Parameters& parameters) { + segmentation->setParameters(parameters); +} + +/// Constructor to used when creating a new object +Segmentation::Segmentation(const string& type, const string& name) : + Handle<Implementation>() { + BaseSegmentation* s = DDSegmentation::SegmentationFactory::instance()->create(type); + if (s != 0) { + assign(new Object(s), name, ""); + } else { + throw runtime_error("FAILED to create segmentation: " + type + ". Missing factory method for: " + type + "!"); + } } /// Accessor: Segmentation type std::string Segmentation::type() const { - return segmentation()->type(); + return data<Object>()->type(); } bool Segmentation::useForHitPosition() const { - return object<Object>().useForHitPosition != 0; + return data<Object>()->useForHitPosition != 0; } /// Access to the parameters -DDSegmentation::Parameters Segmentation::parameters() const { - Object& o = object<Object>(); - if (o.segmentation != 0) { - return o.segmentation->parameters(); - } - throw runtime_error("DD4hep: The segmentation object " + string(type()) + " is invalid!"); +Parameters Segmentation::parameters() const { + return data<Object>()->parameters(); } /// Access segmentation object -DD4hep::DDSegmentation::Segmentation* Segmentation::segmentation() const { - Object& o = object<Object>(); - if (o.segmentation != 0) - return o.segmentation; - throw runtime_error( - "DD4hep: The segmentation object " + string(type()) - + " knows no implementation object [This is no longer allowed in the presence of DDSegmentation]"); +DDSegmentation::Segmentation* Segmentation::segmentation() const { + return data<Object>()->segmentation; } /// determine the local position based on the cell ID diff --git a/DDCore/src/plugins/Compact2Objects.cpp b/DDCore/src/plugins/Compact2Objects.cpp index b2cf444e953d1f9bdcd446aa291f1ebd7a9cf122..86b04284a1c154287b310895a8cb9a1890a2a342 100644 --- a/DDCore/src/plugins/Compact2Objects.cpp +++ b/DDCore/src/plugins/Compact2Objects.cpp @@ -15,7 +15,6 @@ #include "DD4hep/Plugins.h" #include "XML/DocumentHandler.h" #include "XML/Conversions.h" -#include "DDSegmentation/SegmentationFactory.h" // Root/TGeo include files #include "TGeoManager.h" @@ -440,18 +439,14 @@ template <> void Converter<Readout>::operator()(xml_h e) const { Ref_t idSpec; if (seg) { // Segmentation is not mandatory! - typedef DDSegmentation::Segmentation BaseSegmentation; string type = seg.attr < string > (_U(type)); - BaseSegmentation* s = DDSegmentation::SegmentationFactory::instance()->create(type); - if (not s) { - throw_print("FAILED to create segmentation: " + type + ". Missing factory method for: " + type + "!"); - } - Segmentation segment(s, name, type); + Segmentation segment(type, name); if (segment.isValid()) { - DDSegmentation::Parameters parameters = s->parameters(); - DDSegmentation::Parameters::iterator it; + segment->parameters(); + Segmentation::Parameters parameters = segment.parameters(); + Segmentation::Parameters::iterator it; for (it = parameters.begin(); it != parameters.end(); ++it) { - DDSegmentation::Parameter p = *it; + Segmentation::Parameter p = *it; if (seg.hasAttr(Unicode(p->name()))) { p->value() = seg.attr<double>(Unicode(p->name())); } else if (not p->isOptional()) { diff --git a/DDSegmentation/include/DDSegmentation/Segmentation.h b/DDSegmentation/include/DDSegmentation/Segmentation.h index 59beb6753d867d5ae70e26236d85797cd645fea7..f22fa5e3695add7531aa042aee2cbcd72080b975 100644 --- a/DDSegmentation/include/DDSegmentation/Segmentation.h +++ b/DDSegmentation/include/DDSegmentation/Segmentation.h @@ -66,61 +66,48 @@ public: virtual CellID cellID(const Position& localPosition, const Position& globalPosition, const VolumeID& volumeID) const = 0; /// Access the encoding string - std::string fieldDescription() const { + virtual std::string fieldDescription() const { return _decoder->fieldDescription(); } /// Access the segmentation name - const std::string& name() const { + virtual const std::string& name() const { return _name; } /// Set the segmentation name - void setName(const std::string& value) { + virtual void setName(const std::string& value) { _name = value; } /// Access the segmentation type - const std::string& type() const { + virtual const std::string& type() const { return _type; } - /// Set the segmentation type - void setType(const std::string& value) { - //FIXME: Should not be able to change the type! - _type = value; - } /// Access the description of the segmentation - const std::string& description() const { + virtual const std::string& description() const { return _description; } /// Access the underlying decoder - BitField64* decoder() { + virtual BitField64* decoder() { return _decoder; } /// Set the underlying decoder - void setDecoder(BitField64* decoder); - /// Access the set of parameters -// Parameters parameters() const; - /// Access to parameter by name - SegmentationParameter* parameter(const std::string& parameterName); + virtual void setDecoder(BitField64* decoder); /// Access to parameter by name - const SegmentationParameter* parameter(const std::string& parameterName) const; - /// Access to parameter value by name - double parameterValue(const std::string& parameterName) const; - /// Set the parameter value by name - void setParameterValue(const std::string& parameterName, double value); - /// Access to all parameters - std::vector<SegmentationParameter*> parameters(); + virtual Parameter parameter(const std::string& parameterName) const; /// Access to all parameters - std::vector<const SegmentationParameter*> parameters() const; + virtual Parameters parameters() const; + /// Set all parameters from an existing set of parameters + virtual void setParameters(const Parameters& parameters); protected: /// Default constructor used by derived classes passing the encoding string Segmentation(const std::string& cellEncoding = ""); /// Add a parameter to this segmentation. Used by derived classes to define their parameters - void registerParameter(const std::string& name, const std::string& description, double& parameter, double defaultValue = 0., bool isOptional = false); + virtual void registerParameter(const std::string& name, const std::string& description, double& parameter, const double& defaultValue, bool isOptional = false); /// Helper method to convert a bin number to a 1D position - double binToPosition(CellID bin, double cellSize, double offset = 0.) const; + static double binToPosition(CellID bin, double cellSize, double offset = 0.); /// Helper method to convert a 1D position to a cell ID - int positionToBin(double position, double cellSize, double offset = 0.) const; + static int positionToBin(double position, double cellSize, double offset = 0.); /// The cell ID encoder and decoder mutable BitField64* _decoder; @@ -132,10 +119,12 @@ protected: std::string _type; /// The description of the segmentation std::string _description; + /// The parameters for this segmentation + std::map<std::string, Parameter> _parameters; private: - /// The parameters for this segmentation - std::map<std::string, SegmentationParameter*> _parameters; + /// No copy constructor allowed + Segmentation(const Segmentation&); }; /// Macro to instantiate a new SegmentationCreator by its type name diff --git a/DDSegmentation/include/DDSegmentation/SegmentationParameter.h b/DDSegmentation/include/DDSegmentation/SegmentationParameter.h index 1b9e09f44e6dba915bc780628f8bc69363e3a0c3..6abf54375f7841dc33ee04a117a9cdf2f259bc53 100644 --- a/DDSegmentation/include/DDSegmentation/SegmentationParameter.h +++ b/DDSegmentation/include/DDSegmentation/SegmentationParameter.h @@ -10,6 +10,7 @@ #ifndef DDSegmentation_SEGMENTATIONPARAMETER_H_ #define DDSegmentation_SEGMENTATIONPARAMETER_H_ +#include <sstream> #include <string> namespace DD4hep { @@ -20,28 +21,58 @@ class SegmentationParameter { public: /// Default constructor SegmentationParameter(const std::string& name, const std::string& description, double& parameter, - double defaultValue = 0., bool isOptional = false); + const double& defaultValue, bool isOptional = false) : + _name(name), _description(description), _value(parameter), _defaultValue(defaultValue), _isOptional( + isOptional) { + _value = defaultValue; + } /// Destructor - virtual ~SegmentationParameter() {}; + virtual ~SegmentationParameter() { + } /// Access to the parameter name - const std::string& name() const; + const std::string& name() const { + return _name; + } /// Access to the parameter description - const std::string& description() const; + const std::string& description() const { + return _description; + } /// Access to the parameter value - double& value(); + double& value() { + return _value; + } /// Access to the parameter value - double value() const; + double value() const { + return _value; + } + /// Set the parameter value + void setValue(const double& value) { + _value = value; + } + /// Access to the parameter default value + const double& defaultValue() const { + return _defaultValue; + } /// Check if this parameter is optional - bool isOptional() const; + bool isOptional() const { + return _isOptional; + } /// Printable version - std::string toString() const; + std::string toString() const { + std::stringstream s; + s << _name << " = " << _value; + if (not _description.empty()) { + s << " (" << _description << ")"; + } + return s.str(); + } protected: /// The parameter name std::string _name; /// The parameter description std::string _description; /// The parameter value - double& _parameter; + double& _value; /// The parameter default value double _defaultValue; /// Store if parameter is optional diff --git a/DDSegmentation/src/Segmentation.cpp b/DDSegmentation/src/Segmentation.cpp index ce91a8c3f994f6e6a897e67b01c99037e9e6732c..2a8333d60bfe1503a4f291b772e11d336f49fbcf 100644 --- a/DDSegmentation/src/Segmentation.cpp +++ b/DDSegmentation/src/Segmentation.cpp @@ -17,6 +17,7 @@ namespace DDSegmentation { using std::cerr; using std::endl; using std::map; +using std::runtime_error; using std::string; using std::stringstream; using std::vector; @@ -52,19 +53,8 @@ void Segmentation::setDecoder(BitField64* decoder) { } /// Access to parameter by name -SegmentationParameter* Segmentation::parameter(const std::string& parameterName) { - map<string, SegmentationParameter*>::iterator it; - it = _parameters.find(parameterName); - if (it != _parameters.end()) { - return it->second; - } - stringstream s; - s << "Unknown parameter " << parameterName << " for segmentation type " << _type; - throw std::runtime_error(s.str()); -} -/// Access to parameter by name -const SegmentationParameter* Segmentation::parameter(const std::string& parameterName) const { - map<string, SegmentationParameter*>::const_iterator it; +Parameter Segmentation::parameter(const std::string& parameterName) const { + map<string, Parameter>::const_iterator it; it = _parameters.find(parameterName); if (it != _parameters.end()) { return it->second; @@ -74,61 +64,40 @@ const SegmentationParameter* Segmentation::parameter(const std::string& paramete throw std::runtime_error(s.str()); } -/// Access to parameter values by name -double Segmentation::parameterValue(const std::string& parameterName) const { - return this->parameter(parameterName)->value(); -} - -/// Set the parameter value by name -void Segmentation::setParameterValue(const std::string& parameterName, double value) { - this->parameter(parameterName)->value() = value; -} - -/// Access to all parameters -//Parameters Segmentation::parameters() const { -// Parameters parameters; -// map<string, SegmentationParameter*>::const_iterator it; -// for (it = _parameters.begin(); it != _parameters.end(); ++it) { -// parameters.push_back(std::make_pair(it->first, it->second->value())); -// } -// return parameters; -//} - /// Access to all parameters -vector<SegmentationParameter*> Segmentation::parameters() { - vector<SegmentationParameter*> parameters; - map<string, SegmentationParameter*>::iterator it; +Parameters Segmentation::parameters() const { + Parameters parameters; + map<string, Parameter>::const_iterator it; for (it = _parameters.begin(); it != _parameters.end(); ++it) { parameters.push_back(it->second); } return parameters; } -/// Access to all parameters -vector<const SegmentationParameter*> Segmentation::parameters() const { - vector<const SegmentationParameter*> parameters; - map<string, SegmentationParameter*>::const_iterator it; - for (it = _parameters.begin(); it != _parameters.end(); ++it) { - parameters.push_back(it->second); +/// Set all parameters from an existing set of parameters +void Segmentation::setParameters(const Parameters& parameters) { + Parameters::const_iterator it; + for (it = parameters.begin(); it != parameters.end(); ++it) { + Parameter p = *it; + parameter(p->name())->value() = p->value(); } - return parameters; } /// Add a parameter to this segmentation. Used by derived classes to define their parameters -void Segmentation::registerParameter(const std::string& name, const std::string& description, double& parameter, double defaultValue, bool isOptional) { +void Segmentation::registerParameter(const std::string& name, const std::string& description, double& parameter, const double& defaultValue, bool isOptional) { _parameters[name] = new SegmentationParameter(name, description, parameter, defaultValue, isOptional); } /// Helper method to convert a bin number to a 1D position -double Segmentation::binToPosition(long64 bin, double cellSize, double offset) const { +double Segmentation::binToPosition(long64 bin, double cellSize, double offset) { return bin * cellSize + offset; } /// Helper method to convert a 1D position to a cell ID -int Segmentation::positionToBin(double position, double cellSize, double offset) const { +int Segmentation::positionToBin(double position, double cellSize, double offset) { if (cellSize == 0.) { - throw std::runtime_error("Invalid cell size: 0.0"); + throw runtime_error("Invalid cell size: 0.0"); } return int((position + 0.5 * cellSize - offset)/cellSize); } diff --git a/DDSegmentation/src/SegmentationParameter.cpp b/DDSegmentation/src/SegmentationParameter.cpp deleted file mode 100644 index a172d9b70ebda4828fdb551b333ecd945fce2179..0000000000000000000000000000000000000000 --- a/DDSegmentation/src/SegmentationParameter.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SegmentationParameter.cpp - * - * Created on: Dec 16, 2013 - * Author: Christian Grefe, CERN - */ - -#include "DDSegmentation/SegmentationParameter.h" - -#include <sstream> - -namespace DD4hep { -namespace DDSegmentation { - -using std::string; -using std::stringstream; - -/// Default constructor -SegmentationParameter::SegmentationParameter(const string& name, const string& description, double& parameter, - double defaultValue, bool isOptional) : - _name(name), _description(description), _parameter(parameter), _defaultValue(defaultValue), _isOptional( - isOptional) { - _parameter = defaultValue; -} - -/// Access to the parameter name -const string& SegmentationParameter::name() const { - return _name; -} - -/// Access to the parameter description -const string& SegmentationParameter::SegmentationParameter::description() const { - return _description; -} - -/// Access to the parameter value -double& SegmentationParameter::value() { - return _parameter; -} - -/// Access to the parameter value -double SegmentationParameter::value() const { - return _parameter; -} - -/// Check if this parameter is optional -bool SegmentationParameter::isOptional() const { - return _isOptional; -} - -/// Printable version -string SegmentationParameter::toString() const { - stringstream s; - s << _name << " = " << _parameter; - if (not _description.empty()) { - s << " (" << _description << ")"; - } - return s.str(); -} - -} /* namespace DDSegmentation */ -} /* namespace DD4hep */