
# coincidence.py
# ----------------------------------------------------------------------
# A Program for determining expected coincidence rates for different
# arrangements of RPC and scintillator paddles

# IMPORT CLASSES

# See seperate file ParticleModelClasses.py for classes imported in the
# following statement
from ParticleModelClasses import *

# MODEL VARIABLES

# Takes 'num_sets' number of sets of data. each set has 'events_per_set'
# number of particles run through the model. Take the mean of each set,
# then takes the mean of the set means as the output
#num_sets = input("Enter number of statistical sets: ")
#events_per_set = input("Enter number of particles in each set: ")
num_sets = 30 # Gives generally consistent results
events_per_set = 30

# Create the detectors in a list for easy access and expandability
dets = {}
dets['coincidence1'] = Detector([110,100,5],[0,0,360]) # Small paddle
dets['rpc']   = Detector([100,100,2],[0,0,100])        # RPC
dets['coincidence2'] = Detector([600,300,70],[0,0,0])  # Base scintillator

# MAIN ROUTINE

# Creates a particle inside first coincidence detector and passes it
# through the array of detectors logging hits every mm if it is inside a
# detector

# Initiate some vars to store data from each set
ratios = []

# Iterate over each set
for set in range(num_sets):
	# Iterate over each particle
	for id in range(events_per_set):
		# Create a particle inside first paddle
		muon = CosmicRay(dets['coincidence1'])
		# Create a place to store all hits from this particle in each detector
		for det in dets.itervalues():
			det.hits[id] = 0
		# Trace particle path through detector group until it leaves
		while Detector.model_limits.inside(muon.pos):
			# Check to see if particle is currently inside a detector
			for det in  dets.itervalues():
				# Since detectors tend to be narrow in z direction, check
				# this first to avoid unecessary function calls (improves
				# speed)
				if muon.pos[2] < det.nr[2] and muon.pos[2] > det.far[2]:
					# Check other co-ords
					if det.inside(muon.pos):
						# If particle is inside of detector then 
						# register a hit
						det.hits[id] = det.hits[id] + 1 
			# Update particle position for next iteration
			muon.pos = muon.pos + muon.vel
			
	# Extract data stored in our detector instances
	rpc_hits = 0
	scrn_captures = 0
	for c1, rpc, c2 in zip(dets['coincidence1'].hits.itervalues(), \
						dets['rpc'].hits.itervalues(), \
						dets['coincidence2'].hits.itervalues()):
		if c1 and c2:
			scrn_captures = scrn_captures + 1
			if rpc:
				rpc_hits = rpc_hits + 1

	# Place data for this set in array
	if scrn_captures != 0:	
		ratios.append(1.0 * rpc_hits/scrn_captures)
	

# Print out the data
print 'Calculated from ',num_sets,' sets of ',events_per_set,' ionising cosmic rays:'
print '\t Can expect ', round(100.0* sum(ratios)/len(ratios)) , \
			'% of triggers to have a signal from RPC'

