package hep.lcd.geometry;
import hep.lcd.geometry.component.*;

abstract class AbstractCalorimeterCell implements CalorimeterCell
{
	protected abstract SegmentedDetectorComponent findComponent();
        public int findTowerIDfromCartesian(double x,double y,double z){return 0;};
        public int findTowerIDfromCylindrical(double r,double phi,double z){return 0;};
        public int findTowerIDfromSpherical(double rr,double theta,double phi){return 0;};
        public int constructID(double r,double z,double theta,double phi){return 0;};
        public int constructID(int thetabin, int phibin, int layer, String system){return 0;};
        public Calorimeter findSystem(double r,double z){return null;};
	public void setTowerID(int id)
	{
		this.id = id;
		this.comp = null;
	}
	public int getTowerID()
	{
		return id;
	}
	public double getCosTheta()
	{
		if (comp == null) comp = findComponent();
		return comp.getCosTheta(getThetaBin(),getPhiBin(),getLayer()); 
	}
	public double getTheta()
	{
		if (comp == null) comp = findComponent();
		return comp.getTheta(getThetaBin(),getPhiBin(),getLayer()); 
	}
	public double getPhi()
	{
		if (comp == null) comp = findComponent();
		return comp.getPhi(getThetaBin(),getPhiBin(),getLayer());
	}
	public double getZ()
	{
		if (comp == null) comp = findComponent();
		return comp.getZ(getThetaBin(),getPhiBin(),getLayer()); 
	}
	public double getX()
	{
		if (comp == null) comp = findComponent();
		return comp.getX(getThetaBin(),getPhiBin(),getLayer());
	}
	public double getY()
	{
		if (comp == null) comp = findComponent();
		return comp.getY(getThetaBin(),getPhiBin(),getLayer());
	}
	public double[] getPosition()
	{ 
		double[] result = { getX(), getY(), getZ() };
		return result;
	}
	final public SegmentedDetectorComponent getComponent()
	{
		if (comp == null) comp = findComponent();
		return comp;
	}
	protected static int findFirstBit(int mask)
	{
		for (int i=0; i<32; i++) if ((mask & 1<<i) != 0) return i;
		throw new RuntimeException("Invalid mask exception");
	}
	protected int id;
	protected Detector det;
	protected SegmentedDetectorComponent comp;
}
