
! *** OPTICS=AD_ACCEL_HE-04JUL20 ***

! LCLS2sc undulator and undulator extension

! ==============================================================================
! Modification History
! ------------------------------------------------------------------------------
! 22-JUN-2020, M. Woodley
!  * remove XLEAP-II wiggler (WIGXLh) from SXCEL18
!  * populate empty SXR cells #17-25 as standard cells, including undulators
!  * change keyword of BLM's to MARK ... will be non-MAD per K. Luchini
! 03-MAR-2020, M. Woodley
!  * move XLEAP wiggler to SXR cell #18
!  * change keyword for "type-V" correctors from HKIC/VKIC to INST
! ------------------------------------------------------------------------------
! 12-DEC-2019, M. Woodley
!  * swap HXR cells 20 and 21 ... cell 21 is now the empty cell per H.-D. Nuhn
!  * add "h" to HXR and SXR undulators, quadrupoles, and phase shifters
! 20-NOV-2019, M. Woodley
!  * add BOD10 and BOD12 for SXRSS-II project per D. Bruch
! 13-NOV-2019, M. Woodley
!  * KQDHXM, KQUE1, KQUE2, and KQDMP values come from Y. Nosochkov's
!    UNDH_KQ4_USDS.xsif file, for E= 4.0 GeV and KHXU= 2.0 (IntgHX= 30.0 kG)
! 17-JUL-2019, M. Woodley
!  * add cell number to phase shifter names
! ------------------------------------------------------------------------------
! 10-MAY-2019, M. Woodley
!  * move BLM's in HXR cells from before undulator to after per H.-D. Nuhn
! 30-APR-2019, M. Woodley
!  * remove M1, M2, and YAGBRAG from HXRSS self-seeding chicane per H.-D. Nuhn
! 19-FEB-2019, M. Woodley
!  * move SXRSS self-seeding chicane from undulator cell 33 to cell 35 per
!    G. Marcus (DCR_2018-012)
!  * move undulator 35 and associated devices to cell 33 (replace "35" with
!    "33" in device names)
! ------------------------------------------------------------------------------
! 08-JAN-2019, M. Woodley
!  * define HXRSS and SXRSS chicane bend angles (rather than integrated
!    strengths) to allow constant R56 when energy changes
!  * set nominal SXRSS beam offset to +17.0 mm per G. Marcus
!  * set nominal HXRSS R56 to +15.0 um per G. Marcus
! ------------------------------------------------------------------------------
! 27-NOV-2018, M. Woodley
!  * move definition of Eu to LCLS2sc_master.xsif and LCLS2cu_master.xsif
!    (for BMAD compatability)
! 29-MAY-2018, M. Woodley
!  * install LCLS HXRSS self-seeding chicane in HXR cell #28 per
!    LCLSII-3.2-PR-0102-R1; chicane is off for high-rate beams from SC linac
!  * install LCLS SXRSS self-seeding chicane in SXR cell #33 per
!    LCLSII-3.2-PR-0101-R0
! 30-APR-2018, M. Woodley
!  * set aperture radius of PCPM0/PCPM0B to 1.75 cm (circular) per RP-14-15-R5
!    section 4.3 (two permanant magnet bends in SXR safety dump line)
! 27-APR-2018, M. Woodley
!  * add a second set of beam phase monitors 1.5 m u/s of original set per
!    C. Xu (TID); original cavities (PH31-32,PH31B-32B, for low-rate beam) are
!    2805 MHz ... new cavities (PH33-34,PH33B-34B, for high-rate beam) will be
!    2604 MHz; all but original LCLS cavities (PH31-32) are deferred
! ------------------------------------------------------------------------------
! 22-JAN-2018, M. Woodley
!  * move SXR phase shifters 5 mm upstream per D. Bruch
! 20-DEC-2017, M. Woodley
!  * defer (level 0) PH31b and PH32b
! 04-DEC-2017, M. Woodley
!  * define dzHXTES parameter to adjust lengths of DUE5e and DDLWALL by ~0.7 um
!    to set BSY-Z of MDLWALL to exactly 685 m (to align with HXR XTES system)
!  * define drifts DUE5eB and DDLWALLB (DUE5e and DDLWALL on SXR side)
!  * define dzSXTES parameter to adjust lengths of DUE5eB and DDLWALLB by ~0.7 um
!    to set BSY-Z of MDLWALLb to exactly 685 m (to align with SXR XTES system)
! ------------------------------------------------------------------------------
! 06-SEP-2017, Y. Nosochkov
!  * move PH32, PH32B 0.012 m upstream to Z = 674.062492 (BSY coordinates)
!    (M. Kosovsky)
!  * change length of DU2h drift from negative value to zero, adjust lengths
!    of nearby drifts to keep the same device positions
!  * update definition of phase shifters in undulators (H.-D. Nuhn),
!    increase phase shifter period to 75 mm (SXR) and 45 mm (HXR),
!    define phase shifter undulator K-parameter through phase integral (PI)
!  * note: the present setting of the integrated field IntgHX of HXR undulator
!    quadrupoles is 12 kG. Per H.-D. Nuhn, it needs to be increased to 26 kG.
!    This however, does not match well with the dumpline optics at 4 GeV.
!    Additional quad(s) may need to be included downstream of the undulator
!    or in the dumpline -- to be studied
! 05-SEP-2017, M. Woodley
!  * move IntgSX and IntgHX defn's to LCLS2sc_main.xsif and LCLS2cu_main.xsif
!  * move XbandF definition to common.xsif
! ------------------------------------------------------------------------------
! 05-MAY-2017, M. Woodley
!  * Beam Loss Monitors (BLMS*, BLMH*): keyword=INST
! ------------------------------------------------------------------------------
! 24-FEB-2017, Y. Nosochkov
!  * update device positions in the undulator cells (D. Bruch)
!    note that RFBHX12 moves upstream into LTUH
!  * remove RFBHX49 (D. Bruch, H-D. Nuhn)
!  * move RFBHX51, RFBSX51 0.057 m upstream (D. Bruch)
!  * move BTMQUEB 0.1016 m downstream (M. Owens)
! ------------------------------------------------------------------------------
! 02-NOV-2016, Y. Nosochkov
!  * specify half-aperture of PCTCX, PCTCXB to be 4.5 mm (per deck note)
! ------------------------------------------------------------------------------
! 24-JUN-2016, Y. Nosochkov
!  * defer PCTCXB, SPTCXB to level @1 (P. Emma)
!  * change the type of existing BPMUE1, BPMUE2 from Stripline-1 to
!    Stripline-13 (M. Woodley, Alev)
!  * change the type of BPMUE1B, BPMUE2B from Stripline-1 to Stripline-5 to
!    fit the 2Q10 quads
!  * remove existing toroids IMUNDO, IMBCS3 as they are not compatible
!    with SCRF beam (S. Mao, P. Emma)
!  * remove deferred IMUNDOB (S. Mao)
!  * update positions of BPMUE1&2, BPMUE1B&2B and BTMQUEB (M. Owens)
! ------------------------------------------------------------------------------
! 26-FEB-2016, Y. Nosochkov
!  * replace the HXR undulator with HGVPU undulator (D. Bruch, M. Rowen)
!  * VPU undulator length: 3.373968 m
!  * VPU undulator cell length: 4.01266666667 m
!  * same undulator period and K-value in VPU as before (H-D. Nuhn)
!  * move the RFBHX16, previously located upstream of undulator, to just after
!    the VPU undulator and rename to RFBHX49 -- this keeps the number
!    of RFBPMs unchanged
! 26-FEB-2016, M. Woodley
!  * remove SEQnn MARKers
!  * define new areas: UEH (HXR end to BYD1) and UES (SXR end to BYD1B)
! ------------------------------------------------------------------------------
! 24-AUG-2015, Y. Nosochkov
!  * adjust positions of BPMUE1, BPMUE2 (per M. Owens)
!  * match dumpline optics to new TCAV/OTR constraints (per Y. Ding)
! ------------------------------------------------------------------------------
! 19-JUN-2015, Y. Nosochkov
!  * assign IMUNDO, IMUNDOb to be BCS current monitors
! 20-MAY-2015, Y. Nosochkov
!  * change the status of BPMUE1, BPMUE2, BPMUE1B, BPMUE2B from deferred
!    to baseline
!  * update aperture of PCPM0, PCPM0B (per Shanjie)
!  * defer THz Be foils TRUE1, TRUE1B at level @5 (per Tor and A. Fisher)
! ------------------------------------------------------------------------------
! 20-MAR-2015, M. Woodley
!  * assign TYPEs to BCS devices ... defer at level 0
! 12-MAR-2015, Y. Nosochkov
!  * change type of QUE1B, QUE2B from "1.97Q10" to "2Q10"
! ------------------------------------------------------------------------------
! 12-DEC-2014, M. Woodley
!  * change some RFBs (defer=2) to BPMs (defer=0)
!  * assign BPM TYPE attributes per PRD LCLSII-2.4-PR-0136
! 09-DEC-2014, Y. Nosochkov
!  * restore dumpline toroid IMBCS3 in HXR (per LCLSII-2.4-PR-0107)
!  * assign the non-baseline level "4" to TRUE1b (Be foil, THz) in SXR
!  * change type of QUE1b, QUE2b to "1.97Q10"
!  * change type of undulator quads to "0.433Q3.1" (per J. Amann)
!  * rename BTM0, BTM0b to BTMQUE, BTMQUEb (per Shanjie Xiao)
!  * add BTM0, BTM0b downstream of PCPM0 and PCPM0b (per Shanjie Xiao)
!  * change stripline to RF bpms in the SXR undulator extension:
!    BPMSX16 -> RFBSX16, BPMSX19 -> RFBSX19, BPMSX21 -> RFBSX21
!  * add PH31, PH32, PH31b, PH32b post-undulator phase measurement cavities
! ------------------------------------------------------------------------------
! 13-OCT-2014, Y. Nosochkov
!  * remove RFBUE3, RFBUE3B in the dumplines
!  * replace XCUE1 -> YCUE1, YCUE2 -> XCUE2, XCUE1b -> YCUE1b, YCUE2b -> XCUE2b
!  * add marker for dumpline wall
! 10-OCT-2014, Y. Nosochkov
!  * update TYPE of (6) RFBPMs to indicate the non-baseline level "0"
! 07-AUG-2014, M. Woodley
!  * decorate device TYPE attributes to indicate non-baseline status
! 31-JUL-2014, Y. Nosochkov
!  * remove IMBCS3, IMBCS3b
! 23-JUL-2014, Y. Nosochkov
!  * remove SXR extension cell SXCEL15, use this space (4.4 m) for moving
!    TDUNDb 2.8 m downstream,
!  * replace the quads QSXh15 and QSXh22 with QSXh16 and QSXh21
! 17-JUN-2014, Y. Nosochkov
!  * replace all stripline BPMs downstream of the undulators with RFBPMs
!    (per J. Frisch)
!  * change type of IMUNDO, IMUNDOB from toroid to beam current monitor
!    (per J. Frisch)
! 21-MAY-2014, Y. Nosochkov
!  * move marker points ENDSXR, BEGDMPS and ENDHXR, BEGDMPH to the downstream
!    end of RFBSX51 and RFBHX51 in order to include these RFBPMs into the
!    SXR and HXR areas as defined in PRD LCLSII-2.1-PR-0134
!    (per request from D. Hanquist)
! 02-MAY-2014, M. Woodley
!  * add MTCX01 and MTCX01b MARKERs (for ELEGANT WATCH points)
!  * explicitly define R55=R66=1 in MATRs (for translation to BMAD)
! 24-APR-2014, Y. Nosochkov
!  * include place holder DBKXDMPS(H) for dumpline x-kicker (per J. Frisch)
! 22-APR-2014, Y. Nosochkov
!  * restore T-cavities TCX01B, TCX02B in SXR dumpline (per J. Frisch)
! 07-APR-2014, M. Woodley
!  * move undulator phase shifter definitions earlier in this file to avoid
!    using LPSSH and LPSHX parameters before they are defined
! ------------------------------------------------------------------------------
! 31-MAR-2014, M. Woodley
!  * element names changed to conform to nomenclature PRD
! 26-MAR-2014, Y. Nosochkov
!  * put phase shifter and RFBPM positions back to 03/14 version
! 21-MAR-2014, Y. Nosochkov
!  * update positions of phase shifters and RFBPMs in undulator cells
!  * add Y-corrector to each undulator
!  * update phase shifter parameters (per H-D. Nuhn)
! 07-MAR-2014, Y. Nosochkov
!  * remove transverse deflecting cavities from SXR dumpline
!  * slightly adjust Z-position of dumpline bends
! 06-MAR-2014, Y. Nosochkov
!  * minor matching update
! ------------------------------------------------------------------------------
! 28-FEB-2014, Y. Nosochkov
!  * reduce undulator break length from 1.15 m to 1.0 m for cell length of 4.4 m
!    use cell numbering from 15 to 50 (this includes cells with and without
!    undulators)
!    put SXR undulators in cells 26-32, 34-47
!    put HXR undulators in cells 17-23, 25-31, 33-50
!    the SXR cell-33 and HXR cells 24 and 32 have only quad and RFBPM --
!    these cells will be used for self-seeding
!    reduce length of undulator quadrupole to 0.084 m
!    add one extra RFBPM about ~1 m after end of cell-50 in SXR and HXR
!    add two RFBPMs in two cells upstream of SXR and HXR undulators
!    use the last undulator quadrupole in cell-50 for matching to dump line
! 17-JAN-2014, Y. Nosochkov
!  * minor matching update
! 18-DEC-2013, Y. Nosochkov
!  * minor matching update
! 16-DEC-2013, Y. Nosochkov
!  * minor matching update
! 23-OCT-2013, Y. Nosochkov
!  * change the number of SXR undulator cells to 22 and the number of
!    SXR extension cells to 12
!  * change the number of HXR undulator cells to 34
!  * change undulator cell length to 4.55 m
!  * update undulator parameters and undulator quad field (per H.-D. Nuhn)
!  * new position of SXRTERM, HXRTERM is at Z'=669.586491 m in BSY coordinates
!  * move QUE1, QUE1B 4.0 m downstream
!  * adjust strengths in the last two undulator quads to help with
!    beta match in the dumpline
! 17-OCT-2013, Y. Nosochkov
!  * change quadrupole type of QUE1B, QUE2B, QDMP1B, QDMP2B to 3.94Q17
! ------------------------------------------------------------------------------

 !Eu   :         !definition moved to LCLS2sc_master.xsif and LCLS2cu_master.xsif
  gamu := Eu/mc2 !Lorentz energy factor in undulator [ ]

! ==============================================================================
! SXR undulator (north)
! ==============================================================================

! ------------------------------------------------------------------------------
! SXR undulator PPM phase shifter
! - Author: Heinz-Dieter Nuhn, Stanford Linear Accelerator Center
! - Last edited September 06, 2017
! - 10.0 mm miminum Undulator Gap
! ------------------------------------------------------------------------------
! - LPSSX  = SXR Phase Shifter length
! - luPSSX = SXR Phase Shifter period
! - PIPSSX = SXR Phase Shifter phase integral
! - KPSSX  = SXR Phase Shifter Undulator parameter (rms); range 1.34-3.59
! - kQPSSX = natural SXR Phase Shifter undulator focusing "k" in y-plane
! ------------------------------------------------------------------------------

  LPSSX  := 0.0825                             !m
  luPSSX := 0.075                              !m
  LPSSXh := LPSSX/2
  PIPSSX := 3814                               !T^2mm^3 (180-3814)
  KPSSX  := 1.E-9*CLIGHT/mc2*SQRT(2.E-9*PIPSSX/luPSSX)
  kQPSSX := (KPSSX*2*pi/luPSSX/sqrt(2)/gamu)^2 !m^-2

  PSSXh : MATR, TYPE="phase shifter", L=LPSSXh,       &
    RM(1,1) = 1.0,                                    &
    RM(1,2) = LPSSXh,                                 &
    RM(2,1) = 0.0,                                    &
    RM(2,2) = 1.0,                                    &
    RM(3,3) =  cos(LPSSXh*sqrt(kQPSSX)),              &
    RM(3,4) =  sin(LPSSXh*sqrt(kQPSSX))/sqrt(kQPSSX), &
    RM(4,3) = -sin(LPSSXh*sqrt(kQPSSX))*sqrt(kQPSSX), &
    RM(4,4) =  cos(LPSSXh*sqrt(kQPSSX)),              &
    RM(5,5) = 1.0,                                    &
    RM(6,6) = 1.0

  PSSXh17 : PSSXh
  PSSXh18 : PSSXh
  PSSXh19 : PSSXh
  PSSXh20 : PSSXh
  PSSXh21 : PSSXh
  PSSXh22 : PSSXh
  PSSXh23 : PSSXh
  PSSXh24 : PSSXh
  PSSXh25 : PSSXh
  PSSXh26 : PSSXh
  PSSXh27 : PSSXh
  PSSXh28 : PSSXh
  PSSXh29 : PSSXh
  PSSXh30 : PSSXh
  PSSXh31 : PSSXh
  PSSXh32 : PSSXh
  PSSXh33 : PSSXh
  PSSXh34 : PSSXh

  PSSXh36 : PSSXh
  PSSXh37 : PSSXh
  PSSXh38 : PSSXh
  PSSXh39 : PSSXh
  PSSXh40 : PSSXh
  PSSXh41 : PSSXh
  PSSXh42 : PSSXh
  PSSXh43 : PSSXh
  PSSXh44 : PSSXh
  PSSXh45 : PSSXh
  PSSXh46 : PSSXh

! ------------------------------------------------------------------------------
! - Author: Heinz-Dieter Nuhn, Stanford Linear Accelerator Center
! - Last edited June 22, 2020 (LCLS-II-HE)
! - 7.2 mm miminum Undulator Gap; only constant break length each
! - include natural vertical focusing over all but edge terminations
! ------------------------------------------------------------------------------
! - IntgSX  = integrated quadrupole gradient
! - GQFSX   = QF and QD gradients can be made different to compensate for
!             undulator focussing ...
! - GQDSX   = ... but since undulator focussing depends on gamma^2, compensation
!             will only work for one energy
! - kQFSX   = QF undulator quadrupole focusing "k"
! - kQDSX   = QD undulator quadrupole focusing "k"
! - LDUSEGS = SXR Undulator segment length
! - luSXU   = SXR Undulator period
! - NpSXU   = SXR Undulator period count
! - LSXUCR  = SXR Undulator magnetic length
! - LSXUe   = SXR Undulator spacing between magnet array and strongback end
! - KSXU    = SXR Undulator parameter (rms); range 2-5.5
! - kQSX    = natural SXR undulator focusing "k" in y-plane
! ------------------------------------------------------------------------------

  LDUSEGS := 3.4
  LDU1s   := 0.1
  LDU2s   := 0.03
  LDU3s   := 0.25-LPSSXh -0.04 -0.005
  LDU4s   := (0.25-LPSSXh-0.5*LQu)/2 +0.04 +0.005
  LDU5s   := (0.25-LPSSXh-0.5*LQu)/2
  LDU6s   := 0.12-LQu/2-LRFBub/2 +0.01
  LDU7s   := 0.25-LRFBub/2 -0.01
 dLDU0s   := 0.0 !fine tune Z in undulator
  LDU0s   := 2.1359-LDU6s-LDU7s-LQu +dLDU0s

  DU0s : DRIF, L=LDU0s
  DU1s : DRIF, L=LDU1s
  DU2s : DRIF, L=LDU2s
  DU3s : DRIF, L=LDU3s
  DU4s : DRIF, L=LDU4s
  DU5s : DRIF, L=LDU5s
  DU6s : DRIF, L=LDU6s
  DU7s : DRIF, L=LDU7s

  DUE1ab : DRIF, L=0.9-LRFBub -0.057
  DUE1e  : DRIF, L=0.057

 !IntgSX :   definition moved to LCLS2sc_main.xsif and LCLS2cu_main.xsif
  GQFSX  :=  IntgSX/LQu/10*1.0           !T/m
  GQDSX  := -IntgSX/LQu/10*1.0           !T/m
  kQFSX  :=  1.E-9*GQFSX*CLIGHT/gamu/mc2 !m^-2
  kQDSX  :=  1.E-9*GQDSX*CLIGHT/gamu/mc2 !m^-2
  kQDSXm := -1.843482712046 !UNDS_BDES_DS_fit.xlsx (Eu=8.0, KSXU=3.5)

  QSXh16 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu !partial cell
  QSXh17 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh18 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh19 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh20 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh21 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh22 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh23 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh24 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh25 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh26 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh27 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh28 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh29 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh30 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh31 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh32 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh33 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh34 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh35 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh36 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh37 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh38 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh39 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh40 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh41 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh42 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh43 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh44 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh45 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSX, APER=rQu
  QSXh46 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFSX, APER=rQu
  QSXh47 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDSXm, APER=rQu

  luSXU  := 0.039                            !m
  NpSXU  := 87
  LSXUCR := luSXU*NpSXU                      !m
  LSXUe  := (LDUSEGS-LSXUCR)/2               !m
  LSXUh  := LSXUCR/2
  KSXU   := 5.0
  kQSX   := (KSXU*2*pi/luSXU/sqrt(2)/gamu)^2 !m^-2

  UMASXh : MATR, TYPE="VGHPU", L=LSXUh,         &
    RM(1,1) = 1.0,                               &
    RM(1,2) = LSXUh,                             &
    RM(2,1) = 0.0,                               &
    RM(2,2) = 1.0,                               &
    RM(3,3) =  cos(LSXUh*sqrt(kQSX)),            &
    RM(3,4) =  sin(LSXUh*sqrt(kQSX))/sqrt(kQSX), &
    RM(4,3) = -sin(LSXUh*sqrt(kQSX))*sqrt(kQSX), &
    RM(4,4) =  cos(LSXUh*sqrt(kQSX)),            &
    RM(5,5) = 1.0,                               &
    RM(6,6) = 1.0

  UMASXh17 : UMASXh
  UMASXh18 : UMASXh
  UMASXh19 : UMASXh
  UMASXh20 : UMASXh
  UMASXh21 : UMASXh
  UMASXh22 : UMASXh
  UMASXh23 : UMASXh
  UMASXh24 : UMASXh
  UMASXh25 : UMASXh
  UMASXh26 : UMASXh
  UMASXh27 : UMASXh
  UMASXh28 : UMASXh
  UMASXh29 : UMASXh
  UMASXh30 : UMASXh
  UMASXh31 : UMASXh
  UMASXh32 : UMASXh
  UMASXh33 : UMASXh
  UMASXh34 : UMASXh

  UMASXh36 : UMASXh
  UMASXh37 : UMASXh
  UMASXh38 : UMASXh
  UMASXh39 : UMASXh
  UMASXh40 : UMASXh
  UMASXh41 : UMASXh
  UMASXh42 : UMASXh
  UMASXh43 : UMASXh
  UMASXh44 : UMASXh
  UMASXh45 : UMASXh
  UMASXh46 : UMASXh
  UMASXh47 : UMASXh

! ------------------------------------------------------------------------------
! SXR undulator X-steering coils in quads
! ------------------------------------------------------------------------------

  XCSX16 : HKIC, TYPE="type-U" !partial cell
  XCSX17 : HKIC, TYPE="type-U"
  XCSX18 : HKIC, TYPE="type-U"
  XCSX19 : HKIC, TYPE="type-U"
  XCSX20 : HKIC, TYPE="type-U"
  XCSX21 : HKIC, TYPE="type-U"
  XCSX22 : HKIC, TYPE="type-U"
  XCSX23 : HKIC, TYPE="type-U"
  XCSX24 : HKIC, TYPE="type-U"
  XCSX25 : HKIC, TYPE="type-U"
  XCSX26 : HKIC, TYPE="type-U"
  XCSX27 : HKIC, TYPE="type-U"
  XCSX28 : HKIC, TYPE="type-U"
  XCSX29 : HKIC, TYPE="type-U"
  XCSX30 : HKIC, TYPE="type-U"
  XCSX31 : HKIC, TYPE="type-U"
  XCSX32 : HKIC, TYPE="type-U"
  XCSX33 : HKIC, TYPE="type-U"
  XCSX34 : HKIC, TYPE="type-U"
  XCSX35 : HKIC, TYPE="type-U"
  XCSX36 : HKIC, TYPE="type-U"
  XCSX37 : HKIC, TYPE="type-U"
  XCSX38 : HKIC, TYPE="type-U"
  XCSX39 : HKIC, TYPE="type-U"
  XCSX40 : HKIC, TYPE="type-U"
  XCSX41 : HKIC, TYPE="type-U"
  XCSX42 : HKIC, TYPE="type-U"
  XCSX43 : HKIC, TYPE="type-U"
  XCSX44 : HKIC, TYPE="type-U"
  XCSX45 : HKIC, TYPE="type-U"
  XCSX46 : HKIC, TYPE="type-U"
  XCSX47 : HKIC, TYPE="type-U"

! ------------------------------------------------------------------------------
! SXR undulator Y-steering coils in quads
! ------------------------------------------------------------------------------

  YCSX16 : VKIC, TYPE="type-U" !partial cell
  YCSX17 : VKIC, TYPE="type-U"
  YCSX18 : VKIC, TYPE="type-U"
  YCSX19 : VKIC, TYPE="type-U"
  YCSX20 : VKIC, TYPE="type-U"
  YCSX21 : VKIC, TYPE="type-U"
  YCSX22 : VKIC, TYPE="type-U"
  YCSX23 : VKIC, TYPE="type-U"
  YCSX24 : VKIC, TYPE="type-U"
  YCSX25 : VKIC, TYPE="type-U"
  YCSX26 : VKIC, TYPE="type-U"
  YCSX27 : VKIC, TYPE="type-U"
  YCSX28 : VKIC, TYPE="type-U"
  YCSX29 : VKIC, TYPE="type-U"
  YCSX30 : VKIC, TYPE="type-U"
  YCSX31 : VKIC, TYPE="type-U"
  YCSX32 : VKIC, TYPE="type-U"
  YCSX33 : VKIC, TYPE="type-U"
  YCSX34 : VKIC, TYPE="type-U"
  YCSX35 : VKIC, TYPE="type-U"
  YCSX36 : VKIC, TYPE="type-U"
  YCSX37 : VKIC, TYPE="type-U"
  YCSX38 : VKIC, TYPE="type-U"
  YCSX39 : VKIC, TYPE="type-U"
  YCSX40 : VKIC, TYPE="type-U"
  YCSX41 : VKIC, TYPE="type-U"
  YCSX42 : VKIC, TYPE="type-U"
  YCSX43 : VKIC, TYPE="type-U"
  YCSX44 : VKIC, TYPE="type-U"
  YCSX45 : VKIC, TYPE="type-U"
  YCSX46 : VKIC, TYPE="type-U"
  YCSX47 : VKIC, TYPE="type-U"

! ------------------------------------------------------------------------------
! SXR undulator X-steering coils in undulator segments
! ------------------------------------------------------------------------------

  XCSU17 : INST, TYPE="type-V"
  XCSU18 : INST, TYPE="type-V"
  XCSU19 : INST, TYPE="type-V"
  XCSU20 : INST, TYPE="type-V"
  XCSU21 : INST, TYPE="type-V"
  XCSU22 : INST, TYPE="type-V"
  XCSU23 : INST, TYPE="type-V"
  XCSU24 : INST, TYPE="type-V"
  XCSU25 : INST, TYPE="type-V"
  XCSU26 : INST, TYPE="type-V"
  XCSU27 : INST, TYPE="type-V"
  XCSU28 : INST, TYPE="type-V"
  XCSU29 : INST, TYPE="type-V"
  XCSU30 : INST, TYPE="type-V"
  XCSU31 : INST, TYPE="type-V"
  XCSU32 : INST, TYPE="type-V"
  XCSU33 : INST, TYPE="type-V"
  XCSU34 : INST, TYPE="type-V"

  XCSU36 : INST, TYPE="type-V"
  XCSU37 : INST, TYPE="type-V"
  XCSU38 : INST, TYPE="type-V"
  XCSU39 : INST, TYPE="type-V"
  XCSU40 : INST, TYPE="type-V"
  XCSU41 : INST, TYPE="type-V"
  XCSU42 : INST, TYPE="type-V"
  XCSU43 : INST, TYPE="type-V"
  XCSU44 : INST, TYPE="type-V"
  XCSU45 : INST, TYPE="type-V"
  XCSU46 : INST, TYPE="type-V"
  XCSU47 : INST, TYPE="type-V"

! ------------------------------------------------------------------------------
! SXR undulator Y-steering coils in undulator segments
! ------------------------------------------------------------------------------

  YCSU17 : INST, TYPE="type-V"
  YCSU18 : INST, TYPE="type-V"
  YCSU19 : INST, TYPE="type-V"
  YCSU20 : INST, TYPE="type-V"
  YCSU21 : INST, TYPE="type-V"
  YCSU22 : INST, TYPE="type-V"
  YCSU23 : INST, TYPE="type-V"
  YCSU24 : INST, TYPE="type-V"
  YCSU25 : INST, TYPE="type-V"
  YCSU26 : INST, TYPE="type-V"
  YCSU27 : INST, TYPE="type-V"
  YCSU28 : INST, TYPE="type-V"
  YCSU29 : INST, TYPE="type-V"
  YCSU30 : INST, TYPE="type-V"
  YCSU31 : INST, TYPE="type-V"
  YCSU32 : INST, TYPE="type-V"
  YCSU33 : INST, TYPE="type-V"
  YCSU34 : INST, TYPE="type-V"

  YCSU36 : INST, TYPE="type-V"
  YCSU37 : INST, TYPE="type-V"
  YCSU38 : INST, TYPE="type-V"
  YCSU39 : INST, TYPE="type-V"
  YCSU40 : INST, TYPE="type-V"
  YCSU41 : INST, TYPE="type-V"
  YCSU42 : INST, TYPE="type-V"
  YCSU43 : INST, TYPE="type-V"
  YCSU44 : INST, TYPE="type-V"
  YCSU45 : INST, TYPE="type-V"
  YCSU46 : INST, TYPE="type-V"
  YCSU47 : INST, TYPE="type-V"

! ------------------------------------------------------------------------------
! SXR undulator BPMs
! ------------------------------------------------------------------------------

  RFBSX16 : MONI, L=LRFBub, TYPE="CavityX-1" !partial cell
  RFBSX17 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX18 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX19 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX20 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX21 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX22 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX23 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX24 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX25 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX26 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX27 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX28 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX29 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX30 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX31 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX32 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX33 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX34 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX35 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX36 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX37 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX38 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX39 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX40 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX41 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX42 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX43 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX44 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX45 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX46 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBSX47 : MONI, L=LRFBub, TYPE="CavityX-1"
  DRFBS48 : DRIF, L=LRFBub
  DRFBS49 : DRIF, L=LRFBub
  DRFBS50 : DRIF, L=LRFBub
  RFBSX51 : MONI, L=LRFBub, TYPE="CavityX-1"

! ------------------------------------------------------------------------------
! DTSXU  = SXU undulator segment small terminations modeled as drift
! ------------------------------------------------------------------------------

  DTSXU : DRIF, L=LSXUe

! ------------------------------------------------------------------------------
! markers
! ------------------------------------------------------------------------------

  SXRSTART : MARK !start of undulator system
  RWWAKE5S : MARK, TYPE="WAKE" !SXR beampipe wake applied here
  SXRTERM  : MARK !~end of undulator system
  MUQS     : MARK
  MPHS     : MARK

! ------------------------------------------------------------------------------
! SXR undulator phase shifters
! ------------------------------------------------------------------------------

  PSSXh17_full : LINE=(PSSXh17,MPHS,PSSXh17)
  PSSXh18_full : LINE=(PSSXh18,MPHS,PSSXh18)
  PSSXh19_full : LINE=(PSSXh19,MPHS,PSSXh19)
  PSSXh20_full : LINE=(PSSXh20,MPHS,PSSXh20)
  PSSXh21_full : LINE=(PSSXh21,MPHS,PSSXh21)
  PSSXh22_full : LINE=(PSSXh22,MPHS,PSSXh22)
  PSSXh23_full : LINE=(PSSXh23,MPHS,PSSXh23)
  PSSXh24_full : LINE=(PSSXh24,MPHS,PSSXh24)
  PSSXh25_full : LINE=(PSSXh25,MPHS,PSSXh25)
  PSSXh26_full : LINE=(PSSXh26,MPHS,PSSXh26)
  PSSXh27_full : LINE=(PSSXh27,MPHS,PSSXh27)
  PSSXh28_full : LINE=(PSSXh28,MPHS,PSSXh28)
  PSSXh29_full : LINE=(PSSXh29,MPHS,PSSXh29)
  PSSXh30_full : LINE=(PSSXh30,MPHS,PSSXh30)
  PSSXh31_full : LINE=(PSSXh31,MPHS,PSSXh31)
  PSSXh32_full : LINE=(PSSXh32,MPHS,PSSXh32)
  PSSXh33_full : LINE=(PSSXh33,MPHS,PSSXh33)
  PSSXh34_full : LINE=(PSSXh34,MPHS,PSSXh34)
  DPSSX35      : DRIF, L=LPSSX
  PSSXh36_full : LINE=(PSSXh36,MPHS,PSSXh36)
  PSSXh37_full : LINE=(PSSXh37,MPHS,PSSXh37)
  PSSXh38_full : LINE=(PSSXh38,MPHS,PSSXh38)
  PSSXh39_full : LINE=(PSSXh39,MPHS,PSSXh39)
  PSSXh40_full : LINE=(PSSXh40,MPHS,PSSXh40)
  PSSXh41_full : LINE=(PSSXh41,MPHS,PSSXh41)
  PSSXh42_full : LINE=(PSSXh42,MPHS,PSSXh42)
  PSSXh43_full : LINE=(PSSXh43,MPHS,PSSXh43)
  PSSXh44_full : LINE=(PSSXh44,MPHS,PSSXh44)
  PSSXh45_full : LINE=(PSSXh45,MPHS,PSSXh45)
  PSSXh46_full : LINE=(PSSXh46,MPHS,PSSXh46)
  DPSSX47      : DRIF, L=LPSSX
  DPSSX48      : DRIF, L=LPSSX
  DPSSX49      : DRIF, L=LPSSX
  DPSSX50      : DRIF, L=LPSSX

! ------------------------------------------------------------------------------
! SXR undulator inline valves, VAT Series 48
! ------------------------------------------------------------------------------

  VVSXU20 : MARK
  VVSXU26 : MARK
  VVSXU32 : MARK
  VVSXU38 : MARK
  VVSXU44 : MARK
  VVSXU47 : MARK

! ------------------------------------------------------------------------------
! SXR undulator Beam Loss Monitors
! ------------------------------------------------------------------------------

  BLMS17 : MARK
  BLMS18 : MARK
  BLMS19 : MARK
  BLMS20 : MARK
  BLMS21 : MARK
  BLMS22 : MARK
  BLMS23 : MARK
  BLMS24 : MARK
  BLMS25 : MARK
  BLMS26 : MARK
  BLMS27 : MARK
  BLMS28 : MARK
  BLMS29 : MARK
  BLMS30 : MARK
  BLMS31 : MARK
  BLMS32 : MARK
  BLMS33 : MARK
  BLMS34 : MARK

  BLMS36 : MARK
  BLMS37 : MARK
  BLMS38 : MARK
  BLMS39 : MARK
  BLMS40 : MARK
  BLMS41 : MARK
  BLMS42 : MARK
  BLMS43 : MARK
  BLMS44 : MARK
  BLMS45 : MARK
  BLMS46 : MARK
  BLMS47 : MARK

! ------------------------------------------------------------------------------
! SXRSS self-seeding chicane (LCLSII-3.2-PR-0101-R0 doesn't specify chicane
! requirements ... use LCLS PRD SLAC-I-081-101-003-00-R000)
! NOTE: chicane slides across the aisle from LCLS to SXR; now bends toward the
!       (north) wall, away from the aisle
! ------------------------------------------------------------------------------

! NOTEs:
! - in LCLS: BXSS1-4
! - nominal beam energy range is 3.35-4.74 GeV
! - minimum energy is 2.6 GeV; maximum energy is 5.2 GeV
! - Bmax = 2.56 kG-m @ 9 A (20 mm maximum beam offset)
! - use series approximation for sinc(x)=sin(x)/x to allow AB5=0
! - deflects toward +X (to the left/north/wall)
! - bends toward coil side
! - Dieter Walz designation: 1.575D14-C (pole-width rather than gap height)

! BCXSS1 gets an X-offset of +1.0 mm (toward the wall)
! BCXSS2 gets an X-offset of +9.7 mm (toward the wall)
! BCXSS3 gets an X-offset of +9.7 mm (toward the wall)
! BCXSS4 gets an X-offset of +1.0 mm (toward the wall)

  Brho5    := Cb*Eu                             !beam rigidity at chicane (kG-m)
  GB5      := 0.315*in2m                        !full gap height [m]
  ZB5      := 14.315*in2m                       !on-axis effective length is 14" + gap (rule-of-thumb)
  FB5      := 0.5                               !not measured
  AB5      := -0.014249046471*SETSXRSS          !bend angle (rad) for dX=+17.0 mm
  BLB5     := Brho5*AB5                         !integrated strength (-1.901188 kG-m @ 4 GeV)
  AB5_2    := AB5*AB5
  AB5_4    := AB5_2*AB5_2
  AB5_6    := AB5_4*AB5_2
  SINCAB5  := 1-AB5_2/6+AB5_4/120-AB5_6/5040    !~sinc(AB5)=sin(AB5)/AB5
  LB5      := ZB5/SINCAB5                       !chicane bend path length (m)
  AB5s     := ASIN(SIN(AB5)/2)                  !"short" half chicane bend angle (rad)
  AB5s_2   := AB5s*AB5s
  AB5s_4   := AB5s_2*AB5s_2
  AB5s_6   := AB5s_4*AB5s_2
  SINCAB5s := 1-AB5s_2/6+AB5s_4/120-AB5s_6/5040 !~sinc(AB5s)=sin(AB5s)/AB5s
  LB5s     := ZB5/(2*SINCAB5s)                  !"short" half chicane bend path length (m)
  AB5L     := AB5-AB5S                          !"long" half chicane bend angle (rad)
  LB5L     := LB5-LB5S                          !"long" half chicane bend path length (m)

  BCXSS1a : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5S, ANGLE=+AB5S, &
                  E1=0, FINT=0.5, FINTX=0
  BCXSS1b : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5L, ANGLE=+AB5L, &
                  E2=+AB5, FINT=0, FINTX=0.5
  BCXSS2a : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5L, ANGLE=-AB5L, &
                  E1=-AB5, FINT=0.5, FINTX=0
  BCXSS2b : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5S, ANGLE=-AB5S, &
                  E2=0, FINT=0, FINTX=0.5
  BCXSS3a : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5S, ANGLE=-AB5S, &
                  E1=0, FINT=0.5, FINTX=0
  BCXSS3b : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5L, ANGLE=-AB5L, &
                  E2=-AB5, FINT=0, FINTX=0.5
  BCXSS4a : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5L, ANGLE=+AB5L, &
                  E1=+AB5, FINT=0.5, FINTX=0
  BCXSS4b : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5S, ANGLE=+AB5S, &
                  E2=0, FINT=0, FINTX=0.5

! define unsplit SBENs for BMAD ... not used by MAD

  BCXSS1 : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5, ANGLE=+AB5, &
                 E1=0, FINT=0.5, E2=+AB5, FINTX=0.5
  BCXSS2 : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5, ANGLE=-AB5, &
                 E1=-AB5, FINT=0.5, E2=0, FINTX=0.5
  BCXSS3 : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5, ANGLE=-AB5, &
                 E1=0, FINT=0.5, E2=-AB5, FINTX=0.5
  BCXSS4 : SBEN, TYPE="0.315D14-C", HGAP=GB5/2, L=LB5, ANGLE=+AB5, &
                 E1=+AB5, FINT=0.5, E2=0, FINTX=0.5

  LSssB2Bo := 1.193 !outer bend center-to-center (PRD Table 3)
  LSssB2Bi := 0.52  !inner bend center-to-center (PRD Table 3)

  ZD1S  := LSssB2Bo-ZB5
  ZD1Sa := 0.44
  ZD1Sb := 0.049
  ZD1Sc := ZD1S-(ZD1Sa+ZD1Sb)
  ZD1Sd := 0.077
  ZD1Se := 0.185
  ZD1Sf := 0.128
  ZD1Sg := ZD1S-(ZD1Sd+ZD1Se+ZD1Sf)

  DMONOS : DRIF, L=(3.4-2*LSssB2Bo-LSssB2Bi-ZB5)/2
  D1Sa   : DRIF, L=ZD1Sa/COS(AB5)
  D1Sb   : DRIF, L=ZD1Sb/COS(AB5)
  D1Sc   : DRIF, L=ZD1Sc/COS(AB5)
  D1Sd   : DRIF, L=ZD1Sd/COS(AB5)
  D1Se   : DRIF, L=ZD1Se/COS(AB5)
  D1Sf   : DRIF, L=ZD1Sf/COS(AB5)
  D1Sg   : DRIF, L=ZD1Sg/COS(AB5)
  DCHS   : DRIF, L=(LSssB2Bi-ZB5)/2

  SXRSSBEG : MARK
  GSXS1    : INST, TYPE="grating"
  MSXS1    : INST, TYPE="mirror"
  CNTRS    : MARK
  SLSXS1   : INST, TYPE="slit"
  MSXS2    : INST, TYPE="mirror"
  MSXS3    : INST, TYPE="mirror"
  SXRSSEND : MARK

  BCXSS1_full : LINE=(BCXSS1a,BCXSS1b)
  BCXSS2_full : LINE=(BCXSS2a,BCXSS2b)
  BCXSS3_full : LINE=(BCXSS3a,BCXSS3b)
  BCXSS4_full : LINE=(BCXSS4a,BCXSS4b)

  SCHICANE : LINE=(SXRSSBEG,DMONOS,&
    BCXSS1_full,D1Sa,GSXS1,D1Sb,MSXS1,D1Sc,&
    BCXSS2_full,DCHS,CNTRS,DCHS,&
    BCXSS3_full,D1Sd,SLSXS1,D1Se,MSXS2,D1Sf,MSXS3,D1Sg,&
    BCXSS4_full,DMONOS,SXRSSEND)

! ------------------------------------------------------------------------------
! SXR undulator cells
! ------------------------------------------------------------------------------

  UMASXh17_full : LINE=(UMASXh17,XCSU17,YCSU17,UMASXh17)
  UMASXh18_full : LINE=(UMASXh18,XCSU18,YCSU18,UMASXh18)
  UMASXh19_full : LINE=(UMASXh19,XCSU19,YCSU19,UMASXh19)
  UMASXh20_full : LINE=(UMASXh20,XCSU20,YCSU20,UMASXh20)
  UMASXh21_full : LINE=(UMASXh21,XCSU21,YCSU21,UMASXh21)
  UMASXh22_full : LINE=(UMASXh22,XCSU22,YCSU22,UMASXh22)
  UMASXh23_full : LINE=(UMASXh23,XCSU23,YCSU23,UMASXh23)
  UMASXh24_full : LINE=(UMASXh24,XCSU24,YCSU24,UMASXh24)
  UMASXh25_full : LINE=(UMASXh25,XCSU25,YCSU25,UMASXh25)
  UMASXh26_full : LINE=(UMASXh26,XCSU26,YCSU26,UMASXh26)
  UMASXh27_full : LINE=(UMASXh27,XCSU27,YCSU27,UMASXh27)
  UMASXh28_full : LINE=(UMASXh28,XCSU28,YCSU28,UMASXh28)
  UMASXh29_full : LINE=(UMASXh29,XCSU29,YCSU29,UMASXh29)
  UMASXh30_full : LINE=(UMASXh30,XCSU30,YCSU30,UMASXh30)
  UMASXh31_full : LINE=(UMASXh31,XCSU31,YCSU31,UMASXh31)
  UMASXh32_full : LINE=(UMASXh32,XCSU32,YCSU32,UMASXh32)
  UMASXh33_full : LINE=(UMASXh33,XCSU33,YCSU33,UMASXh33)
  UMASXh34_full : LINE=(UMASXh34,XCSU34,YCSU34,UMASXh34)

  UMASXh36_full : LINE=(UMASXh36,XCSU36,YCSU36,UMASXh36)
  UMASXh37_full : LINE=(UMASXh37,XCSU37,YCSU37,UMASXh37)
  UMASXh38_full : LINE=(UMASXh38,XCSU38,YCSU38,UMASXh38)
  UMASXh39_full : LINE=(UMASXh39,XCSU39,YCSU39,UMASXh39)
  UMASXh40_full : LINE=(UMASXh40,XCSU40,YCSU40,UMASXh40)
  UMASXh41_full : LINE=(UMASXh41,XCSU41,YCSU41,UMASXh41)
  UMASXh42_full : LINE=(UMASXh42,XCSU42,YCSU42,UMASXh42)
  UMASXh43_full : LINE=(UMASXh43,XCSU43,YCSU43,UMASXh43)
  UMASXh44_full : LINE=(UMASXh44,XCSU44,YCSU44,UMASXh44)
  UMASXh45_full : LINE=(UMASXh45,XCSU45,YCSU45,UMASXh45)
  UMASXh46_full : LINE=(UMASXh46,XCSU46,YCSU46,UMASXh46)
  UMASXh47_full : LINE=(UMASXh47,XCSU47,YCSU47,UMASXh47)

  USEGSX17 : LINE=(DTSXU,UMASXh17_full,DTSXU)
  USEGSX18 : LINE=(DTSXU,UMASXh18_full,DTSXU)
  USEGSX19 : LINE=(DTSXU,UMASXh19_full,DTSXU)
  USEGSX20 : LINE=(DTSXU,UMASXh20_full,DTSXU)
  USEGSX21 : LINE=(DTSXU,UMASXh21_full,DTSXU)
  USEGSX22 : LINE=(DTSXU,UMASXh22_full,DTSXU)
  USEGSX23 : LINE=(DTSXU,UMASXh23_full,DTSXU)
  USEGSX24 : LINE=(DTSXU,UMASXh24_full,DTSXU)
  USEGSX25 : LINE=(DTSXU,UMASXh25_full,DTSXU)
  USEGSX26 : LINE=(DTSXU,UMASXh26_full,DTSXU)
  USEGSX27 : LINE=(DTSXU,UMASXh27_full,DTSXU)
  USEGSX28 : LINE=(DTSXU,UMASXh28_full,DTSXU)
  USEGSX29 : LINE=(DTSXU,UMASXh29_full,DTSXU)
  USEGSX30 : LINE=(DTSXU,UMASXh30_full,DTSXU)
  USEGSX31 : LINE=(DTSXU,UMASXh31_full,DTSXU)
  USEGSX32 : LINE=(DTSXU,UMASXh32_full,DTSXU)
  USEGSX33 : LINE=(DTSXU,UMASXh33_full,DTSXU)
  USEGSX34 : LINE=(DTSXU,UMASXh34_full,DTSXU)
  DUSEGS35 : LINE=(SCHICANE) !DRIF, L=LDUSEGS
  USEGSX36 : LINE=(DTSXU,UMASXh36_full,DTSXU)
  USEGSX37 : LINE=(DTSXU,UMASXh37_full,DTSXU)
  USEGSX38 : LINE=(DTSXU,UMASXh38_full,DTSXU)
  USEGSX39 : LINE=(DTSXU,UMASXh39_full,DTSXU)
  USEGSX40 : LINE=(DTSXU,UMASXh40_full,DTSXU)
  USEGSX41 : LINE=(DTSXU,UMASXh41_full,DTSXU)
  USEGSX42 : LINE=(DTSXU,UMASXh42_full,DTSXU)
  USEGSX43 : LINE=(DTSXU,UMASXh43_full,DTSXU)
  USEGSX44 : LINE=(DTSXU,UMASXh44_full,DTSXU)
  USEGSX45 : LINE=(DTSXU,UMASXh45_full,DTSXU)
  USEGSX46 : LINE=(DTSXU,UMASXh46_full,DTSXU)
  USEGSX47 : LINE=(DTSXU,UMASXh47_full,DTSXU)
  DUSEGS48 : DRIF, L=LDUSEGS
  DUSEGS49 : DRIF, L=LDUSEGS
  DUSEGS50 : DRIF, L=LDUSEGS

  QSXh16_full : LINE=(QSXh16,XCSX16,MUQS,YCSX16,QSXh16) !partial cell
  QSXh17_full : LINE=(QSXh17,XCSX17,MUQS,YCSX17,QSXh17)
  QSXh18_full : LINE=(QSXh18,XCSX18,MUQS,YCSX18,QSXh18)
  QSXh19_full : LINE=(QSXh19,XCSX19,MUQS,YCSX19,QSXh19)
  QSXh20_full : LINE=(QSXh20,XCSX20,MUQS,YCSX20,QSXh20)
  QSXh21_full : LINE=(QSXh21,XCSX21,MUQS,YCSX21,QSXh21)
  QSXh22_full : LINE=(QSXh22,XCSX22,MUQS,YCSX22,QSXh22)
  QSXh23_full : LINE=(QSXh23,XCSX23,MUQS,YCSX23,QSXh23)
  QSXh24_full : LINE=(QSXh24,XCSX24,MUQS,YCSX24,QSXh24)
  QSXh25_full : LINE=(QSXh25,XCSX25,MUQS,YCSX25,QSXh25)
  QSXh26_full : LINE=(QSXh26,XCSX26,MUQS,YCSX26,QSXh26)
  QSXh27_full : LINE=(QSXh27,XCSX27,MUQS,YCSX27,QSXh27)
  QSXh28_full : LINE=(QSXh28,XCSX28,MUQS,YCSX28,QSXh28)
  QSXh29_full : LINE=(QSXh29,XCSX29,MUQS,YCSX29,QSXh29)
  QSXh30_full : LINE=(QSXh30,XCSX30,MUQS,YCSX30,QSXh30)
  QSXh31_full : LINE=(QSXh31,XCSX31,MUQS,YCSX31,QSXh31)
  QSXh32_full : LINE=(QSXh32,XCSX32,MUQS,YCSX32,QSXh32)
  QSXh33_full : LINE=(QSXh33,XCSX33,MUQS,YCSX33,QSXh33)
  QSXh34_full : LINE=(QSXh34,XCSX34,MUQS,YCSX34,QSXh34)
  QSXh35_full : LINE=(QSXh35,XCSX35,MUQS,YCSX35,QSXh35)
  QSXh36_full : LINE=(QSXh36,XCSX36,MUQS,YCSX36,QSXh36)
  QSXh37_full : LINE=(QSXh37,XCSX37,MUQS,YCSX37,QSXh37)
  QSXh38_full : LINE=(QSXh38,XCSX38,MUQS,YCSX38,QSXh38)
  QSXh39_full : LINE=(QSXh39,XCSX39,MUQS,YCSX39,QSXh39)
  QSXh40_full : LINE=(QSXh40,XCSX40,MUQS,YCSX40,QSXh40)
  QSXh41_full : LINE=(QSXh41,XCSX41,MUQS,YCSX41,QSXh41)
  QSXh42_full : LINE=(QSXh42,XCSX42,MUQS,YCSX42,QSXh42)
  QSXh43_full : LINE=(QSXh43,XCSX43,MUQS,YCSX43,QSXh43)
  QSXh44_full : LINE=(QSXh44,XCSX44,MUQS,YCSX44,QSXh44)
  QSXh45_full : LINE=(QSXh45,XCSX45,MUQS,YCSX45,QSXh45)
  QSXh46_full : LINE=(QSXh46,XCSX46,MUQS,YCSX46,QSXh46)
  QSXh47_full : LINE=(QSXh47,XCSX47,MUQS,YCSX47,QSXh47)
  DQSX48      : DRIF, L=LQu
  DQSX49      : DRIF, L=LQu
  DQSX50      : DRIF, L=LQu

! SXRSS-II BOD's

  DU4sa : DRIF, L=0.117258
  BOD10 : INST, TYPE="Overlap Detector"
  DU4sb : DRIF, L=DU4s[L]-DU4sa[L]
  DU7sa : DRIF, L=0.078508
  BOD12 : INST, TYPE="Overlap Detector"
  DU7sb : DRIF, L=DU7s[L]-DU7sa[L]

  SXBRK17 : LINE=(&
    DU3s,PSSXh17_full,DU4s,        DU5s,QSXh17_full,DU6s,RFBSX17,DU7s)
  SXBRK18 : LINE=(&
    DU3s,PSSXh18_full,DU4s,        DU5s,QSXh18_full,DU6s,RFBSX18,DU7s)
  SXBRK19 : LINE=(&
    DU3s,PSSXh19_full,DU4s,        DU5s,QSXh19_full,DU6s,RFBSX19,DU7s)
  SXBRK20 : LINE=(&
    DU3s,PSSXh20_full,DU4s,VVSXU20,DU5s,QSXh20_full,DU6s,RFBSX20,DU7s)
  SXBRK21 : LINE=(&
    DU3s,PSSXh21_full,DU4s,        DU5s,QSXh21_full,DU6s,RFBSX21,DU7s)
  SXBRK22 : LINE=(&
    DU3s,PSSXh22_full,DU4s,        DU5s,QSXh22_full,DU6s,RFBSX22,DU7s)
  SXBRK23 : LINE=(&
    DU3s,PSSXh23_full,DU4s,        DU5s,QSXh23_full,DU6s,RFBSX23,DU7s)
  SXBRK24 : LINE=(&
    DU3s,PSSXh24_full,DU4s,        DU5s,QSXh24_full,DU6s,RFBSX24,DU7s)
  SXBRK25 : LINE=(&
    DU3s,PSSXh25_full,DU4s,        DU5s,QSXh25_full,DU6s,RFBSX25,DU7s)
  SXBRK26 : LINE=(&
    DU3s,PSSXh26_full,DU4s,VVSXU26,DU5s,QSXh26_full,DU6s,RFBSX26,DU7s)
  SXBRK27 : LINE=(&
    DU3s,PSSXh27_full,DU4s,        DU5s,QSXh27_full,DU6s,RFBSX27,DU7s)
  SXBRK28 : LINE=(&
    DU3s,PSSXh28_full,DU4s,        DU5s,QSXh28_full,DU6s,RFBSX28,DU7s)
  SXBRK29 : LINE=(&
    DU3s,PSSXh29_full,DU4s,        DU5s,QSXh29_full,DU6s,RFBSX29,DU7s)
  SXBRK30 : LINE=(&
    DU3s,PSSXh30_full,DU4s,        DU5s,QSXh30_full,DU6s,RFBSX30,DU7s)
  SXBRK31 : LINE=(&
    DU3s,PSSXh31_full,DU4s,        DU5s,QSXh31_full,DU6s,RFBSX31,DU7s)
  SXBRK32 : LINE=(&
    DU3s,PSSXh32_full,DU4s,VVSXU32,DU5s,QSXh32_full,DU6s,RFBSX32,DU7s)
  SXBRK33 : LINE=(&
    DU3s,PSSXh33_full,DU4s,        DU5s,QSXh33_full,DU6s,RFBSX33,DU7s)
  SXBRK34 : LINE=(&
    DU3s,PSSXh34_full,DU4s,        DU5s,QSXh34_full,DU6s,RFBSX34,DU7s)
  SXBRK35 : LINE=(&
    DU3s,DPSSX35,DU4sa,BOD10,DU4sb,DU5s,QSXh35_full,DU6s,RFBSX35,DU7s)
  SXBRK36 : LINE=(&
    DU3s,PSSXh36_full,DU4s,        DU5s,QSXh36_full,DU6s,RFBSX36,DU7s)
  SXBRK37 : LINE=(&
    DU3s,PSSXh37_full,DU4s,DU5s,QSXh37_full,DU6s,RFBSX37,DU7sa,BOD12,DU7Sb)
  SXBRK38 : LINE=(&
    DU3s,PSSXh38_full,DU4s,VVSXU38,DU5s,QSXh38_full,DU6s,RFBSX38,DU7s)
  SXBRK39 : LINE=(&
    DU3s,PSSXh39_full,DU4s,        DU5s,QSXh39_full,DU6s,RFBSX39,DU7s)
  SXBRK40 : LINE=(&
    DU3s,PSSXh40_full,DU4s,        DU5s,QSXh40_full,DU6s,RFBSX40,DU7s)
  SXBRK41 : LINE=(&
    DU3s,PSSXh41_full,DU4s,        DU5s,QSXh41_full,DU6s,RFBSX41,DU7s)
  SXBRK42 : LINE=(&
    DU3s,PSSXh42_full,DU4s,        DU5s,QSXh42_full,DU6s,RFBSX42,DU7s)
  SXBRK43 : LINE=(&
    DU3s,PSSXh43_full,DU4s,        DU5s,QSXh43_full,DU6s,RFBSX43,DU7s)
  SXBRK44 : LINE=(&
    DU3s,PSSXh44_full,DU4s,VVSXU44,DU5s,QSXh44_full,DU6s,RFBSX44,DU7s)
  SXBRK45 : LINE=(&
    DU3s,PSSXh45_full,DU4s,        DU5s,QSXh45_full,DU6s,RFBSX45,DU7s)
  SXBRK46 : LINE=(&
    DU3s,PSSXh46_full,DU4s,        DU5s,QSXh46_full,DU6s,RFBSX46,DU7s)
  SXBRK47 : LINE=(&
    DU3s,DPSSX47     ,DU4s,VVSXU47,DU5s,QSXh47_full,DU6s,RFBSX47,DU7s)
  SXBRK48 : LINE=(&
    DU3s,DPSSX48     ,DU4s,        DU5s,DQSX48     ,DU6s,DRFBS48,DU7s)
  SXBRK49 : LINE=(&
    DU3s,DPSSX49     ,DU4s,        DU5s,DQSX49     ,DU6s,DRFBS49,DU7s)
  SXBRK50 : LINE=(&
    DU3s,DPSSX50     ,DU4s,        DU5s,DQSX50     ,DU6s,DRFBS50,DU7s)

  SXCEL17  : LINE=(DU1s,BLMS17,DU2s,USEGSX17,SXBRK17)
  SXCEL18  : LINE=(DU1s,BLMS18,DU2s,USEGSX18,SXBRK18)
  SXCEL19  : LINE=(DU1s,BLMS19,DU2s,USEGSX19,SXBRK19)
  SXCEL20  : LINE=(DU1s,BLMS20,DU2s,USEGSX20,SXBRK20)
  SXCEL21  : LINE=(DU1s,BLMS21,DU2s,USEGSX21,SXBRK21)
  SXCEL22  : LINE=(DU1s,BLMS22,DU2s,USEGSX22,SXBRK22)
  SXCEL23  : LINE=(DU1s,BLMS23,DU2s,USEGSX23,SXBRK23)
  SXCEL24  : LINE=(DU1s,BLMS24,DU2s,USEGSX24,SXBRK24)
  SXCEL25  : LINE=(DU1s,BLMS25,DU2s,USEGSX25,SXBRK25)
  SXCEL26  : LINE=(DU1s,BLMS26,DU2s,USEGSX26,SXBRK26)
  SXCEL27  : LINE=(DU1s,BLMS27,DU2s,USEGSX27,SXBRK27)
  SXCEL28  : LINE=(DU1s,BLMS28,DU2s,USEGSX28,SXBRK28)
  SXCEL29  : LINE=(DU1s,BLMS29,DU2s,USEGSX29,SXBRK29)
  SXCEL30  : LINE=(DU1s,BLMS30,DU2s,USEGSX30,SXBRK30)
  SXCEL31  : LINE=(DU1s,BLMS31,DU2s,USEGSX31,SXBRK31)
  SXCEL32  : LINE=(DU1s,BLMS32,DU2s,USEGSX32,SXBRK32)
  SXCEL33  : LINE=(DU1s,BLMS33,DU2s,USEGSX33,SXBRK33)
  SXCEL34  : LINE=(DU1s,BLMS34,DU2s,USEGSX34,SXBRK34)
  SXCEL35  : LINE=(DU1s,       DU2s,DUSEGS35,SXBRK35) !empty with Q+RFBPM
  SXCEL36  : LINE=(DU1s,BLMS36,DU2s,USEGSX36,SXBRK36)
  SXCEL37  : LINE=(DU1s,BLMS37,DU2s,USEGSX37,SXBRK37)
  SXCEL38  : LINE=(DU1s,BLMS38,DU2s,USEGSX38,SXBRK38)
  SXCEL39  : LINE=(DU1s,BLMS39,DU2s,USEGSX39,SXBRK39)
  SXCEL40  : LINE=(DU1s,BLMS40,DU2s,USEGSX40,SXBRK40)
  SXCEL41  : LINE=(DU1s,BLMS41,DU2s,USEGSX41,SXBRK41)
  SXCEL42  : LINE=(DU1s,BLMS42,DU2s,USEGSX42,SXBRK42)
  SXCEL43  : LINE=(DU1s,BLMS43,DU2s,USEGSX43,SXBRK43)
  SXCEL44  : LINE=(DU1s,BLMS44,DU2s,USEGSX44,SXBRK44)
  SXCEL45  : LINE=(DU1s,BLMS45,DU2s,USEGSX45,SXBRK45)
  SXCEL46  : LINE=(DU1s,BLMS46,DU2s,USEGSX46,SXBRK46)
  SXCEL47  : LINE=(DU1s,BLMS47,DU2s,USEGSX47,SXBRK47)
  SXCEL48  : LINE=(DU1s,       DU2s,DUSEGS48,SXBRK48) !empty
  SXCEL49  : LINE=(DU1s,       DU2s,DUSEGS49,SXBRK49) !empty
  SXCEL50  : LINE=(DU1s,       DU2s,DUSEGS50,SXBRK50) !empty

  SXRCL    : LINE=(&
    SXCEL17,SXCEL18,SXCEL19,SXCEL20,SXCEL21,SXCEL22,SXCEL23,SXCEL24,&
    SXCEL25,SXCEL26,SXCEL27,SXCEL28,SXCEL29,SXCEL30,SXCEL31,SXCEL32,&
    SXCEL33,SXCEL34,SXCEL35,SXCEL36,SXCEL37,SXCEL38,SXCEL39,SXCEL40,&
    SXCEL41,SXCEL42,SXCEL43,SXCEL44,SXCEL45,SXCEL46,SXCEL47,SXCEL48,&
    SXCEL49,SXCEL50)

  SXR : LINE=(DU0s,QSXh16_full,DU6s,RFBSX16,DU7s,&
    SXRSTART,SXRCL,SXRTERM,RWWAKE5S,DUE1ab,RFBSX51,DUE1e,ENDUNDS)

! ------------------------------------------------------------------------------
! SXR undulator exit section
! ------------------------------------------------------------------------------

! note: the below K-values are for SC beam; the settings for Cu beam are
! in the "LCLS2cu_main.mad8" file

  KQUE1B :=  0.325725075565 !UNDS_BDES_DS_fit.xlsx (Eu=8.0, KSXU=3.5)
  KQUE2B := -0.144356640751 !UNDS_BDES_DS_fit.xlsx (Eu=8.0, KSXU=3.5)

  QUE1B : QUAD, TYPE="2Q10", L=LQR/2, K1=KQUE1b, APER=rQR
  QUE2B : QUAD, TYPE="2Q10", L=LQR/2, K1=KQUE2b, APER=rQR

  TCX01B : LCAV, FREQ=XbandF, TYPE="TRANS_DEFL", L=1.0/2 !horiz. deflection
  TCX02B : LCAV, FREQ=XbandF, TYPE="TRANS_DEFL", L=1.0/2 !horiz. deflection

  LBKXDMP := 1.0 !x-kicker length in dumpline
  DBKXDMPS : DRIF, L=LBKXDMP !placeholder for x-kicker in SXR dumpline

  dzSXTES := 0.468389399848E-10 !set MDLWALLb at 685 m exactly to align with SXR XTES system
  dzHXTES := 0.749541186639E-6  !set MDLWALL at 685 m exactly to align with HXR XTES system

  DUE1d   : DRIF, L=0.1
  DUE1b   : DRIF, L=1.5
  DUE1c   : DRIF, L=0.5
  DUE2a   : DRIF, L=0.5
  DUE2d   : DRIF, L=0.188 -0.012 !phase cavity center-to-center spacing
  DUE2b   : DRIF, L=0.8-DUE2d[L]+0.005555
  DUE2e   : DRIF, L=1.740055-DUE2b[L]-3*DUE2d[L]
  DUE2cb  : DRIF, L=0.615    +(LQP-LQR)/2
  DUE3ab  : DRIF, L=0.375504 +(LQP-LQR)/2 -0.014955
  DUE3bb  : DRIF, L=1.2630495 +0.014955
  DUE3c   : DRIF, L=(1.7968755-LBKXDMP)/2
  DPCVV   : DRIF, L=0.397
  DVVTCX  : DRIF, L=0.32
  DTCX12  : DRIF, L=0.373/2
  DTCXSP  : DRIF, L=0.381
  DUE4b   : DRIF, L=0.454    +(LQP-LQR)/2
  DUE5ab  : DRIF, L=0.477799 +(LQP-LQR)/2 -0.114162
  DUE5bb  : DRIF, L=0.264 +0.114162-0.1016+0.1016
  DUE5cb  : DRIF, L=0.2674         +0.1016-0.1016
  DUE5d   : DRIF, L=0.137055-0.05
  DUE5f   : DRIF, L=0.05
  DUE5e   : DRIF, L=0.252825-DUE5d[L]-DUE5f[L]-dzHXTES
  DSB0a   : DRIF, L=0.260150
  DSB0b   : DRIF, L=0.344
  DSB0c   : DRIF, L=0.32646
  DSB0d   : DRIF, L=0.123366
  dDSB0e  := -0.403705866174E-3
  DSB0e   : DRIF, L=0.1059327 +dDSB0e
  DDLWALL : DRIF, L=0.250825+dzHXTES !length of dumpline thermal barrier wall

  DUE5eB   : DRIF, L=0.252825-DUE5d[L]-DUE5f[L]+dzSXTES
  DDLWALLB : DRIF, L=0.250825-dzSXTES !length of dumpline thermal barrier wall

  XCUE2B  : HKIC,TYPE="class-4"
  XCD3B   : HKIC,TYPE="class-5"

  YCUE1B  : VKIC,TYPE="class-4"
  YCD3B   : VKIC,TYPE="class-5"

  BPMUE1B : MONI, TYPE="Stripline-5" !RFBUE1B : MONI, TYPE="@2,CavityS-1"
  BPMUE2B : MONI, TYPE="Stripline-5" !RFBUE2B : MONI, TYPE="@2,CavityS-1"
 !RFBUE3B : MONI, TYPE="@2,CavityS-1"

  TRUE1B  : INST, TYPE="@5" !Be foil inserter (THz)
  SPTCXB  : INST, TYPE="@1" !XTCAV spoiler
  BTMQUEB : INST !Burn-Through-Monitor
  BTM0B   : INST !Burn-Through-Monitor behind the PCPM0B

  PCTCXB  : ECOL, TYPE="@1", XSIZE=4.5E-3, YSIZE=4.5E-3 !XTCAV photon collimator (9 mm aperture)
  PCPM0B  : ECOL, L=LPCPM, XSIZE=17.5E-3, YSIZE=17.5E-3

 !IMUNDOB : IMON, TYPE="@0,BCS ACM" !BCS ACM

  MIMUNDOB : MARK

  PH31B : INST, TYPE="2805 MHz" !post-undulator phase measurement RF cavity (low rate)
  PH32B : INST, TYPE="2805 MHz" !post-undulator phase measurement RF cavity (low rate)
  PH33B : INST, TYPE="2604 MHz" !post-undulator phase measurement RF cavity (high rate)
  PH34B : INST, TYPE="2604 MHz" !post-undulator phase measurement RF cavity (high rate)

  UEbegb   : MARK
  VVTCXb   : MARK !XTCAV vacuum valve
  MTCX01b  : MARK !entrance to TCX01B (for ELEGANT WATCH point)
  MTCXb    : MARK !centerline between TCX01B and TCX02B (for matching)
  UEendb   : MARK
  VV36b    : MARK !treaty-point vacuum valve just downbeam of undulator
  VV37b    : MARK !vac. valve in dumpline
  DLSTARTb : MARK
  MDLWALLb : MARK !front face of dumpline thermal barrier wall

  QUE1B_full : LINE=(QUE1B,QUE1B)
  QUE2B_full : LINE=(QUE2B,QUE2B)

  UNDEXITb : LINE=(BEGDMPS_1,&
      UEbegb,DUE1d,VV36b,DUE1b,MIMUNDOB,DUE1c,DUE2a,YCUE1B,&
      DUE2b,PH31B,DUE2d,PH32B,DUE2d,PH33B,DUE2d,PH34B,DUE2e,XCUE2B,DUE2cb,&
    QUE1B_full,DUE3ab,BPMUE1B,DUE3bb,TRUE1B,DUE3c,DBKXDMPS,DUE3c,PCTCXB,&
      DPCVV,VVTCXb,DVVTCX,MTCX01b,&
    2*TCX01B,DTCX12,MTCXb,DTCX12,&
    2*TCX02B,DTCXSP,SPTCXB,DUE4b,&
    QUE2B_full,DUE5ab,BPMUE2B,DUE5bb,BTMQUEB,DUE5cb,PCPM0B,DUE5f,BTM0B,&
      DUE5d,DUE5eB,MDLWALLb,DDLWALLB,UEendb,DLstartb,DSB0a,YCD3B,DSB0b,&
      XCD3B,DSB0c,VV37b,DSB0d,DSB0e,&
    ENDDMPS_1)

  SXRUND : LINE=(SXR,UNDEXITb)

! ==============================================================================
! HXR HGVPU undulator (south)
! ==============================================================================

! ------------------------------------------------------------------------------
! HXR undulator PPM phase shifter
! - Author: Heinz-Dieter Nuhn, Stanford Linear Accelerator Center
! - Last edited September 06, 2017
! - 10.0 mm miminum Undulator Gap
! ------------------------------------------------------------------------------
! - LPSHX  = HXR Phase Shifter length
! - luPSHX = HXR Phase Shifter period
! - PIPSHX = HXR Phase Shifter phase integral 
! - KPSHX  = HXR Phase Shifter Undulator parameter (rms); range 0.18-1.91
! - kQPSHX = natural HXR Phase Shifter undulator focusing "k" in y-plane
! ------------------------------------------------------------------------------

  LPSHX  := 0.0495                             !m
  luPSHX := 0.045                              !m
  LPSHXh := LPSHX/2
  PIPSHX := 490                                !T^2mm^3 (80-490)
  KPSHX  := 1.E-9*CLIGHT/mc2*SQRT(2.E-9*PIPSHX/luPSHX)
  kQPSHX := (KPSHX*2*pi/luPSHX/sqrt(2)/gamu)^2 !m^-2

  PSHXh : MATR, TYPE="phase shifter", L=LPSHXh,       &
    RM(1,1) = 1.0,                                    &
    RM(1,2) = LPSHXh,                                 &
    RM(2,1) = 0.0,                                    &
    RM(2,2) = 1.0,                                    &
    RM(3,3) =  cos(LPSHXh*sqrt(kQPSHX)),              &
    RM(3,4) =  sin(LPSHXh*sqrt(kQPSHX))/sqrt(kQPSHX), &
    RM(4,3) = -sin(LPSHXh*sqrt(kQPSHX))*sqrt(kQPSHX), &
    RM(4,4) =  cos(LPSHXh*sqrt(kQPSHX)),              &
    RM(5,5) = 1.0,                                    &
    RM(6,6) = 1.0

  PSHXh13 : PSHXh
  PSHXh14 : PSHXh
  PSHXh15 : PSHXh
  PSHXh16 : PSHXh
  PSHXh17 : PSHXh
  PSHXh18 : PSHXh
  PSHXh19 : PSHXh
  PSHXh20 : PSHXh
  PSHXh22 : PSHXh
  PSHXh23 : PSHXh
  PSHXh24 : PSHXh
  PSHXh25 : PSHXh
  PSHXh26 : PSHXh
  PSHXh27 : PSHXh
  PSHXh29 : PSHXh
  PSHXh30 : PSHXh
  PSHXh31 : PSHXh
  PSHXh32 : PSHXh
  PSHXh33 : PSHXh
  PSHXh34 : PSHXh
  PSHXh35 : PSHXh
  PSHXh36 : PSHXh
  PSHXh37 : PSHXh
  PSHXh38 : PSHXh
  PSHXh39 : PSHXh
  PSHXh40 : PSHXh
  PSHXh41 : PSHXh
  PSHXh42 : PSHXh
  PSHXh43 : PSHXh
  PSHXh44 : PSHXh
  PSHXh45 : PSHXh

! ------------------------------------------------------------------------------
! - Author: Heinz-Dieter Nuhn, Stanford Linear Accelerator Center
! - Edited Mar 03, 2014
! - Updated Dec 02, 2015 for HGVPU (Y. Nosochkov, D. Bruch)
! - 7.2 mm miminum Undulator Gap; only constant break length each
! - include natural horizontal focusing over all but edge terminations
! ------------------------------------------------------------------------------
! - IntgHX  = integrated quadrupole gradient
! - GQFHX   = QF and QD gradients can be made different to compensate for
!             undulator focussing ...
! - GQDHX   = ... but since undulator focussing depends on gamma^2, compensation
!             will only work for one energy
! - kQFHX   = QF undulator quadrupole focusing "k"
! - kQDHX   = QD undulator quadrupole focusing "k"
! - LDUSEGH = HXR Undulator segment length
! - luHXU   = HXR Undulator period
! - NpHXU   = HXR Undulator period count
! - LHXUCR  = HXR Undulator magnetic length
! - LHXUe   = HXR Undulator spacing between magnet array and strongback end
! - KHXU    = HXR Undulator parameter (rms); range 0.54-2.45
! - kQHX    = natural HXR undulator focusing "k" in x-plane
! ------------------------------------------------------------------------------

  LDUSEGH := 3.372032
  LDU1h   := 0.040984
  LDU2h   := 0.0
  LDU3h   := (1.83-LDUSEGH/2-LQu/2)/2
  LDU4h   := 0.13-LQu/2-LRFBub/2
  LDU5h   := 0.1275-LPSHXh-LRFBub/2
  LDU6h   := 0.109243-LPSHXh
  LDU7h   := 0.08892366667
  LDU8h   := 0.025
 dLDU0h   := 0.0 !fine tune Z of undulator
  LDU0h   := 0.094391984077-LDU8h-LRFBub +LRFBub +dLDU0h

  DU0h : DRIF, L=LDU0h
  DU1h : DRIF, L=LDU1h
  DU2h : DRIF, L=LDU2h
  DU3h : DRIF, L=LDU3h
  DU4h : DRIF, L=LDU4h
  DU5h : DRIF, L=LDU5h
  DU6h : DRIF, L=LDU6h
  DU7h : DRIF, L=LDU7h
  DU8h : DRIF, L=LDU8h

 dDUE1a := 0.0
  DUE1a : DRIF, L=2.853174666558-LRFBub +dDUE1a

 !IntgHX :   definition moved to LCLS2sc_main.xsif and LCLS2cu_main.xsif
  GQFHX  :=  IntgHX/LQu/10*1.0           !T/m
  GQDHX  := -IntgHX/LQu/10*1.0           !T/m
  kQFHX  :=  1.E-9*GQFHX*CLIGHT/gamu/mc2 !m^-2
  kQDHX  :=  1.E-9*GQDHX*CLIGHT/gamu/mc2 !m^-2
  kQDHXm := -1.734624260269 !UNDH_KQ4_USDS.xsif (Eu=8.0, KHXU=2.0)

  QHXh13 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh14 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh15 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh16 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh17 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh18 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh19 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh20 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh21 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh22 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh23 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh24 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh25 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh26 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh27 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh28 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh29 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh30 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh31 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh32 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh33 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh34 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh35 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh36 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh37 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh38 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh39 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh40 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh41 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh42 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh43 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh44 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHX, APER=rQu
  QHXh45 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQFHX, APER=rQu
  QHXh46 : QUAD, TYPE="0.433Q3.1", L=LQu/2, K1=kQDHXm, APER=rQu

  luHXU   := 0.026                            !m
  NpHXU   := 129
  LHXUCR  := luHXU*NpHXU                      !m
  LHXUe   := (LDUSEGH-LHXUCR)/2               !m
  LHXUh   := LHXUCR/2
  KHXU    := 2.0
  kQHX    := (KHXU*2*pi/luHXU/sqrt(2)/gamu)^2 !m^-2

  UMAHXh : MATR, TYPE="HGVPU", L=LHXUh,          &
    RM(1,1) =  cos(LHXUh*sqrt(kQHX)),            &
    RM(1,2) =  sin(LHXUh*sqrt(kQHX))/sqrt(kQHX), &
    RM(2,1) = -sin(LHXUh*sqrt(kQHX))*sqrt(kQHX), &
    RM(2,2) =  cos(LHXUh*sqrt(kQHX)),            &
    RM(3,3) = 1.0,                               &
    RM(3,4) = LHXUh,                             &
    RM(4,3) = 0.0,                               &
    RM(4,4) = 1.0,                               &
    RM(5,5) = 1.0,                               &
    RM(6,6) = 1.0

  UMAHXh13 : UMAHXh
  UMAHXh14 : UMAHXh
  UMAHXh15 : UMAHXh
  UMAHXh16 : UMAHXh
  UMAHXh17 : UMAHXh
  UMAHXh18 : UMAHXh
  UMAHXh19 : UMAHXh
  UMAHXh20 : UMAHXh
  UMAHXh22 : UMAHXh
  UMAHXh23 : UMAHXh
  UMAHXh24 : UMAHXh
  UMAHXh25 : UMAHXh
  UMAHXh26 : UMAHXh
  UMAHXh27 : UMAHXh
  UMAHXh29 : UMAHXh
  UMAHXh30 : UMAHXh
  UMAHXh31 : UMAHXh
  UMAHXh32 : UMAHXh
  UMAHXh33 : UMAHXh
  UMAHXh34 : UMAHXh
  UMAHXh35 : UMAHXh
  UMAHXh36 : UMAHXh
  UMAHXh37 : UMAHXh
  UMAHXh38 : UMAHXh
  UMAHXh39 : UMAHXh
  UMAHXh40 : UMAHXh
  UMAHXh41 : UMAHXh
  UMAHXh42 : UMAHXh
  UMAHXh43 : UMAHXh
  UMAHXh44 : UMAHXh
  UMAHXh45 : UMAHXh
  UMAHXh46 : UMAHXh

! ------------------------------------------------------------------------------
! HXR undulator X-steering coils in quads
! ------------------------------------------------------------------------------

  XCHX13 : HKIC, TYPE="type-U"
  XCHX14 : HKIC, TYPE="type-U"
  XCHX15 : HKIC, TYPE="type-U"
  XCHX16 : HKIC, TYPE="type-U"
  XCHX17 : HKIC, TYPE="type-U"
  XCHX18 : HKIC, TYPE="type-U"
  XCHX19 : HKIC, TYPE="type-U"
  XCHX20 : HKIC, TYPE="type-U"
  XCHX21 : HKIC, TYPE="type-U"
  XCHX22 : HKIC, TYPE="type-U"
  XCHX23 : HKIC, TYPE="type-U"
  XCHX24 : HKIC, TYPE="type-U"
  XCHX25 : HKIC, TYPE="type-U"
  XCHX26 : HKIC, TYPE="type-U"
  XCHX27 : HKIC, TYPE="type-U"
  XCHX28 : HKIC, TYPE="type-U"
  XCHX29 : HKIC, TYPE="type-U"
  XCHX30 : HKIC, TYPE="type-U"
  XCHX31 : HKIC, TYPE="type-U"
  XCHX32 : HKIC, TYPE="type-U"
  XCHX33 : HKIC, TYPE="type-U"
  XCHX34 : HKIC, TYPE="type-U"
  XCHX35 : HKIC, TYPE="type-U"
  XCHX36 : HKIC, TYPE="type-U"
  XCHX37 : HKIC, TYPE="type-U"
  XCHX38 : HKIC, TYPE="type-U"
  XCHX39 : HKIC, TYPE="type-U"
  XCHX40 : HKIC, TYPE="type-U"
  XCHX41 : HKIC, TYPE="type-U"
  XCHX42 : HKIC, TYPE="type-U"
  XCHX43 : HKIC, TYPE="type-U"
  XCHX44 : HKIC, TYPE="type-U"
  XCHX45 : HKIC, TYPE="type-U"
  XCHX46 : HKIC, TYPE="type-U"

! ------------------------------------------------------------------------------
! HXR undulator Y-steering coils in quads
! ------------------------------------------------------------------------------

  YCHX13 : VKIC, TYPE="type-U"
  YCHX14 : VKIC, TYPE="type-U"
  YCHX15 : VKIC, TYPE="type-U"
  YCHX16 : VKIC, TYPE="type-U"
  YCHX17 : VKIC, TYPE="type-U"
  YCHX18 : VKIC, TYPE="type-U"
  YCHX19 : VKIC, TYPE="type-U"
  YCHX20 : VKIC, TYPE="type-U"
  YCHX21 : VKIC, TYPE="type-U"
  YCHX22 : VKIC, TYPE="type-U"
  YCHX23 : VKIC, TYPE="type-U"
  YCHX24 : VKIC, TYPE="type-U"
  YCHX25 : VKIC, TYPE="type-U"
  YCHX26 : VKIC, TYPE="type-U"
  YCHX27 : VKIC, TYPE="type-U"
  YCHX28 : VKIC, TYPE="type-U"
  YCHX29 : VKIC, TYPE="type-U"
  YCHX30 : VKIC, TYPE="type-U"
  YCHX31 : VKIC, TYPE="type-U"
  YCHX32 : VKIC, TYPE="type-U"
  YCHX33 : VKIC, TYPE="type-U"
  YCHX34 : VKIC, TYPE="type-U"
  YCHX35 : VKIC, TYPE="type-U"
  YCHX36 : VKIC, TYPE="type-U"
  YCHX37 : VKIC, TYPE="type-U"
  YCHX38 : VKIC, TYPE="type-U"
  YCHX39 : VKIC, TYPE="type-U"
  YCHX40 : VKIC, TYPE="type-U"
  YCHX41 : VKIC, TYPE="type-U"
  YCHX42 : VKIC, TYPE="type-U"
  YCHX43 : VKIC, TYPE="type-U"
  YCHX44 : VKIC, TYPE="type-U"
  YCHX45 : VKIC, TYPE="type-U"
  YCHX46 : VKIC, TYPE="type-U"

! ------------------------------------------------------------------------------
! HXR undulator X-steering coils in undulator segments
! ------------------------------------------------------------------------------

  XCHU13 : INST, TYPE="type-V"
  XCHU14 : INST, TYPE="type-V"
  XCHU15 : INST, TYPE="type-V"
  XCHU16 : INST, TYPE="type-V"
  XCHU17 : INST, TYPE="type-V"
  XCHU18 : INST, TYPE="type-V"
  XCHU19 : INST, TYPE="type-V"
  XCHU20 : INST, TYPE="type-V"
  XCHU22 : INST, TYPE="type-V"
  XCHU23 : INST, TYPE="type-V"
  XCHU24 : INST, TYPE="type-V"
  XCHU25 : INST, TYPE="type-V"
  XCHU26 : INST, TYPE="type-V"
  XCHU27 : INST, TYPE="type-V"
  XCHU29 : INST, TYPE="type-V"
  XCHU30 : INST, TYPE="type-V"
  XCHU31 : INST, TYPE="type-V"
  XCHU32 : INST, TYPE="type-V"
  XCHU33 : INST, TYPE="type-V"
  XCHU34 : INST, TYPE="type-V"
  XCHU35 : INST, TYPE="type-V"
  XCHU36 : INST, TYPE="type-V"
  XCHU37 : INST, TYPE="type-V"
  XCHU38 : INST, TYPE="type-V"
  XCHU39 : INST, TYPE="type-V"
  XCHU40 : INST, TYPE="type-V"
  XCHU41 : INST, TYPE="type-V"
  XCHU42 : INST, TYPE="type-V"
  XCHU43 : INST, TYPE="type-V"
  XCHU44 : INST, TYPE="type-V"
  XCHU45 : INST, TYPE="type-V"
  XCHU46 : INST, TYPE="type-V"

! ------------------------------------------------------------------------------
! HXR undulator Y-steering coils in undulator segments
! ------------------------------------------------------------------------------

  YCHU13 : INST, TYPE="type-V"
  YCHU14 : INST, TYPE="type-V"
  YCHU15 : INST, TYPE="type-V"
  YCHU16 : INST, TYPE="type-V"
  YCHU17 : INST, TYPE="type-V"
  YCHU18 : INST, TYPE="type-V"
  YCHU19 : INST, TYPE="type-V"
  YCHU20 : INST, TYPE="type-V"
  YCHU22 : INST, TYPE="type-V"
  YCHU23 : INST, TYPE="type-V"
  YCHU24 : INST, TYPE="type-V"
  YCHU25 : INST, TYPE="type-V"
  YCHU26 : INST, TYPE="type-V"
  YCHU27 : INST, TYPE="type-V"
  YCHU29 : INST, TYPE="type-V"
  YCHU30 : INST, TYPE="type-V"
  YCHU31 : INST, TYPE="type-V"
  YCHU32 : INST, TYPE="type-V"
  YCHU33 : INST, TYPE="type-V"
  YCHU34 : INST, TYPE="type-V"
  YCHU35 : INST, TYPE="type-V"
  YCHU36 : INST, TYPE="type-V"
  YCHU37 : INST, TYPE="type-V"
  YCHU38 : INST, TYPE="type-V"
  YCHU39 : INST, TYPE="type-V"
  YCHU40 : INST, TYPE="type-V"
  YCHU41 : INST, TYPE="type-V"
  YCHU42 : INST, TYPE="type-V"
  YCHU43 : INST, TYPE="type-V"
  YCHU44 : INST, TYPE="type-V"
  YCHU45 : INST, TYPE="type-V"
  YCHU46 : INST, TYPE="type-V"

! ------------------------------------------------------------------------------
! HXR undulator BPMs
! ------------------------------------------------------------------------------

  RFBHX13 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX14 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX15 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX16 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX17 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX18 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX19 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX20 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX21 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX22 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX23 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX24 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX25 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX26 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX27 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX28 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX29 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX30 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX31 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX32 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX33 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX34 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX35 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX36 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX37 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX38 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX39 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX40 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX41 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX42 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX43 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX44 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX45 : MONI, L=LRFBub, TYPE="CavityX-1"
  RFBHX46 : MONI, L=LRFBub, TYPE="CavityX-1"
  DRFBH47 : DRIF, L=LRFBub
  DRFBH48 : DRIF, L=LRFBub
  DRFBH49 : DRIF, L=LRFBub
  DRFBH50 : DRIF, L=LRFBub
  RFBHX51 : MONI, L=LRFBub, TYPE="CavityX-1"

! ------------------------------------------------------------------------------
! DTHXU = HXU undulator segment small terminations modeled as drift
! ------------------------------------------------------------------------------

  DTHXU : DRIF, L=LHXUe

! ------------------------------------------------------------------------------
! markers
! ------------------------------------------------------------------------------

  HXRSTART : MARK !start of undulator system
  RWWAKE5H : MARK, TYPE="WAKE" !HXR beampipe wake applied here
  HXRTERM  : MARK !end of undulator system
  MUQH     : MARK
  MPHH     : MARK

! ------------------------------------------------------------------------------
! HXR undulator phase shifters
! ------------------------------------------------------------------------------

  PSHXh13_full : LINE=(PSHXh13,MPHH,PSHXh13)
  PSHXh14_full : LINE=(PSHXh14,MPHH,PSHXh14)
  PSHXh15_full : LINE=(PSHXh15,MPHH,PSHXh15)
  PSHXh16_full : LINE=(PSHXh16,MPHH,PSHXh16)
  PSHXh17_full : LINE=(PSHXh17,MPHH,PSHXh17)
  PSHXh18_full : LINE=(PSHXh18,MPHH,PSHXh18)
  PSHXh19_full : LINE=(PSHXh19,MPHH,PSHXh19)
  PSHXh20_full : LINE=(PSHXh20,MPHH,PSHXh20)
  DPSHX21      : DRIF, L=LPSHX
  PSHXh22_full : LINE=(PSHXh22,MPHH,PSHXh22)
  PSHXh23_full : LINE=(PSHXh23,MPHH,PSHXh23)
  PSHXh24_full : LINE=(PSHXh24,MPHH,PSHXh24)
  PSHXh25_full : LINE=(PSHXh25,MPHH,PSHXh25)
  PSHXh26_full : LINE=(PSHXh26,MPHH,PSHXh26)
  PSHXh27_full : LINE=(PSHXh27,MPHH,PSHXh27)
  DPSHX28      : DRIF, L=LPSHX
  PSHXh29_full : LINE=(PSHXh29,MPHH,PSHXh29)
  PSHXh30_full : LINE=(PSHXh30,MPHH,PSHXh30)
  PSHXh31_full : LINE=(PSHXh31,MPHH,PSHXh31)
  PSHXh32_full : LINE=(PSHXh32,MPHH,PSHXh32)
  PSHXh33_full : LINE=(PSHXh33,MPHH,PSHXh33)
  PSHXh34_full : LINE=(PSHXh34,MPHH,PSHXh34)
  PSHXh35_full : LINE=(PSHXh35,MPHH,PSHXh35)
  PSHXh36_full : LINE=(PSHXh36,MPHH,PSHXh36)
  PSHXh37_full : LINE=(PSHXh37,MPHH,PSHXh37)
  PSHXh38_full : LINE=(PSHXh38,MPHH,PSHXh38)
  PSHXh39_full : LINE=(PSHXh39,MPHH,PSHXh39)
  PSHXh40_full : LINE=(PSHXh40,MPHH,PSHXh40)
  PSHXh41_full : LINE=(PSHXh41,MPHH,PSHXh41)
  PSHXh42_full : LINE=(PSHXh42,MPHH,PSHXh42)
  PSHXh43_full : LINE=(PSHXh43,MPHH,PSHXh43)
  PSHXh44_full : LINE=(PSHXh44,MPHH,PSHXh44)
  PSHXh45_full : LINE=(PSHXh45,MPHH,PSHXh45)
  DPSHX46      : DRIF, L=LPSHX
  DPSHX47      : DRIF, L=LPSHX
  DPSHX48      : DRIF, L=LPSHX
  DPSHX49      : DRIF, L=LPSHX
  DPSHX50      : DRIF, L=LPSHX

! ------------------------------------------------------------------------------
! HXR undulator inline valves (VAT Series 48?)
! ------------------------------------------------------------------------------

  VVHXU13 : MARK
  VVHXU14 : MARK
  VVHXU15 : MARK
  VVHXU16 : MARK
  VVHXU17 : MARK
  VVHXU18 : MARK
  VVHXU19 : MARK
  VVHXU20 : MARK
  VVHXU22 : MARK
  VVHXU23 : MARK
  VVHXU24 : MARK
  VVHXU25 : MARK
  VVHXU26 : MARK
  VVHXU27 : MARK
  VVHXU29 : MARK
  VVHXU30 : MARK
  VVHXU31 : MARK
  VVHXU32 : MARK
  VVHXU33 : MARK
  VVHXU34 : MARK
  VVHXU35 : MARK
  VVHXU36 : MARK
  VVHXU37 : MARK
  VVHXU38 : MARK
  VVHXU39 : MARK
  VVHXU40 : MARK
  VVHXU41 : MARK
  VVHXU42 : MARK
  VVHXU43 : MARK
  VVHXU44 : MARK
  VVHXU45 : MARK
  VVHXU46 : MARK

! ------------------------------------------------------------------------------
! HXR undulator Beam Loss Monitors
! ------------------------------------------------------------------------------

  BLMH13 : MARK
  BLMH14 : MARK
  BLMH15 : MARK
  BLMH16 : MARK
  BLMH17 : MARK
  BLMH18 : MARK
  BLMH19 : MARK
  BLMH20 : MARK
  BLMH22 : MARK
  BLMH23 : MARK
  BLMH24 : MARK
  BLMH25 : MARK
  BLMH26 : MARK
  BLMH27 : MARK
  BLMH29 : MARK
  BLMH30 : MARK
  BLMH31 : MARK
  BLMH32 : MARK
  BLMH33 : MARK
  BLMH34 : MARK
  BLMH35 : MARK
  BLMH36 : MARK
  BLMH37 : MARK
  BLMH38 : MARK
  BLMH39 : MARK
  BLMH40 : MARK
  BLMH41 : MARK
  BLMH42 : MARK
  BLMH43 : MARK
  BLMH44 : MARK
  BLMH45 : MARK
  BLMH46 : MARK

! ------------------------------------------------------------------------------
! HXRSS self-seeding chicane (LCLSII-3.2-PR-0102-R1)
! ------------------------------------------------------------------------------

! NOTEs:
! - in LCLS: BXHS1-4
! - Bmax = 1.9 kG-m @ 6 A (~24 mm beam offset)
! - use series approximation for sinc(x)=sin(x)/x to allow BLB4=0
! - deflects toward -X (to the right/south/wall)
! - bends away from coil side

! BCXHS1 gets an X-offset of  0    mm (toward the wall)
! BCXHS2 gets an X-offset of -2.39 mm (toward the wall)
! BCXHS3 gets an X-offset of -2.39 mm (toward the wall)
! BCXHS4 gets an X-offset of  0    mm (toward the wall)

  Brho4    := Cb*Eu                             !beam rigidity at chicane (kG-m)
  GB4      := 0.315*in2m                        !full gap height [m]
  ZB4      := 14.315*in2m                       !on-axis effective length is 14" + gap (rule-of-thumb)
  FB4      := 0.5                               !not measured
  AB4      := +3.02786616797E-3*SETHXRSS        !bend angle (rad) for R56=+15.0 um
  BLB4     := Brho4*AB4                         !integrated strength (1.377623 kG-m @ 13.64 GeV)
  AB4_2    := AB4*AB4
  AB4_4    := AB4_2*AB4_2
  AB4_6    := AB4_4*AB4_2
  SINCAB4  := 1-AB4_2/6+AB4_4/120-AB4_6/5040    !~sinc(AB4)=sin(AB4)/AB4
  LB4      := ZB4/SINCAB4                       !chicane bend path length (m)
  AB4s     := ASIN(SIN(AB4)/2)                  !"short" half chicane bend angle (rad)
  AB4s_2   := AB4s*AB4s
  AB4s_4   := AB4s_2*AB4s_2
  AB4s_6   := AB4s_4*AB4s_2
  SINCAB4s := 1-AB4s_2/6+AB4s_4/120-AB4s_6/5040 !~sinc(AB4s)=sin(AB4s)/AB4s
  LB4s     := (ZB4/2)/SINCAB4s                  !"short" half chicane bend path length (m)
  AB4L     := AB4-AB4S                          !"long" half chicane bend angle (rad)
  LB4L     := LB4-LB4S                          !"long" half chicane bend path length (m)

  BCXHS11a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=+AB4S, &
                   E1=0, FINT=FB4, FINTX=0
  BCXHS11b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=+AB4L, &
                   E2=+AB4, FINT=0, FINTX=FB4
  BCXHS12a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=-AB4L, &
                   E1=-AB4, FINT=FB4, FINTX=0
  BCXHS12b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=-AB4S, &
                   E2=0, FINT=0, FINTX=FB4
  BCXHS13a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=-AB4S, &
                   E1=0, FINT=FB4, FINTX=0
  BCXHS13b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=-AB4L, &
                   E2=-AB4, FINT=0, FINTX=FB4
  BCXHS14a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=+AB4L, &
                   E1=+AB4, FINT=FB4, FINTX=0
  BCXHS14b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=+AB4S, &
                   E2=0, FINT=0, FINTX=FB4

  BCXHS21a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=+AB4S, &
                   E1=0, FINT=FB4, FINTX=0
  BCXHS21b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=+AB4L, &
                   E2=+AB4, FINT=0, FINTX=FB4
  BCXHS22a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=-AB4L, &
                   E1=-AB4, FINT=FB4, FINTX=0
  BCXHS22b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=-AB4S, &
                   E2=0, FINT=0, FINTX=FB4
  BCXHS23a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=-AB4S, &
                   E1=0, FINT=FB4, FINTX=0
  BCXHS23b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=-AB4L, &
                   E2=-AB4, FINT=0, FINTX=FB4
  BCXHS24a : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4L, ANGLE=+AB4L, &
                   E1=+AB4, FINT=FB4, FINTX=0
  BCXHS24b : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4S, ANGLE=+AB4S, &
                   E2=0, FINT=0, FINTX=FB4

! define unsplit SBENs for BMAD ... not used by MAD

  BCXHS11 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=+AB4, &
                  E1=0, FINT=FB4, E2=+AB4, FINTX=FB4
  BCXHS12 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=-AB4, &
                  E1=-AB4, FINT=FB4, E2=0, FINTX=FB4
  BCXHS13 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=-AB4, &
                  E1=0, FINT=FB4, E2=-AB4, FINTX=FB4
  BCXHS14 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=+AB4, &
                  E1=+AB4, FINT=FB4, E2=0, FINTX=FB4
  BCXHS21 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=+AB4, &
                  E1=0, FINT=FB4, E2=+AB4, FINTX=FB4
  BCXHS22 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=-AB4, &
                  E1=-AB4, FINT=FB4, E2=0, FINTX=FB4
  BCXHS23 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=-AB4, &
                  E1=0, FINT=FB4, E2=-AB4, FINTX=FB4
  BCXHS24 : SBEN, TYPE="0.315D14-C", HGAP=GB4/2, L=LB4, ANGLE=+AB4, &
                  E1=+AB4, FINT=FB4, E2=0, FINTX=FB4

  LHssB2Bo := 0.939 !outer bend center-to-center (PRD Table 3)
  LHssB2Bi := 0.936 !inner bend center-to-center (PRD Table 3)

  D1      : DRIF, L=(LHssB2Bo-ZB4)/COS(AB4)
  DCH     : DRIF, L=(LHssB2Bi-ZB4)/2
  DMONO   : DRIF, L=(LDUSEGH-2*LHssB2Bo-LHssB2Bi-ZB4)/2

  HXRSS1BEG : MARK
  DIAMOND1  : INST
  HXRSS1END : MARK
  HXRSS2BEG : MARK
  DIAMOND2  : INST
  HXRSS2END : MARK

  HCHICANE1 : LINE=(HXRSS1BEG,DMONO,&
    BCXHS11a,BCXHS11b,D1,&
    BCXHS12a,BCXHS12b,DCH,DIAMOND1,DCH,&
    BCXHS13a,BCXHS13b,D1,&
    BCXHS14a,BCXHS14b,DMONO,HXRSS1END)

  HCHICANE2 : LINE=(HXRSS2BEG,DMONO,&
    BCXHS21a,BCXHS21b,D1,&
    BCXHS22a,BCXHS22b,DCH,DIAMOND2,DCH,&
    BCXHS23a,BCXHS23b,D1,&
    BCXHS24a,BCXHS24b,DMONO,HXRSS2END)

! ------------------------------------------------------------------------------
! HXR undulator cells
! ------------------------------------------------------------------------------

  UMAHXh13_full : LINE=(UMAHXh13,XCHU13,YCHU13,UMAHXh13)
  UMAHXh14_full : LINE=(UMAHXh14,XCHU14,YCHU14,UMAHXh14)
  UMAHXh15_full : LINE=(UMAHXh15,XCHU15,YCHU15,UMAHXh15)
  UMAHXh16_full : LINE=(UMAHXh16,XCHU16,YCHU16,UMAHXh16)
  UMAHXh17_full : LINE=(UMAHXh17,XCHU17,YCHU17,UMAHXh17)
  UMAHXh18_full : LINE=(UMAHXh18,XCHU18,YCHU18,UMAHXh18)
  UMAHXh19_full : LINE=(UMAHXh19,XCHU19,YCHU19,UMAHXh19)
  UMAHXh20_full : LINE=(UMAHXh20,XCHU20,YCHU20,UMAHXh20)
  UMAHXh22_full : LINE=(UMAHXh22,XCHU22,YCHU22,UMAHXh22)
  UMAHXh23_full : LINE=(UMAHXh23,XCHU23,YCHU23,UMAHXh23)
  UMAHXh24_full : LINE=(UMAHXh24,XCHU24,YCHU24,UMAHXh24)
  UMAHXh25_full : LINE=(UMAHXh25,XCHU25,YCHU25,UMAHXh25)
  UMAHXh26_full : LINE=(UMAHXh26,XCHU26,YCHU26,UMAHXh26)
  UMAHXh27_full : LINE=(UMAHXh27,XCHU27,YCHU27,UMAHXh27)
  UMAHXh29_full : LINE=(UMAHXh29,XCHU29,YCHU29,UMAHXh29)
  UMAHXh30_full : LINE=(UMAHXh30,XCHU30,YCHU30,UMAHXh30)
  UMAHXh31_full : LINE=(UMAHXh31,XCHU31,YCHU31,UMAHXh31)
  UMAHXh32_full : LINE=(UMAHXh32,XCHU32,YCHU32,UMAHXh32)
  UMAHXh33_full : LINE=(UMAHXh33,XCHU33,YCHU33,UMAHXh33)
  UMAHXh34_full : LINE=(UMAHXh34,XCHU34,YCHU34,UMAHXh34)
  UMAHXh35_full : LINE=(UMAHXh35,XCHU35,YCHU35,UMAHXh35)
  UMAHXh36_full : LINE=(UMAHXh36,XCHU36,YCHU36,UMAHXh36)
  UMAHXh37_full : LINE=(UMAHXh37,XCHU37,YCHU37,UMAHXh37)
  UMAHXh38_full : LINE=(UMAHXh38,XCHU38,YCHU38,UMAHXh38)
  UMAHXh39_full : LINE=(UMAHXh39,XCHU39,YCHU39,UMAHXh39)
  UMAHXh40_full : LINE=(UMAHXh40,XCHU40,YCHU40,UMAHXh40)
  UMAHXh41_full : LINE=(UMAHXh41,XCHU41,YCHU41,UMAHXh41)
  UMAHXh42_full : LINE=(UMAHXh42,XCHU42,YCHU42,UMAHXh42)
  UMAHXh43_full : LINE=(UMAHXh43,XCHU43,YCHU43,UMAHXh43)
  UMAHXh44_full : LINE=(UMAHXh44,XCHU44,YCHU44,UMAHXh44)
  UMAHXh45_full : LINE=(UMAHXh45,XCHU45,YCHU45,UMAHXh45)
  UMAHXh46_full : LINE=(UMAHXh46,XCHU46,YCHU46,UMAHXh46)

  USEGHX13 : LINE=(DTHXU,UMAHXh13_full,DTHXU)
  USEGHX14 : LINE=(DTHXU,UMAHXh14_full,DTHXU)
  USEGHX15 : LINE=(DTHXU,UMAHXh15_full,DTHXU)
  USEGHX16 : LINE=(DTHXU,UMAHXh16_full,DTHXU)
  USEGHX17 : LINE=(DTHXU,UMAHXh17_full,DTHXU)
  USEGHX18 : LINE=(DTHXU,UMAHXh18_full,DTHXU)
  USEGHX19 : LINE=(DTHXU,UMAHXh19_full,DTHXU)
  USEGHX20 : LINE=(DTHXU,UMAHXh20_full,DTHXU)
  DUSEGH21 : LINE=(HCHICANE1) !DRIF, L=LDUSEGH
  USEGHX22 : LINE=(DTHXU,UMAHXh22_full,DTHXU)
  USEGHX23 : LINE=(DTHXU,UMAHXh23_full,DTHXU)
  USEGHX24 : LINE=(DTHXU,UMAHXh24_full,DTHXU)
  USEGHX25 : LINE=(DTHXU,UMAHXh25_full,DTHXU)
  USEGHX26 : LINE=(DTHXU,UMAHXh26_full,DTHXU)
  USEGHX27 : LINE=(DTHXU,UMAHXh27_full,DTHXU)
  DUSEGH28 : LINE=(HCHICANE2) !DRIF, L=LDUSEGH
  USEGHX29 : LINE=(DTHXU,UMAHXh29_full,DTHXU)
  USEGHX30 : LINE=(DTHXU,UMAHXh30_full,DTHXU)
  USEGHX31 : LINE=(DTHXU,UMAHXh31_full,DTHXU)
  USEGHX32 : LINE=(DTHXU,UMAHXh32_full,DTHXU)
  USEGHX33 : LINE=(DTHXU,UMAHXh33_full,DTHXU)
  USEGHX34 : LINE=(DTHXU,UMAHXh34_full,DTHXU)
  USEGHX35 : LINE=(DTHXU,UMAHXh35_full,DTHXU)
  USEGHX36 : LINE=(DTHXU,UMAHXh36_full,DTHXU)
  USEGHX37 : LINE=(DTHXU,UMAHXh37_full,DTHXU)
  USEGHX38 : LINE=(DTHXU,UMAHXh38_full,DTHXU)
  USEGHX39 : LINE=(DTHXU,UMAHXh39_full,DTHXU)
  USEGHX40 : LINE=(DTHXU,UMAHXh40_full,DTHXU)
  USEGHX41 : LINE=(DTHXU,UMAHXh41_full,DTHXU)
  USEGHX42 : LINE=(DTHXU,UMAHXh42_full,DTHXU)
  USEGHX43 : LINE=(DTHXU,UMAHXh43_full,DTHXU)
  USEGHX44 : LINE=(DTHXU,UMAHXh44_full,DTHXU)
  USEGHX45 : LINE=(DTHXU,UMAHXh45_full,DTHXU)
  USEGHX46 : LINE=(DTHXU,UMAHXh46_full,DTHXU)
  DUSEGH47 : DRIF, L=LDUSEGH
  DUSEGH48 : DRIF, L=LDUSEGH
  DUSEGH49 : DRIF, L=LDUSEGH
  DUSEGH50 : DRIF, L=LDUSEGH

  QHXh13_full : LINE=(QHXh13,XCHX13,MUQH,YCHX13,QHXh13)
  QHXh14_full : LINE=(QHXh14,XCHX14,MUQH,YCHX14,QHXh14)
  QHXh15_full : LINE=(QHXh15,XCHX15,MUQH,YCHX15,QHXh15)
  QHXh16_full : LINE=(QHXh16,XCHX16,MUQH,YCHX16,QHXh16)
  QHXh17_full : LINE=(QHXh17,XCHX17,MUQH,YCHX17,QHXh17)
  QHXh18_full : LINE=(QHXh18,XCHX18,MUQH,YCHX18,QHXh18)
  QHXh19_full : LINE=(QHXh19,XCHX19,MUQH,YCHX19,QHXh19)
  QHXh20_full : LINE=(QHXh20,XCHX20,MUQH,YCHX20,QHXh20)
  QHXh21_full : LINE=(QHXh21,XCHX21,MUQH,YCHX21,QHXh21)
  QHXh22_full : LINE=(QHXh22,XCHX22,MUQH,YCHX22,QHXh22)
  QHXh23_full : LINE=(QHXh23,XCHX23,MUQH,YCHX23,QHXh23)
  QHXh24_full : LINE=(QHXh24,XCHX24,MUQH,YCHX24,QHXh24)
  QHXh25_full : LINE=(QHXh25,XCHX25,MUQH,YCHX25,QHXh25)
  QHXh26_full : LINE=(QHXh26,XCHX26,MUQH,YCHX26,QHXh26)
  QHXh27_full : LINE=(QHXh27,XCHX27,MUQH,YCHX27,QHXh27)
  QHXh28_full : LINE=(QHXh28,XCHX28,MUQH,YCHX28,QHXh28)
  QHXh29_full : LINE=(QHXh29,XCHX29,MUQH,YCHX29,QHXh29)
  QHXh30_full : LINE=(QHXh30,XCHX30,MUQH,YCHX30,QHXh30)
  QHXh31_full : LINE=(QHXh31,XCHX31,MUQH,YCHX31,QHXh31)
  QHXh32_full : LINE=(QHXh32,XCHX32,MUQH,YCHX32,QHXh32)
  QHXh33_full : LINE=(QHXh33,XCHX33,MUQH,YCHX33,QHXh33)
  QHXh34_full : LINE=(QHXh34,XCHX34,MUQH,YCHX34,QHXh34)
  QHXh35_full : LINE=(QHXh35,XCHX35,MUQH,YCHX35,QHXh35)
  QHXh36_full : LINE=(QHXh36,XCHX36,MUQH,YCHX36,QHXh36)
  QHXh37_full : LINE=(QHXh37,XCHX37,MUQH,YCHX37,QHXh37)
  QHXh38_full : LINE=(QHXh38,XCHX38,MUQH,YCHX38,QHXh38)
  QHXh39_full : LINE=(QHXh39,XCHX39,MUQH,YCHX39,QHXh39)
  QHXh40_full : LINE=(QHXh40,XCHX40,MUQH,YCHX40,QHXh40)
  QHXh41_full : LINE=(QHXh41,XCHX41,MUQH,YCHX41,QHXh41)
  QHXh42_full : LINE=(QHXh42,XCHX42,MUQH,YCHX42,QHXh42)
  QHXh43_full : LINE=(QHXh43,XCHX43,MUQH,YCHX43,QHXh43)
  QHXh44_full : LINE=(QHXh44,XCHX44,MUQH,YCHX44,QHXh44)
  QHXh45_full : LINE=(QHXh45,XCHX45,MUQH,YCHX45,QHXh45)
  QHXh46_full : LINE=(QHXh46,XCHX46,MUQH,YCHX46,QHXh46)
  DQHX47      : DRIF, L=LQu
  DQHX48      : DRIF, L=LQu
  DQHX49      : DRIF, L=LQu
  DQHX50      : DRIF, L=LQu

  HXBRK13 : LINE=(DU3h,BLMH13,DU3h,QHXh13_full,DU4h,RFBHX13,DU5h,&
                  PSHXh13_full,DU6h,VVHXU13,DU7h)
  HXBRK14 : LINE=(DU3h,BLMH14,DU3h,QHXh14_full,DU4h,RFBHX14,DU5h,&
                  PSHXh14_full,DU6h,VVHXU14,DU7h)
  HXBRK15 : LINE=(DU3h,BLMH15,DU3h,QHXh15_full,DU4h,RFBHX15,DU5h,&
                  PSHXh15_full,DU6h,VVHXU15,DU7h)
  HXBRK16 : LINE=(DU3h,BLMH16,DU3h,QHXh16_full,DU4h,RFBHX16,DU5h,&
                  PSHXh16_full,DU6h,VVHXU16,DU7h)
  HXBRK17 : LINE=(DU3h,BLMH17,DU3h,QHXh17_full,DU4h,RFBHX17,DU5h,&
                  PSHXh17_full,DU6h,VVHXU17,DU7h)
  HXBRK18 : LINE=(DU3h,BLMH18,DU3h,QHXh18_full,DU4h,RFBHX18,DU5h,&
                  PSHXh18_full,DU6h,VVHXU18,DU7h)
  HXBRK19 : LINE=(DU3h,BLMH19,DU3h,QHXh19_full,DU4h,RFBHX19,DU5h,&
                  PSHXh19_full,DU6h,VVHXU19,DU7h)
  HXBRK20 : LINE=(DU3h,BLMH20,DU3h,QHXh20_full,DU4h,RFBHX20,DU5h,&
                  PSHXh20_full,DU6h,VVHXU20,DU7h)
  HXBRK21 : LINE=(DU3h       ,DU3h,QHXh21_full,DU4h,RFBHX21,DU5h,&
                  DPSHX21     ,DU6h        ,DU7h)
  HXBRK22 : LINE=(DU3h,BLMH22,DU3h,QHXh22_full,DU4h,RFBHX22,DU5h,&
                  PSHXh22_full,DU6h,VVHXU22,DU7h)
  HXBRK23 : LINE=(DU3h,BLMH23,DU3h,QHXh23_full,DU4h,RFBHX23,DU5h,&
                  PSHXh23_full,DU6h,VVHXU23,DU7h)
  HXBRK24 : LINE=(DU3h,BLMH24,DU3h,QHXh24_full,DU4h,RFBHX24,DU5h,&
                  PSHXh24_full,DU6h,VVHXU24,DU7h)
  HXBRK25 : LINE=(DU3h,BLMH25,DU3h,QHXh25_full,DU4h,RFBHX25,DU5h,&
                  PSHXh25_full,DU6h,VVHXU25,DU7h)
  HXBRK26 : LINE=(DU3h,BLMH26,DU3h,QHXh26_full,DU4h,RFBHX26,DU5h,&
                  PSHXh26_full,DU6h,VVHXU26,DU7h)
  HXBRK27 : LINE=(DU3h,BLMH27,DU3h,QHXh27_full,DU4h,RFBHX27,DU5h,&
                  PSHXh27_full,DU6h,VVHXU27,DU7h)
  HXBRK28 : LINE=(DU3h       ,DU3h,QHXh28_full,DU4h,RFBHX28,DU5h,&
                  DPSHX28     ,DU6h        ,DU7h)
  HXBRK29 : LINE=(DU3h,BLMH29,DU3h,QHXh29_full,DU4h,RFBHX29,DU5h,&
                  PSHXh29_full,DU6h,VVHXU29,DU7h)
  HXBRK30 : LINE=(DU3h,BLMH30,DU3h,QHXh30_full,DU4h,RFBHX30,DU5h,&
                  PSHXh30_full,DU6h,VVHXU30,DU7h)
  HXBRK31 : LINE=(DU3h,BLMH31,DU3h,QHXh31_full,DU4h,RFBHX31,DU5h,&
                  PSHXh31_full,DU6h,VVHXU31,DU7h)
  HXBRK32 : LINE=(DU3h,BLMH32,DU3h,QHXh32_full,DU4h,RFBHX32,DU5h,&
                  PSHXh32_full,DU6h,VVHXU32,DU7h)
  HXBRK33 : LINE=(DU3h,BLMH33,DU3h,QHXh33_full,DU4h,RFBHX33,DU5h,&
                  PSHXh33_full,DU6h,VVHXU33,DU7h)
  HXBRK34 : LINE=(DU3h,BLMH34,DU3h,QHXh34_full,DU4h,RFBHX34,DU5h,&
                  PSHXh34_full,DU6h,VVHXU34,DU7h)
  HXBRK35 : LINE=(DU3h,BLMH35,DU3h,QHXh35_full,DU4h,RFBHX35,DU5h,&
                  PSHXh35_full,DU6h,VVHXU35,DU7h)
  HXBRK36 : LINE=(DU3h,BLMH36,DU3h,QHXh36_full,DU4h,RFBHX36,DU5h,&
                  PSHXh36_full,DU6h,VVHXU36,DU7h)
  HXBRK37 : LINE=(DU3h,BLMH37,DU3h,QHXh37_full,DU4h,RFBHX37,DU5h,&
                  PSHXh37_full,DU6h,VVHXU37,DU7h)
  HXBRK38 : LINE=(DU3h,BLMH38,DU3h,QHXh38_full,DU4h,RFBHX38,DU5h,&
                  PSHXh38_full,DU6h,VVHXU38,DU7h)
  HXBRK39 : LINE=(DU3h,BLMH39,DU3h,QHXh39_full,DU4h,RFBHX39,DU5h,&
                  PSHXh39_full,DU6h,VVHXU39,DU7h)
  HXBRK40 : LINE=(DU3h,BLMH40,DU3h,QHXh40_full,DU4h,RFBHX40,DU5h,&
                  PSHXh40_full,DU6h,VVHXU40,DU7h)
  HXBRK41 : LINE=(DU3h,BLMH41,DU3h,QHXh41_full,DU4h,RFBHX41,DU5h,&
                  PSHXh41_full,DU6h,VVHXU41,DU7h)
  HXBRK42 : LINE=(DU3h,BLMH42,DU3h,QHXh42_full,DU4h,RFBHX42,DU5h,&
                  PSHXh42_full,DU6h,VVHXU42,DU7h)
  HXBRK43 : LINE=(DU3h,BLMH43,DU3h,QHXh43_full,DU4h,RFBHX43,DU5h,&
                  PSHXh43_full,DU6h,VVHXU43,DU7h)
  HXBRK44 : LINE=(DU3h,BLMH44,DU3h,QHXh44_full,DU4h,RFBHX44,DU5h,&
                  PSHXh44_full,DU6h,VVHXU44,DU7h)
  HXBRK45 : LINE=(DU3h,BLMH45,DU3h,QHXh45_full,DU4h,RFBHX45,DU5h,&
                  PSHXh45_full,DU6h,VVHXU45,DU7h)
  HXBRK46 : LINE=(DU3h,BLMH46,DU3h,QHXh46_full,DU4h,RFBHX46,DU5h,&
                  DPSHX46     ,DU6h,VVHXU46,DU7h)
  HXBRK47 : LINE=(DU3h       ,DU3h,DQHX47     ,DU4h,DRFBH47,DU5h,&
                  DPSHX47     ,DU6h        ,DU7h)
  HXBRK48 : LINE=(DU3h       ,DU3h,DQHX48     ,DU4h,DRFBH48,DU5h,&
                  DPSHX48     ,DU6h        ,DU7h)
  HXBRK49 : LINE=(DU3h       ,DU3h,DQHX49     ,DU4h,DRFBH49,DU5h,&
                  DPSHX49     ,DU6h        ,DU7h)
  HXBRK50 : LINE=(DU3h       ,DU3H,DQHX50     ,DU4h,DRFBH50,DU5h,&
                  DPSHX50     ,DU6h        ,DU7h)

  HXCEL13  : LINE=(DU1h,DU2h,USEGHX13,HXBRK13)
  HXCEL14  : LINE=(DU1h,DU2h,USEGHX14,HXBRK14)
  HXCEL15  : LINE=(DU1h,DU2h,USEGHX15,HXBRK15)
  HXCEL16  : LINE=(DU1h,DU2h,USEGHX16,HXBRK16)
  HXCEL17  : LINE=(DU1h,DU2h,USEGHX17,HXBRK17)
  HXCEL18  : LINE=(DU1h,DU2h,USEGHX18,HXBRK18)
  HXCEL19  : LINE=(DU1h,DU2h,USEGHX19,HXBRK19)
  HXCEL20  : LINE=(DU1h,DU2h,USEGHX20,HXBRK20)
  HXCEL21  : LINE=(DU1h,DU2h,DUSEGH21,HXBRK21) !empty with Q+RFBPM
  HXCEL22  : LINE=(DU1h,DU2h,USEGHX22,HXBRK22)
  HXCEL23  : LINE=(DU1h,DU2h,USEGHX23,HXBRK23)
  HXCEL24  : LINE=(DU1h,DU2h,USEGHX24,HXBRK24)
  HXCEL25  : LINE=(DU1h,DU2h,USEGHX25,HXBRK25)
  HXCEL26  : LINE=(DU1h,DU2h,USEGHX26,HXBRK26)
  HXCEL27  : LINE=(DU1h,DU2h,USEGHX27,HXBRK27)
  HXCEL28  : LINE=(DU1h,DU2h,DUSEGH28,HXBRK28) !empty with Q+RFBPM
  HXCEL29  : LINE=(DU1h,DU2h,USEGHX29,HXBRK29)
  HXCEL30  : LINE=(DU1h,DU2h,USEGHX30,HXBRK30)
  HXCEL31  : LINE=(DU1h,DU2h,USEGHX31,HXBRK31)
  HXCEL32  : LINE=(DU1h,DU2h,USEGHX32,HXBRK32)
  HXCEL33  : LINE=(DU1h,DU2h,USEGHX33,HXBRK33)
  HXCEL34  : LINE=(DU1h,DU2h,USEGHX34,HXBRK34)
  HXCEL35  : LINE=(DU1h,DU2h,USEGHX35,HXBRK35)
  HXCEL36  : LINE=(DU1h,DU2h,USEGHX36,HXBRK36)
  HXCEL37  : LINE=(DU1h,DU2h,USEGHX37,HXBRK37)
  HXCEL38  : LINE=(DU1h,DU2h,USEGHX38,HXBRK38)
  HXCEL39  : LINE=(DU1h,DU2h,USEGHX39,HXBRK39)
  HXCEL40  : LINE=(DU1h,DU2h,USEGHX40,HXBRK40)
  HXCEL41  : LINE=(DU1h,DU2h,USEGHX41,HXBRK41)
  HXCEL42  : LINE=(DU1h,DU2h,USEGHX42,HXBRK42)
  HXCEL43  : LINE=(DU1h,DU2h,USEGHX43,HXBRK43)
  HXCEL44  : LINE=(DU1h,DU2h,USEGHX44,HXBRK44)
  HXCEL45  : LINE=(DU1h,DU2h,USEGHX45,HXBRK45)
  HXCEL46  : LINE=(DU1h,DU2h,USEGHX46,HXBRK46) !no phase shifter
  HXCEL47  : LINE=(DU1h,DU2h,DUSEGH47,HXBRK47) !empty
  HXCEL48  : LINE=(DU1h,DU2h,DUSEGH48,HXBRK48) !empty
  HXCEL49  : LINE=(DU1h,DU2h,DUSEGH49,HXBRK49) !empty with RFBPM
  HXCEL50  : LINE=(DU1h,DU2h,DUSEGH50,HXBRK50) !empty

  HXRCL    : LINE=(&
    HXCEL13,HXCEL14,HXCEL15,HXCEL16,HXCEL17,HXCEL18,HXCEL19,HXCEL20,&
    HXCEL21,HXCEL22,HXCEL23,HXCEL24,HXCEL25,HXCEL26,HXCEL27,HXCEL28,&
    HXCEL29,HXCEL30,HXCEL31,HXCEL32,HXCEL33,HXCEL34,HXCEL35,HXCEL36,&
    HXCEL37,HXCEL38,HXCEL39,HXCEL40,HXCEL41,HXCEL42,HXCEL43,HXCEL44,&
    HXCEL45,HXCEL46,HXCEL47,HXCEL48,HXCEL49,HXCEL50)

  HXR : LINE=(DU0h,DU8h,HXRSTART,HXRCL,RWWAKE5H,HXRTERM,DUE1a,RFBHX51,DUE1e,&
              ENDUNDH)

! ------------------------------------------------------------------------------
! HXR undulator exit section
! ------------------------------------------------------------------------------

! note: the below K-values are for SC beam; the settings for Cu beam are
! in the "LCLS2cu_main.mad8" file

  KQUE1 :=  0.169109252491 !UNDH_KQ4_USDS.xsif (Eu=8.0, KHXU=2.0)
  KQUE2 := -0.137840816238 !UNDH_KQ4_USDS.xsif (Eu=8.0, KHXU=2.0)

  QUE1 : QUAD, TYPE="3.25Q20", L=LQD/2, K1=KQUE1, APER=rQD
  QUE2 : QUAD, TYPE="3.25Q20", L=LQD/2, K1=KQUE2, APER=rQD

  TCX01 : LCAV, FREQ=XbandF, TYPE="TRANS_DEFL", L=1.0/2 !horizontal deflection
  TCX02 : LCAV, FREQ=XbandF, TYPE="TRANS_DEFL", L=1.0/2 !horizontal deflection

  DBKXDMPH : DRIF, L=LBKXDMP !placeholder for x-kicker in HXR dumpline

  DUE2c  : DRIF, L=DUE2cb[L]+(LQR-LQD)/2 !0.555
  DUE3a  : DRIF, L=0.375504 +(LQP-LQD)/2 +0.11496 -0.063643
  DUE3b  : DRIF, L=1.2630495 -0.11496 +0.063643
  DUE4   : DRIF, L=DUE4b[L] +(LQR-LQD)/2 !0.394
  DUE5a  : DRIF, L=0.477799 +(LQP-LQD)/2 +0.012675 -0.063653
  DUE5b  : DRIF, L=0.264 -0.012675 +0.063653
  DUE5c  : DRIF, L=0.2674

  XCUE2  : HKIC,TYPE="class-4"
  XCD3   : HKIC,TYPE="class-5"

  YCUE1  : VKIC,TYPE="class-4"
  YCD3   : VKIC,TYPE="class-5"

  BPMUE1 : MONI, TYPE="Stripline-13" !RFBUE1 : MONI, TYPE="@2,CavityS-1"
  BPMUE2 : MONI, TYPE="Stripline-13" !RFBUE2 : MONI, TYPE="@2,CavityS-1"
 !RFBUE3 : MONI, TYPE="@2,CavityS-1"

 !IMUNDO : IMON, TYPE="BCS toroid" !BCS toroid
 !IMBCS3 : IMON, TYPE="BCS toroid" !BCS toroid before dump bend (comparator with IMBCS4)

  MIMUNDO : MARK
  MIMBCS3 : MARK

  TRUE1  : INST, TYPE="@5" !Be foil inserter (THz) -- existing LCLS device
  SPTCX  : INST !XTCAV spoiler
  BTMQUE : INST !Burn-Through-Monitor
  BTM0   : INST !Burn-Through-Monitor behind the PCPM0

  PCTCX : ECOL, XSIZE=4.5E-3, YSIZE=4.5E-3 !XTCAV photon collimator (9 mm aperture)
  PCPM0 : ECOL, L=LPCPM, XSIZE=17.5E-3, YSIZE=17.5E-3

  PH31 : INST, TYPE="2805 MHz" !post-undulator phase measurement RF cavity (existing LCLS device)
  PH32 : INST, TYPE="2805 MHz" !post-undulator phase measurement RF cavity (existing LCLS device)
  PH33 : INST, TYPE="2604 MHz" !post-undulator phase measurement RF cavity (high rate)
  PH34 : INST, TYPE="2604 MHz" !post-undulator phase measurement RF cavity (high rate)

  UEbeg   : MARK
  UEend   : MARK
  VVTCX   : MARK !XTCAV vacuum valve
  VV36    : MARK !treaty-point vacuum valve just downbeam of undulator
  VV37    : MARK !vac. valve in dumpline
  MTCX01  : MARK !entrance to TCX01 (for ELEGANT WATCH point)
  MTCX    : MARK !centerline between TCX01 and TCX02 (for matching)
  DLSTART : MARK !start of dumpline
  MDLWALL : MARK !front face of dumpline thermal barrier wall

  QUE1_full : LINE=(QUE1,QUE1)
  QUE2_full : LINE=(QUE2,QUE2)

  UNDEXIT : LINE=(BEGDMPH_1,&
      UEbeg,DUE1d,VV36,DUE1b,MIMUNDO,DUE1c,DUE2a,YCUE1,&
      DUE2b,PH31,DUE2d,PH32,DUE2d,PH33,DUE2d,PH34,DUE2e,XCUE2,DUE2c,&
    QUE1_full,DUE3a,BPMUE1,DUE3b,TRUE1,DUE3c,DBKXDMPH,DUE3c,PCTCX,&
      DPCVV,VVTCX,DVVTCX,MTCX01,&
    2*TCX01,DTCX12,MTCX,DTCX12,&
    2*TCX02,DTCXSP,SPTCX,DUE4,&
    QUE2_full,DUE5a,BPMUE2,DUE5b,BTMQUE,DUE5c,PCPM0,DUE5f,BTM0,&
      DUE5d,MIMBCS3,DUE5e,MDLWALL,DDLWALL,UEend,DLstart,DSB0a,YCD3,DSB0b,&
      XCD3,DSB0c,VV37,DSB0d,DSB0e,&
    ENDDMPH_1)

  HXRUND : LINE=(HXR,UNDEXIT)

  RETURN
