def test_deploy_calib_array(): print(80 * '_', '\nTest deploy_calib_array') cdir = './calib' if not os.path.exists(cdir): gu.create_directory(cdir, verb=True) #cdir = '/reg/d/psdm/CXI/cxi83714/calib' src = 'CxiDs1.0:Cspad.0' type = 'pedestals' run_start = 9991 run_end = None arr = gu.np.ones((32, 185, 388)) cmts = { 'exp': 'cxi83714', 'ifname': 'input-file-name', 'app': 'my-app-name', 'comment': 'my-comment' } deploy_calib_array(cdir, src, type, run_start, run_end, arr, cmts, fmt='%.1f', pbits=3)
def makeCalibFileName(self, src, type, run_start, run_end=None): """Returns calibration file name. """ if os.path.basename(self.cdir.rstrip('/')) != 'calib': if self.pbits & 1: print('WARNING! NOT calib DIRECTORY: %s' % self.cdir) return None # there have been problems with calib-dir mounts on the mon nodes. # raise an exception here to try to detect this problem #assert os.path.isdir(self.cdir), 'psana calib-dir must exist: '+self.cdir if not os.path.isdir(self.cdir): print('WARNING! psana calib-dir is not found: %s' % self.cdir) return None if not self._setGroup(src): return None if run_start < 0: if self.pbits & 1: print('WARNING! START RUN NUMBER IS NEGATIVE: %d' % run_start) return None if run_start > 9999: if self.pbits & 1: print('WARNING! START RUN NUMBER EXCEEDS 4-DIGITS: %d' % run_start) return None if run_end is None: self.cfname = '%d-end.data' % (run_start) else: if run_end < 0: if self.pbits & 1: print('WARNING! END RUN NUMBER IS NEGATIVE: %d' % run_end) return None if run_end > 9999: if self.pbits & 1: print('WARNING! END RUN NUMBER IS TOO BIG: %d' % run_end) return None if run_end < run_start: if self.pbits & 1: print('WARNING! END RUN:%d < START RUN:%d' % (run_end, run_start)) return None self.cfname = '%d-%d.data' % (run_start, run_end) dir = self.cdir for subdir in (self.group, src, type): dir = os.path.join(dir, subdir) gu.create_directory(dir, self.pbits) return os.path.join(dir, self.cfname)
def do_work(): prefix = './%s-figs-ti_vs_tj' % gu.str_tstamp( fmt='%Y-%m-%d', time_sec=None) # '%Y-%m-%dT%H:%M:%S%z' gu.create_directory(prefix, mode=0o775) path = os.path.abspath(os.path.dirname(__file__)) print('path to npy flies dir:', path) ti_vs_tj = np.load('%s/ti_vs_tj.npy' % path) t_all = np.load('%s/t_all.npy' % path) trange = (1400., 2900.) print_ndarr(ti_vs_tj, 'ti_vs_tj:\n') print_ndarr(t_all, 't_all:\n') sum_bkg = t_all.sum() sum_cor = ti_vs_tj.sum() print('sum_bkg:', sum_bkg) print('sum_cor:', sum_cor) imrange = trange + trange # (1400., 2900., 1400., 2900.) axim = gr.plotImageLarge(ti_vs_tj, img_range=imrange, amp_range=(0,500), figsize=(11,10),\ title='ti_vs_tj', origin='lower', window=(0.10, 0.08, 0.88, 0.88), cmap='inferno') gr.save('%s/fig-ti_vs_tj.png' % prefix) bkg = np.outer(t_all, t_all) / sum_bkg print_ndarr(bkg, 'bkg:\n') axim = gr.plotImageLarge(bkg, img_range=imrange, amp_range=(0,500), figsize=(11,10),\ title='bkg', origin='lower', window=(0.10, 0.08, 0.88, 0.88), cmap='inferno') gr.save('%s/fig-ti_vs_tj-bkg.png' % prefix) harr = t_all nbins = harr.size ht = HBins(trange, nbins, vtype=np.float32) # ht.binedges() fig, axhi, hi = gr.hist1d(ht.bincenters(), bins=nbins, amp_range=ht.limits(), weights=harr, color='b', show_stat=True,\ log=True, figsize=(7,6), axwin=(0.10, 0.10, 0.88, 0.85), title='1-d bkg',\ xlabel='time of all hits (ns)', ylabel='number of hits', titwin='1-d bkg') gr.save('%s/fig-time-hits.png' % prefix) gr.show()
def test_deploy_calib_file(): print(80 * '_', '\nTest deploy_calib_file') cdir = './calib' if not os.path.exists(cdir): gu.create_directory(cdir, verb=True) #cdir = '/reg/d/psdm/CXI/cxi83714/calib' src = 'CxiDs1.0:Cspad.0' type = 'geometry' run_start = 9992 run_end = None fname = '/reg/g/psdm/detector/alignment/cspad/calib-cxi-camera1-2014-09-24/2016-06-15-geometry-cxil0216-r150-camera1-z95mm.txt' cmts = {'exp': 'cxi83714', 'app': 'my-app-name', 'comment': 'my-comment'} deploy_calib_file(cdir, src, type, run_start, run_end, fname, cmts, pbits=3)
def test01(): # assuming /reg/d/psdm/CXI/cxid2714/calib/CsPad::CalibV1/CxiDs1.0:Cspad.0/pedestals/15-end.data #cdir = '/reg/d/psdm/CXI/cxid2714/calib/' #cdir = '/reg/d/psdm/CXI/cxi80410/calib/' cdir = '/reg/d/psdm/CXI/cxi83714/calib/' group = 'CsPad::CalibV1' src = 'CxiDs1.0:Cspad.0' type = 'pedestals' rnum = 134 #rnum = 123456789 #-------------------------- print(80 * '_', '\nTest 1') print('Finding calib file for\n dir = %s\n grp = %s\n src = %s\n type= %s\n run = %d' % \ (cdir, group, src, type, rnum)) cff = CalibFileFinder(cdir, group, 0o377) fname = cff.findCalibFile(src, type, rnum) #-------------------------- print(80 * '_', '\nTest 2') print('Test methods find_calib_file and make_calib_file_name') fname_existing = find_calib_file(cdir, src, type, rnum, pbits=1) print(' fname_existing : %s' % fname_existing) cdir = './calib' run_start = 134 gu.create_directory(cdir, True) fname_new = make_calib_file_name(cdir, src, type, run_start, run_end=None, pbits=0) print(' fname_new : %s' % fname_new)
def __init__(self, **kwargs): logger.info('__init__, **kwargs: %s' % str(kwargs)) #logger.info(gu.str_kwargs(kwargs, title='input parameters:')) #DSNAME = kwargs.get('dsname', '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e000100.xtc2') #COMMAND = kwargs.get('command', 0) #DETNAME = kwargs.get('detname', 'tmo_hexanode') #EVSKIP = kwargs.get('evskip', 0) #EVENTS = kwargs.get('events', 1000000) + EVSKIP NUM_CHANNELS = kwargs.get('numchs', 5) NUM_HITS = kwargs.get('numhits', 16) OFPREFIX = kwargs.get('ofprefix', './figs-DLD/plot') TDC_RESOLUTION = kwargs.get( 'tdc_resolution', 0.250) # ns !!! SHOULD BE TAKEN FROM DETECTOR CONFIGURATION self.VERBOSE = kwargs.get('verbose', False) calibtab = kwargs.get('calibtab', None) calibcfg = kwargs.get('calibcfg', None) CALIBCFG = calibcfg #if calibcfg is not None else file.find_calib_file(type=self.CTYPE_CALIBCFG) CALIBTAB = calibtab #if calibtab is not None else file.find_calib_file(type=self.CTYPE_CALIBTAB) CONSTS = kwargs.get('consts', None) DETOBJ = kwargs.get('detobj', None) self.CALIBTAB = CALIBTAB #------------------------------ gu.create_directory(OFPREFIX.split('/')[0], mode=0o775) logger.info('TDC_RESOLUTION : %s' % TDC_RESOLUTION) logger.info('CALIBTAB : %s' % CALIBTAB) logger.info('CALIBCFG : %s' % CALIBCFG) #logger.info('file calib_dir : %s' % file.calib_dir()) #logger.info('file calib_src : %s' % file.calib_src()) #logger.info('file calib_group : %s' % file.calib_group()) #logger.info('file ctype_dir : %s' % file.calibtype_dir()) # load config & calib constants distributed for detector or from file txt_cfg = None txt_calib = None det_consts = CONSTS if CONSTS is not None else DETOBJ.calibconst if DETOBJ is not None else None if det_consts is not None: txt_cfg, meta_cfg = det_consts.get(self.CTYPE_CALIBCFG, None) txt_calib, meta_calib = det_consts.get(self.CTYPE_CALIBTAB, None) else: txt_cfg = text_data( CALIBCFG ) # str object (from file) with configuration constants txt_calib = text_data(CALIBTAB) # ... with calibration constants s = 'from DB' if det_consts is not None else 'default' logger.info('>>>> configuration and calibration %s' % s) # create the sorter: self.sorter = sorter = hexanode.py_sort_class() #status, command_cfg, self.offset_sum_u, self.offset_sum_v, self.offset_sum_w,\ #self.w_offset, self.pos_offset_x, self.pos_offset_y=\ # hexanode.py_read_config_file(CALIBCFG.encode(), sorter) status, command_cfg, self.offset_sum_u, self.offset_sum_v, self.offset_sum_w,\ self.w_offset, self.pos_offset_x, self.pos_offset_y=\ load_config_pars(txt_cfg, sorter, **kwargs) # The "command"-value is set in the first line of "config.txt" or CLI parameter # 0 = only convert to new file format # 1 = sort and write new file # 2 = calibrate fv, fw, w_offset # 3 = create calibration table files self.command = command = command_cfg msg = 'load_config_pars status:%s COMMAND:%d'%(status, command)\ + '\n offset_sum_u:%.3f offset_sum_v:%.3f offset_sum_w:%.3f'%\ (self.offset_sum_u, self.offset_sum_v, self.offset_sum_w)\ + '\n w_offset:%.3f pos_offset_x:%.3f pos_offset_y:%.3f'%\ (self.w_offset, self.pos_offset_x, self.pos_offset_y) logger.info(msg) if not status: logger.info("WARNING: can't load config parameters %s" % CALIBCFG) del sorter sys.exit(0) logger.info('use_sum_correction %s' % sorter.use_sum_correction) logger.info('use_pos_correction HEX ONLY %s' % sorter.use_pos_correction) if sorter is not None: if sorter.use_sum_correction or sorter.use_pos_correction: #status = hexanode.py_read_calibration_tables(CALIBTAB.encode(), sorter) status = load_calibration_tables(txt_calib, sorter=None) if command == -1: logger.info("no config file was read. Nothing to do.") if sorter is not None: del sorter sys.exit(0) Cu1, Cu2, Cv1, Cv2, Cw1, Cw2, Cmcp = sorter.channel_indexes logger.info("Numeration of channels - u1:%i u2:%i v1:%i v2:%i w1:%i w2:%i mcp:%i"%\ (Cu1, Cu2, Cv1, Cv2, Cw1, Cw2, Cmcp)) #logger.info("chanel increments:", self.inds_incr) #===================== logger.info("init sorter... ") self.tdc_ns = np.zeros((NUM_CHANNELS, NUM_HITS), dtype=np.float64) self.number_of_hits = np.zeros((NUM_CHANNELS, ), dtype=np.int32) sorter.set_tdc_resolution_ns(TDC_RESOLUTION) sorter.set_tdc_array_row_length(NUM_HITS) sorter.set_count(self.number_of_hits) sorter.set_tdc_pointer(self.tdc_ns) #sorter.set_use_reflection_filter_on_u1(False) # Achim recommended False #sorter.set_use_reflection_filter_on_u2(False) self._on_command_23_init() error_code = sorter.init_after_setting_parameters() if error_code: logger.info("sorter could not be initialized\n") error_text = sorter.get_error_text(error_code, 512) logger.info('Error %d: %s' % (error_code, error_text)) sys.exit(0) logger.info("Calibration factors:\n f_U (mm/ns) =%f\n f_V (mm/ns) =%f\n f_W (mm/ns) =%f\n Offset on layer W (ns) =%f\n"%\ (2*sorter.fu, 2*sorter.fv, 2*sorter.fw, self.w_offset)) logger.info("ok for sorter initialization\n") self.evnum_old = None
def create_output_directory(prefix): dirname = os.path.dirname(prefix) print('Output directory: "%s"' % dirname) if dirname in ('', './', None): return gu.create_directory(dirname, mode=0o775)