anchor.diff:  
     ! Note: Not in firstrun
     if(dowrite_tracks) then
  -    gammax = (one + talphax(ie)**2)/tbetax(ie)
  -    gammay = (one + talphay(ie)**2)/tbetay(ie)
  -
  -    do j=1,napx
  -      xj  = (xv1(j)-torbx(ie)) /c1e3
  -      xpj = (yv1(j)-torbxp(ie))/c1e3
  -      yj  = (xv2(j)-torby(ie)) /c1e3
  -      ypj = (yv2(j)-torbyp(ie))/c1e3
  -
  -      if(xj <= zero) then
  -        xdisp = xj + (((pj-c_enom)/c_enom)*2.5_fPrec)*sqrt(tbetax(ie)/c180e0)
  -      else
  -        xdisp = xj - (((pj-c_enom)/c_enom)*2.5_fPrec)*sqrt(tbetax(ie)/c180e0)
  -      end if
  -
  -    ! nspxd = sqrt(abs(gammax*xdisp**2 +  two*talphax(ie)*xdisp*xpj + tbetax(ie)*xpj**2)/c_emitx0_collgap) ! Not used
  -      nspx  = sqrt(abs(gammax*xj**2    + two*talphax(ie)*xj*xpj     + tbetax(ie)*xpj**2)/c_emitx0_collgap)
  -      nspy  = sqrt(abs(gammay*yj**2    + two*talphay(ie)*yj*ypj     + tbetay(ie)*ypj**2)/c_emity0_collgap)
  -
  -      if(part_abs_pos(j) == 0 .and. part_abs_turn(j) == 0) then
  -        ! Here we apply the same kind of cut than the sigsecut parameter
  -        if(nhit_type(j) > 0 .and. xv1(j) < 99.0_fPrec .and. xv2(j) < 99.0_fPrec .and. ( &
  -            (((xv1(j)*c1m3)**2 / (tbetax(ie)*c_emitx0_collgap)) >= sigsecut2).or. &
  -            (((xv2(j)*c1m3)**2 / (tbetay(ie)*c_emity0_collgap)) >= sigsecut2).or. &
  -            (((xv1(j)*c1m3)**2 / (tbetax(ie)*c_emitx0_collgap)) +                 &
  -            ( (xv2(j)*c1m3)**2 / (tbetay(ie)*c_emity0_collgap)) >= sigsecut3))    &
  -          ) then
  -#ifdef HDF5
  -          if(h5_writeTracks2) then
  -            call h5tr2_writeLine(partID(j),iturn,dcum(ie),xv1(j),yv1(j),xv2(j),yv2(j),&
  -              (ejv(j)-c_enom)/c_enom,nhit_type(j))
  -          else
  -#endif
  -            write(coll_tracksUnit,"(i8,1x,i8,1x,f10.2,4(1x,e12.5),1x,e11.3,1x,i4)") partID(j),iturn,dcum(ie), &
  -              xv1(j),yv1(j),xv2(j),yv2(j),(ejv(j)-c_enom)/c_enom,nhit_type(j)
  -#ifdef HDF5
  -          end if
  -#endif
  -        end if
  -      end if
  -    end do
  +    call coll_writeTracks2(2)
     end if
   
   end subroutine collimate_end_element
  
positive.code:         end if
  #endif
        end if
  
        ! Particle has impacted
        if(part_abs_pos(j) /= 0 .and. part_abs_turn(j) /= 0) then
          if(dowrite_impact) then
  
negative.code: 
  end subroutine coll_getMinGapID
  
  ! ================================================================================================ !
  !  WRITE TO FILES
  ! ================================================================================================ !
  
  ! ================================================================================================ !
  !  Writing of tracks2.dat
  ! ================================================================================================ !
  subroutine coll_writeTracks2(iMode)
  
    use coll_db
    use coll_common
    use mod_common
    use mod_common_main
    use mod_common_track
  #ifdef HDF5
    use hdf5_output
    use hdf5_tracks2
  #endif
  
    integer, intent(in) :: iMode
  
    integer j
    real(kind=fPrec) sigX2, sigY2, halfLen
    real(kind=fPrec) xj, xpj, yj, ypj, sj, pj
    real(kind=fPrec) xk, xpk, yk, ypk, sk
  
    sigX2   = tbetax(ie)*c_emitx0_collgap
    sigY2   = tbetay(ie)*c_emity0_collgap
    halfLen = half*cdb_cLength(icoll)
  
    if(iMode == 1) then
  
      sj = dcum(ie) - halfLen
      sk = dcum(ie) + halfLen
  
      do j=1,napx
        if(                                                                                 &
          part_hit_pos(j) == ie .and. part_hit_turn(j) == iturn .and.                       &
          part_abs_pos(j) == 0  .and. part_abs_turn(j) == 0     .and.                       &
          nhit_type(j)     > 0  .and. xv1(j) < 99.0_fPrec .and. xv2(j) < 99.0_fPrec .and. ( &
            (xv1(j)*c1m3)**2 / sigX2 >= sigsecut2 .or.                                      &
            (xv2(j)*c1m3)**2 / sigY2 >= sigsecut2 .or.                                      &
            (xv1(j)*c1m3)**2 / sigX2 + (xv2(j)*c1m3)**2 / sigY2 >= sigsecut3                &
          )                                                                                 &
        ) then
  
          xpj = rcxp0(j)*c1e3 + torbxp(ie)
          xj  =  rcx0(j)*c1e3 +  torbx(ie) - halfLen*xpj
          ypj = rcyp0(j)*c1e3 + torbyp(ie)
          yj  =  rcy0(j)*c1e3 +  torby(ie) - halfLen*ypj
          xk  = xv1(j) + halfLen*yv1(j)
          xpk = yv1(j)
          yk  = xv2(j) + halfLen*yv2(j)
          ypk = yv2(j)
          pj  = (ejv(j) - c_enom)/c_enom
  
  #ifdef HDF5
          if(h5_writeTracks2) then
            call h5tr2_writeLine(partID(j),iturn,sj,xj,xpj,yj,ypj,pj,nhit_type(j))
            call h5tr2_writeLine(partID(j),iturn,sk,xk,xpk,yk,ypk,pj,nhit_type(j))
          else
  #endif
            write(coll_tracksUnit,"(i8,1x,i8,1x,f10.2,4(1x,e12.5),1x,e11.3,1x,i4)") &
              partID(j),iturn,sj,xj,xpj,yj,ypj,pj,nhit_type(j)
            write(coll_tracksUnit,"(i8,1x,i8,1x,f10.2,4(1x,e12.5),1x,e11.3,1x,i4)") &
              partID(j),iturn,sk,xk,xpk,yk,ypk,pj,nhit_type(j)
  #ifdef HDF5
          end if
  #endif
        end if
      end do
  
    elseif(iMode == 2) then ! End of element record
  
      do j=1,napx
        if(                                                                                &
          part_abs_pos(j) == 0 .and. part_abs_turn(j) == 0 .and.                           &
          nhit_type(j)     > 0 .and. xv1(j) < 99.0_fPrec .and. xv2(j) < 99.0_fPrec .and. ( &
            (xv1(j)*c1m3)**2 / sigX2 >= sigsecut2 .or.                                     &
            (xv2(j)*c1m3)**2 / sigY2 >= sigsecut2 .or.                                     &
            (xv1(j)*c1m3)**2 / sigX2 + (xv2(j)*c1m3)**2 / sigY2 >= sigsecut3               &
          )                                                                                &
        ) then
  
  #ifdef HDF5
          if(h5_writeTracks2) then
            call h5tr2_writeLine(partID(j),iturn,dcum(ie),xv1(j),yv1(j),xv2(j),yv2(j),&
              (ejv(j)-c_enom)/c_enom,nhit_type(j))
          else
  #endif
            write(coll_tracksUnit,"(i8,1x,i8,1x,f10.2,4(1x,e12.5),1x,e11.3,1x,i4)") partID(j),iturn,&
              dcum(ie),xv1(j),yv1(j),xv2(j),yv2(j),(ejv(j)-c_enom)/c_enom,nhit_type(j)
  #ifdef HDF5
          end if
  #endif
        end if
      end do
  
    end if
  
  end subroutine coll_writeTracks2
  
  #ifdef HDF5
  subroutine coll_hdf5_writeCollScatter(icoll,iturn,ipart,nabs,dp,dx,dy)
  
  