/*
 * trckg.cpp
 * CherenProj
 *
 *  Created by Alexander Sage on 7/25/13.
 *  Supported by Stony Brook University. No rights reserved.
 *  
 */

#include <stdio.h>
#include <iostream>
#include "cherenkov.h"
using namespace std;


void trckg (int prtnm)
{
	bool htbl = 0, htmry = 0, leftvac = 0;          //htbl determines if the particle hit the detector
																									//htmry remembers if parte ever hit detector
																							    //leftvac is true if particle left vacuum chamber
																							    //by absorbtion into the wall. 
	double pos[4], vel[4], ipos[4], ivel[4];
	int mvg, mult =0;
																								//ipos and ivel are used to save the initial values 
	//define [0]=x [1]=y [2]=z [3]=t
	mvg =0;                                      //mvg determines if proton is still 'moving'
	posfunc(pos, vel);       //define position and velocity
	
	
	//to test : make hit detector
	//vel[0] = 0; vel[2] = 0; vel[1] = 0.5;
	
  memcpy(ipos, pos, sizeof pos);   //save initial values
	memcpy(ivel, vel, sizeof vel);
	
	
	
	// start time
	for (int i =0; i< 400; i++) 
	{
		for (int j=0; j < 3; j++) 
		{
			pos[j] += vel[j]*0.01;  // updating position by one increment of velocity with each passing sec.
		}
		pos[3] = i*0.01;   //here i is in seconds
		vel[3] = i*0.01;
		//printf("time = %f\n", pos[3]);
		geom(pos, htbl, leftvac);
		
		if (htbl) 
		{
			htmry = true;
			
			/*
			printf("this is from trckg\n");
			for (int i = 0; i<4; i++) 
			{
				printf("pos [ %d ] = %f\n", i, pos[i]);
			}
			for (int i = 0; i<4; i++) 
			{
				printf("vel [ %d ] = %f\n", i, vel[i]);
			}
			printf("\n");
			*/
			
			slwp(vel, mvg);                //call function to slow particle in material
		  mult += tphoton(vel, pos);             //call funtion for tracking photon from radiation
		}
		
		if (leftvac)       //if hit wall break loop
		{
			break;
		}
		if ((mvg == 3))    //mvg is moving. mvg==3 if not moving
		{
			printf("particle stopped (mvg is %d)  so loop broke\n", mvg);
			break;
		}
	}                     //time stops
	
		
	
	if (htmry)                 //if particle hit the detctor the lines below print out information
	{                          // initial values, ending values and if it stopped inside detector
	
		printf("\nthis is the %d particle\n", prtnm);
		printf("initial values \n");
		/*for (int i = 0; i<4; i++) 
		{
			printf("pos [ %d ] = %f\n", i, ipos[i]);
		}*/
		for (int i = 0; i<4; i++) 
		{
			printf("vel [ %d ] = %f\n", i, ivel[i]);
		}
		printf("   \n");
		
		printf("final position and vel is ");
	  if (htbl) 
		{
			printf("inside detector\n");
		}
		else 
		{
			printf("outside detector\n");
		}
		
		for (int i = 0; i<4; i++) 
		{
			printf("pos [ %d ] = %f\n", i, pos[i]);
		}
		
		for (int i = 0; i<4; i++) 
		{
			printf("vel [ %d ] = %f\n", i, vel[i]);
		}
		
		if (leftvac) 
		{
			printf("particle left detector\n");
		}
		
		if (pos[3] == 40) 
		{
			printf("time ran out \n");
		}
		printf("%d photon hit photo-multiplier\n", mult);
		printf("   \n");
		printf("---------------------------------------\n");
	}
	/*else 
	{
		if (pos[3] >= 20) 
		{
			printf("time ran out \n");
			printf("---------------------------------------\n");
		}
	}*/

	
}