Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:16:28

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 
0014 // Framework include files
0015 #include <DD4hep/Detector.h>
0016 #include <XML/Conversions.h>
0017 #include <XML/XMLElements.h>
0018 #include <XML/DocumentHandler.h>
0019 #include <DD4hep/Printout.h>
0020 #include <DD4hep/DetectorTools.h>
0021 #include <DD4hep/DetFactoryHelper.h>
0022 
0023 #include <DDCond/ConditionsTags.h>
0024 #include <DDCond/ConditionsEntry.h>
0025 #include <DDCond/ConditionsManager.h>
0026 #include <DDCond/ConditionsDataLoader.h>
0027 
0028 /// Namespace for the AIDA detector description toolkit
0029 namespace dd4hep  {
0030 
0031   namespace {
0032     /// Some utility class to specialize the converters:
0033     class include;
0034     class arbitrary;
0035     class conditions;
0036 
0037     class iov;
0038     class iov_type;
0039     class manager;
0040     class repository;
0041     class detelement;
0042     class align_conditions;
0043     class align_arbitrary;
0044     /// Conditions types
0045     class value;
0046     class mapping;
0047     class sequence;
0048     class alignment;
0049     class position;
0050     class rotation;
0051     class pivot;
0052   }
0053   template <> void Converter<include>::operator()(xml_h seq)  const;
0054   template <> void Converter<arbitrary>::operator()(xml_h seq)  const;
0055   template <> void Converter<conditions>::operator()(xml_h seq)  const;
0056 }
0057   
0058 using namespace dd4hep::cond;
0059 
0060 /// Namespace for the AIDA detector description toolkit
0061 namespace dd4hep {
0062 
0063   /// Helper structure for data conversion
0064   struct ConversionArg {
0065     DetElement         detector;
0066     ConditionsStack*   stack;
0067     ConversionArg(DetElement det, ConditionsStack* stk)
0068       : detector(det), stack(stk)
0069     {
0070     }
0071   };
0072 
0073   /// Helper: Extract the validity from the xml element
0074   std::string _getValidity(xml_h elt)  {
0075     if ( !elt.ptr() )
0076       return "Infinite";
0077     else if ( !elt.hasAttr(_UC(validity)) )
0078       return _getValidity(elt.parent());
0079     return elt.attr<std::string>(_UC(validity));
0080   }
0081 
0082   /// Helper: Extract the required detector element from the parsing information
0083   DetElement _getDetector(void* param, xml_h e)  {
0084     ConversionArg* arg  = static_cast<ConversionArg*>(param);
0085     DetElement detector = arg ? arg->detector : DetElement();
0086     std::string     subpath  = e.hasAttr(_U(path)) ? e.attr<std::string>(_U(path)) : std::string();
0087     return subpath.empty() ? detector : detail::tools::findDaughterElement(detector,subpath);
0088   }
0089 
0090   /// Helper: Extract the string value from the xml element 
0091   Entry* _createStackEntry(void* param, xml_h element)  {
0092     xml_comp_t e(element);
0093     DetElement elt = _getDetector(param, element);
0094     std::string name = e.hasAttr(_U(name)) ? e.nameStr() : e.tag();
0095     return new Entry(elt,name,e.tag(),_getValidity(element),detail::hash32(name));
0096   }
0097 
0098   
0099   /** Convert arbitrary conditon objects containing standard tags
0100    *
0101    *    Function entry expects as a parameter a valid DetElement handle
0102    *    pointing to the subdetector, which detector elements should be 
0103    *    realigned.
0104    *
0105    *      <temperature path="/world/TPC" name="AbientTemperatur" value="20.9*Celcius"/>
0106    *      <temperature path="TPC" name="AbientTemperatur" value="20.9*Celcius"/>
0107    *      <temperature name="AbientTemperatur" value="20.9*Celcius"/>
0108    *
0109    *      <temperature name="AbientTemperatur" value="20.9*Kelvin"/>
0110    *      <pressure name="external_pressure" value="980*hPa"/>
0111    *      <include ref="..."/>
0112    *
0113    *    The object tag name is passed as the conditons type to the system.
0114    *    The data payload may either be specified as an attribute to the
0115    *    element or as text (data payload as the inner XML of the element).
0116    *
0117    *  These items have:
0118    *  - a name defining the condition within the detector element
0119    *  - a value interpreted as a double. In XML the value may be dressed with a unit
0120    *    which will be correctly treated by the expression evaluator
0121    *  - a path (optionally). attribute_values are ALWAYS treated within the context
0122    *    of the containing detector element. If pathes are relative, they are 
0123    *    relative to the embedding element. If pathes are absolute, the embedding
0124    *    element is ignored.
0125    *
0126    *  @author  M.Frank
0127    *  @version 1.0
0128    *  @date    01/04/2014
0129    */
0130   template <> void Converter<arbitrary>::operator()(xml_h e) const {
0131     xml_comp_t elt(e);
0132     std::string tag = elt.tag();
0133     ConversionArg* arg  = _param<ConversionArg>();
0134     if ( !arg )
0135       except("ConditionsParser","++ Invalid parser argument [Internal Error]");
0136     else if ( tag == "conditions" )  
0137       Converter<conditions>(description,param,optional)(e);
0138     else if ( arg->stack && tag == "detelement" )
0139       Converter<conditions>(description,param,optional)(e);
0140     else if ( tag == "open_transaction" )
0141       return;
0142     else if ( tag == "close_transaction" ) 
0143       return;
0144     else if ( tag == "include" )
0145       Converter<include>(description,param,optional)(e);
0146     else if ( tag == "detelements" )
0147       xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
0148     else if ( tag == "subdetectors" )
0149       xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
0150     else if ( tag == "alignment" )   {
0151       dd4hep_ptr<Entry> val(_createStackEntry(param,e));
0152       val->value = elt.attr<std::string>(_U(ref));
0153       if ( !arg->stack )
0154         except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
0155       else
0156         arg->stack->emplace_back(val.release());
0157     }
0158     else  {
0159       dd4hep_ptr<Entry> val(_createStackEntry(param,e));
0160       val->value = elt.hasAttr(_U(value)) ? elt.valueStr() : e.text();
0161       if ( !arg->stack )
0162         except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
0163       else
0164         arg->stack->emplace_back(val.release());
0165     }
0166   }
0167 
0168   /** Convert include objects
0169    *
0170    *  @author  M.Frank
0171    *  @version 1.0
0172    *  @date    01/04/2014
0173    */
0174   template <> void Converter<include>::operator()(xml_h element) const {
0175     xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
0176     xml_coll_t(doc.root(),_U(star)).for_each(Converter<arbitrary>(description,param,optional));
0177   }
0178 
0179   /** Convert objects containing standard conditions tags
0180    *
0181    *    Function entry expects as a parameter a valid DetElement handle
0182    *    pointing to the subdetector, which detector elements should be 
0183    *    realigned. A absolute or relative DetElement path may be supplied by
0184    *    the element as an attribute:
0185    *
0186    *    <conditions path="/world/TPC/TPC_SideA/TPC_SideA_sector02">
0187    *        ...
0188    *    </conditions>
0189    *
0190    *  @author  M.Frank
0191    *  @version 1.0
0192    *  @date    01/04/2014
0193    */
0194   template <> void Converter<conditions>::operator()(xml_h e) const {
0195     ConversionArg* arg  = _param<ConversionArg>();
0196     DetElement elt = arg->detector;
0197     arg->detector = _getDetector(param,e);
0198     xml_coll_t(e,_U(star)).for_each(Converter<arbitrary>(description,param,optional));
0199     arg->detector = elt;
0200   }
0201 }
0202 
0203 /** Basic entry point to read global conditions files
0204  *
0205  *  @author  M.Frank
0206  *  @version 1.0
0207  *  @date    01/04/2014
0208  */
0209 static void* setup_global_Conditions(dd4hep::Detector& description, int argc, char** argv)  {
0210   if ( argc == 2 )  {
0211     xml_h e = xml_h::Elt_t(argv[0]);
0212     ConditionsStack* stack = (ConditionsStack*)argv[1];
0213     dd4hep::ConversionArg args(description.world(), stack);
0214     (dd4hep::Converter<dd4hep::conditions>(description,&args))(e);
0215     return &description;
0216   }
0217   dd4hep::except("XML_DOC_READER","Invalid number of arguments to interprete conditions: %d != %d.",argc,2);
0218   return 0;
0219 }
0220 DECLARE_DD4HEP_CONSTRUCTOR(XMLConditionsParser,setup_global_Conditions)