00001 from Detector.PyDetector import detector_factory as _detector_factory
00002 import datasource
00003
00004 def _getEnv(local_env=None):
00005 if local_env is None:
00006 if datasource._global_env is None:
00007 raise RuntimeError('Detector object cannot be created before an instance'
00008 ' of psana.DataSource exists')
00009 return datasource._global_env
00010 else:
00011 return local_env
00012
00013 def Detector(name, local_env=None, accept_missing=False):
00014 """
00015 Create a python Detector from a string identifier.
00016
00017 Parameters
00018 ----------
00019 source_string : str
00020 A string identifying a piece of data to access, examples include:
00021 - 'cspad' # a DAQ detector alias
00022 - 'XppGon.0:Cspad.0' # a DAQ detector full-name
00023 - 'DIAG:FEE1:202:241:Data' # an EPICS variable name (or alias)
00024 - 'EBeam' # a BldInfo identifier\n
00025 The idea is that you should be able to pass something that makes
00026 sense to you as a human here, and you automatically get the right
00027 detector object in return.
00028
00029 local_env : psana.Env
00030 The psana environment object associated with the psana.DataSource
00031 you are interested in (from method DataSource.env()).
00032
00033 accept_missing : bool
00034 If False, KeyError exception will be raised if "name" is not
00035 present in the current DataSource. If True, an object will
00036 be returned that returns None for all method calls. This allows
00037 software to not crash if a detector is missing from a run.
00038
00039 Returns
00040 -------
00041 A psana-python detector object. Try detector(psana.Event) to
00042 access your data.
00043
00044 HOW TO GET MORE HELP
00045 --------------------
00046 The Detector method returns an object that has methods that
00047 change depending on the detector type. To see help for a particular
00048 detector type execute commands similar to the following
00049
00050 env = DataSource('exp=xpptut15:run=54').env()
00051 det = Detector('cspad',env)
00052
00053 and then use the standard ipython "det?" command (and tab completion) to
00054 see the documentation for that particular detector type.
00055 """
00056 env = _getEnv(local_env)
00057 return _detector_factory(name, env, accept_missing=accept_missing)
00058
00059 def _epicsNames(local_env=None):
00060 env = _getEnv(local_env)
00061 epics = env.epicsStore()
00062
00063 return [(pv, epics.alias(pv),'') for pv in epics.pvNames()]
00064
00065 def _detNames(local_env=None):
00066 env = _getEnv(local_env)
00067 amap = env.aliasMap()
00068 namelist = []
00069
00070 for s in amap.srcs():
00071 if str(s).find('EpicsArch')!=-1: continue
00072 namelist.append((str(s).split('(')[-1].split(')')[0],amap.alias(s),''))
00073 cstore = env.configStore()
00074 for k in cstore.keys():
00075 if 'ControlData.Config' in str(k.type()):
00076 namelist.append(('ControlData','',''))
00077 break
00078 return namelist
00079
00080 def DetNames(nametype='detectors',local_env=None):
00081 """
00082 Return tuples of detector names. Nametype should be one of
00083 'detectors','epics','all'. 'detectors' returns the names of all standard
00084 detectors included in the data. 'epics' returns the names of all epics
00085 variables (epics variables are typically updated at 1Hz with information
00086 about slow quantities like temperatures, voltages, motor positions).
00087 'all' returns both of the above.
00088
00089 Each detector can have 3 names: a "full name", a simpler "daq alias", or a
00090 "user alias". The last two are optional.
00091
00092 This routine takes an optional "DataSource.Env" argument. If not provided
00093 the Env from the most recently created DataSource will be used. It typically
00094 only needs to be specified when using multiple DataSource instances simultaneously.
00095 """
00096 if nametype=='detectors':
00097 return _detNames(local_env)
00098 elif nametype=='epics':
00099 return _epicsNames(local_env)
00100 elif nametype=='all':
00101 return _detNames(local_env)+_epicsNames(local_env)
00102 else:
00103 raise ValueError('Name type must be one of "detectors, epics, all"')