C
C   $Id: ums_geom.F,v 1.13 2001/03/08 20:04:27 godang Exp $
C   $Author: godang $
C

      Subroutine ums_geom

      Implicit none

#include "ckm_media.inc"
#include "ckm_dim.inc"
#include "ckm_pos.inc"

c-----REMEMBER TO PUT ALL THE WIRE GEOMETRY PARAMETERS INTO A
C     COMMON BLOCK

c-----number of chambers----------------------------------------

      integer ncham, p, k, l, m    ! 3 upstreams + 3 downstreams
      parameter (ncham=6)

c-----number of He bags ----------------------------------------

      integer nbag                 ! 3-1-2 = 6 bags
      parameter (nbag=6)

c-----half x,y,z dimensions of each wire chamber

      real hdim_cham1(3),hdim_cham2(3),hdim_cham3(3),hdim_cham4(3),
     +     hdim_cham5(3),hdim_cham6(3)

c-------------------------------------------------------------------
c     01/29/01 We need to make a larger chamber so that it cover
c     all beam size with more wires on each chamber.
c     390 wires in x and 390 wires in x'
c     342 wires in u and 342 wires in u' 
c     342 wires in v and 342 wires in v' 
c     total wires/chamber = (390+342+342) x 2 = 2148 wires/chamber
c     total wires (UMS)   = 6 X 2148 = 12,888 
c-------------------------------------------------------------------

      integer nplane(ncham)
      integer nwires_x, nwires_x1        ! x, x'
      integer nwires_u, nwires_u1        ! u, u'
      integer nwires_v, nwires_v1        ! v, v'

      data nplane    /6,6,6,6,6,6/       ! x,x',u,u',v,v'
      data nwires_x  /390/
      data nwires_x1 /390/
      data nwires_u  /342/
      data nwires_u1 /342/
      data nwires_v  /342/
      data nwires_v1 /342/

c-----wire dimensions (in cm)---------------------------------------

      real wire_d     /12e-4/     ! sense wire diameter
      real wire_l     /28/        ! the length of wire (large chamber)
      real wire_pitch /.08/       ! pitch between wires
      real plane_sep  /.5/        ! separation between wire planes

c-----Helium bag dimension (in cm)----------------------------------

      real bag_r    /20/          ! tranverse dimension for now
      real bag_z    /200/         ! He bag length 

c-----Polyethylene dimension (in cm)--------------------------------

      real poly_r    /2.54e-3/    
      real poly_l    /200/        

c-----offset of each chamber relative to magnet centroid------------
c     the magnet BM105 is 6 ft long = 182.88 cm --> 184 cm
c     gap magnet and chamber = 50 cm
c     chamber 1 (C1) = -(92+50+4.8+200+4.8+200+2.4) = -554 cm
c     chamber 2 (C2) = -(92+50+4.8+200+2.4)         = -349.2 cm
c     chamber 3 (C3) = -(92+50+2.4)                 = -144.4 cm
c     chamber 4 (C4) =  (92+50+2.4)                 =  144.4 cm 
c     chamber 5 (C5) =  (92+50+4.8+200+2.4)         =  349.2 cm
c     chamber 6 (C6) =  (92+50+4.8+200+4.8+200+2.4) =  554 cm
c-------------------------------------------------------------------

      real offset(ncham)    
      data offset /-554.,-349.2,-144.4,144.4,349.2,554./

c------------------------------------------------------
c     3 He bags in front and 3 He bags in rear
c     (1 he bag for dogleg magnet) 
c     bag1 = (-700 )                  => -700 cm  
c     bag2 = -(92+50+4.8+200+4.8+100) => -451.6 cm
c     bag3 = -(92+50+4.8+100)         => -246.8 cm
c     bag4 =                          =>   0 cm 
c     bag5 = (92+50+4.8+100)          =>  246.8 cm
c     bag6 = (92+50+4.8+200+4.8+100)  =>  451.6 c
c------------------------------------------------------

      real offbag(nbag)    
      data offbag /-700.,-451.6,-246.8,0.,246.8,451.6/

c-----local variables---------------------------------- 
c
      integer I,ivolu
      real par(3),xpos,ypos,zpos,xoff,xbag,angle

      integer irotx90,irotz60,irotz300
      data irotx90  /2001/
      data irotz60  /2002/
      data irotz300 /2003/

c-----define 90 degree rotation around x-axis

      call gsrotm(irotx90,90.,0.,0.,0.,90.,-90.)

c-----define 60,300 degree rotations about the z-axis
c   01-20-01 change to 120 and -120 degree rotation
c      call gsrotm(irotz60,90.,60.,90.,150.,0.,0.)
c      call gsrotm(irotz300,90.,-60.,90.,30.,0.,0.)

      call gsrotm(irotz60,90.,120.,90.,210.,0.,0.)
      call gsrotm(irotz300,90.,-120.,90.,-30.,0.,0.)

c-----Start by making a single wire volume, put in w------------
c     The gas is isobutane:cf4 = 50%:50% 
c     WICH is sensewire volume for UMS 
c---------------------------------------------------------------

      par(1) = 0.             ! inner radius
      par(2) = wire_d/2       ! outer radius  
      par(3) = wire_l/2       ! half length of wire
      call gsvolu('WICH','TUBE',ckmmed_umswire,par,3,ivolu)  ! for sensewire

c
c define 6 wire plane volumes for chambers:
c
      par(1) = ((nwires_x+2)*wire_pitch)/2.     
      par(2) = wire_l/2.
      par(3) = plane_sep/2. - poly_r/2.      
      call gsvolu('PXX ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plane x
      call gsdvn('PLXX','PXX ',nwires_x,1)
      call gspos('WICH',1,'PLXX',0.,0.,0.,irotx90,'ONLY')

      par(1) = ((nwires_x1+2)*wire_pitch)/2.     
      call gsvolu('PX1 ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plane x'
      call gsdvn('PLX1','PX1 ',nwires_x1,1)
      call gspos('WICH',1,'PLX1',0.,0.,0.,irotx90,'ONLY')

      par(1) = ((nwires_u+2)*wire_pitch)/2.     
      call gsvolu('PUU ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plave u
      call gsdvn('PLUU','PUU ',nwires_u,1)
      call gspos('WICH',1,'PLUU',0.,0.,0.,irotx90,'ONLY')

      par(1) = ((nwires_u1+2)*wire_pitch)/2.     
      call gsvolu('PU1 ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plane u'
      call gsdvn('PLU1','PU1 ',nwires_u1,1)
      call gspos('WICH',1,'PLU1',0.,0.,0.,irotx90,'ONLY')

      par(1) = ((nwires_v+2)*wire_pitch)/2.     
      call gsvolu('PVV ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plane v
      call gsdvn('PLVV','PVV ',nwires_v,1)
      call gspos('WICH',1,'PLVV',0.,0.,0.,irotx90,'ONLY')

      par(1) = ((nwires_v1+2)*wire_pitch)/2.    
      call gsvolu('PV1 ','BOX ',ckmmed_butcf4,par,3,ivolu)  ! plane v'
      call gsdvn('PLV1','PV1 ',nwires_v1,1)
      call gspos('WICH',1,'PLV1',0.,0.,0.,irotx90,'ONLY')
      
c------------------------------------------------------------
c     Make a chamber volume of isobutane+cf4 big enough to 
c     contain rotated planes
c     (Make all chambers the same for now - EJR)
c     Increase the chamber frame size to be 60 cm  RG    
c     Increase the chamber frame size slightly to include all 
c     the mylar planes 1.913 -> 1.914  2/27/01
c------------------------------------------------------------

      par(1)=1.914*(nwires_u*wire_pitch+4.)/2.
      par(2)=par(1)
      par(3)=(nplane(1)+2)*plane_sep/2.   
      call gsvolu('UMSC','BOX ',ckmmed_butcf4,par,3,ivolu)
c
c-----Material for windows---------------------------------- 
c     Choose mylar for now

      par(3) = poly_r/2    
      call gsvolu('POLY','BOX ',ckmmed_mylar,par,3,ivolu)
c
c-----Make a helium bag volume of Helium-------------------- 
c     3 He bags in front and 2 He bags in rear 

      par(1)=0.
      par(2)=bag_r
      par(3)=bag_z/2.   
      call gsvolu('HEBG','TUBE ',ckmmed_helium,par,3,ivolu) ! for He bags

c-----insert the layers and cathode planes into the chamber

      call gspos('POLY',1,'UMSC',0.,0.,-4.*plane_sep + poly_r/2,0,
     +                                               'ONLY')
      call gspos('POLY',2,'UMSC',0.,0.,-3.5*plane_sep,0,'ONLY')
      do i=-3,3
       call gspos('POLY',i+6,'UMSC',0.,0.,i*plane_sep,0,'ONLY')
      end do
      call gspos('POLY',10,'UMSC',0.,0.,+3.5*plane_sep,0,'ONLY')
      call gspos('POLY',11,'UMSC',0.,0.,+4.*plane_sep - poly_r/2,0,
     +                                                'ONLY')

      zpos = -2.5*plane_sep       
      call gspos('PXX ',1,'UMSC',+wire_pitch/4.,0.,zpos,0,'ONLY')

      zpos = -1.5*plane_sep
      call gspos('PX1 ',1,'UMSC',-wire_pitch/4.,0.,zpos,0,'ONLY')

      angle = 60.0*(3.14159/180.0)
      xpos  =  -0.25*wire_pitch*cos(angle)
      ypos  =  +0.25*wire_pitch*sin(angle)
      zpos  = -.5*plane_sep
      call gspos('PUU ',1,'UMSC',xpos,ypos,zpos,irotz60,'ONLY')

      xpos  =  +0.25*wire_pitch*cos(angle)
      ypos  =  -0.25*wire_pitch*sin(angle)
      zpos  =  .5*plane_sep
      call gspos('PU1 ',1,'UMSC',xpos,ypos,zpos,irotz60,'ONLY')

      xpos  =  -0.25*wire_pitch*cos(angle)
      ypos  =  -0.25*wire_pitch*sin(angle)
      zpos  = 1.5*plane_sep
      call gspos('PVV ',1,'UMSC',xpos,ypos,zpos,irotz300,'ONLY')

      xpos  =  +0.25*wire_pitch*cos(angle)
      ypos  =  +0.25*wire_pitch*sin(angle)
      zpos  = 2.5*plane_sep
      call gspos('PV1 ',1,'UMSC',xpos,ypos,zpos,irotz300,'ONLY')

c-----define 2 degree rotations about the y-axis---------------------
c      call gsrotm(4001,135.,0.,90.,90.,45.,0.)
c-----insert chambers into CKM  (put first 3 chambers at X offset 
c     to center on incoming beam:  .77 GeV/22  *  700 cm = 24.6  
c     see gufld.F for magnetic field)
c     to make a bend angle exactly 2 degree with fixed z coordinate
c     for the first chamber then we need to make xoff(1) = -19.346
c     then    xoff = 24.6*offset(i)/700 -> xoff = 24.444*offset(i)/700
c---------------------------------------------------------------------

      do i=1,ncham
       if(i.le.3)then
         xoff = 24.444*offset(i)/700.
         call gspos('UMSC',i,'CKM ',xoff,0.,
     &              ucham_z+offset(i),0,'ONLY')
       else
         call gspos('UMSC',i,'CKM ',0.,0.,ucham_z+offset(i),0,'ONLY')
       end if
      end do
c
c  Now put magnetic fields in:  (put at ucham_z and ucham_z-700. 
c  for now.  EJR 10/30)
c  RG magnet size is 89 cm in x and y direction 
c
      par(1)=40.
      par(2)=40.
      par(3)=92.
      call gsvolu('UMAG','BOX ',ckmmed_magair,par,3,ivolu)
      call gspos('UMAG',1,'CKM ',-24.444,0.,ucham_z-700.,0,'ONLY')
      call gspos('UMAG',2,'CKM ',0.,0.,ucham_z,0,'ONLY')

c----------------------------------------------------
c  Finally insert He bags into UMS. HEBG is for He bags
c  add 1 He bag within the dogleg magnet, 
c  we have 6 identical He bags for UMS.
c----------------------------------------------------

      do i=1,nbag
       if(i.le.3)then
         xbag = 24.444*offset(i)/700.
         call gspos('HEBG',i,'CKM ',xbag,0.,ucham_z+offbag(i),0,'ONLY')
       else if(i.eq.4)then
         call gspos('HEBG',i,'CKM ',0.,0.,ucham_z,0,'ONLY')
       else if(i.gt.4)then 
         call gspos('HEBG',i,'CKM ',0.,0.,ucham_z+offbag(i),0,'ONLY')
       end if
      end do


      Return

      End

C
C   $Log: ums_geom.F,v $
C   Revision 1.13  2001/03/08 20:04:27  godang
C   shift the first and last layers of mylar by poly_r/2
C
C   Revision 1.12  2001/03/01 18:26:39  godang
C   fixed the sense wire diameter
C
C   Revision 1.11  2001/02/27 23:15:56  godang
C   increase UMSC chamber frame size slightly
C
C   Revision 1.10  2001/02/12 16:50:22  godang
C   updated the length of wires and some comments
C
C   Revision 1.9  2001/02/01 22:48:30  godang
C   changed the number of wires and chamber frame
C
C   Revision 1.8  2001/01/19 15:29:24  jurgen
C   Change 0. to 0, (Linux complain)
C
C   Revision 1.7  2001/01/18 16:05:21  ramberg
C   changed window volume to mylar
C
C   Revision 1.6  2001/01/17 04:49:22  godang
C   added cathode planes into the chamber & calculate wire hit - by Erik & Romulus
C
C   Revision 1.5  2000/12/27 02:50:23  jurgen
C   change 0. to 0 in gspos call.
C
C   Revision 1.4  2000/12/20 00:53:14  jurgen
C   re-fixed overwritten changes by ramberg
C
C   Revision 1.3  2000/12/19 23:06:29  ramberg
C   Added Helium bags between chambers
C
C   Revision 1.2  2000/11/07 17:46:20  jurgen
C   remove declaration of ucham_z. Already declared in ckm_pos.inc
C
C   Revision 1.1  2000/11/03 10:50:51  ramberg
C   adding files for UMS system - written by Romulus Godang and Erik Ramberg
C

