Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:11:49

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 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DD4HEP_DETECTOR_H
0014 #define DD4HEP_DETECTOR_H
0015 
0016 #include <DD4hep/Version.h>
0017 
0018 // Framework includes
0019 #include <DD4hep/Handle.h>
0020 #include <DD4hep/Fields.h>
0021 #include <DD4hep/Objects.h>
0022 #include <DD4hep/Shapes.h>
0023 #include <DD4hep/Volumes.h>
0024 #include <DD4hep/Readout.h>
0025 #include <DD4hep/DetElement.h>
0026 #include <DD4hep/NamedObject.h>
0027 #include <DD4hep/Segmentations.h>
0028 #include <DD4hep/VolumeManager.h>
0029 #include <DD4hep/OpticalSurfaceManager.h>
0030 #include <DD4hep/ExtensionEntry.h>
0031 #include <DD4hep/BuildType.h>
0032 
0033 // C/C++ include files
0034 #include <map>
0035 #include <vector>
0036 #include <string>
0037 #include <cstdio>
0038 #include <memory>
0039 
0040 // Forward declarations
0041 class TGeoManager;
0042 
0043 /// Namespace for the AIDA detector description toolkit
0044 namespace dd4hep {
0045 
0046   /// return a string with the current dd4hep version in the form vXX-YY.
0047   std::string versionString();
0048   
0049   // Foward declarations
0050   class NamedObject;
0051 
0052   /// Namespace for the AIDA detector description toolkit supporting XML utilities
0053   namespace xml {
0054     class UriReader;
0055   }
0056 
0057   /// Helper class to access default temperature and pressure
0058   class STD_Conditions   {
0059   public:
0060     enum  Conventions {
0061       STP           = 1<<0, // Standard temperature and pressure (273.15 Kelvin, 1 ATM)
0062       NTP           = 1<<1, // Normal   temperature and pressure (293.15 Kelvin, 1 ATM)
0063       USER          = 1<<2, // Explicitly set before materials are defined (recommended)
0064       USER_SET      = 1<<3,
0065       USER_NOTIFIED = 1<<4
0066     };
0067   public:
0068     double pressure;
0069     double temperature;
0070     long   convention;
0071     bool is_NTP()  const           {  return (convention&NTP)  != 0; }
0072     bool is_STP()  const           {  return (convention&STP)  != 0; }
0073     bool is_user_defined()  const  {  return (convention&USER) != 0; }
0074   };
0075   
0076   /// The main interface to the dd4hep detector description package
0077   /**
0078    *  Note: The usage of the factory method:
0079    *
0080    *      static Detector& getInstance(void);
0081    *
0082    *  is DEPRECATED!
0083    *
0084    *  You should rather use the plugin mechanism to create a new instance.
0085    *
0086    *  \author  M.Frank
0087    *  \version 1.0
0088    *  \ingroup DD4HEP_CORE
0089    */
0090   class Detector {
0091   public:
0092     /// Type definition of a map of named handles
0093     typedef std::map<std::string, Handle<NamedObject> > HandleMap;
0094     typedef std::map<std::string, std::string>          PropertyValues;
0095     typedef std::map<std::string, PropertyValues>       Properties;
0096 
0097     /// The detector description states
0098     enum State   {
0099       /// The detector description object is freshly created. No geometry nothing.
0100       NOT_READY = 1<<0,
0101       /// The geometry is being created and loaded. (parsing ongoing)
0102       LOADING   = 1<<1,
0103       /// The geometry is loaded.
0104       READY     = 1<<2
0105     };
0106     
0107     /// Destructor
0108     virtual ~Detector() = default;
0109 
0110     /// Access flag to steer the detail of building of the geometry/detector description
0111     virtual DetectorBuildType buildType() const = 0;
0112     /// Initialize geometry
0113     virtual void init() = 0;
0114     /// Finalize the geometry
0115     virtual void endDocument(bool close_geometry=true) = 0;
0116 
0117     /// Access the state of the geometry
0118     virtual State state()  const = 0;
0119     /// Access the geometry manager of this instance
0120     virtual TGeoManager& manager() const = 0;
0121     /// Access to properties map
0122     virtual Properties& properties() const = 0;
0123     /// Return handle to material describing air
0124     virtual Material air() const = 0;
0125     /// Return handle to material describing vacuum
0126     virtual Material vacuum() const = 0;
0127     /// Return handle to "invisible" visualization attributes
0128     virtual VisAttr invisible() const = 0;
0129 
0130     /// Return reference to the top-most (world) detector element
0131     virtual DetElement world() const = 0;
0132     /// Return reference to detector element with all tracker devices.
0133     virtual DetElement trackers() const = 0;
0134 
0135     /// Return handle to the world volume containing everything
0136     virtual Volume worldVolume() const = 0;
0137     /// Return handle to the parallel world volume
0138     virtual Volume parallelWorldVolume() const = 0;
0139     /// Return handle to the volume containing the tracking devices
0140     virtual Volume trackingVolume() const = 0;
0141     /// Set the tracking volume of the detector
0142     virtual void setTrackingVolume(Volume vol) = 0;
0143 
0144     /// Return handle to the VolumeManager
0145     virtual VolumeManager volumeManager() const = 0;
0146 
0147     /// Access the optical surface manager
0148     virtual OpticalSurfaceManager surfaceManager()  const = 0;
0149 
0150     /// Access default conditions (temperature and pressure
0151     virtual const STD_Conditions& stdConditions()  const = 0;
0152     /// Set the STD temperature and pressure
0153     virtual void setStdConditions(double temp, double pressure) = 0;
0154     /// Set the STD conditions according to defined types (STP or NTP)
0155     virtual void setStdConditions(const std::string& type) = 0;
0156     
0157     
0158     /// Accessor to the map of header entries
0159     virtual Header header() const = 0;
0160     /// Accessor to the header entry
0161     virtual void setHeader(Header h) = 0;
0162 
0163     /// Return handle to the combined electromagentic field description.
0164     virtual OverlayedField field() const = 0;
0165 
0166     /// Accessor to the map of constants
0167     virtual const HandleMap& constants() const = 0;
0168     /// Accessor to the map of region settings
0169     virtual const HandleMap& regions() const = 0;
0170     /// Accessor to the map of sub-detectors
0171     virtual const HandleMap& detectors() const = 0;
0172     /// Accessor to the map of sub-detectors
0173     virtual const HandleMap& sensitiveDetectors() const = 0;
0174     /// Accessor to the map of readout structures
0175     virtual const HandleMap& readouts() const = 0;
0176     /// Accessor to the map of visualisation attributes
0177     virtual const HandleMap& visAttributes() const = 0;
0178     /// Accessor to the map of limit settings
0179     virtual const HandleMap& limitsets() const = 0;
0180     /// Accessor to the map of field entries, which together form the global field
0181     virtual const HandleMap& fields() const = 0;
0182     /// Accessor to the map of ID specifications
0183     virtual const HandleMap& idSpecifications() const = 0;
0184 
0185 #ifndef __MAKECINT__
0186     /** Access to predefined caches of subdetectors according to the sensitive type */
0187     /// Access a set of subdetectors according to the sensitive type.
0188     /**
0189        Please note:
0190        - The sensitive type of a detector is set in the 'detector constructor'.
0191        - Not sensitive detector structures have the name 'passive'
0192        - Compounds (ie. nested detectors) are of type 'compound'
0193        - If throw_exc is set to true, an exception is thrown if the type
0194        is not present. Otherwise an empty detector container is returned.
0195     */
0196     virtual const std::vector<DetElement>& detectors(const std::string& type,
0197                                                      bool throw_exc=false) const = 0;
0198 
0199     /// Access a set of subdetectors according to several sensitive types.
0200     virtual std::vector<DetElement> detectors(const std::string& type1,
0201                                               const std::string& type2,
0202                                               const std::string& type3="",
0203                                               const std::string& type4="",
0204                                               const std::string& type5="" ) = 0;
0205 
0206     /// Access the available detector types
0207     virtual std::vector<std::string> detectorTypes() const = 0;
0208 
0209 
0210     /** return a vector with all detectors that have all the type properties in
0211      *  includeFlag set but none of the properties given in excludeFlag
0212      */
0213     virtual std::vector<DetElement> detectors(unsigned int includeFlag, 
0214                                               unsigned int excludeFlag=0 ) const = 0 ;
0215 #endif
0216 
0217     /** Miscaneleous accessors to the detexctor description  */
0218 
0219     /// Register new parent detector using the detector name.
0220     /** Volumes must be registered/declared PRIOR to be picked up!
0221      *  Once registered, Detector::pickMotherVolume(detector) will automatically return the
0222      *  proper parent volume!
0223      * 
0224      *  The method throws an exception if another volume was already declared for this subdetector
0225      *  The method throws an exception if the volume to be registered is invalid.
0226      */
0227     virtual void   declareParent(const std::string& detector_name, const DetElement& det) = 0;
0228 
0229     /// Access mother volume by detector element
0230     /** The method uses the detector element's name for volume identification. 
0231      *  Unregistered detectors are hosted by the world volume.
0232      */
0233     virtual Volume pickMotherVolume(const DetElement& sd) const = 0;
0234 
0235     /// Typed access to constants: access string values
0236     virtual std::string constantAsString(const std::string& name) const = 0;
0237     /// Typed access to constants: long values
0238     virtual long constantAsLong(const std::string& name) const = 0;
0239     /// Typed access to constants: double values
0240     virtual double constantAsDouble(const std::string& name) const = 0;
0241 
0242     /// Retrieve a constant by its name from the detector description
0243     virtual Constant constant(const std::string& name) const = 0;
0244     /// Typed access to constants: access any type values
0245     template <class T> T constant(const std::string& name) const;
0246 
0247     /// Retrieve a matrial by its name from the detector description
0248     virtual Material material(const std::string& name) const = 0;
0249     /// Retrieve a id descriptor by its name from the detector description
0250     virtual IDDescriptor idSpecification(const std::string& name) const = 0;
0251     /// Retrieve a region object by its name from the detector description
0252     virtual Region region(const std::string& name) const = 0;
0253     /// Retrieve a visualization attribute by its name from the detector description
0254     virtual VisAttr visAttributes(const std::string& name) const = 0;
0255     /// Retrieve a limitset by its name from the detector description
0256     virtual LimitSet limitSet(const std::string& name) const = 0;
0257     /// Retrieve a readout object by its name from the detector description
0258     virtual Readout readout(const std::string& name) const = 0;
0259     /// Retrieve a sensitive detector by its name from the detector description
0260     virtual SensitiveDetector sensitiveDetector(const std::string& name) const = 0;
0261     /// Retrieve a field component by its name from the detector description
0262     virtual CartesianField field(const std::string& name) const = 0;
0263     /// Retrieve a subdetector element by its name from the detector description
0264     virtual DetElement detector(const std::string& name) const = 0;
0265 
0266     /// Add a new constant to the detector description
0267     virtual Detector& add(Constant constant) = 0;
0268     /// Add a new visualisation attribute to the detector description
0269     virtual Detector& add(VisAttr attr) = 0;
0270     /// Add a new limit set to the detector description
0271     virtual Detector& add(LimitSet limitset) = 0;
0272     /// Add a new detector region to the detector description
0273     virtual Detector& add(Region region) = 0;
0274     /// Add a new id descriptor to the detector description
0275     virtual Detector& add(IDDescriptor spec) = 0;
0276     /// Add a new detector readout to the detector description
0277     virtual Detector& add(Readout readout) = 0;
0278     /// Add a new sensitive detector to the detector description
0279     virtual Detector& add(SensitiveDetector entry) = 0;
0280     /// Add a new subdetector to the detector description
0281     virtual Detector& add(DetElement detector) = 0;
0282     /// Add a field component to the detector description
0283     virtual Detector& add(CartesianField entry) = 0;
0284 
0285     /// Add a new constant by named reference to the detector description
0286     virtual Detector& addConstant(const Handle<NamedObject>& element) = 0;
0287     /// Add a new visualisation attribute by named reference to the detector description
0288     virtual Detector& addVisAttribute(const Handle<NamedObject>& element) = 0;
0289     /// Add a new limit set by named reference to the detector description
0290     virtual Detector& addLimitSet(const Handle<NamedObject>& limset) = 0;
0291     /// Add a new id descriptor by named reference to the detector description
0292     virtual Detector& addIDSpecification(const Handle<NamedObject>& element) = 0;
0293     /// Add a new detector region by named reference to the detector description
0294     virtual Detector& addRegion(const Handle<NamedObject>& region) = 0;
0295     /// Add a new detector readout by named reference to the detector description
0296     virtual Detector& addReadout(const Handle<NamedObject>& readout) = 0;
0297     /// Add a new sensitive detector by named reference to the detector description
0298     virtual Detector& addSensitiveDetector(const Handle<NamedObject>& element) = 0;
0299     /// Add a new subdetector by named reference to the detector description
0300     virtual Detector& addDetector(const Handle<NamedObject>& detector) = 0;
0301     /// Add a field component by named reference to the detector description
0302     virtual Detector& addField(const Handle<NamedObject>& field) = 0;
0303     
0304     /// Deprecated call (use fromXML): Read compact geometry description or alignment file
0305     virtual void fromCompact(const std::string& fname,
0306                              DetectorBuildType type = BUILD_DEFAULT) = 0;
0307     /// Read any geometry description or alignment file
0308     virtual void fromXML    (const std::string& fname,
0309                              DetectorBuildType type = BUILD_DEFAULT) = 0;
0310     /// Read any geometry description or alignment file with external XML entity resolution
0311     virtual void fromXML    (const std::string& fname,
0312                              xml::UriReader* entity_resolver,
0313                              DetectorBuildType type = BUILD_DEFAULT) = 0;
0314 
0315     /// Stupid legacy method
0316     virtual void dump() const = 0;
0317     /// Manipulate geometry using factory converter
0318     virtual long apply(const char* factory, int argc, char** argv)  const = 0;
0319 
0320     /// Add an extension object to the detector element (low level member function)
0321     virtual void* addUserExtension(unsigned long long int key, ExtensionEntry* entry) = 0;
0322 
0323     /// Remove an existing extension object from the Detector instance.
0324     /** If not destroyed, the instance is returned  (low level member function) */
0325     virtual void* removeUserExtension(unsigned long long int key, bool destroy) = 0;
0326 
0327     /// Access an existing extension object from the detector element (low level member function)
0328     virtual void* userExtension(unsigned long long int key, bool alert=true) const = 0;
0329 
0330     /// Extend the sensitive detector element with an arbitrary structure accessible by the type
0331     template <typename IFACE, typename CONCRETE> IFACE* addExtension(CONCRETE* c)  {
0332       return (IFACE*) addUserExtension(detail::typeHash64<IFACE>(),
0333                                        new detail::DeleteExtension<IFACE,CONCRETE>(c));
0334     }
0335 
0336     /// Remove an existing extension object from the Detector instance. If not destroyed, the instance is returned
0337     template <class IFACE> IFACE* removeExtension(bool destroy=true)  {
0338       return (IFACE*) removeUserExtension(detail::typeHash64<IFACE>(),destroy);
0339     }
0340 
0341     /// Access extension element by the type
0342     template <class IFACE> IFACE* extension(bool alert=true) const {
0343       return (IFACE*) userExtension(detail::typeHash64<IFACE>(),alert);
0344     }
0345 
0346     ///---Factory method-------
0347     static Detector& getInstance(const std::string& name="default");
0348     /// Destroy the singleton instance
0349     static void destroyInstance(const std::string& name="default");
0350     /// Unique creation without internal registration
0351     static std::unique_ptr<Detector> make_unique(const std::string& name);
0352 
0353   };
0354 
0355   /*
0356    *   The following are convenience implementations to access constants by type.
0357    *   I do not think this violates the interface approach, but it is so much
0358    *   more intuitiv to say constant<int>(name) than constantAsInt(name).
0359    */
0360 #ifndef __CINT__
0361   /// Typed access to constants: short values
0362   template <> inline short Detector::constant<short>(const std::string& name) const {
0363     return (short) constantAsLong(name);
0364   }
0365 
0366   /// Typed access to constants: unsigned short values
0367   template <> inline unsigned short Detector::constant<unsigned short>(const std::string& name) const {
0368     return (unsigned short) constantAsLong(name);
0369   }
0370 
0371   /// Typed access to constants: integer values
0372   template <> inline int Detector::constant<int>(const std::string& name) const {
0373     return (int) constantAsLong(name);
0374   }
0375 
0376   /// Typed access to constants: unsigned integer values
0377   template <> inline unsigned int Detector::constant<unsigned int>(const std::string& name) const {
0378     return (unsigned int) constantAsLong(name);
0379   }
0380 
0381   /// Typed access to constants: long values
0382   template <> inline long Detector::constant<long>(const std::string& name) const {
0383     return constantAsLong(name);
0384   }
0385 
0386   /// Typed access to constants: unsigned long values
0387   template <> inline unsigned long Detector::constant<unsigned long>(const std::string& name) const {
0388     return (unsigned long) constantAsLong(name);
0389   }
0390 
0391   /// Typed access to constants: float values
0392   template <> inline float Detector::constant<float>(const std::string& name) const {
0393     return (float) constantAsDouble(name);
0394   }
0395 
0396   /// Typed access to constants: double values
0397   template <> inline double Detector::constant<double>(const std::string& name) const {
0398     return constantAsDouble(name);
0399   }
0400 
0401   /// Typed access to constants: string values
0402   template <> inline std::string Detector::constant<std::string>(const std::string& name) const {
0403     return constantAsString(name);
0404   }
0405 #endif
0406 }         /* End namespace dd4hep           */
0407 #endif // DD4HEP_DETECTOR_H