anchor.diff:      write(outlun,*) 'Optics beta y [m]:   ', tbetay(ie)
     end if
   
  -!-------------------------------------------------------------------
  -!++  Calculate aperture of collimator
  -!JUNE2005   HERE ONE HAS TO HAVE PARTICULAR TREATMENT OF THE OPENING OF
  -!JUNE2005   THE PRIMARY COLLIMATOR OF RHIC
  -  nsig = nsig + gap_rms_error(icoll)
  -  xmax = nsig*sqrt(bx_dist*c_emitx0_collgap)
  -  ymax = nsig*sqrt(by_dist*c_emity0_collgap)
  +  ! Calculate aperture of collimator
  +  nsig        = nsig + gap_rms_error(icoll)
  +  c_rotation  = cdb_cRotation(icoll)
  +  c_length    = cdb_cLength(icoll)
  +  c_offset    = cdb_cOffset(icoll)
  +  c_tilt(:)   = cdb_cTilt(:,icoll)
  +
  +  xmax        = nsig*sqrt(bx_dist*c_emitx0_collgap)
  +  ymax        = nsig*sqrt(by_dist*c_emity0_collgap)
     xmax_pencil = (nsig+pencil_offset)*sqrt(bx_dist*c_emitx0_collgap)
     ymax_pencil = (nsig+pencil_offset)*sqrt(by_dist*c_emity0_collgap)
  -  xmax_nom   = cdb_cNSig(icoll)*sqrt(cdb_cBx(icoll)*c_emitx0_collgap)
  -  ymax_nom   = cdb_cNSig(icoll)*sqrt(cdb_cBy(icoll)*c_emity0_collgap)
  -  c_rotation = cdb_cRotation(icoll)
  -  c_length   = cdb_cLength(icoll)
  -  c_offset   = cdb_cOffset(icoll)
  -  c_tilt(1)  = cdb_cTilt(1,icoll)
  -  c_tilt(2)  = cdb_cTilt(2,icoll)
  +  xmax_nom    = cdb_cNSig(icoll)*sqrt(cdb_cBx(icoll)*c_emitx0_collgap)
  +  ymax_nom    = cdb_cNSig(icoll)*sqrt(cdb_cBy(icoll)*c_emity0_collgap)
  +
     cry_proc(:) = -1
   
  -  calc_aperture   = sqrt(xmax**2 * cos_mb(c_rotation)**2 + ymax**2 * sin_mb(c_rotation)**2)
  -  nom_aperture    = sqrt(xmax_nom**2 * cos_mb(c_rotation)**2 + ymax_nom**2 * sin_mb(c_rotation)**2)
  -! pencil_aperture = sqrt(xmax_pencil**2 * cos_mb(c_rotation)**2+ ymax_pencil**2 * sin_mb(c_rotation)**2) ! Not in use
  -
  -!++  Get x and y offsets at collimator center point
  +  calc_aperture = sqrt(xmax**2 * cos_mb(c_rotation)**2 + ymax**2 * sin_mb(c_rotation)**2)
  +  nom_aperture  = sqrt(xmax_nom**2 * cos_mb(c_rotation)**2 + ymax_nom**2 * sin_mb(c_rotation)**2)
  +
  +  ! Get x and y offsets at collimator center point
     x_pencil(icoll) = xmax_pencil * (cos_mb(c_rotation))
     y_pencil(icoll) = ymax_pencil * (sin_mb(c_rotation))
   
  -!++  Get corresponding beam angles (uses xp_max)
  -  xp_pencil(icoll) = -one * sqrt(c_emitx0_collgap/tbetax(ie))*talphax(ie)* xmax / sqrt(c_emitx0_collgap*tbetax(ie))
  -  yp_pencil(icoll) = -one * sqrt(c_emity0_collgap/tbetay(ie))*talphay(ie)* ymax / sqrt(c_emity0_collgap*tbetay(ie))
  +  ! Get corresponding beam angles (uses xp_max)
  +  xp_pencil(icoll) = -one*sqrt(c_emitx0_collgap/tbetax(ie))*talphax(ie)*xmax / sqrt(c_emitx0_collgap*tbetax(ie))
  +  yp_pencil(icoll) = -one*sqrt(c_emity0_collgap/tbetay(ie))*talphay(ie)*ymax / sqrt(c_emity0_collgap*tbetay(ie))
     xp_pencil0 = xp_pencil(icoll)
     yp_pencil0 = yp_pencil(icoll)
   
     pencil_dx(icoll) = sqrt(xmax_pencil**2 * cos_mb(c_rotation)**2 + ymax_pencil**2 * sin_mb(c_rotation)**2)-calc_aperture
   
  -!++ TW -- tilt for of jaw for pencil beam
  -!++ as in Ralphs orig routine, but not in collimate subroutine itself
  -!            nprim = 3
  -!            if ( (icoll.eq.ipencil) &
  -!     &           icoll.le.nprim .and. (j.ge.(icoll-1)*nev/nprim)        &
  -!     &           .and. (j.le.(icoll)*nev/nprim))) then
  -! this is done for every bunch (64 particle bucket)
  -! important: Sixtrack calculates in "mm" and k2coll_collimate in "m"
  -! therefore 1E-3 is used to
  -
  -! RB: added condition that pencil_distr.ne.3 in order to do the tilt
  -  if((icoll.eq.ipencil).and.(iturn.eq.1).and. (pencil_distr.ne.3)) then
  -!!               write(*,*) " ************************************** "
  -!!               write(*,*) " * INFO> seting tilt for pencil beam  * "
  -!!               write(*,*) " ************************************** "
  -
  -!! respects if the tilt symmetric or not, for systilt_antiymm c_tilt is
  -!! -systilt + rmstilt otherwise +systilt + rmstilt
  -!!               if (systilt_antisymm) then
  -!! to align the jaw/pencil to the beam always use the minus regardless which
  -!! orientation of the jaws was used (symmetric/antisymmetric)
  +  ! Added condition that pencil_distr /= 3 in order to do the tilt
  +  if(icoll == ipencil .and. iturn == 1 .and. pencil_distr /= 3) then
  +    ! To align the jaw/pencil to the beam always use the minus regardless which
  +    ! orientation of the jaws was used (symmetric/antisymmetric)
       c_tilt(1) = c_tilt(1) +    (xp_pencil0*cos_mb(c_rotation) + sin_mb(c_rotation)*yp_pencil0)
       c_tilt(2) = c_tilt(2) -one*(xp_pencil0*cos_mb(c_rotation) + sin_mb(c_rotation)*yp_pencil0)
       write(lout,*) "INFO> Changed tilt1  ICOLL  to  ANGLE  ", icoll, c_tilt(1)
       write(lout,*) "INFO> Changed tilt2  ICOLL  to  ANGLE  ", icoll, c_tilt(2)
     end if
  -!++ TW -- tilt angle changed (added to genetated on if spec. in fort.3)
   
     ! Extract number of jaw fit slices for this collimator
     if(cdb_cSliced(icoll) > 0) then ! Collimator is sliced
  
positive.code: 
  end subroutine coll_endTurn
  
  ! ================================================================================================ !
  !  Find the smallest gap, and also write sigmasettings.out
  !  Updated: 2019-10-10
  
negative.code:   use geant4
  #endif
  
    real(kind=fPrec), intent(in) :: stracki
  
    integer j, iSlice, nSlices
    logical onesided, linside(napx), isAbs, isHit
    real(kind=fPrec) jawLength,jawAperture,jawOffset,jawTilt(2),x_Dump,xpDump,y_Dump,ypDump,s_Dump,   &
      xmax,ymax,calc_aperture,zpj,xmax_pencil,ymax_pencil,xmax_nom,ymax_nom,nom_aperture,     &
      scale_bx,scale_by,c_tilt(2),c_offset,c_aperture,c_rotation,cry_bendangle,cry_tilt,cry_tilt0
  
  #ifdef G4COLLIMATION
  