All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ICARUSutils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 __doc__ = """
5 Collection of utilities to interface ICARUS with python, gallery and LArSoft.
6 
7 This module requires ROOT.
8 """
9 
10 __all__ = [
11  'loadICARUSgeometry',
12  'justLoadICARUSgeometry',
13 ]
14 
15 import galleryUtils
16 import LArSoftUtils
17 import ROOTutils
18 from ROOTutils import ROOT
19 
20 
21 ################################################################################
22 ICARUSchannelMappings = {
23  'ICARUSsplitInductionChannelMapSetupTool': {
24  'tool_type': 'ICARUSsplitInductionChannelMapSetupTool',
25  'mapperClassName': 'icarus::ICARUSChannelMapAlg',
26  'load': [
27  'larcorealg_Geometry',
28  'icarusalg/Geometry/ICARUSChannelMapAlg.h',
29  'icarusalg/Geometry/ICARUSstandaloneGeometrySetup.h',
30  'icarusalg_Geometry',
31  ],
32  }, # 'ICARUSsplitInductionChannelMapSetupTool'
33 }
34 DefaultChannelMapping = 'ICARUSsplitInductionChannelMapSetupTool'
35 
36 ################################################################################
37 ### Geometry
38 ###
40  config: "ConfigurationClass object with complete job configuration",
41  registry: "ServiceRegistryClass object with the configuration of all services",
42  ) -> "configuration of channel mapping algorithm as a FHiCL parameter set":
43 
44  #
45  # Try first if there is a configuration in the geometry service configuration;
46  # this is the "default" for the future. If not, back up to
47  # ExptGeoHelperInterface service.
48  #
49 
50  serviceName = 'Geometry'
51  try:
52  serviceConfig = config.service(serviceName) if config else registry.config(serviceName)
53  except Exception: serviceConfig = None
54  if serviceConfig and serviceConfig.has_key('ChannelMapping'):
55  mapperConfig = galleryUtils.getTableIfPresent(serviceConfig, 'ChannelMapping')
56  else:
57  serviceName = 'ExptGeoHelperInterface'
58  serviceConfig = config.service(serviceName) if config else registry.config(serviceName)
59  if serviceConfig is None:
60  raise RuntimeError("Failed to retrieve the configuration for %s service" % serviceName)
61  if serviceConfig.get(str)('service_provider') != 'IcarusGeometryHelper':
62  raise RuntimeError(
63  "{} in configuration is '{}', not IcarusGeometryHelper"
64  .format(serviceName, serviceConfig['service_provider'])
65  )
66  # if
67  mapperConfig = galleryUtils.getTableIfPresent(serviceConfig, 'Mapper')
68  # if no mapper in geometry service (or no geometry service??)
69 
70  if mapperConfig:
71  try:
72  plugin_type = mapperConfig.get(str)('tool_type')
73  except:
74  raise RuntimeError(
75  "{} service configuration of channel mapping is missing the tool_type:\n{}"
76  .format(serviceName, mapperConfig.to_indented_string(" "))
77  )
78  # try ... except
79  else: plugin_type = DefaultChannelMapping
80 
81  return plugin_type
82 # getChannelMappingConfiguration()
83 
84 
86  config: "ConfigurationClass object with complete job configuration",
87  registry: "ServiceRegistryClass object with the configuration of all services",
88  ) -> "Class object for the proper channel mapping":
89 
90  #
91  # we need to:
92  # 1. find out which mapping is required
93  # 2. load the proper libraries
94  # 3. return the Python class object for the mapping class we want
95  #
96 
97  #
98  # 1. find out which mapping is required: known configurations
99  #
100  plugin_type = getChannelMappingConfiguration(config=config, registry=registry)
101 
102  #
103  # 2. load the proper libraries
104  #
105 
106  # get the specification record
107  try: mappingInfo = ICARUSchannelMappings[plugin_type]
108  except KeyError:
109  # when you get to this error, check that the tool name in the configuration
110  # is actually spelled correctly first...
111  raise RuntimeError(
112  "Mapping plug in not supported: '{}': Python library needs to be updated."
113  .format(plugin_type)
114  )
115  # try ... except
116 
117  # load the libraries
118  for codeObj in mappingInfo.get('load', []):
119  LArSoftUtils.SourceCode.load(codeObj)
120 
121  # get the class object
122  try: mapperClass = ROOTutils.getROOTclass(mappingInfo['mapperClassName'])
123  except AttributeError:
124  # this needs investigation, as the code above should be sufficient to it
125  raise RuntimeError(
126  "The library with '{}' has not been correctly loaded!"
127  .format(mappingInfo['mapperClassName'])
128  )
129  # try ... except
130 
131  #
132  # 3. return the Python class object for the mapping class we want
133  #
134  return mapperClass
135 
136 # loadICARUSchannelMappingClass()
137 
138 
140  config = None, registry = None, mappingClass = None,
141  ):
142  """Loads and returns ICARUS geometry with the standard ICARUS channel mapping.
143 
144  See `loadGeometry()` for the meaning of the arguments.
145  """
146 
147  if mappingClass is None:
148  mappingClass = loadICARUSchannelMappingClass(config=config, registry=registry)
149  return LArSoftUtils.loadGeometry \
150  (config=config, registry=registry, mapping=mappingClass)
151 # loadICARUSgeometry()
152 
153 
154 def justLoadICARUSgeometry(configFile, mappingClass = None):
155  """Loads and returns ICARUS geometry from the specified configuration file.
156 
157  This is a one-stop procedure recommended only when running interactively.
158  """
159  return loadICARUSgeometry(config=LArSoftUtils.ConfigurationClass(configFile))
160 # justLoadICARUSgeometry()
161 
162 
163 ################################################################################
def getChannelMappingConfiguration
Geometry.
Definition: ICARUSutils.py:40
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
Definition: fclmodule.cxx:374
def loadICARUSchannelMappingClass
Definition: ICARUSutils.py:86
def justLoadICARUSgeometry
Definition: ICARUSutils.py:154
def loadICARUSgeometry
Definition: ICARUSutils.py:141