!============================================================
!
!  G R E A T   Online sort    V0.0       25.7.02   RDH
!
!============================================================

!============================================================
!    F O R M A T S
!============================================================
*FORMATS

Si[41:96,201:240] (e,t)
PIN[1:28] (e,t)
gas[171] (e,t,x1,x2,y1,y2)

!planar[] (e,t)
!Ge[] (e,t)

TAC[32:36] (t)
clock[192] (tag,c1,c2)
slowclk[254] (t)

!============================================================
!    D A T A
!============================================================

*DATA

! Gas detector variables

WORD Xgas
WORD Ygas
WORD gaspatt
WORD gaspresent

gainword gasxgain 0.0 1.0 0.0
gainword gasygain 0.0 1.0 0.0

! Si detector variables

WORD xfold
WORD yfold
WORD xstrip
WORD ystrip
WORD Si_Xenergy
WORD Si_Yenergy

gainarray SiXgains
 1 (0.0 1.0 0.0)
 2 (0.0 1.0 0.0)
 3 (0.0 1.0 0.0)
 4 (0.0 1.0 0.0)
 5 (0.0 1.0 0.0)
 6 (0.0 1.0 0.0)
 7 (0.0 1.0 0.0)
 8 (0.0 1.0 0.0)
 9 (0.0 1.0 0.0)
10 (0.0 1.0 0.0)
11 (0.0 1.0 0.0)
12 (0.0 1.0 0.0)
13 (0.0 1.0 0.0)
14 (0.0 1.0 0.0)
15 (0.0 1.0 0.0)
16 (0.0 1.0 0.0)
17 (0.0 1.0 0.0)
18 (0.0 1.0 0.0)
19 (0.0 1.0 0.0)
20 (0.0 1.0 0.0)
21 (0.0 1.0 0.0)
22 (0.0 1.0 0.0)
23 (0.0 1.0 0.0)
24 (0.0 1.0 0.0)
25 (0.0 1.0 0.0)
26 (0.0 1.0 0.0)
27 (0.0 1.0 0.0)
28 (0.0 1.0 0.0)
29 (0.0 1.0 0.0)
30 (0.0 1.0 0.0)
31 (0.0 1.0 0.0)
32 (0.0 1.0 0.0)
33 (0.0 1.0 0.0)
34 (0.0 1.0 0.0)
35 (0.0 1.0 0.0)
36 (0.0 1.0 0.0)
37 (0.0 1.0 0.0)
38 (0.0 1.0 0.0)
39 (0.0 1.0 0.0)
40 (0.0 1.0 0.0)
41 (0.0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)
 3 (0.0 1.0 0.0)
 4 (0.0 1.0 0.0)
 5 (0.0 1.0 0.0)
 6 (0.0 1.0 0.0)
 7 (0.0 1.0 0.0)
 8 (0.0 1.0 0.0)
 9 (0.0 1.0 0.0)
10 (0.0 1.0 0.0)
11 (0.0 1.0 0.0)
12 (0.0 1.0 0.0)
13 (0.0 1.0 0.0)
14 (0.0 1.0 0.0)
15 (0.0 1.0 0.0)
16 (0.0 1.0 0.0)
17 (0.0 1.0 0.0)
18 (0.0 1.0 0.0)
19 (0.0 1.0 0.0)
20 (0.0 1.0 0.0)
21 (0.0 1.0 0.0)
22 (0.0 1.0 0.0)
23 (0.0 1.0 0.0)
24 (0.0 1.0 0.0)
25 (0.0 1.0 0.0)
26 (0.0 1.0 0.0)
27 (0.0 1.0 0.0)
28 (0.0 1.0 0.0)

valuearray PINenergies[1:28]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

valuearray PINtimes[1:28]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

valuearray PINpos[1:28]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

WORD foldraw
WORD foldclean
WORD PINid
WORD i
WORD j
WORD iminus1
WORD ex
WORD ey

WORD Alpha_E
WORD Recoil_E

!============================================================
!    S P E C T R A
!============================================================

*SPECTRA

stat           512  32  ! Statistics spectrum

! TACs

tacraw[32:36] 4096  32  ! Raw TACs in the system
gassitac      4096  32  ! ToF Si(start) and Gas (dld stop) detector
pinsitac      4096  32  ! TAC Si(start) and PIN (dld stop) detectors

! Clocks

clockbits       64  32  ! Bits of fast event clock
slowclock    32678  32  ! Wall clock

! Gas detector

gaspattern     128  32  ! Gas detector pattern 
gasE          4096  32  ! Energy
gasT          4096  32  ! Cathode time
gasX1         4096  32  ! X1 time
gasX2         4096  32  ! X2 time
gasY1         4096  32  ! Y1 time
gasY2         4096  32  ! Y2 time
gasXdiff      8192  32  ! X-position timediff
gasYdiff      8192  32  ! Y-position timediff
gasXpos        256  32  ! X-Position calibrated
gasYpos        128  32  ! Y-Position calibrated
gasXY      256*128  16  ! gas detector map

! PIN diodes

PINraw[1:28]  4096  32  ! Raw PIN data
PINrawtot     4096  32  ! Raw PIN data total
PINt[1:28]    4096  32  ! PIN times
PINttot       4096  32  ! PIN times total
PINE[1:28]    4096  32  ! PIN energies (calibrated)
PINEtot       4096  32  ! PIN cal.E. total
PINpatt         32  32  ! PIN hitpattern
PINfoldraw      32  32  ! PIN fold distribution raw
PINfoldclean    32  32  ! PIN fold distribution clean
PINwalpha     4096  32  ! PIN spectrum coinc with any alpha
PINwrecoil    4096  32  ! PIN spectrum coinc with any recoil

ppmat      512*512  16  ! PIN-PIN coincidence matrix

! Si detector

SiXEraw[1:60] 4096  32  ! Si X strips energy raw
SiXErawtot    4096  32  ! Si X strips energy raw total
SiXE[1:60]    4096  32  ! Si X strips energy calibrated
SiXEtot       4096  32  ! Si X strips energy calibrated total
SiXT[1:60]    4096  32  ! Si X strips time
SiXTtot       4096  32  ! Si X strips time total
SiYEraw[1:40] 4096  32  ! Si Y strips energy raw
SiYErawtot    4096  32  ! Si Y strips energy raw total
SiYE[1:40]    4096  32  ! Si Y strips energy calibrated
SiYEtot       4096  32  ! Si Y strips energy calibrated total
SiYT[1:40]    4096  32  ! Si Y strips time
SiYTtot       4096  32  ! Si Y strips time total
SiXfold         64  32  ! Si X fold
SiYfold         64  32  ! Si Y fold
SiXYfold     64*64  16  ! Si fold xy distribution     
SiXY         64*64  16  ! Si detector map

RecoilX[1:60] 4096  32  ! Recoil spectra X strips
RecoilXtot    4096  32  ! Recoil spectra X strips total
RecoilY[1:60] 4096  32  ! Recoil spectra Y strips
RecoilYtot    4096  32  ! Recoil spectra Y strips total

AlphaX[1:60]  4096  32  ! Alpha spectra X strips
AlphaXtot     4096  32  ! Alpha spectra X strips total
AlphaY[1:60]  4096  32  ! Alpha spectra Y strips
AlphaYtot     4096  32  ! Alpha spectra Y strips total
AlphaEmean    4096  32  ! Alphaspec from average of front and back energy
AlphaEdiff    8192  32  ! Alphaspec from difference of front and back energy

tofemat  1024*1024  16  ! ToF vs Si energy
edemat   1024*1024  16  ! E (Si) vs dE (Gas)

Apinmat  1024*1024  16  ! Alpha PIN matrix
PINgate       4096  32  ! PIN total spectrum gated by alpha
Alphagate     4096  32  ! Alpha total spectrum gated by PIN

!============================================================
!    C O M M A N D S
!============================================================

*COMMANDS

inc stat(1)    ! Number of raw events

inc tacraw(tac[32].t) indexed 32 ! Raw tacs
inc tacraw(tac[33].t) indexed 33 ! Raw tacs
inc tacraw(tac[34].t) indexed 34 ! Raw tacs
inc tacraw(tac[35].t) indexed 35 ! Raw tacs
inc tacraw(tac[36].t) indexed 36 ! Raw tacs

!============================================================
! Monitor Clock bits
!============================================================

incbits clockbits(clock.c1) offset 0
incbits clockbits(clock.c2) offset 16

!============================================================
! Handle gas detector
! x1,x2,y1,y2: times are relative to cathode time
!
!============================================================

gaspatt=0
if gas[171].x1 passes (0,4095) gaspatt=gaspatt+1
if gas[171].x2 passes (0,4095) gaspatt=gaspatt+2
if gas[171].y1 passes (0,4095) gaspatt=gaspatt+4
if gas[171].y2 passes (0,4095) gaspatt=gaspatt+8
if gas[171].e  passes (0,4095) gaspatt=gaspatt+16
if gas[171].t  passes (0,4095) gaspatt=gaspatt+32

inc gaspattern(gaspatt)

inc gasE(gas[171].e)
inc gasT(gas[171].t)
inc gasX1(gas[171].x1)
inc gasX2(gas[171].x2)
inc gasY1(gas[171].y1)
inc gasY2(gas[171].y2)

Xgas=gas[171].x2-gas[171].x1+4096
Ygas=gas[171].y2-gas[171].y1+4096

inc gasXdiff(Xgas)
inc gasYdiff(Ygas)

!============================================================
! Mapping of gas detector positions onto Si detector positions:
! leave a rim around the Si detector, i.e.
! ideally x-strip 1 of Si gets to lie in ch 21 of Gas detector x
! y-strip 1 of Si gets to lie in ch 21 of Gas detector y
!============================================================

gain Xgas gasXgain

if (Xgas) LE 0   Xgas=0
if (Xgas) GE 160 Xgas=160

gain Ygas gasYgain
if (Ygas) LE 0   Ygas=0
if (Ygas) GE 80  Ygas=80

inc gasXpos(Xgas)
inc gasYpos(Ygas)
inc gasXY(Xgas,Ygas)

gaspresent=0

if (gaspatt) ge 48   ! Need at least the gas energy and the cathode time
 {
  gaspresent=1
  inc stat(10)       ! Number of events with Gas detector 
 }

!============================================================
! PIN Diodes
!============================================================

doloop i from 1 to 28 step 1
 {
  PINenergies(i)=0
  PINtimes(i)=0
  PINpos(i)=0
 }

createlist pinlist from PIN

foldclean=0
foldraw=0

loopif $p=pinlist VALID
 {
  PINid=group($p)
  inc stat(PINid+300)           ! Pattern of PIN diode hits

  inc PINraw($p.e) indexed PINid
  inc PINrawtot($p.e)
  inc PINt($p.t) indexed PINid
  inc PINttot($p.t)

  gain $p.e PINgains indexed PINid
  inc PINE($p.e) indexed PINid
  inc PINEtot($p.e)

  foldraw=foldraw+1
  if ($p.e) passes (5,4090)      ! Want good energy and time       
   {                             ! Set threshold out of noise!!
    if ($p.t) passes (5,4090)    ! Check if PIN times are cabled up!!!
     {   
      foldclean=foldclean+1      
      PINenergies(foldclean)=$p.e
      PINpos(foldclean)=PINid
      PINtimes(foldclean)=$p.t
     }
   }
 }
inc PINfoldclean(foldclean)
inc PINfoldraw(foldraw)

if (foldclean) ge 2
 {
  doloop i from 2 to foldclean step 1
   {
    iminus1=i-1
    doloop j from 1 to iminus1 step 1
     {
      ex=PINenergies(i)/8
      ey=PINenergies(j)/8
      inc ppmat(ex,ey)
      inc ppmat(ey,ex)
     }
   }
 }

!============================================================
! Si strip detector
!============================================================

createlist sixlist from Si[41:96]
createlist siylist from Si[201:240]

xstrip=-1
xfold=0
loopif $s=sixlist valid
 {
  xstrip=group($s)-40
  inc SiXEraw($s.e) indexed xstrip
  inc SiXErawtot($s.e)
  gain $s.e sixgains indexed xstrip
  inc SiXE($s.e) indexed xstrip 
  inc SiXEtot($s.e)
  inc SiXT($s.t) indexed xstrip
  inc SiXTtot($s.t)
  inc stat (xstrip+100)     ! Pattern of Si X strip hits
  xfold=xfold+1

  Si_Xenergy=$s.e
 }

ystrip=-1
yfold=0
loopif $s=siylist valid
 {
  ystrip=group($s)-200
  inc SiYEraw($s.e) indexed ystrip
  inc SiYErawtot($s.e)
  gain $s.e siygains indexed ystrip
  inc SiXE($s.e) indexed ystrip 
  inc SiXEtot($s.e)
  inc SiYT($s.t) indexed ystrip
  inc SiXTtot($s.e)
  inc stat (ystrip+200)     ! Pattern of Si X strip hits
  yfold=yfold+1

  Si_Yenergy=$s.e
 }

inc SiXfold(xfold)
inc SiYfold(yfold)
inc SiXYfold(xfold,yfold)

if (xfold) eq 1
 {
  if (yfold) eq 1
   {
    inc SiXY(xstrip,ystrip)   ! Si detectormap 
    inc gassitac(TAC[33].t)

    if (gaspresent) eq 1      ! We have a recoil
     {
      inc recoilX(Si_Xenergy) indexed xstrip
      inc recoilXtot(Si_Xenergy)
      inc recoilY(Si_Yenergy) indexed ystrip
      inc recoilYtot(Si_Yenergy)

      inc edemat(Si_Xenergy/4,gas[171].e/4)
      inc tofemat(Si_Xenergy/4,tac[33].t/4) 

      Recoil_E=Si_Xenergy
      doloop i FROM 1 TO foldclean step 1
       {
        ex=PINenergies(i)
        inc PINwrecoil(ex)
       }
     }
    if (gaspresent) eq 0      ! We have an alpha
     {
      inc alphaX(Si_Xenergy) indexed xstrip
      inc alphaXtot(Si_Xenergy)
      inc alphaY(Si_Yenergy) indexed ystrip
      inc alphaYtot(Si_Yenergy)

      Alpha_E_mean=(Si_Xenergy+Si_Yenergy)/2
      Alpha_E_diff=(Si_Xenergy-Si_Yenergy)+4096

      inc AlphaEmean(Alpha_E_mean)
      inc AlphaEdiff(Alpha_E_diff)

      inc pinsitac(tac[34].t)

      Alpha_E=Si_Xenergy

      ! Alpha PIN Matrix and gated spectrum

      doloop i FROM 1 TO foldclean step 1
       {
        ex=PINenergies(i)
        inc apinmat(Alpha_E/4,ex/4)

        inc PINwalpha(ex)

        if (Alpha_E) passes (5,4090)        ! CHANGE GATE !!!
         {
          inc PINgate(ex)
         }
        if (ex) passes (1,4095)        ! CHANGE GATE !!!
         {
          inc Alphagate(Alpha_E)
         }
       } !doloop
     } ! if gaspresent eq 0
   } ! if yfold eq 1
 } ! if xfold eq 1

END

*FINISH
