*-- Author :    Elena Taddei-Sergio Bottai 09/08/2005
      
	SUBROUTINE GPUCOLLECTION(np,nt)

***************************************************************************
*                                                                         *
*   				                                          *
*								          *
*      Here we calculate the signal appearing on each ADC channel         *
*      starting from the charge collected on every strip of the silicon   *
*      sensor. Noise is kept from data of beam test. Capacitive coupling  *
*      between strips and energy to ADC calibration are introduced too.   *
*									  *
*                                                                         *
* Called by: GPDSPE                                        		  *
*                                                                         *
***************************************************************************

#include "gpstripspe.inc" 

        dimension stripqxtanti(nstripx)
        dimension stripqytanti(nstripy)
	



	
********zeroing**************

 	do i=1,nstripx
            stripqxtanti(i)=0.
        enddo
	do i=1,nstripy
           stripqytanti(i)=0.
        enddo
	
***************************
        if(NT.eq.1.or.NT.eq.4) LADD=1
        if(NT.eq.2.or.NT.eq.5) LADD=2
        if(NT.eq.3.or.NT.eq.6) LADD=3	
	
*
*      Odd strip, X side --> charge shared between adjacent strips
*      EFFCOUPL          --> coupling efficiency (capacitance to backplane =0?)
*      SHIFTMASK	 --> asymmetry in charge division on X view
*                            different in each sensor (mask)
	
	 
        do j=(1+8),(nstripx-7),2
      		proxtanti(np,nt,j-1)=
     +  	proxtanti(np,nt,j-1)+
     +          0.5*effcoupl*proxtanti(np,nt,j)*(1.-shiftmask) 

         	proxtanti(np,nt,j+1)=
     +  	proxtanti(np,nt,j+1)+
     +          0.5*effcoupl*proxtanti(np,nt,j)*(1.+shiftmask)
	    proxtanti(np,nt,j)=0.
         enddo	


*
*      Capacitive coupling: a fraction of the charge arriving at one strip
*      appears at the output of the neighbouring amplifiers
*      This fraction is about Cis/Cdec, where Cis is the interstrip 
*      capacitance and Cdec is the decoupling capacitance (see Turchetta)
*      An algorithm has been chosen that is precise for low values of the 
*      capacitive coupling, i.e. if the INDUCED charge on a given strip 
*      is little compared with the COLLECTED one.
*


******************** X side *************************
			    
	c=cistrx/cdec
        c2=cistrx2/cdec
	



	do j=8,nstripx-6,2	 ! only even strips on X side
            jm2=1
            IF(j-2.lt.8) jm2=0
            jp2=1
            IF(j+2.gt.(nstripx-6)) jp2=0
            jm4=1
            IF(j-4.lt.8) jm4=0
            jp4=1
            IF(j+4.gt.(nstripx-6)) jp4=0

	    stripqxtanti(j)=proxtanti(np,nt,j)*(1.-2.*(c+c2))+ 	   
     +   c*(1.-2.*c)*(jm2*proxtanti(np,nt,j-2)
     +   +jp2*proxtanti(np,nt,j+2))+
     +  (c**2)*(jm4*proxtanti(np,nt,j-4)*(1.+c2/(c**2))+
     +   2.*proxtanti(np,nt,j)+jp4*proxtanti(np,nt,j+4)*(1.+c2/(c**2)))
	    if(stripqxtanti(j).gt.1.e-8) then
              NSTRPX=NSTRPX+1 
              floatadcx=stripqxtanti(j)*xcalib
	      IF(NSTRPX.GT.MAXSTR) THEN
		WRITE(6,*) 'ERROR - NSTRPX > MAXSTR, NSTRPX=',NSTRPX
		NSTRPX=MAXSTR
	      ENDIF	      
	      NPSTRIPX(NSTRPX)=NP
	      NTSTRIPX(NSTRPX)=NT
	      ISTRIPX(NSTRPX)=(J/2)+(LADD-1)*1024
	      QSTRIPX(NSTRPX)=floatadcx
	      XSTRIPX(NSTRPX)=GLOBSTRIPX(NP,NT,J)
c	      PRINT*,'1NPSTRIPX ',NP, ' NTSTRX ', NT,' ISTRIPX ', ISTRIPX(NSTRPX)
c              PRINT*,'2Q ',QSTRIPX(NSTRPX),' XS ', XSTRIPX(NSTRPX)
c              PRINT*,' ' 
	    endif
        enddo
	
c       PRINT*, 'NSTRPX', NSTRPX
        
******************** Y side *************************

        c=cistry/cdec
	

	do j=1,nstripy

            jmen1=j-1
            jm1=1
            if(jmen1.lt.1) then
             jm1=0
             jmen1=1
            endif

            jmen2=j-2
            jm2=1
            if(jmen2.lt.1) then
             jm2=0
             jmen2=1
            endif

            jpiu1=j+1
            jp1=1
            if(jpiu1.gt.nstripy) then
             jp1=0
             jpiu1=nstripy
            endif

            jpiu2=j+2
            jp2=1
            if(jpiu2.gt.nstripy) then
             jp2=0
             jpiu2=nstripy
            endif

	   stripqytanti(j)=proytanti(np,nt,j)*(1.-2.*c)+ 	   
     +    c*(1.-2.*c)*(jm1*proytanti(np,nt,jmen1)+jp1*
     +    proytanti(np,nt,jpiu1))+
     +    (c**2)*(jm2*proytanti(np,nt,jmen2)+2.*proytanti(np,nt,j)+
     +    jp2*proytanti(np,nt,jpiu2))

	   if(stripqytanti(j).gt.1.e-8) then
	     NSTRPY=NSTRPY+1
	     IF(NSTRPY.GT.MAXSTR) THEN
	       WRITE(6,*) 'ERROR - NSTRPY > MAXSTR, NSTRPY=',NSTRPY
	       NSTRPY=MAXSTR
             ENDIF
	     floatadcy=stripqytanti(j)*ycalib
             NPSTRIPY(NSTRPY)=NP
	     NTSTRIPY(NSTRPY)=NT
	     ISTRIPY(NSTRPY)=J+(LADD-1)*1024
             QSTRIPY(NSTRPY)=FLOATADCY
             YSTRIPY(NSTRPY)=GLOBSTRIPY(NP,NT,J)
c	      PRINT*,'1NPSTRIPY ',NP, ' NTSTRY ', NT,' ISTRIPY ', ISTRIPY(NSTRPY)
c              PRINT*,'2Q ',QSTRIPY(NSTRPY),' YS ', YSTRIPY(NSTRPY)
	    endif

         enddo

c	PRINT*, 'NSTRPY', NSTRPY	
	 END

