Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:46

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 catdir.py
0023 ===========
0024 
0025 """
0026 
0027 from datetime import datetime
0028 import os, re, logging
0029 import numpy as np
0030 from collections import OrderedDict as odict
0031 log = logging.getLogger(__name__)
0032 
0033 from opticks.ana.base import ini_, json_, splitlines_
0034 from opticks.ana.datedfolder import DatedFolder, dateparser
0035 from opticks.ana.metadata import Metadata
0036 
0037 
0038 class Catdir(object):
0039     """
0040     Reads in metadata from dated folders corresponding to runs of::
0041 
0042           ggv 
0043           ggv --compute
0044           ggv --cfg4
0045 
0046     ::
0047 
0048         INFO:__main__:Catdir searching for date stamped folders beneath : /usr/local/env/opticks/rainbow 
0049         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160104_141234
0050         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160104_144620
0051         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160104_150114
0052         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160105_155311
0053         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160107_201949
0054         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-5/20160224_120049
0055         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-6/20160105_172431
0056         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/-6/20160105_172538
0057         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/5/20160103_165049
0058         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/5/20160103_165114
0059         INFO:__main__:/usr/local/env/opticks/rainbow/mdtorch/5/20160104_111515
0060 
0061     """
0062     def __init__(self, path):
0063         """
0064         Path should be the folder above the tagdir in order
0065         to allow comparisons between equivalent (ie G4 negated) tags 
0066         """
0067         dirs, dfolds, dtimes = DatedFolder.find(path)
0068 
0069         log.info("Catdir searching for date stamped folders beneath : %s " % path)
0070         print( "\n".join(dirs))
0071 
0072         metamap = odict()
0073         for p in dirs:
0074             log.debug("%s", p)
0075             md = Metadata(p, base=path)
0076             tag = md.tag
0077             if tag not in metamap:
0078                 metamap[tag] = []
0079             metamap[tag].append(md)
0080         pass 
0081 
0082         self.metamap = metamap
0083         self.path = path
0084 
0085         self.dump()
0086 
0087 
0088     def dump(self):
0089         log.info("Catdir %s tags: %s beneath %s " % ( len(self.metamap), repr(self.metamap.keys()), self.path))
0090         for tag, mds in self.metamap.items():
0091             print "%5s : %d " % (tag, len(mds)) 
0092 
0093     def tags(self): 
0094         return self.metamap.keys()
0095 
0096     def times(self, tag):
0097         """
0098         :param tag: event tag, eg 1,2 (be negated to get cfg4 tags)
0099         :return: recarray with propagate times and corresponding flags indicating cfg4/interop/compute
0100         """
0101         tags = [tag, "-%s" % tag]   # convention, negate to get equivalent cfg4 tag
0102         mds = []
0103         for tag in tags:
0104             mds.extend(self.metamap.get(tag, [])) 
0105         pass
0106 
0107         log.info("times metadata for tag %s " % tag + "\n".join(map(str,mds)))
0108 
0109         n = len(mds)
0110         if n == 0:
0111             log.warning("no metadata found")
0112             return None  
0113 
0114         numpho0 = mds[0].numPhotons
0115 
0116         ## collect indices with consistent photon counts
0117         consistent = []
0118         for i in range(n):
0119             md = mds[i]
0120             if md.numPhotons == numpho0:
0121                 consistent.append(i)
0122             pass
0123         pass
0124             
0125         nc = len(consistent)
0126         if nc != n:
0127             log.warning("skipped metadata with inconsistent photon count n %s nc %s " % (n, nc)) 
0128         pass
0129 
0130         a = np.recarray((nc,), dtype=[("index", np.int32), ("time", "|O8"), ("propagate", np.float32), ("flgs", np.uint32 ), ("numPhotons",np.uint32)])
0131 
0132         j = 0 
0133         for i in range(n):
0134             md = mds[i]
0135             dat = (i, md.timestamp, md.propagate, md.flags, md.numPhotons )
0136             if i in consistent:
0137                 a[j] = dat
0138                 j += 1 
0139                 print(dat) 
0140             else:
0141                 log.warning("skipped inconsistent %s " % repr(dat))
0142             pass
0143         pass
0144 
0145         return a 
0146 
0147     def __repr__(self):
0148          return "%s tags %s " % (self.path, repr(self.tags()))
0149 
0150 
0151 
0152 
0153 
0154 def test_catdir():
0155     #cat, tag= "rainbow", "6"
0156     cat, tag= "PmtInBox", "4"
0157     catd = Catdir(cat)
0158     a = catd.times(tag)
0159 
0160 def test_catdir_find():
0161     cat = Catdir(os.path.expandvars("/tmp/$USER/opticks/evt/PmtInBox/torch"))
0162     tags = cat.tags()
0163     for tag in tags:
0164         a = cat.times(tag)
0165         print "a", a
0166 
0167 
0168 
0169 if __name__ == '__main__':
0170     from opticks.ana.main import opticks_main
0171     ok = opticks_main() 
0172     print(ok.brief)
0173     print("tagdir : %s " % ok.tagdir)
0174     print("catdir : %s " % ok.catdir)
0175 
0176     cat = Catdir(ok.catdir)
0177     tags = cat.tags()
0178     for tag in tags:
0179         a = cat.times(tag)
0180         print("a ", a)
0181     pass
0182 
0183 
0184 
0185