def make_event_time(sec, nanosec, fid): if not has_psana: print("No psana") return time = int((sec << 32) | nanosec) et = psana.EventTime(time, fid) return time, et
def runclient(args): # Read cxi file #print args.exprun, args.exprun.split('run=')[-1] runNum = int(args.exprun.split('run=')[-1]) runStr = str(runNum).zfill(4) src = srcDir + '/r' + runStr + '/cxic0415_' + runStr + '.cxi' f = h5py.File(src, 'r') sec = f['/LCLS/machineTime'].value nsec = f['/LCLS/machineTimeNanoSeconds'].value fiducial = f['/LCLS/fiducial'].value print len(sec) zipall = zip(sec, nsec, fiducial) lst_zipall = list(zipall) numImages = len(sec) f.close() ds = psana.DataSource(args.exprun + ':idx') run = ds.runs().next() det1 = psana.Detector(args.areaDetName) md = mpidata() for nevents, val in enumerate(lst_zipall): if nevents == args.noe: break if nevents % (size - 1) != rank - 1: continue # different ranks look at different events #print rank, nevents et = psana.EventTime(int((sec[nevents] << 32) | nsec[nevents]), fiducial[nevents]) evt = run.event(et) #print sec[nevents], nsec[nevents], fiducial[nevents] #print evt calib = det1.calib(evt) absError = 30. # ADU #Important: unbonded pixels must not be SZ compressed noise = np.random.uniform( low=-absError / 2, high=absError / 2, size=calib.shape) * det1.mask_comb(evt, mbits=1) noisyCalib = np.round(noise + calib) # adding uniform random noise of 30 ADU #error = calib - noisyCalib counter = 0 img = np.zeros((1480, 1552)) for quad in range(4): for seg in range(8): img[seg * 185:(seg + 1) * 185, quad * 388:(quad + 1) * 388] = noisyCalib[counter, :, :] counter += 1 if nevents % 100 == 0: print "Progress: ", nevents if img is None: continue md.addarray('noisyCalib', img) md.small.index = nevents md.small.runStr = runStr md.send() md.endrun()
def next_event(self): """Grabs the next event and returns the translated version""" if self.timestamps: try: evt = self.run.event(self.timestamps[self.i]) except (IndexError, StopIteration) as e: logging.warning('End of Run.') if 'end_of_run' in dir(Worker.conf): Worker.conf.end_of_run() ipc.mpi.slave_done() return None self.i += 1 elif self.times is not None: evt = None while self.i < len(self.times) and evt is None: time = psana.EventTime(int(self.times[self.i]), self.fiducials[self.i]) self.i += 1 evt = self.run.event(time) if evt is None: print "Unable to find event listed in index file" # We got to the end without a valid event, time to call it a day if evt is None: logging.warning('End of Run.') if 'end_of_run' in dir(Worker.conf): Worker.conf.end_of_run() ipc.mpi.slave_done() return None else: try: while (self.i % self.event_slice.step) != self.event_slice.start: evt = self.data_source.events().next() self.i += 1 if self.N is not None and self.i >= self.N: raise StopIteration evt = self.data_source.events().next() self.i += 1 except StopIteration: logging.warning('End of Run.') if 'end_of_run' in dir(Worker.conf): Worker.conf.end_of_run() ipc.mpi.slave_done() return None return EventTranslator(evt, self)
def setup_exp(exp_name, run_num, det_name, mask_calib_on=True, mask_status_on=True, mask_edges_on=True, mask_central_on=True, mask_unbond_on=True, mask_unbondnrs_on=True): """ :param exp_name: :param run_num: :param mask_calib_on: :param mask_status_on: :param mask_edges_on: :param mask_central_on: :param mask_unbond_on: :param mask_unbondnrs_on: :return: """ ds = psana.DataSource('exp={}:run{}:idx'.format(exp_name, run_num)) run = ds.runs().next() times = run.times() env = ds.env() det = psana.Detector(det_name, env) et = psana.EventTime(int(ts[0]), fid[0]) evt = run.event(et) example = det.image(evt) shape = example.shape # get mask and save mask mask_stack = det.mask(evt, calib=mask_calib_on, status=mask_status_on, edges=mask_edges_on, central=mask_central_on, unbond=mask_unbond_on, unbondnbrs=mask_unbondnrs_on) mask_2d = det.image(evt, mask_stack) return
fout.create_dataset('ipm5_pre',data=ipm5_pre) fout.create_dataset('ipm5_prepre',data=ipm5_prepre) fout.create_dataset('evr162_off',data=evr162_drop) fout.create_dataset('evr162_pre',data=evr162_pre) fout.create_dataset('evr162_prepre',data=evr162_prepre) offModuleDatasets=[] preModuleDatasets=[] for imodule,module in enumerate(ped): offModuleDatasets.append(fout.create_dataset('epix10ka2m_off_%02d'%imodule,moduleShape)) preModuleDatasets.append(fout.create_dataset('epix10ka2m_pre_%02d'%imodule,moduleShape)) print 'we have %d events for ghost fitting'%offfids.shape[0] for ievt, evtfid, evttime, preevtfid, preevttime in itertools.izip(itertools.count(), offfids, offtimes, preofffids, preofftimes): print 'get events...', evttime, evtfid, ' --- ', preevttime, preevtfid evtt = psana.EventTime(evttime,evtfid) evt = runIdx.event(evtt) preevtt = psana.EventTime(preevttime,preevtfid) preevt = runIdx.event(preevtt) try: epixDet.getData(evt) epixDet.processFuncs() epixDict=getUserData(epixDet) epixDetPre.getData(preevt) epixDetPre.processFuncs() epixDictPre=getUserData(epixDetPre) for offDs, preDs, module, modulePre in zip( offModuleDatasets, preModuleDatasets, epixDict['full_area'], epixDictPre['full_area']): offDs[ievt] = module preDs[ievt] = modulePre
#get the position of the "best" event at this step #selecting the highest userVal event here. Can do whatever though. selUserVal = np.argmin(np.abs(selVars-pickVal)).values.tolist() #and now get the event ID & selection variable at this point selEvt_fiducials.append(fids[selUserVal].values.tolist()) selEvt_evt_times.append(times[selUserVal].values.tolist()) selEvt_selVar.append(selVars[selUserVal].values.tolist()) selEvt_fiducials=np.array(selEvt_fiducials) selEvt_evt_times=np.array(selEvt_evt_times) selEvt_selVar=np.array(selEvt_selVar) #now loop over these events to get data. Get both raw data & image here. cs140_data = [] cs140_img = [] for evtfid,evttime in itertools.izip(selEvt_fiducials, selEvt_evt_times): evtt = psana.EventTime(evttime,evtfid) evt = runIdx.event(evtt) #now loop over detectors in this event det.getData(evt) det.processFuncs() cs140_data.append(det.evt.write_full) cs140_img.append(det.det.image(run,det.evt.write_full)) #now write hdf5 file & add detector info to it. cs140_data = np.array(cs140_data) cs140_img = np.array(cs140_img) fh5 = h5py.File('%s/%s_%d_%s_%d.h5'%(dirname,expname, run, selectionVar.replace('/','_'), ipV),'w') fh5['cs140_raw']=cs140_data.astype(float) fh5['cs140_img']=cs140_img.astype(float) fh5['nano_y']=nano_y.astype(float) fh5['nano_z']=nano_z.astype(float)
def main(): params = parse_input_arguments(sys.argv) exp_data_path = params['idir'] outdir = params['odir'] # specific experimental parameters, change as necessary det_name = 'pnccdBack' run_num_list = [ '182', '183', '184', '185', '186', '188', '190', '191', '192', '193', '194', '196', '197' ] exp_name = 'amo86615' h5file_suffix = '_PR772_single' for fileInd in range(len(run_num_list)): t1 = time.time() # Open files and get data run_num = run_num_list[fileInd] f = h5py.File(exp_data_path + '_' + run_num + h5file_suffix + '.h5') ts = f['/photonConverter/eventTime'].value print(ts) fid = f['/photonConverter/fiducials'].value ds = psana.DataSource('exp=' + exp_name + ':run=' + run_num + ':idx') run = ds.runs().next() times = run.times() env = ds.env() det = psana.Detector(det_name, env) et = psana.EventTime(int(ts[0]), fid[0]) evt = run.event(et) example = det.image(evt) shape = example.shape ## get mask and save mask mask_calibOn = True mask_statusOn = True mask_edgesOn = True mask_centralOn = True mask_unbondOn = True mask_unbondnrsOn = True psanaMask = det.mask(evt, calib=mask_calibOn, status=mask_statusOn, edges=mask_edgesOn, central=mask_centralOn, unbond=mask_unbondOn, unbondnbrs=mask_unbondnrsOn) mask = det.image(evt, psanaMask) np.save(outdir + 'mask_2d_%s.npy' % (run_num_list[fileInd]), mask) np.save(outdir + 'mask_stack_%s.npy' % (run_num_list[fileInd]), psanaMask) # Collect photons patterns photons = np.zeros((len(ts), shape[0], shape[1])) ADUs = np.zeros((len(ts), shape[0], shape[1])) print("There are totally %d patterns to process." % len(ts)) for num in range(len(ts)): print("This is the %d th pattern" % num) et = psana.EventTime(int(ts[num]), fid[num]) evt = run.event(et) photon_stack = det.photons(evt, adu_per_photon=130) photons[num, :, :] = det.image(evt, photon_stack) adu = det.image(evt) ADUs[num, :, :] = adu np.save(outdir + 'photons_%s.npy' % (run_num_list[fileInd]), photons) np.save(outdir + 'ADUs_%s.npy' % (run_num_list[fileInd]), ADUs) ## close files f.close() print 'Finished %s' % run_num t2 = time.time() print('total time usage: %f' % (t2 - t1))