Example #1
0
def loop_run_step_evt(args):
    """Data access example for confluence
     run, step, event loops
  """
    typeinfo = args.typeinfo.lower()
    do_loopruns = 'r' in typeinfo
    do_loopevts = 'e' in typeinfo
    do_loopsteps = 's' in typeinfo

    from psana.pyalgos.generic.NDArrUtils import info_ndarr
    #from psana import DataSource
    #ds = DataSource(exp=args.expt, run=args.run, dir=f'/cds/data/psdm/{args.expt[:3]}/{args.expt}/xtc', max_events=1000)

    ds = DataSource(**datasource_arguments(args))

    if do_loopruns:
        for irun, run in enumerate(ds.runs()):
            print('\n==== %02d run: %d exp: %s detnames: %s' %
                  (irun, run.runnum, run.expt, ','.join(run.detnames)))

            if not do_loopsteps: continue
            print('%s detector object' % args.detname)
            det = run.Detector(args.detname)
            is_epix10ka = 'epix' in det.raw._uniqueid
            try:
                step_docstring = run.Detector('step_docstring')
            except:
                step_docstring = None
            print('step_docstring detector object is %s' %
                  ('missing' if step_docstring is None else 'created'))

            dcfg = ue.config_object_epix10ka(det) if is_epix10ka else None

            for istep, step in enumerate(run.steps()):
                print('\nStep %02d' % istep, end='')

                metadic = None if step_docstring is None else json.loads(
                    step_docstring(step))
                print('  metadata: %s' % str(metadic))

                if not do_loopevts: continue
                ievt, evt, segs = None, None, None
                for ievt, evt in enumerate(step.events()):
                    #if ievt>args.evtmax: exit('exit by number of events limit %d' % args.evtmax)
                    if not selected_record(ievt): continue
                    if segs is None:
                        segs = det.raw._segment_numbers(evt)
                        print('  Event %05d %s' %
                              (ievt, info_ndarr(segs, 'segments ')))
                        #print('gain mode statistics:' + ue.info_pixel_gain_mode_statistics(gmaps))
                        if dcfg is not None:
                            s = '    gain mode fractions for: FH       FM       FL'\
                                '       AHL-H    AML-M    AHL-L    AML-L\n%s' % (29*' ')
                            print(
                                ue.info_pixel_gain_mode_fractions(
                                    dcfg, data=det.raw.raw(evt), msg=s))

                    print(info_det_evt(det, evt, ievt), end='\r')
                print(info_det_evt(det, evt, ievt), end='\n')
Example #2
0
def get_config_info_for_dataset_detname(**kwargs):

    detname = kwargs.get('det', None)
    idx     = kwargs.get('idx', None)

    ds = DataSource(**data_source_kwargs(**kwargs))
    logger.debug('ds.runnum_list = %s' % str(ds.runnum_list))
    logger.debug('ds.detectors = %s' % str(ds.detectors))

    #for orun in ds.runs():
    orun = next(ds.runs())
    if orun:

      logger.debug('==run.runnum   : %d' % orun.runnum)        # 27
      logger.debug('  run.detnames : %s' % str(orun.detnames)) # {'epixquad'}
      logger.debug('  run.expt     : %s', orun.expt)           # ueddaq02

      runtstamp = orun.timestamp    # 4193682596073796843 relative to 1990-01-01
      trun_sec = seconds(runtstamp) # 1607569818.532117 sec
      #tstamp_run = str_tstamp(time_sec=int(trun_sec)) #fmt='%Y-%m-%dT%H:%M:%S%z'
      tstamp_run, tstamp_now = tstamps_run_and_now(int(trun_sec)) # (str) 20201209191018, 20201217140026
      logger.debug('  run.timestamp: %d' % orun.timestamp) 
      logger.debug('  run unix epoch time %06f sec' % trun_sec)
      logger.debug('  run tstamp: %s' % tstamp_run)
      logger.debug('  now tstamp: %s' % tstamp_now)

      det = orun.Detector(detname)

      co = ue.config_object_epix10ka(det)

      cpdic = {}
      cpdic['expname']    = orun.expt
      cpdic['calibdir']   = None
      cpdic['strsrc']     = None
      cpdic['shape']      = (352, 384)
      cpdic['gain_mode']  = ue.find_gain_mode(co, data=None) #data=raw: distinguish 5-modes w/o data
      cpdic['panel_ids']  = ue.segment_ids_epix10ka_detector(det)
      cpdic['panel_inds'] = ue.segment_indices_epix10ka_detector(det)
      cpdic['longname']   = ue.fullname_epix10ka_detector(det) #det.raw._uniqueid
      cpdic['det_name']   = det._det_name # det.raw._det_name epixquad
      cpdic['dettype']    = det._dettype # epix
      cpdic['tstamp']     = tstamp_run # (str) 20201209191018
      cpdic['tstamp_now'] = tstamp_now # (str) 20201217140026
      cpdic['trun_sec']   = int(trun_sec) # 1607569818.532117 sec
      cpdic['tsrun_dark'] = str_tstamp(time_sec=int(trun_sec)) #fmt='%Y-%m-%dT%H:%M:%S%z'
      return cpdic
Example #3
0
def pedestals_calibration(*args, **kwa):
    """NEWS significant ACCELERATION is acheived:
       - accumulate data for entire epix10kam_2m/quad array
       - use MPI
       all-panel or selected-panel one-step (gain range) or all steps calibration of pedestals
    """
    fname      = kwa.get('fname', None)
    detname    = kwa.get('det', None)
    exp        = kwa.get('exp', None)
    runs       = kwa.get('runs', None)
    nrecs      = kwa.get('nrecs', 1000)
    stepnum    = kwa.get('stepnum', None)
    stepmax    = kwa.get('stepmax', 5)
    evskip     = kwa.get('evskip', 0)
    events     = kwa.get('events', 1000)
    dirxtc     = kwa.get('dirxtc', None)
    dirrepo    = kwa.get('dirrepo', CALIB_REPO_EPIX10KA)
    fmt_peds   = kwa.get('fmt_peds', '%.3f')
    fmt_rms    = kwa.get('fmt_rms',  '%.3f')
    fmt_status = kwa.get('fmt_status', '%4i')
    idx_sel    = kwa.get('idx', None)
    dirmode    = kwa.get('dirmode', 0o777)
    filemode   = kwa.get('filemode', 0o666)
    usesmd     = kwa.get('usesmd', False)
    logmode    = kwa.get('logmode', 'DEBUG')
    errskip    = kwa.get('errskip', False)

    logger.setLevel(DICT_NAME_TO_LEVEL[logmode])

    #irun = runs[0] if isinstance(runs, list) else\
    #       int(runs.split(',',1)[0].split('-',1)[0]) # int first run number from str of run(s)
    irun = irun_first(runs)

    #dsname = 'exp=%s:run=%s'%(exp,runs) if dirxtc is None else 'exp=%s:run=%s:dir=%s'%(exp, runs, dirxtc)
    #if usesmd: dsname += ':smd'

    #_name = sys._getframe().f_code.co_name
    _name = SCRNAME
    logger.info('In %s\n  exp: %s\n  runs: %s\n  detector: %s' % (_name, exp, str(runs), detname))
    save_log_record_at_start(dirrepo, _name, dirmode, filemode, logmode)

    #cpdic = get_config_info_for_dataset_detname(dsname, detname)
    #tstamp      = cpdic.get('tstamp', None)
    #panel_ids   = cpdic.get('panel_ids', None)
    #expnum      = cpdic.get('expnum', None)
    #dettype     = cpdic.get('dettype', None)
    #shape       = cpdic.get('shape', None)
    #ny,nx = shape

    #panel_id = get_panel_id(panel_ids, idx)
    #logger.debug('Found panel ids:\n%s' % ('\n'.join(panel_ids)))

    #read input xtc file and accumulate block of data

    #================= MPI

    #from mpi4py import MPI
    #comm = MPI.COMM_WORLD
    #rank = comm.Get_rank()
    #size = comm.Get_size() # number of MPI nodes; 1 for regular python command

    #=================

    kwa = data_source_kwargs(**kwa)
    #ds = DataSource(**kwa)
    try: ds = DataSource(**kwa)
    except Exception as err:
        logger.error('DataSource(**kwa) does not work:\n    %s' % err)
        sys.exit('EXIT - requested DataSource does not exist or is not accessible.')

    logger.debug('ds.runnum_list = %s' % str(ds.runnum_list))
    logger.debug('ds.detectors = %s' % str(ds.detectors))
    logger.info('ds.xtc_files:\n  %s' % ('\n  '.join(ds.xtc_files)))

    mode = None # gain_mode
    nstep_tot = -1

    #orun = next(ds.runs())
    for orun in ds.runs():
      logger.debug('==run.runnum   : %d' % orun.runnum)        # 27
      logger.debug('  run.detnames : %s' % str(orun.detnames)) # {'epixquad'}
      logger.debug('  run.expt     : %s', orun.expt)           # ueddaq02

      runtstamp = orun.timestamp    # 4193682596073796843 relative to 1990-01-01
      trun_sec = seconds(runtstamp) # 1607569818.532117 sec
      #tstamp = str_tstamp(time_sec=int(trun_sec)) #fmt='%Y-%m-%dT%H:%M:%S%z'

      tstamp_run, tstamp_now = tstamps_run_and_now(int(trun_sec))
      tstamp = tstamp_run

      logger.debug('  run.timestamp: %d' % orun.timestamp)
      logger.debug('  run unix epoch time %06f sec' % trun_sec)
      logger.debug('  run tstamp: %s' % tstamp_run)
      logger.debug('  now tstamp: %s' % tstamp_now)
      det = orun.Detector(detname)
      #step_value = orun.Detector('step_value')
      try: step_docstring = orun.Detector('step_docstring')
      except Exception as err:
        logger.error('run.Detector("step_docstring") does not work:\n    %s' % err)
        sys.exit('Exit processing due to missing info about dark data step.')
      #cd = orun.Detector('ControlData') #LCLS1

      logger.debug('--- det.raw._det_name: %s' % det.raw._det_name) # epixquad
      logger.debug('    det.raw._dettype : %s' % det.raw._dettype)  # epix
      logger.debug('    det.raw._calibconst.keys(): %s' % str(det.raw._calibconst.keys())) # dict_keys(['geometry'])
      #logger.debug('    det.raw._uniqueid: %s' % det.raw._uniqueid)
      #logger.debug('    det.raw._sorted_segment_ids: %s' % str(det.raw._sorted_segment_ids))
      #logger.debug('    det.raw._fullname: %s' % det.raw._fullname())

      segment_ids = det.raw._segment_ids() #ue.segment_ids_det(det)
      segment_inds = det.raw._segment_indices() #ue.segment_indices_det(det)
      s = 'segment inds and ids in the detector'
      for i,id in zip(segment_inds,segment_ids):
          s += '\n  seg:%02d id:%s' % (i,id)
      logger.info(s)

      BIT_MASK = det.raw._data_bit_mask
      logger.info('    det.raw._data_bit_mask BIT_MASK: %s' % oct(BIT_MASK))

      #logger.debug('    det.raw._segment_ids: %s' % str(det.raw._segment_ids()))
      #logger.debug('    det.raw._segment_indices: %s' % str(det.raw._segment_indices()))

      dcfg = det.raw._config_object() #ue.config_object_det(det)

      for nstep_run, step in enumerate(orun.steps()): #(loop through calyb cycles, using only the first):
        nstep_tot += 1
        logger.info('\n=============== step %2d ===============' % nstep_tot)
        logger.debug('    step.evt._seconds: %d' % step.evt._seconds)

        metadic = json.loads(step_docstring(step))
        nstep = step_counter(metadic, nstep_tot, nstep_run, stype='pedestal')

        if nstep is None: continue

        #if size > 1:
        #    # if MPI is on process all steps, step per rank
        #    if nstep < rank: continue
        #    if nstep > rank: break

        if nstep_tot>=stepmax:
            logger.info('==== Step:%02d loop is terminated, --stepmax=%d' % (nstep_tot, stepmax))
            break

        elif stepnum is not None:
            if   nstep < stepnum:
                logger.info('==== Step:%02d is skipped, --stepnum=%d' % (nstep, stepnum))
                continue
            elif nstep > stepnum:
                logger.info('==== Step:%02d loop is terminated, --stepnum=%d' % (nstep, stepnum))
                break

        #for k,v in det.raw._seg_configs().items(): # cpo's pattern DOES NOT WORK
        for k,v in dcfg.items():
            scob = v.config
            logger.info(info_ndarr(scob.asicPixelConfig, 'seg:%02d trbits: %s asicPixelConfig:'%(k, str(scob.trbit))))
            #logger.info(info_ndarr(scob.asicPixelConfig[:,:-2,:], 'seg:%02d trbits: %s asicPixelConfig:'%(k, str(scob.trbit))))

        gmaps = ue.gain_maps_epix10ka_any(det.raw, evt=None) #dcfg, data=None)
        logger.debug('gain mode statistics:' + ue.info_pixel_gain_mode_statistics(gmaps))
        logger.debug(ue.info_pixel_gain_mode_fractions(det.raw, evt=None, msg='gain mode fractions :'))

        logger.debug('gain maps'\
          + info_ndarr(gmaps[0],'\n    FH  ')\
          + info_ndarr(gmaps[1],'\n    FM  ')\
          + info_ndarr(gmaps[2],'\n    FL  ')\
          + info_ndarr(gmaps[3],'\n    AHL ')\
          + info_ndarr(gmaps[4],'\n    AML ')\
        )

        mode = ue.find_gain_mode(det.raw, evt=None).upper()   #dcfg, data=None).upper()

        if mode in ue.GAIN_MODES_IN:
            mode_in_step = ue.GAIN_MODES_IN[nstep]
            logger.info('== step %d: dark run processing for gain mode in configuration %s and step number %s'\
                        %(nstep, mode, mode_in_step))
            if mode != mode_in_step:
              logger.warning('INCONSISTENT GAIN MODES IN CONFIGURATION AND STEP NUMBER/METADATA')
              if not errskip: sys.exit()
              logger.warning('FLAG ERRSKIP IS %s - keep processing assuming gain mode %s' % (errskip,mode))
              #continue
        else:
            logger.warning('UNRECOGNIZED GAIN MODE: %s, DARKS NOT UPDATED...'%mode)
            sys.exit()
            #return

        sh = gmaps[0].shape
        shape_block = [nrecs,] + list(sh) # [nrecs, <number-of-segments>, 352, 384]
        logger.info('Accumulate raw frames in block shape = %s' % str(shape_block))

        block=np.zeros(shape_block,dtype=np.uint16)
        nrec,nevt = -1,0

        ss = None
        for nevt,evt in enumerate(step.events()):
            raw = det.raw.raw(evt)
            do_print = selected_record(nevt)
            if raw is None:
                logger.info('==== Ev:%04d rec:%04d raw is None' % (nevt,nrec))
                continue

            if nevt < evskip:
                logger.debug('==== Ev:%04d is skipped, --evskip=%d' % (nevt,evskip))
                continue
            elif evskip>0 and (nevt == evskip):
                s = 'Events < --evskip=%d are skipped' % evskip
                #print(s)
                logger.info(s)

            if nevt > events-1:
                logger.info(ss)
                logger.info('==== Ev:%04d event loop is terminated, --events=%d' % (nevt,events))
                print()
                break

            if nrec > nrecs-2:
                logger.info(ss)
                logger.info('==== Ev:%04d event loop is terminated - collected sufficient number of frames, --nrecs=%d' % (nevt,nrecs))
                break
            else:
                nrec += 1
                ss = info_ndarr(raw & BIT_MASK, 'Ev:%04d rec:%04d raw & BIT_MASK ' % (nevt,nrec))
                if do_print: logger.info(ss)
                block[nrec]=(raw & BIT_MASK)

        if nevt < events: logger.info('==== Ev:%04d end of events in run step %d' % (nevt,nstep_run))

        print_statistics(nevt, nrec)

        #---- process statistics in block-array for panels

        for idx, panel_id in zip(segment_inds,segment_ids):

            if idx_sel is not None and idx_sel != idx: continue # skip panels with inices other than idx_sel if specified

            logger.info('\n%s\nprocess panel:%02d id:%s' % (96*'=', idx, panel_id))

            #if mode is None:
            #    msg = 'Gain mode for dark processing is not defined "%s" try to set option -m <gain-mode>' % mode
            #    logger.warning(msg)
            #    sys.exit(msg)

            dir_panel, dir_offset, dir_peds, dir_plots, dir_work, dir_gain, dir_rms, dir_status = dir_names(dirrepo, panel_id)

            #print('XXXX panel_id, tstamp, exp, irun', panel_id, tstamp, exp, irun)

            fname_prefix, panel_alias = file_name_prefix(dirrepo, det.raw._dettype, panel_id, tstamp, exp, irun)
            logger.debug('\n  fname_prefix:%s\n  panel_alias :%s' % (fname_prefix, panel_alias))

            prefix_offset, prefix_peds, prefix_plots, prefix_gain, prefix_rms, prefix_status =\
                path_prefixes(fname_prefix, dir_offset, dir_peds, dir_plots, dir_gain, dir_rms, dir_status)

            #logger.debug('Directories under %s\n  SHOULD ALREADY EXIST after charge-injection offset_calibration' % dir_panel)
            #assert os.path.exists(dir_offset), 'Directory "%s" DOES NOT EXIST' % dir_offset
            #assert os.path.exists(dir_peds),   'Directory "%s" DOES NOT EXIST' % dir_peds

            create_directory(dir_panel,  mode=dirmode)
            create_directory(dir_peds,   mode=dirmode)
            create_directory(dir_offset, mode=dirmode)
            create_directory(dir_gain,   mode=dirmode)
            create_directory(dir_rms,    mode=dirmode)
            create_directory(dir_status, mode=dirmode)


            #dark=block[:nrec,:].mean(0)  #Calculate mean

            #block.sahpe = (1024, 16, 352, 384)
            dark, rms, status = proc_dark_block(block[:nrec,idx,:], **kwa) # process pedestals per-panel (352, 384)


            #continue # TEST
            #==========

            fname = '%s_pedestals_%s.dat' % (prefix_peds, mode)
            save_2darray_in_textfile(dark, fname, filemode, fmt_peds)

            fname = '%s_rms_%s.dat' % (prefix_rms, mode)
            save_2darray_in_textfile(rms, fname, filemode, fmt_rms)

            fname = '%s_status_%s.dat' % (prefix_status, mode)
            save_2darray_in_textfile(status, fname, filemode, fmt_status)

            #if this is an auto gain ranging mode, also calculate the corresponding _L pedestal:

            if mode=='AHL-H': # evaluate AHL_L from AHL_H
                ped_hl_h = dark #[3,:,:]

                offset_hl_h = load_panel_constants(dir_offset, 'offset_AHL-H', tstamp)
                offset_hl_l = load_panel_constants(dir_offset, 'offset_AHL-L', tstamp)
                gain_hl_h   = load_panel_constants(dir_gain,   'gainci_AHL-H', tstamp)
                gain_hl_l   = load_panel_constants(dir_gain,   'gainci_AHL-L', tstamp)

                #if offset is not None:
                if all([v is not None for v in (offset_hl_h, offset_hl_l, gain_hl_h, gain_hl_l)]):
                    ped_hl_l = offset_hl_l - (offset_hl_h - ped_hl_h) * divide_protected(gain_hl_l, gain_hl_h) #V3 Gabriel's
                    fname = '%s_pedestals_AHL-L.dat' % prefix_peds
                    save_2darray_in_textfile(ped_hl_l, fname, filemode, fmt_peds)

            elif mode=='AML-M': # evaluate AML_L from AML_M
                ped_ml_m = dark #[4,:,:]

                offset_ml_m = load_panel_constants(dir_offset, 'offset_AML-M', tstamp)
                offset_ml_l = load_panel_constants(dir_offset, 'offset_AML-L', tstamp)
                gain_ml_m   = load_panel_constants(dir_gain,   'gainci_AML-M', tstamp)
                gain_ml_l   = load_panel_constants(dir_gain,   'gainci_AML-L', tstamp)

                #if offset is not None:
                if all([v is not None for v in (offset_ml_m, offset_ml_l, gain_ml_m, gain_ml_l)]):
                    ped_ml_l = offset_ml_l - (offset_ml_m - ped_ml_m) * divide_protected(gain_ml_l, gain_ml_m) #V3 Gabriel's
                    fname = '%s_pedestals_AML-L.dat' % prefix_peds
                    save_2darray_in_textfile(ped_ml_l, fname, filemode, fmt_peds)
def test_image(args):

    import psana.detector.UtilsEpix10ka as ue
    from psana.detector.UtilsGraphics import gr, fleximage, flexhist, fleximagespec

    dograph = args.dograph.lower()
    flimg, flspe, flims = None, None, None

    ds, run, det = ds_run_det(args)
    peds = det.raw._pedestals(
    ) if args.grindex is None else det.raw._pedestals()[args.grindex, :]

    is_epix10ka = 'epix' in det.raw._uniqueid
    dcfg = ue.config_object_epix10ka(det) if is_epix10ka else None

    break_event_loop = False

    nframes = 0
    sum_arr, sum_sta = None, None
    med_vs_evt = np.zeros(args.events - args.evskip + 10, dtype=np.float64)
    nrec_med = 0

    for stepnum, step in enumerate(run.steps()):
        print('%s\nStep %1d' % (50 * '_', stepnum))

        if args.stepsel is not None and stepnum != args.stepsel:
            print('  skip - step selected in option -M is %1d' %
                  (args.stepsel))
            continue
        print('%s\n  begin event loop' % (50 * '_'))
        for evnum, evt in enumerate(step.events()):
            if evnum < args.evskip:
                print('Step %1d Event %04d - skip first %04d events' % (stepnum, evnum, args.evskip),\
                       end=('\r' if evnum<args.evskip-1 else '\n'))
                continue

            if evnum > args.events:
                print('break by number of events limit %d set in option -N' %
                      args.events)
                break_event_loop = True
                break
            if evnum > 2 and evnum % args.evjump != 0: continue
            print('%s\nStep %1d Event %04d' % (50 * '_', stepnum, evnum))

            if dcfg is not None:
                s = '    gain mode fractions for: FH       FM       FL'\
                    '       AHL-H    AML-M    AHL-L    AML-L\n%s' % (29*' ')
                #ue.info_pixel_gain_mode_for_fractions(dcfg, data=det.raw.raw(evt), msg=s))
                gmfracs = ue.pixel_gain_mode_fractions(dcfg,
                                                       data=det.raw.raw(evt))
                print(ue.info_pixel_gain_mode_for_fractions(gmfracs, msg=s))
                gmind = ue.gain_mode_index_from_fractions(gmfracs)
                gmname = ue.gain_mode_name_for_index(gmind).upper()
                print('  == major gain mode %d : %s' % (gmind, gmname))
                #print('  == gain mode: %s' % ue.find_gain_mode(dcfg, data=None).upper())

                if peds is None: peds = det.raw._pedestals()[gmind, :]

            #user_mask = np.ones_like(det.raw.raw(evt), dtype=DTYPE_MASK) #np.uint8
            #user_mask[0,100:150,200:250] = 0
            user_mask = None

            arr = None
            if args.show == 'raw-peds-med':
                arr = (det.raw.raw(evt) & 0x3fff) - peds
                med = np.median(arr)
                print('XXX from raw-peds subtract its median = %.3f' % med)
                arr -= med

            if arr is None:
                arr = det.raw.calib(evt, cmpars=(7,7,100,10),\
                                 mbits=0o7, mask=user_mask, edge_rows=10, edge_cols=10, center_rows=5, center_cols=5)\
                                                      if args.show == 'calibcm'  else\
                   det.raw.calib(evt, cmpars=(8,7,10,10))\
                                                      if args.show == 'calibcm8' else\
                   det.raw.calib(evt)                 if args.show == 'calib'    else\
                   peds                               if args.show == 'peds'     else\
                   det.raw._gain_range_index(evt)     if args.show == 'grind'    else\
                   (det.raw.raw(evt) & 0x3fff) - peds if args.show == 'raw-peds' else\
                   (det.raw.raw(evt) & 0x3fff)        if args.show == 'rawbm'    else\
                    np.ones_like(det.raw.raw(evt))    if args.show == 'ones'     else\
                   (det.raw.raw(evt) & args.bitmask)

            #if args.show == 'calibcm': arr += 1 # to see panel edges

            logger.info(info_ndarr(arr, 'arr '))
            if arr is None: continue

            med = np.median(arr)
            med_vs_evt[nrec_med] = med
            nrec_med += 1

            if args.cumulat:
                if (med > args.thrmin) and (med < args.thrmax):
                    nframes += 1
                    cond = arr > args.thrpix
                    if nframes != 1:
                        _ = np.add(sum_arr[cond], arr[cond], out=sum_arr[cond])
                        sum_sta[cond] += 1
                    else:
                        sum_arr = np.array(arr, dtype=np.float64)
                        sum_sta = np.zeros_like(arr, dtype=np.uint64)

                    #if nframes > 1: arr = sum_arr/float(nframes)
                    if nframes > 1: arr = divide_protected(sum_arr, sum_sta)
                    print('Step %1d event:%04d nframes:%04d arr median:%.3f' %
                          (stepnum, evnum, nframes, med))
                else:
                    continue

            t0_sec = time()
            img = det.raw.image(evt,
                                nda=arr,
                                pix_scale_size_um=args.pscsize,
                                mapmode=args.mapmode)
            print('image composition time = %.6f sec ' % (time() - t0_sec))

            logger.info(info_ndarr(img, 'img '))
            logger.info(info_ndarr(arr, 'arr '))
            if img is None: continue

            title = '%s %s run:%s ev:%d' % (args.detname, args.expname,
                                            args.runs, evnum)

            if 'i' in dograph:
                if flimg is None:
                    flimg = fleximage(img, arr=arr, fraclo=0.05, frachi=0.95)
                    flimg.move(10, 20)
                else:
                    flimg.update(img, arr=arr)
                    flimg.fig.canvas.set_window_title(title)
                    flimg.axtitle(title)

            if 'h' in dograph:
                if flspe is None:
                    flspe = flexhist(arr,
                                     bins=50,
                                     color='green',
                                     fraclo=0.001,
                                     frachi=0.999)
                    flspe.move(800, 20)
                else:
                    flspe.update(arr,
                                 bins=50,
                                 color='green',
                                 fraclo=0.001,
                                 frachi=0.999)
                    flspe.fig.canvas.set_window_title(title)
                    flspe.axtitle(title)

            if 'c' in dograph:
                if flims is None:
                    flims = fleximagespec(img, arr=arr, bins=100, color='lightgreen',\
                                          amin=args.gramin,   amax=args.gramax,\
                                          nneg=args.grnneg,   npos=args.grnpos,\
                                          fraclo=args.grfrlo, frachi=args.grfrhi,\
                    )
                    flims.move(10, 20)
                else:
                    #print(info_ndarr(arr, 'YYY before update arr: ', last=5))
                    flims.update(img, arr=arr)
                    flims.axtitle(title)

                gr.show(mode=1)

        if break_event_loop: break

    med_vs_evt = med_vs_evt[:nrec_med]
    med = np.median(med_vs_evt)
    q05 = np.quantile(med_vs_evt, 0.05, interpolation='linear')
    q95 = np.quantile(med_vs_evt, 0.95, interpolation='linear')

    print(info_ndarr(med_vs_evt, 'per event median  ', last=nrec_med - 1))
    print('  median over %d event-records: %.3f' % (nrec_med, med))
    print('  quantile(med_vs_evt, 0.05): %.3f' % q05)
    print('  quantile(med_vs_evt, 0.95): %.3f' % q95)

    if args.dograph:
        print(
            '\n  !!! TO EXIT - close graphical window(s) - click on [x] in the window corner'
        )
        gr.show()
        if args.ofname is not None:
            if 'i' in dograph:
                gr.save_fig(flimg.fig, fname=args.ofname + '-img', verb=True)
            if 'h' in dograph:
                gr.save_fig(flspe.fig, fname=args.ofname + '-spe', verb=True)
            if 'c' in dograph:
                gr.save_fig(flims.fig,
                            fname=args.ofname + '-imgspe',
                            verb=True)

    print(50 * '-')
Example #5
0
def loop_run_step_evt(args):
    """Data access example for confluence
     run, step, event loops
  """
    typeinfo = args.typeinfo.lower()
    do_loopruns = 'r' in typeinfo
    do_loopevts = 'e' in typeinfo
    do_loopsteps = 's' in typeinfo

    from psana.pyalgos.generic.NDArrUtils import info_ndarr
    #from psana import DataSource
    #ds = DataSource(exp=args.expt, run=args.run, dir=f'/cds/data/psdm/{args.expt[:3]}/{args.expt}/xtc', max_events=1000)

    ds = DataSource(**datasource_arguments(args))

    if do_loopruns:
        for irun, run in enumerate(ds.runs()):
            print('\n==== %02d run: %d exp: %s detnames: %s' %
                  (irun, run.runnum, run.expt, ','.join(run.detnames)))

            if not do_loopsteps: continue
            print('%s detector object' % args.detname)
            det = None if args.detname is None else run.Detector(args.detname)

            is_epix10ka = False if det is None else det.raw._dettype == 'epix10ka'
            is_epixhr2x2 = False if det is None else det.raw._dettype == 'epixhr2x2'

            try:
                step_docstring = run.Detector('step_docstring')
            except:
                step_docstring = None
            print('step_docstring detector object is %s' %
                  ('missing' if step_docstring is None else 'created'))
            print(
                'det.raw._seg_geo.shape():',
                det.raw._seg_geo.shape()
                if det.raw._seg_geo is not None else '_seg_geo is None')

            dcfg = det.raw._config_object() if '_config_object' in dir(
                det.raw) else None
            if dcfg is None: print('det.raw._config_object is MISSING')

            for istep, step in enumerate(run.steps()):
                print('\nStep %02d' % istep, end='')

                if step_docstring is not None:
                    sds = step_docstring(step)
                    try:
                        sdsdict = json.loads(sds)
                    except Exception as err:
                        print('\nERROR FOR step_docstring: ', sds)
                        logger.error(
                            'json.loads(step_docstring(step)) err: %s' %
                            str(err))
                        sdsdict = None

                metadic = None if step_docstring is None else sdsdict
                print('  metadata: %s' % str(metadic))

                if not do_loopevts: continue
                ievt, evt, segs = None, None, None
                for ievt, evt in enumerate(step.events()):
                    #if ievt>args.evtmax: exit('exit by number of events limit %d' % args.evtmax)
                    if not selected_record(ievt): continue
                    if segs is None:
                        segs = det.raw._segment_numbers(
                            evt) if det is not None else None
                        print('  Event %05d %s     ' %
                              (ievt, info_ndarr(segs, 'segments')))
                        raw = det.raw.raw(evt)
                        print(info_ndarr(raw, '    det.raw.raw(evt)'))
                        #print('gain mode statistics:' + ue.info_pixel_gain_mode_statistics(gmaps))

                        if dcfg is not None:
                            s = '    gain mode fractions for: FH       FM       FL'\
                                '       AHL-H    AML-M    AHL-L    AML-L\n%s' % (29*' ')
                            print(
                                ue.info_pixel_gain_mode_fractions(det.raw,
                                                                  evt,
                                                                  msg=s))

                    print(info_det_evt(det, evt, ievt), end='\r')
                print(info_det_evt(det, evt, ievt), end='\n')