Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:12:34

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_FACTORIES_H
0014 #define DD4HEP_FACTORIES_H
0015 
0016 // Framework include files
0017 #include <DD4hep/Plugins.h>
0018 #include <DD4hep/DetElement.h>
0019 #include <DD4hep/NamedObject.h>
0020 
0021 // C/C++ include files
0022 #include <cstdarg>
0023 
0024 /// Namespace for the AIDA detector description toolkit
0025 namespace dd4hep {
0026 
0027   // Forward declarations
0028   class Detector;
0029   class NamedObject;
0030   class SensitiveDetector;
0031   class SegmentationObject;
0032   class DetElement;
0033 
0034   /// Namespace for the AIDA detector description toolkit supporting JSON utilities
0035   namespace json {
0036     class Handle_t;
0037   }
0038   /// Namespace for the AIDA detector description toolkit supporting XML utilities
0039   namespace xml {
0040     class Handle_t;
0041     class RefElement;
0042   }
0043 
0044   /// Namespace describing generic detector segmentations
0045   namespace DDSegmentation  {
0046     class BitFieldCoder;
0047   }
0048   
0049   
0050   /// Template class with a generic constructor signature
0051   /**
0052    *  \author  M.Frank
0053    *  \version 1.0
0054    *  \date    2012/07/31
0055    *  \ingroup DD4HEP_CORE
0056    */
0057   template <typename T> class SimpleConstructionFactory {
0058   public:
0059     static void* create();
0060   };
0061   template <typename T> class ConstructionFactory {
0062   public:
0063     static void* create(const char* arg);
0064   };
0065 
0066   /// Template class for a generic dd4hep object constructor
0067   /**
0068    *  \author  M.Frank
0069    *  \version 1.0
0070    *  \date    2012/07/31
0071    *  \ingroup DD4HEP_CORE
0072    */
0073   template <typename T> class DetectorConstructionFactory : public PluginFactoryBase {
0074   public:
0075     static void* create(Detector& description, int argc, char** argv);
0076   };
0077 
0078   /// Template class for a generic segmentation object constructor
0079   /**
0080    *  \author  M.Frank
0081    *  \version 1.0
0082    *  \date    2012/07/31
0083    *  \ingroup DD4HEP_CORE
0084    */
0085   template <typename T> class SegmentationFactory : public PluginFactoryBase {
0086   public:
0087     static SegmentationObject* create(const DDSegmentation::BitFieldCoder* decoder);
0088   };
0089 
0090   /// Template class with a generic signature to apply Detector plugins
0091   /**
0092    *  Specialized factory to translate objects, which can be retrieved from Detector
0093    *  Example: Translate dd4hep geometry to Geant4
0094    *
0095    *  \author  M.Frank
0096    *  \version 1.0
0097    *  \date    2012/07/31
0098    *  \ingroup DD4HEP_CORE
0099    */
0100   template <typename T> class ApplyFactory : public PluginFactoryBase {
0101   public:
0102     static long create(Detector& description, int argc, char** argv);
0103   };
0104 
0105   /// Specialized factory to translate objects, which can be retrieved from Detector
0106   /**
0107    *  Example: Translate dd4hep geometry to Geant4
0108    *
0109    *  \author  M.Frank
0110    *  \version 1.0
0111    *  \date    2012/07/31
0112    *  \ingroup DD4HEP_CORE
0113    */
0114   template <typename T> class TranslationFactory : public PluginFactoryBase {
0115   public:
0116     static Handle<NamedObject> create(Detector& description);
0117   };
0118 
0119   /// Create an arbitrary object from its XML representation.
0120   /**
0121    *
0122    *  \author  M.Frank
0123    *  \version 1.0
0124    *  \date    2012/07/31
0125    *  \ingroup DD4HEP_CORE
0126    */
0127   template <typename T> class XMLElementFactory : public PluginFactoryBase {
0128   public:
0129     static Handle<NamedObject> create(Detector& description, xml::Handle_t e);
0130   };
0131 
0132   /// Standard factory to create ROOT objects from an XML representation.
0133   /**
0134    *
0135    *  \author  M.Frank
0136    *  \version 1.0
0137    *  \date    2012/07/31
0138    *  \ingroup DD4HEP_CORE
0139    */
0140   template <typename T> class XMLObjectFactory : public PluginFactoryBase {
0141   public:
0142     static Handle<TObject> create(Detector& description, xml::Handle_t e);
0143   };
0144   
0145   ///  Read an arbitrary XML document and analyze its content
0146   /**
0147    *
0148    *  \author  M.Frank
0149    *  \version 1.0
0150    *  \date    2012/07/31
0151    *  \ingroup DD4HEP_CORE
0152    */
0153   template <typename T> class XMLDocumentReaderFactory : public PluginFactoryBase {
0154   public:
0155     static long create(Detector& description, xml::Handle_t e);
0156   };
0157 
0158   /// Read an arbitrary XML document and analyze its content
0159   /**
0160    *
0161    *  \author  M.Frank
0162    *  \version 1.0
0163    *  \date    2012/07/31
0164    *  \ingroup DD4HEP_CORE
0165    */
0166   template <typename T> class XMLConversionFactory : public PluginFactoryBase {
0167   public:
0168     static long create(Detector& description, xml::RefElement& handle, xml::Handle_t element);
0169   };
0170 
0171   /// Standard factory to create Detector elements from an XML representation.
0172   /**
0173    *
0174    *  \author  M.Frank
0175    *  \version 1.0
0176    *  \date    2012/07/31
0177    *  \ingroup DD4HEP_CORE
0178    */
0179   template <typename T> class XmlDetElementFactory : public PluginFactoryBase {
0180   public:
0181     static Handle<NamedObject> create(Detector& description, xml::Handle_t e, Handle<NamedObject> sens);
0182   };
0183 
0184   /// Standard factory to create Detector elements from the compact XML representation.
0185   /**
0186    *
0187    *  \author  M.Frank
0188    *  \version 1.0
0189    *  \date    2012/07/31
0190    *  \ingroup DD4HEP_CORE
0191    */
0192   template <typename T> class JsonDetElementFactory : public PluginFactoryBase {
0193   public:
0194     static Handle<NamedObject> create(Detector& description, json::Handle_t e, Handle<NamedObject> sens);
0195   };
0196 }
0197 
0198 namespace {
0199 
0200   /// Forward declartion of the base factory template
0201   template <typename P, typename S> class Factory;
0202 
0203   /// Helper structure to shortcut type definitions for the factories
0204   namespace ns {
0205     typedef dd4hep::NamedObject         Named;
0206     typedef dd4hep::xml::Handle_t       xml_h;
0207     typedef dd4hep::json::Handle_t      json_h;
0208     typedef dd4hep::Handle<Named>       ref_t;
0209     typedef dd4hep::SegmentationObject  SegmentationObject;
0210     typedef dd4hep::DDSegmentation::BitFieldCoder   BitFieldCoder;
0211   }
0212 
0213   DD4HEP_PLUGIN_FACTORY_ARGS_0(void*)  
0214   {    return dd4hep::SimpleConstructionFactory<P>::create();                           }
0215 
0216   DD4HEP_PLUGIN_FACTORY_ARGS_1(void*,const char*)  
0217   {    return dd4hep::ConstructionFactory<P>::create(a0);                               }
0218 
0219   DD4HEP_PLUGIN_FACTORY_ARGS_1(ns::Named*,dd4hep::Detector*)
0220   {    return dd4hep::TranslationFactory<P>::create(*a0).ptr();                         }
0221 
0222   DD4HEP_PLUGIN_FACTORY_ARGS_1(ns::SegmentationObject*,const ns::BitFieldCoder*)
0223   {    return dd4hep::SegmentationFactory<P>::create(a0);                               }
0224 
0225   DD4HEP_PLUGIN_FACTORY_ARGS_3(void*,dd4hep::Detector*,int,char**)
0226   {    return dd4hep::DetectorConstructionFactory<P>::create(*a0,a1,a2);                }
0227 
0228   DD4HEP_PLUGIN_FACTORY_ARGS_3(long,dd4hep::Detector*,int,char**)  {
0229     static long ret;
0230     long result = dd4hep::ApplyFactory<P>::create(*a0,a1,a2);
0231     ret = result;
0232     return long(&ret);
0233   }
0234 
0235   DD4HEP_PLUGIN_FACTORY_ARGS_2(ns::Named*,dd4hep::Detector*,ns::xml_h*)
0236   {    return dd4hep::XMLElementFactory<P>::create(*a0,*a1).ptr();                      }
0237 
0238   DD4HEP_PLUGIN_FACTORY_ARGS_2(TObject*,dd4hep::Detector*,ns::xml_h*)
0239   {    return dd4hep::XMLObjectFactory<P>::create(*a0,*a1).ptr();                       }
0240 
0241   DD4HEP_PLUGIN_FACTORY_ARGS_2(long,dd4hep::Detector*,ns::xml_h*)    {
0242     static long ret;
0243     long result = dd4hep::XMLDocumentReaderFactory<P>::create(*a0,*a1);
0244     ret = result;
0245     return long(&ret);
0246   }
0247 
0248   DD4HEP_PLUGIN_FACTORY_ARGS_3(ns::Named*,dd4hep::Detector*,ns::xml_h*,ns::ref_t*)
0249   {    return dd4hep::XmlDetElementFactory<P>::create(*a0,*a1,*a2).ptr();               }
0250 
0251   DD4HEP_PLUGIN_FACTORY_ARGS_3(ns::Named*,dd4hep::Detector*,ns::json_h*,ns::ref_t*)
0252   {    return dd4hep::JsonDetElementFactory<P>::create(*a0,*a1,*a2).ptr();              }
0253 }
0254 
0255 #define DECLARE_DETELEMENT_FACTORY(x)               namespace dd4hep    \
0256   { DD4HEP_PLUGINSVC_FACTORY(x,x,dd4hep::NamedObject*(dd4hep::Detector*,xml::Handle_t*,Ref_t*),__LINE__) }
0257 #define DECLARE_NAMESPACE_DETELEMENT_FACTORY(n,x)   namespace dd4hep    \
0258   { DD4HEP_PLUGINSVC_FACTORY(n::x,x,NamedObject*(dd4hep::Detector*,xml::Handle_t*,Ref_t*),__LINE__)      }
0259 #define DECLARE_NAMED_APPLY_FACTORY(n,x)            namespace dd4hep    \
0260   { DD4HEP_PLUGINSVC_FACTORY(n::x,x,long(dd4hep::Detector*,int, char**),__LINE__) }
0261 #define DECLARE_NAMED_TRANSLATION_FACTORY(n,x)      namespace dd4hep    \
0262   { DD4HEP_PLUGINSVC_FACTORY(n::x,x,dd4hep::NamedObject*(dd4hep::Detector*),__LINE__) }
0263 #define DECLARE_NAMED_XMLELEMENT_FACTORY(n,x)       namespace dd4hep    \
0264   { DD4HEP_PLUGINSVC_FACTORY(n::x,x,dd4hep::NamedObject*(dd4hep::Detector*,xml::Handle_t*),__LINE__) }
0265 #define DECLARE_NAMED_DETELEMENT_FACTORY(n,x)       namespace dd4hep    \
0266   { DD4HEP_PLUGINSVC_FACTORY(n::x,x,dd4hep::*(),__LINE__) }
0267 
0268 // Call function of the type [SegmentationObject (*func)(dd4hep::Detector*,DDSegmentation::BitFieldCoder*)]
0269 #define DECLARE_SEGMENTATION(name,func) DD4HEP_OPEN_PLUGIN(dd4hep,name)   { \
0270     template <> SegmentationObject*                           \
0271       SegmentationFactory<name>::create(const DDSegmentation::BitFieldCoder* d) { return func(d); } \
0272     DD4HEP_PLUGINSVC_FACTORY(name,segmentation_constructor__##name,     \
0273                              SegmentationObject*(const DDSegmentation::BitFieldCoder*),__LINE__)}
0274 
0275 // Call function of the type [void* (*func)()]
0276 #define DECLARE_CREATE(name,func)        DD4HEP_OPEN_PLUGIN(dd4hep,name)   { \
0277     template <> void* SimpleConstructionFactory<name>::create() { return func(); } \
0278     DD4HEP_PLUGINSVC_FACTORY(name,name,void*(),__LINE__)}
0279 
0280 // Call function of the type [long (*func)(const char* arg)]
0281 #define DECLARE_APPLY(name,func)        DD4HEP_OPEN_PLUGIN(dd4hep,name)   { \
0282     template <> long ApplyFactory<name>::create(dd4hep::Detector& l,int n,char** a) {return func(l,n,a);} \
0283     DD4HEP_PLUGINSVC_FACTORY(name,name,long(dd4hep::Detector*,int,char**),__LINE__)}
0284 
0285 // Call function of the type [void* (*func)(const char* arg)]
0286 #define DECLARE_CONSTRUCTOR(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,name) { \
0287     template <> void* ConstructionFactory<name>::create(const char* n) { return func(n);} \
0288     DD4HEP_PLUGINSVC_FACTORY(name,name,void*(const char*),__LINE__) }
0289 
0290 // Call function of the type [void* (*func)(dd4hep::Detector& description, int argc,char** argv)]
0291 #define DECLARE_DD4HEP_CONSTRUCTOR(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,name) { \
0292     template <> void* DetectorConstructionFactory<name>::create(dd4hep::Detector& l, int n,char** a) { return func(l,n,a);} \
0293     DD4HEP_PLUGINSVC_FACTORY(name,name,void*(dd4hep::Detector*,int,char**),__LINE__) }
0294 
0295 // Call function of the type [void* (*func)(dd4hep::Detector& description)]
0296 #define DECLARE_TRANSLATION(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,name)  { \
0297     template <> Ref_t TranslationFactory<name>::create(dd4hep::Detector& l) {return func(l);} \
0298     DECLARE_NAMED_TRANSLATION_FACTORY(Geometry,name)  }
0299 
0300 // Call function of the type [void* (*func)(dd4hep::Detector& description, xml_h handle)]
0301 #define DECLARE_XMLELEMENT(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,xml_element_##name)  {\
0302     template <> Ref_t XMLElementFactory<xml_element_##name>::create(dd4hep::Detector& l,ns::xml_h e) {return func(l,e);} \
0303     DD4HEP_PLUGINSVC_FACTORY(xml_element_##name,name,NamedObject*(dd4hep::Detector*,ns::xml_h*),__LINE__)  }
0304 
0305 // Call function of the type [void* (*func)(dd4hep::Detector& description, xml_h handle)]
0306 #define DECLARE_XML_SHAPE(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,xml_element_##name)  {\
0307     template <> Handle<TObject> XMLObjectFactory<xml_element_##name>::create(dd4hep::Detector& l,ns::xml_h e) {return func(l,e);}\
0308     DD4HEP_PLUGINSVC_FACTORY(xml_element_##name,name,TObject*(dd4hep::Detector*,ns::xml_h*),__LINE__)  }
0309 
0310 // Call function of the type [void* (*func)(dd4hep::Detector& description, xml_h handle)]
0311 #define DECLARE_XML_VOLUME(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,xml_element_##name)  {\
0312     template <> Handle<TObject> XMLObjectFactory<xml_element_##name>::create(dd4hep::Detector& l,ns::xml_h e) {return func(l,e);}\
0313     DD4HEP_PLUGINSVC_FACTORY(xml_element_##name,name,TObject*(dd4hep::Detector*,ns::xml_h*),__LINE__)  }
0314 
0315 // Call function of the type [long (*func)(dd4hep::Detector& description, xml_h handle)]
0316 #define DECLARE_XML_DOC_READER(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,xml_document_##name)  { \
0317     template <> long XMLDocumentReaderFactory<xml_document_##name>::create(dd4hep::Detector& l,ns::xml_h e) {return func(l,e);} \
0318     DD4HEP_PLUGINSVC_FACTORY(xml_document_##name,name##_XML_reader,long(dd4hep::Detector*,ns::xml_h*),__LINE__)  }
0319 
0320 // Call function of the type [long (*func)(dd4hep::Detector& description, xml_h handle)]
0321 #define DECLARE_XML_PLUGIN(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,xml_document_##name)  { \
0322     template <> long XMLDocumentReaderFactory<xml_document_##name>::create(dd4hep::Detector& l,ns::xml_h e) {return func(l,e);} \
0323     DD4HEP_PLUGINSVC_FACTORY(xml_document_##name,name,long(dd4hep::Detector*,ns::xml_h*),__LINE__)  }
0324 
0325 // Call function of the type [NamedObject* (*func)(dd4hep::Detector& description, xml_h handle, ref_t reference)]
0326 #define DECLARE_XML_PROCESSOR_BASIC(name,func,deprecated)  DD4HEP_OPEN_PLUGIN(dd4hep,det_element_##name) {\
0327     template <> Ref_t XmlDetElementFactory< det_element_##name >::create(dd4hep::Detector& l,ns::xml_h e,ns::ref_t h) \
0328     { if (deprecated) warning_deprecated_xml_factory(#name); return func(l,e,h);} \
0329     DD4HEP_PLUGINSVC_FACTORY(det_element_##name,name,NamedObject*(dd4hep::Detector*,ns::xml_h*,Ref_t*),__LINE__)  }
0330 
0331 // Call function of the type [NamedObject* (*func)(dd4hep::Detector& description, json_h handle, ref_t reference)]
0332 #define DECLARE_JSON_PROCESSOR_BASIC(name,func)  DD4HEP_OPEN_PLUGIN(dd4hep,det_element_##name) { \
0333     template <> Ref_t JsonDetElementFactory< det_element_##name >::create(dd4hep::Detector& l,ns::json_h e,ns::ref_t h) \
0334     { return func(l,e,h);}                                              \
0335     DD4HEP_PLUGINSVC_FACTORY(det_element_##name,name,NamedObject*(dd4hep::Detector*,ns::json_h*,ns::ref_t*),__LINE__)  }
0336 
0337 #define DECLARE_XML_PROCESSOR(name,func)          DECLARE_XML_PROCESSOR_BASIC(name,func,0)
0338 #define DECLARE_SUBDETECTOR(name,func)            DECLARE_XML_PROCESSOR_BASIC(name,func,0)
0339 #define DECLARE_DETELEMENT(name,func)             DECLARE_XML_PROCESSOR_BASIC(name,func,0)
0340 #define DECLARE_DEPRECATED_DETELEMENT(name,func)  DECLARE_XML_PROCESSOR_BASIC(name,func,1)
0341 
0342 #define DECLARE_JSON_DETELEMENT(name,func)        DECLARE_JSON_PROCESSOR_BASIC(name,func)
0343 
0344 #endif // DD4HEP_FACTORIES_H
0345