	subroutine get_deform_one(igroup,ncyc,
     +                          xras,yras,zras,rotZras)
	include 'Online.cmm'
        real xras(0:3)
        real yras(0:3)
        real zras(0:3)
        real rotZras(0:3)
        real Z(3)
        Z(1)=487.
        Z(2)=987.
        Z(3)=1080.
        nch=igroup

        tec=23.0E-6
c	Now all relevant in-plane data sits in xras, yras,
c	zras and rotZras. Now it really starts!

c	Torsion of chamber, expressed in relative rotation
c	around chamber X axis of the outer crossplates

	sum=0.
	do ns=0,3
	  sum=sum+rotZras(ns)
	enddo
	torque(nch,ncyc)=1000.*sum/4. ! torque in mrad
	
c	sag of MSK cross plate
	alf1=rotZras(0)+rotZras(1)-rotZras(2)-rotZras(3)
	c1=(z(1)+Z(2))/Z(3)
	alfmsk=-alf1/(2.*c1)
	sagmsk(nch,ncyc)=z(3)*alfmsk*2.
	
c	sag of CCD cross plates
	c1=z(1)/z(2)
	alf1=c1*rotZras(0)-rotZras(1)+rotZras(2)-c1*rotZras(3)
	c2=(z(2)/z(3)+(z(1)*z(2))/(z(2)*z(3)))
	alfccd=alf1/(2.*c2)
	sagccd(nch,ncyc)=z(3)*alfccd*2.

        sagcommon=-(rotZras(1)-rotZras(2))*1.25/2./2./2.*1080.
C         correction for maskpos, two for mask+CCD opposite, two for 1+2, two f cos 
        sagdiff=-(rotZras(0)-rotZras(3))/2./2.*1080. ! two for 0+3, two for cos
        
	sagccd(nch,ncyc)=sagcommon+sagdiff*0.5
	sagmsk(nch,ncyc)=sagcommon-sagdiff*0.5
        yshift=sagcommon !imageshift msk1,2 for middleXp

	
c	sag of LEN cross plate
       
	y1=yras(0)-yras(1)-yras(2)+yras(3)
	c1=Z(1)**2-Z(2)**2
	c2=2.*c1/(Z(3)**2)*sagmsk(nch,ncyc)
	c3=Z(2)**2-((Z(1)-Z(2))/2)**2
	c4=4.*c3/(Z(3)**2)
	saglen(nch,ncyc)=(y1-c2)/c4

        y1=   yras(0)/2.-(yras(1)/2.-0.5*yshift)
     +       -(yras(2)/2.-0.5*yshift)+yras(3)/2.
C /two for normal imageshift., 0.5 from 1,2 to centr, yshift for common sag
	saglen(nch,ncyc)=-y1*2./2. + sagcommon
C  two for 1,2 to centre, two for 0-1 + 3-2
	
c	chamber sag
	sum=0.
	do ns=0,3
	  sum=sum+yras(ns)
	enddo
	chasag(nch,ncyc)=sum/8.

	
c	chamber sag in Z direction (CCS):
	dx=xras(0)+xras(1)+xras(2)+xras(3)
	chasgz(nch,ncyc)=dx/8.

c	Tmsk - Tlen:
	temmsk(nch,ncyc)=
     &  -(xras(0)+xras(1)-xras(2)-xras(3))/
     &  (2.*tec*(Z(1)+Z(2)))
	
c	TCCD - Tlen
	dx=xras(0)-(Z(2)/Z(1))*(xras(1)-xras(2))-xras(3)
	den=2.*Z(2)*tec*(1.+(Z(2)/Z(1)))
	temccd(nch,ncyc)=-dx/den	

c	middle cross plate rotX (CCS):
	dy=yras(0)-yras(3)
	roxlen(nch,ncyc)=1000.*dy/(4.*Z(2))

c	torque from dy:
	dy=yras(1)-yras(2)
	c1=2.*roxlen(nch,ncyc)*(Z(2)-Z(1))/1000.
	torqdy(nch,ncyc)=-1000.*(dy+c1)/(Z(1)+Z(2))
	end
        
	  
      subroutine shift(igroup,xx,yy,zz,xuse,yuse,zuse)
      implicit none
        common/var1/torque,sagmsk
        common/var2/sagccd,saglen
        common/var3/chasag,chasgz
        common/var4/temccd,temmsk
        common/var5/roxlen,torqdy

       real torque(5,1000),sagmsk(5,1000)
       real sagccd(5,1000),saglen(5,1000)
       real chasag(5,1000),chasgz(5,1000)
       real temccd(5,1000),temmsk(5,1000)
       real roxlen(5,1000),torqdy(5,1000)

      real  xx,yy,zz,xuse,yuse,zuse
      integer igroup
      double precision axb1,axl1,axv1,dy1,dz1,ay1,az1,sagy1,sagz1
      double precision axb2,axl2,axv2,dy2,dz2,ay2,az2,sagy2,sagz2
      double precision axb3,axl3,axv3,dy3,dz3,ay3,az3,sagy3,sagz3
      double precision tn,ttn,stn,ctn,tn1,tn2,tn3
      double precision L1,L2,L3
      double precision xc1,xc2,xc3,yc1,yc2,yc3,zc1,zc2,zc3
      double precision wxb1,wxl1,wxv1,gx1
      double precision wxb2,wxl2,wxv2,gx2
      double precision wxb3,wxl3,wxv3,gx3
      double precision wxay1,wxay2,wxay3
      double precision wxsz1,wxsz2,wxsz3
      double precision wxaz1,wxaz2,wxaz3
      double precision wxsy1,wxsy2,wxsy3
      double precision hcham,dtemp
      double precision wxl,wxv
      double precision wxl_a,wxl_b,dz_b,dz_a,a,b
      double precision wxv_a,wxv_b,dy_b,dy_a
      double precision wzl1,wzl2,wzl3,wyl1,wyl2,wyl3
      double precision wzv1,wzv2,wzv3,wyv1,wyv2,wyv3
      double precision ts,xs,ys,zs,tnom,amdt,arpc,xn,yn,zn
      double precision ysagro,ysaghv,ysagln,ysagz,ysagn
      
      integer i
      hcham=50.

      axb3=0. ! overall angle
      axl3=roxlen(igroup,1)/1000.
      axv3=torqdy(igroup,1)/1000. 
      ay3=0.
      az3=0.
      dy3=0.
      dz3=0.
      sagz3=chasgz(igroup,1)
      sagy3=-chasag(igroup,1)
      dtemp=0.
      ysagro=sagmsk(igroup,1) 
      ysaghv=sagccd(igroup,1) 
      ysagln=saglen(igroup,1) 

      xc3=0.
      zc3=0.
      yc3=0.
      yn=yy*1000.
      zn=zz*1000.
      xn=xx*1000.
c      tn=atan((yn-yn)/(zn-zn))
      tn=0.
      tnom=tn
      ttn=tan(tn)
      stn=sin(tn)
      ctn=cos(tn)

      include 'wxfunc.f'





      zs=zn+dz3+ay3*(xn-xc3) -
     +      (axb3*wxb3)*(yn-yc3)
     +      -(axl3*wzl3+axv3*wzv3)
     +      +sagz3*gx3
      ys=yn+dy3+az3*(xn-xc3)+axb3*(zn-zc3)*wxb3+
     +            axl3*wyl3+axv3*wyv3+sagy3*gx3

C Xplate sag
             ysagro=ysagro*(1.-(zn/1000.)**2)
             ysaghv=ysaghv*(1.-(zn/1000.)**2)
             ysagln=ysagln*(1.-(zn/1000.)**2)
             if (igroup.eq.2 .or. igroup.eq.3) then
                ys=yn-(ys-yn)
                ysagro=-ysagro
                ysaghv=-ysaghv
                ysagln=-ysagln
             endif
             ysagz=ysagro
             ysagn=ysaghv
             if (igroup.eq.2 .or. igroup.eq.4) then
                ysagz=ysaghv
                ysagn=ysagro
             endif
             if (xn.gt.0.) then
                ys=ys+ysagn*(xn/2500.)**2
             else 
                ys=ys+ysagz*(xn/2500.)**2
             endif
             ys=ys+ysagln*(1.-(xn/2500.)**2)
	     




      tn3=-2.05

      ts=tn3+axb3*wxb3+axl3*wxl3+axv3*wxv3+ay3*wxay3+
     +     sagz3*wxsz3+az3*wxaz3+sagy3*wxsy3


      xs=xn
      xuse=xs
      yuse=ys 
      zuse=zs
      end

      double precision function wxl(x,L)
      implicit none
      double precision x,L
      wxl=((-abs(x)+L/2)/(L/2))
      end

      double precision function wxv(x,L)
      implicit none
      double precision x,L
      wxv=0.5*((-x+L/2)/(L/2))-0.5*((x+L/2)/(L/2))
      end

