Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:58:00

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_DETECTORPROCESSOR_H
0014 #define DD4HEP_DETECTORPROCESSOR_H
0015 
0016 // Framework includes
0017 #include <DD4hep/DetElement.h>
0018 
0019 // C/C++ include files
0020 #include <memory>
0021 
0022 /// Namespace for the AIDA detector description toolkit
0023 namespace dd4hep {
0024 
0025   /// Generic Detector processor
0026   /**
0027    *   Please note that the principle of locality applies:
0028    *   The object is designed for stack allocation and configuration.
0029    *   It may NOT be shared across threads!
0030    *
0031    *   \author  M.Frank
0032    *   \version 1.0
0033    *   \date    31/05/2017
0034    *   \ingroup DD4HEP_CORE
0035    */
0036   class DetectorProcessor {
0037   public:
0038     /// Initializing constructor
0039     DetectorProcessor() = default;
0040     /// R-value copy from a temporary (Since processor is reference)
0041     DetectorProcessor(DetectorProcessor&& copy) = default;
0042     /// Default copy constructor
0043     DetectorProcessor(const DetectorProcessor& copy) = default;
0044     /// Default destructor
0045     virtual ~DetectorProcessor();
0046     /// Default assignment
0047     DetectorProcessor& operator=(const DetectorProcessor& copy) = default;
0048     /// Callback to output detector information of an single DetElement
0049     virtual int operator()(DetElement de, int level)  const = 0;
0050     /// Callback to output detector information of an entire DetElement
0051     virtual int process(DetElement de, int level, bool recursive)  const;
0052   };
0053 
0054   /// Detector scanner using a Processor object
0055   /**
0056    *   Please see the documentation of the
0057    *   DetectorProcessor base class for further information.
0058    *
0059    *   \author  M.Frank
0060    *   \version 1.0
0061    *   \date    31/05/2017
0062    *   \ingroup DD4HEP_CORE
0063    */
0064   template <typename T> class DetElementProcessor : virtual public DetectorProcessor  {
0065   public:
0066     /// Reference to execution object implementing operator()(DetElement de, int level)
0067     T& processor;
0068   public:
0069     /// Default constructor
0070     DetElementProcessor() = delete;
0071     /// Default constructor
0072     DetElementProcessor(T& p) : processor(p) {}
0073     /// Default move constructor is disabled
0074     DetElementProcessor(T&& p) = delete;
0075     /// R-value copy from a temporary (Since processor is reference)
0076     DetElementProcessor(DetElementProcessor&& copy) = default;
0077     /// Default copy constructor
0078     DetElementProcessor(const DetElementProcessor& copy) = default;
0079     /// Default destructor
0080     virtual ~DetElementProcessor() = default;
0081     /// Default assignment
0082     DetElementProcessor& operator=(const DetElementProcessor& copy) = default;
0083     /// Callback to output detector information of an single DetElement
0084     virtual int operator()(DetElement de, int level)  const final
0085     {   return (processor)(de, level);         }
0086   };
0087 
0088   /// Instantiation helper
0089   template <typename T> inline
0090   DetElementProcessor<typename std::remove_reference<T>::type> detectorProcessor(T&& proc)
0091   { return DetElementProcessor<typename std::remove_reference<T>::type>(std::forward<T>(proc)); }
0092 
0093   /// Wrapper to call objects in the form of a detector element processor.
0094   /**
0095    *   \author  M.Frank
0096    *   \version 1.0
0097    *   \date    31/05/2017
0098    *   \ingroup DD4HEP_CORE
0099    */
0100   template <typename T> class DetectorProcessorShared : public DetectorProcessor {
0101   public:
0102     /// Reference to execution object implementing operator()(DetElement de, int level)
0103     std::shared_ptr<T> processor;
0104   public:
0105     /// Default constructor
0106     DetectorProcessorShared() = delete;
0107     /// Default constructor
0108     DetectorProcessorShared(std::shared_ptr<T>& p) : processor(p) {}
0109     /// Default copy constructor
0110     DetectorProcessorShared(const DetectorProcessorShared& copy) = default;
0111     /// Default destructor
0112     virtual ~DetectorProcessorShared() = default;
0113     /// Default assignment
0114     DetectorProcessorShared& operator=(const DetectorProcessorShared& copy) = default;
0115     /// Callback to output detector information of an single DetElement
0116     virtual int operator()(DetElement de, int level)  const final
0117     {  return (*processor)(de, level);                 }
0118   };
0119 
0120   /// Generic detector element collector of a sub-tree
0121   /**
0122    *   To be used with utilities like DetElementProcessor etc.
0123    *
0124    *  
0125    *  \author  M.Frank
0126    *  \version 1.0
0127    *  \date    01/04/2016
0128    */
0129   template <typename T> class DetElementsCollector  {
0130   public:
0131     /// Collection container
0132     T&             elements;
0133   public:
0134     /// Default constructor
0135     DetElementsCollector(T& d) : elements(d) {}
0136     /// Default move constructor is disabled
0137     DetElementsCollector(T&& p) = delete;
0138     /// R-value copy from a temporary
0139     DetElementsCollector(DetElementsCollector&& copy) = default;
0140     /// Copy constructor
0141     DetElementsCollector(const DetElementsCollector& copy) = default;
0142     /// Default destructor
0143     ~DetElementsCollector() = default;
0144     /// Assignment operator
0145     DetElementsCollector& operator=(const DetElementsCollector& copy) = default;
0146     /// Callback to output elements information
0147     /** Note: Valid implementations exist for the container types:
0148      *        std::set<DetElement>
0149      *        std::list<DetElement>
0150      *        std::vector<DetElement>
0151      */
0152     virtual int operator()(DetElement de, int level)  const final;
0153   };
0154   
0155   /// Creator utility function for DetElementsCollector objects
0156   template <typename T> inline
0157   DetElementsCollector<typename std::remove_reference<T>::type> detElementsCollector(T&& container)
0158   {  return DetElementsCollector<typename std::remove_reference<T>::type>(container); }
0159 
0160   /// Helper to run DetElement scans
0161   /**
0162    *   This wrapper converts any object, which has the signature
0163    *   int operator()(DetElement de, int level) const
0164    *   The object is automatically wrapped to a DetectorProcessor
0165    *   and the detector tree is scanned depending on the scanning
0166    *   arguments.
0167    *  
0168    *   \author  M.Frank
0169    *   \version 1.0
0170    *   \date    01/04/2016
0171    *   \ingroup DD4HEP_CORE
0172    */
0173   class DetectorScanner  {
0174   public:
0175     /// Default constructor
0176     DetectorScanner() = default;
0177     /// Copy constructor
0178     DetectorScanner(const DetectorScanner& copy) = default;
0179     /// Assignment operator
0180     DetectorScanner& operator=(const DetectorScanner& copy) = default;
0181 
0182     /// Constructor performing the scan internally
0183     template <typename Q>
0184     DetectorScanner(Q& proc, DetElement start, int level=0, bool recursive=true)
0185     {  scan(proc, start, level, recursive);     }
0186 
0187     /// Constructor performing the scan internally
0188     template <typename Q>
0189     DetectorScanner(const Q& proc, DetElement start, int level=0, bool recursive=true)
0190     {  scan(proc, start, level, recursive);     }
0191 
0192     /// Detector element tree scanner using wrapped DetectorProcessor objects
0193     template <typename Q>
0194     int scan(Q& p, DetElement start, int level=0, bool recursive=true)  const {
0195       auto proc = detectorProcessor(p);
0196       return proc.process(start, level, recursive);
0197     }
0198 
0199     /// Detector element tree scanner using wrapped DetectorProcessor objects
0200     template <typename Q>
0201     int scan(const Q& p, DetElement start, int level=0, bool recursive=true) const {
0202       auto proc = detectorProcessor(p);
0203       return proc.process(start, level, recursive);
0204     }
0205   };
0206 }      /* End namespace dd4hep               */
0207 #endif // DD4HEP_DETECTORPROCESSOR_H