// Detector.h #ifndef Detector_H #define Detector_H // A detector is a collection of named layers. It provides a method // for retrieving any of these layers by name. // // This class is intended for use as a base class. It is not abstract // but the constructor is protected. Similarly, there are no public // methods for adding, modifying or removing layers or subdetectors. // A subclass is expected to call methods to add a single layer or // all the layers from another detector. Typically these calls would // be made in the constructor. // // The return status for each change is zero if the change was // successful. After all changes, is_ok() may be called to verify // internal consistency. // // The layer names must be unique. New names are checked for uniqueness // each time a layer is added. // // The layers are held with shared ownership pointers. // // ObjStream example: // // [ mydet Detector // layer_names = string( "MUO1" "MUO2" ) // layers = @( muon_layer1 Muon_layer2 ) // subdetectors = @( det_cft det_smt ); // ] // // The removes the need for Detector subclasses. #include #include #include #include #include "ptr/Ptr.h" #include "ptr/LocalSharedDeletePolicy.h" #include "trfobj/TrfObject.h" #include "LayerPtr.h" namespace trf { class Detector; typedef Ptr DetectorPtr; // output stream std::ostream& operator<<(std::ostream& stream, const Detector& rhs); class Detector : public TrfObject { private: // enums // Check status. enum CheckStatus { UNCHECKED, CHECKED_OK, CHECKED_BAD }; public: // typedefs // List of detector pointers. typedef std::vector DetectorList; // List of layer names. typedef std::string LayerName; typedef std::less LayerNameCmp; typedef std::vector LayerNameList; protected: // typedefs // Layer index and list. typedef std::map LayerMap; public: // static methods // Return the type name. static TypeName get_type_name() { return "Detector"; } // Return the creator. static ObjCreator get_creator(); // Return the type. static Type get_static_type() { return get_creator(); } private: // attributes // List of layer names in original order. LayerNameList _names; // Map layer names to layers. LayerMap _layermap; // Check status. // Set by method check. mutable CheckStatus _check; private: // methods // Hide copy constructor. Detector(const Detector& rhs); // Hide assignment. Detector& operator=(const Detector& rhs); // Check internal status. // Note that if _check is set to CHECKED_BAD, it is not changed. // Return nonzero for error. virtual void check() const; protected: // methods // Default constructor. Detector(); // Return the map of layers indexed by name. const LayerMap& get_layer_map() const { return _layermap; } // Add a named layer. // // We take shared ownership of the layer. // // Return 0 for success. // // Return nonzero and do not add layer if the name duplicates // a known name. // virtual int add_layer(LayerName name, const LayerPtr& plyr); // Add a subdetector. // // We take shared ownership of its layers. // // Return 0 for success. // // Return nonzero and add no layers if any of the subdetector // layer names duplicate those here. // virtual int add_subdetector(const DetectorPtr& psdt); public: // methods // Destructor. virtual ~Detector(); // Return the type. Type get_generic_type() const { return get_static_type(); } // Return the type. Type get_type() const { return get_static_type(); } // Write object data. // Should be no need to override in subclasses. ObjData write_data() const; // Return whether the check status is ok. // If the status has not been checked, this will dos so. bool is_ok() const { if ( _check == UNCHECKED ) check(); return _check == CHECKED_OK; } // Return the list of layer names. const LayerNameList& get_layer_names() const { return _names; } // Print the layers. void print_layers(std::ostream& stream =std::cout) const; // Return whether a name has been assigned. bool is_assigned(LayerName name) const; // Return a layer. // Throw exception or crash is name is unrecognized. // To avoid crash, first check with is_assigned(). Layer& get_layer(LayerName name) const; // return a (shared management) pointer to a layer LayerPtr get_layer_pointer(LayerName name) const; // output stream friend std::ostream& operator<<(std::ostream& stream, const Detector& rhs); // Register layers with ObjTable. // Returns the number of layers which could not be registered and were // not already registered with the same name and address. Normally // this should be zero. int register_with_objtable() const; }; } // end namespace trf #endif