def measTbtBetaPhi(nturns = 2000): """ measure the beta function, phase and tune >>> beta, phi, tune = measTbtBetaPhi() """ tw = ap.getTwiss('p*c*', ['s', 'betax', 'betay', 'etax', 'phix', 'phiy']) # trig=0 is internal, trig=1 is external names, x0, y0, Isum0, timestamp, offset = \ ap.nsls2.getSrBpmData(waveform="Tbt",trig=1, count=nturns, output=False) # adjust the offset, align to the original zero nbpm, nturns = np.shape(Isum0) nturns = nturns + np.max(offset) x = np.zeros((nbpm, nturns), 'd') y = np.zeros((nbpm, nturns), 'd') Isum = np.zeros((nbpm, nturns), 'd') # convert nm to mm for i in range(nbpm): x[i,offset[i]:offset[i]+nturns] = x0[i,:]*1e-6 y[i,offset[i]:offset[i]+nturns] = y0[i,:]*1e-6 Isum[i,offset[i]:offset[i]+nturns] = Isum0[i,:] n = nturns - 200 btx = ap.calcBetaAu(x[:,3:n], ref=tw[:,1]) bty = ap.calcBetaAu(y[:,3:n], ref=tw[:,2]) phix = ap.calcPhase(x[:,3:n], ref=tw[:,4]) phiy = ap.calcPhase(y[:,3:n], ref=tw[:,5]) return (btx, bty), (phix, phiy), None
def __init__(self, bpms, cors, parent = None): super(OrbitCorrGeneral, self).__init__(parent) self.bpms, self.cors = bpms, cors self.sb = [bpm.sb for bpm in self.bpms] self.x0, self.y0 = None, None self._update_current_orbit() self.table = QTableWidget(len(self.bpms), 9) self.table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) hdview = QHeaderView(Qt.Horizontal) self.table.setHorizontalHeaderLabels( ['BPM Name', 's', "Beta X", "Beta Y", "Eta X", 'X Bump', 'Y Bump', "Target X", "Target Y"]) self._twiss = getTwiss([b.name for b in self.bpms], ["s", "betax", "betay", "etax"]) for i,bpm in enumerate(self.bpms): it = QTableWidgetItem(bpm.name) it.setFlags(it.flags() & (~Qt.ItemIsEditable)) self.table.setItem(i, 0, it) it = QTableWidgetItem(str(bpm.sb)) it.setFlags(it.flags() & (~Qt.ItemIsEditable)) #it.setMinimumWidth(80) self.table.setItem(i, 1, it) self.table.setItem(i, 2, QTableWidgetItem("%.4f" % self._twiss[i,1])) self.table.setItem(i, 3, QTableWidgetItem("%.4f" % self._twiss[i,2])) self.table.setItem(i, 4, QTableWidgetItem("%.4f" % self._twiss[i,3])) for j in range(5, 9): it = QTableWidgetItem(str(0.0)) it.setData(Qt.DisplayRole, str(0.0)) it.setFlags(it.flags() | Qt.ItemIsEditable) self.table.setItem(i, j, it) # use the current orbit #self.table.item(i,4).setData(Qt.DisplayRole, str(self.x0[i])) #self.table.item(i,5).setData(Qt.DisplayRole, str(self.y0[i])) #self.connect(self.table, SIGNAL("cellClicked(int, int)"), # self._cell_clicked) self.table.resizeColumnsToContents() #self.table.horizontalHeader().setStretchLastSection(True) #for i in range(4): # print "width", i, self.table.columnWidth(i) #self.table.setColumnWidth(0, 300) self.table.setColumnWidth(1, 80) vbox1 = QtGui.QVBoxLayout() frmbox = QFormLayout() self.base_orbit_box = QtGui.QComboBox() #self.base_orbit_box.addItems([ # "Current Orbit", "All Zeros"]) self.base_orbit_box.addItems(["All Zeros", "Current Orbit"]) frmbox.addRow("Orbit Base", self.base_orbit_box) grp = QtGui.QGroupBox("Local Bump") grp.setLayout(frmbox) vbox1.addWidget(grp) frmbox = QFormLayout() hln1 = QtGui.QFrame() hln1.setLineWidth(3) hln1.setFrameStyle(QtGui.QFrame.Sunken) hln1.setFrameShape(QtGui.QFrame.HLine) frmbox.addRow(hln1) self.repeatbox = QSpinBox() self.repeatbox.setRange(1, 20) self.repeatbox.setValue(3) # or connect the returnPressed() signal frmbox.addRow("&Repeat correction", self.repeatbox) self.rcondbox = QLineEdit() self.rcondbox.setValidator(QDoubleValidator(0, 1, 0, self)) self.rcondbox.setText("1e-2") frmbox.addRow("r&cond for SVD", self.rcondbox) self.scalebox = QDoubleSpinBox() self.scalebox.setRange(0.01, 5.00) self.scalebox.setSingleStep(0.01) self.scalebox.setValue(0.68) frmbox.addRow("&Scale correctors", self.scalebox) #hln2 = QtGui.QFrame() #hln2.setLineWidth(3) #hln2.setFrameStyle(QtGui.QFrame.Sunken) #hln2.setFrameShape(QtGui.QFrame.HLine) #frmbox.addRow(hln2) self.progress = QProgressBar() self.progress.setMaximum(self.repeatbox.value()) self.progress.setMaximumHeight(15) frmbox.addRow("Progress", self.progress) grp = QtGui.QGroupBox("Correction") grp.setLayout(frmbox) vbox1.addWidget(grp) #vbox.addStretch(1.0) #self.qdb = QDialogButtonBox(self) #self.qdb.addButton("APP", QDialogButtonBox.ApplyRole) #self.qdb.addButton("R", QDialogButtonBox.ResetRole) #btn.setDefault(True) #self.qdb.addButton(QDialogButtonBox.Cancel) #self.qdb.addButton(QDialogButtonBox.Help) gbox = QtGui.QGridLayout() btn = QPushButton("Clear") self.connect(btn, SIGNAL("clicked()"), self.resetBumps) gbox.addWidget(btn, 0, 1) self.correctOrbitBtn = QPushButton("Apply") #self.correctOrbitBtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.correctOrbitBtn.setStyleSheet("QPushButton:disabled { color: gray }"); self.connect(self.correctOrbitBtn, SIGNAL("clicked()"), self.call_apply) self.correctOrbitBtn.setDefault(True) gbox.addWidget(self.correctOrbitBtn, 1, 1) gbox.setColumnStretch(0, 1) vbox1.addStretch() vbox1.addLayout(gbox) hbox1 = QtGui.QHBoxLayout() hbox1.addWidget(self.table, 2) hbox1.addLayout(vbox1, 0) self.setLayout(hbox1) self.connect(self.base_orbit_box, SIGNAL("currentIndexChanged(QString)"), self.updateTargetOrbit) self.connect(self.repeatbox, SIGNAL("valueChanged(int)"), self.progress.setMaximum) self.connect(self.table, SIGNAL("cellChanged (int, int)"), self.updateBump)
def __init__(self, cors, field, parent=None, nmax=4): super(CorrectorViewer, self).__init__(parent) self._nmax = nmax self._field = field self._cors = cors self._corlst1 = QtGui.QTreeWidget() self._header = dict([("Element", 0), ("Family", 1), ("s [m]", 2), ("Alpha X", 3), ("Alpha Y", 4), ("Beta X", 5), ("Beta Y", 6), ("Phi X", 7), ("Phi Y", 8), ("Eta X", 9)]) self._twiss = np.zeros((len(self._cors), 8), 'd') self._tunes = getTunes(source="database") self._corlst1.setColumnCount(len(self._header)) self._corlst1.setHeaderLabels( sorted(self._header, key=self._header.get)) self._corlst1.header().setStretchLastSection(False) prevcell = None for i,c in enumerate(self._cors): if c.cell and (prevcell is None or c.cell != prevcell.text(0)): # a new parent prevcell = QtGui.QTreeWidgetItem() prevcell.setText(0, c.cell) self._corlst1.addTopLevelItem(prevcell) it = QtGui.QTreeWidgetItem() it.setData(0, Qt.UserRole, i) it.setText(self._header["Element"], c.name) it.setText(self._header["Family"], c.family) it.setText(self._header["s [m]"], "%.3f" % c.sb) try: tw = getTwiss(c.name, ["s", "alphax", "alphay", "betax", "betay", "phix", "phiy", "etax"]) self._twiss[i,:] = tw[0,:] it.setText(self._header["Alpha X"], "%.4f" % self._twiss[i,1]) it.setText(self._header["Alpha Y"], "%.4f" % self._twiss[i,2]) it.setText(self._header["Beta X"], "%.4f" % self._twiss[i,3]) it.setText(self._header["Beta Y"], "%.4f" % self._twiss[i,4]) it.setText(self._header["Phi X"], "%.4f" % self._twiss[i,5]) it.setText(self._header["Phi Y"], "%.4f" % self._twiss[i,6]) it.setText(self._header["Eta X"], "%.4f" % self._twiss[i,7]) except: it.setDisabled(True) pass if c.cell: prevcell.addChild(it) else: self._corlst1.addTopLevelItem(it) prevcell = it for j in range(2, len(self._header)): it.setTextAlignment(j, Qt.AlignRight) self._corlst1.expandAll() for i in range(len(self._header)): self._corlst1.resizeColumnToContents(i) #self._corlst1.setColumnWidth(0, 150) #self.elemlst.setSelectionMode(QAbstractItemView.MultiSelection) columns = ['Corrector', 's', 'Alpha', 'Beta', 'Phi', "dPhi", "Initial Bump", "Cur. Sp", "dBump", "Final Rb"] self.table4 = QTableWidget(0, len(columns)) #self.table4.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) hdview = QHeaderView(Qt.Horizontal) self.table4.setHorizontalHeaderLabels(columns) #for i in range(4): # for j in range(len(columns)): # it = QTableWidgetItem() # if j > 0: it.setTextAlignment( # Qt.AlignRight | Qt.AlignVCenter) # if columns[j] != "dKick": # it.setFlags(it.flags() & (~Qt.ItemIsEditable)) # self.table4.setItem(i, j, it) #self.table4.resizeColumnsToContents() #self.table4.horizontalHeader().setStretchLastSection(True) #hrow = self.table4.rowHeight(0) #htbl = (hrow * 4) + self.table4.horizontalHeader().height() +\ # 2*self.table4.frameWidth() #self.table4.setMinimumHeight(htbl + 10) #self.table4.setMaximumHeight(htbl + 15) #self.table4.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) #print "Size:", htbl + 10 self.table4.resize(self.table4.width(), 150) splitter = QtGui.QSplitter(Qt.Vertical) splitter.addWidget(self._corlst1) splitter.addWidget(self.table4) vbox1 = QtGui.QVBoxLayout() vbox1.addWidget(splitter) self.setLayout(vbox1) self.connect(self._corlst1, SIGNAL("doubleClicked(QModelIndex)"), self.addCorrector) #self.connect(self.src, SIGNAL("returnPressed()"), # self._calc_source) #self.connect(self.table4, SIGNAL("cellChanged(int, int)"), # self.updateTable) #self.connect(self.table4, SIGNAL("doubleClicked(QModelIndex)"), # self.delCorrector) self._x0 = fget(self._cors, "x", handle="setpoint", unitsys=None) self._y0 = fget(self._cors, "y", handle="setpoint", unitsys=None)
def __init__(self, bpms = None, cors = None, parent = None): super(OrbitCorrDlg, self).__init__(parent) # add bpms bpmls = bpms if bpms is None: bpmls = getElements("BPM") bpmls = [bpm for bpm in bpmls if bpm.isEnabled()] pvx = [bpm.pv(field="x", handle="readback")[0] for bpm in bpmls] pvy = [bpm.pv(field="y", handle="readback")[0] for bpm in bpmls] # #self._update_current_orbit() s = [bpm.sb for bpm in bpmls] self.bpm_plot = ApCaArrayPlot([pvx, pvy], x = [s, s]) magprof = getBeamlineProfile() self.bpm_plot.setMagnetProfile(magprof) self.xc = Qwt.QwtPlotCurve() self.xc.setTitle("Target X") self.xc.attach(self.bpm_plot) #self.xc.setData(s, self.x0) self.bpm_plot.showCurve(self.xc, True) self.yc = Qwt.QwtPlotCurve() self.yc.setTitle("Target Y") self.yc.attach(self.bpm_plot) #self.yc.setData(s, self.y0) self.bpm_plot.showCurve(self.yc, True) #self.bpm_plot._cheat = [Qwt.QwtPlotCurve(), Qwt.QwtPlotCurve()] #self.bpm_plot._cheat[0].setTitle("_CHEAT_ X") #self.bpm_plot._cheat[1].setTitle("_CHEAT_ Y") #self.bpm_plot._cheat[0].attach(self.bpm_plot) #self.bpm_plot._cheat[1].attach(self.bpm_plot) #self.bpm_plot._set_symbol(self.bpm_plot._cheat[0], # Qwt.QwtSymbol.Triangle, dsize=2) #self.bpm_plot._set_symbol(self.bpm_plot._cheat[1], # Qwt.QwtSymbol.Diamond, dsize=2) #self.bpm_plot.showCurve(self.bpm_plot._cheat[0], False) #self.bpm_plot.showCurve(self.bpm_plot._cheat[1], False) self.bpm_plot.setContentsMargins(12, 10, 10, 10) # add corrector plots corls = cors if cors is None: corls = [] for c in getElements("HCOR") + getElements("VCOR"): if c not in corls: corls.append(c) pvx = [c.pv(field="x", handle="setpoint")[0] for c in getElements("HCOR") if c in corls] pvy = [c.pv(field="y", handle="setpoint")[0] for c in getElements("VCOR") if c in corls] s = [c.sb for c in corls] self.cor_plot = ApCaArrayPlot([pvx, pvy], x = [s, s], labels=["HCOR Sp", "VCOR Sp"]) #magprof = aphla.getBeamlineProfile() self.cor_plot.setMagnetProfile(magprof) self.cor_plot.setContentsMargins(12, 10, 10, 10) #self.cor_plot.setMinimumHeight(200) #self.cor_plot.setMaximumHeight(250) #print s, pvx # add twiss plots self._twiss = getTwiss("*", ["s", "betax", "betay", "etax"]) #for i in range(len(self._twiss)): # print i, self._twiss[i,0], self._twiss[i,1] self.tw_plot = ApCaPlot() cbetax = Qwt.QwtPlotCurve() pen = cbetax.pen() pen.setColor(Qt.red) cbetax.setPen(pen) cbetay = Qwt.QwtPlotCurve() pen.setColor(Qt.green) cbetay.setPen(pen) cetax = Qwt.QwtPlotCurve() pen.setColor(Qt.blue) cetax.setPen(pen) cbetax.setData(self._twiss[:,0], self._twiss[:,1]) cbetay.setData(self._twiss[:,0], self._twiss[:,2]) cetax.setData(self._twiss[:,0], 10.0*self._twiss[:,3]) cbetax.attach(self.tw_plot) cbetax.setTitle("Beta X") cbetay.attach(self.tw_plot) cbetay.setTitle("Beta Y") cetax.attach( self.tw_plot) cetax.setTitle("10*Eta X") self.tw_plot.setMagnetProfile(magprof) self.tw_plot.setContentsMargins(12, 10, 10, 10) self.tw_plot.showCurve(cbetax, True) self.tw_plot.showCurve(cbetay, True) self.tw_plot.showCurve(cetax, True) # top tabs for plotting tabs = QtGui.QTabWidget() tabs.addTab(self.bpm_plot, "Orbit") tabs.addTab(self.cor_plot, "Correctors") tabs.addTab(self.tw_plot, "Twiss (Design)") tabs.setMinimumHeight(200) tabs.setMaximumHeight(280) layout = QVBoxLayout() layout.addWidget(tabs, 1) tabs = QtGui.QTabWidget() tab_general_cor = OrbitCorrGeneral(bpmls, corls) self.connect(tab_general_cor, SIGNAL("targetOrbitChanged(PyQt_PyObject, PyQt_PyObject)"), self._update_orbit_plot) xobt, yobt = tab_general_cor.getTargetOrbit() self._update_orbit_plot(xobt, yobt) tabs.addTab(tab_general_cor, "General Bump") tab_bump3xcor = Bump3XCor(corls, "x") tab_bump3xsrc = Bump3XSrc(corls, "x") tab_bump4xcor = Bump4XCor(corls, "x") tab_bump4xsrc = Bump4XSrc(corls, "x") #plots=[self.bpm_plot, self.cor_plot, self.tw_plot]) #self.connect(tab_nbump_cor, # SIGNAL("correctorChanged(PyQt_PyObject)"), # self._update_corr_plot) tabs.addTab(tab_bump3xcor, "3 Cors. dI") tabs.addTab(tab_bump3xsrc, "3 Cors. dX") tabs.addTab(tab_bump4xcor, "4 Cors. dI") tabs.addTab(tab_bump4xsrc, "4 Cors. dX dTheta") layout.addWidget(tabs, 3) self.connect(tab_bump3xcor, SIGNAL("zoomInCorrectors(PyQt_PyObject)"), self._zoom_in_cors) self.connect(tab_bump3xsrc, SIGNAL("zoomInCorrectors(PyQt_PyObject)"), self._zoom_in_cors) self.connect(tab_bump4xcor, SIGNAL("zoomInCorrectors(PyQt_PyObject)"), self._zoom_in_cors) self.connect(tab_bump4xsrc, SIGNAL("zoomInCorrectors(PyQt_PyObject)"), self._zoom_in_cors) self.connect(tab_bump3xcor, SIGNAL("clearCorrectors(PyQt_PyObject)"), self._clear_cors) self.connect(tab_bump3xsrc, SIGNAL("clearCorrectors(PyQt_PyObject)"), self._clear_cors) self.connect(tab_bump4xcor, SIGNAL("clearCorrectors(PyQt_PyObject)"), self._clear_cors) self.connect(tab_bump4xsrc, SIGNAL("clearCorrectors(PyQt_PyObject)"), self._clear_cors) #hln = QtGui.QFrame() #hln.setLineWidth(3) #hln.setFrameStyle(QtGui.QFrame.Sunken) #hln.setFrameShape(QtGui.QFrame.HLine) #layout.addWidget(hln) # the cheat button #hbox = QHBoxLayout() #hbox.addStretch() #btn = QPushButton("__CHEAT__") #self.connect(btn, SIGNAL("clicked()"), self._cheat) #hbox.addWidget(btn) #layout.addLayout(hbox) self.setLayout(layout) #self.update_orbit = update_orbit #self._x0 = tuple(x) # save for reset #self._y0 = tuple(y) # save for reset #self.val = [s, x, y] self.setMinimumWidth(1000) self.setWindowFlags(Qt.Window)