Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:45

0001 #!/usr/bin/env python
0002 """
0003 SProp.py 
0004 ==========
0005 
0006 
0007 """
0008 import os, logging, numpy as np
0009 from collections import OrderedDict as odict
0010 log = logging.getLogger(__name__)
0011 
0012 class SProp(object):
0013     BASE = os.environ.get("NP_PROP_BASE", "/tmp")
0014 
0015     @classmethod
0016     def Resolve_(cls, spec):
0017         relp = spec.replace(".","/")
0018         return os.path.join(cls.BASE, relp) 
0019 
0020     @classmethod
0021     def Resolve(cls, spec):
0022         return cls.Resolve_(spec) if spec.count(".") > 1 else spec  
0023 
0024     @classmethod
0025     def Names(cls, spec):
0026         rawnames = os.listdir(cls.Resolve(spec))
0027         names = []
0028         for name in rawnames: 
0029             if name.count(".") == 0:
0030                 names.append(name)
0031             pass
0032         pass
0033         return names  
0034 
0035     @classmethod
0036     def Load(cls, spec):
0037         path = cls.Resolve(spec)
0038         log.debug(" spec %s path %s " % (spec, path))
0039         try:
0040             a = np.loadtxt(path, usecols=(0,2)) # skip "*eV" column 
0041         except (ValueError, IndexError):
0042             a = np.loadtxt(path, dtype=np.object)    
0043         pass
0044         if os.path.exists(path+".npy"):
0045             b = np.load(path+".npy")
0046             assert np.all( a == b )
0047             log.debug(" path %s matches %s " % (path, path+".npy"))
0048         pass 
0049         return a 
0050 
0051     def __init__(self, specbase="PMTProperty.R12860.", symbol="hama"):
0052         props = odict()
0053         specs = []
0054         paths = []
0055         for name in self.Names(specbase):
0056             spec = "%s%s" % ( specbase, name) 
0057             path = self.Resolve(spec)
0058             props[name] = self.Load(path)
0059             specs.append(spec)
0060             paths.append(path)
0061         pass
0062         self.props = props
0063         self.specs = specs
0064         self.paths = paths 
0065         self.specbase = specbase
0066         self.symbol = symbol
0067 
0068     def __repr__(self):
0069         lines = []
0070         lines.append("Prop %s %s " % (self.specbase, self.symbol) )
0071         for k, v in self.props.items():
0072             lines.append("%35s : %10s : %10s " % ("%s.%s" % (self.symbol, k), str(v.shape), str(v.dtype) ))
0073         pass
0074         return "\n".join(lines)
0075 
0076     def __getattr__(self, spec):
0077         if spec in self.props:
0078             return self.props[spec]
0079         else:
0080             raise AttributeError  
0081         pass
0082  
0083 if __name__ == '__main__':
0084     logging.basicConfig(level=logging.INFO)
0085 
0086     hama = SProp("PMTProperty.R12860.", symbol="hama")
0087     nnvt = SProp("PMTProperty.NNVTMCP.", symbol="nnvt")
0088     nnvtq = SProp("PMTProperty.NNVTMCP_HiQE.", symbol="nnvtq")
0089  
0090     print(repr(hama))
0091     print(repr(nnvt))
0092     print(repr(nnvtq))
0093 
0094     for symbol in ["hama", "nnvt", "nnvtq"]:
0095         expr = "np.c_[%(symbol)s.PHC_RINDEX, %(symbol)s.PHC_KINDEX, %(symbol)s.ARC_RINDEX ] " % locals()   
0096         print(expr)
0097         print(eval(expr))
0098     pass
0099     # energy domain consistent within but not between them 
0100