1 package org.lcsim.geometry.layer;
2
3 import static java.lang.Math.abs;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8
9
10
11
12
13
14 public class Layer
15 {
16 List<LayerSlice> slices;
17 List<LayerSlice> sensors;
18 List<Integer> sensorIndices;
19 double preOffset = 0;
20 double thickness = 0;
21 double thicknessToSensor = 0;
22 double thicknessToSensorMid = 0;
23 double sensorThickness = 0;
24 int indexOfFirstSensor = -1;
25 double thicknessWithPreOffset = 0;
26
27 public Layer(List<LayerSlice> slices)
28 {
29 this.slices = slices;
30
31
32 sensorIndices = new ArrayList<Integer>();
33 for (int i=0; i<slices.size(); i++)
34 {
35 if (slices.get(i).isSensitive())
36 sensorIndices.add(i);
37 }
38
39
40 computeThickness();
41 computeIndexOfFirstSensor();
42 computeSensorThickness();
43 computeThicknessToSensor();
44 computeThicknessToSensorMid();
45 computeThicknessWithPreOffset();
46 }
47
48 public List<LayerSlice> getSensors()
49 {
50 if (sensors == null)
51 {
52 sensors = new ArrayList<LayerSlice>();
53 List<Integer> sensorIndices = getSensorIndices();
54 for (int i : sensorIndices)
55 {
56 sensors.add(this.getSlice(i));
57 }
58 }
59 return sensors;
60 }
61
62 public List<Integer> getSensorIndices()
63 {
64 return sensorIndices;
65 }
66
67 public double getThickness()
68 {
69 return thickness;
70 }
71
72 public void setPreOffset(double preOffset)
73 {
74 if ( abs(preOffset) < 1E-7 )
75 {
76 preOffset = 0;
77 }
78
79 this.preOffset = preOffset;
80 }
81
82 public double getPreOffset()
83 {
84 return preOffset;
85 }
86
87 public double getThicknessWithPreOffset()
88 {
89 return thicknessWithPreOffset;
90 }
91
92 public double getThicknessToSensitiveMid()
93 {
94 return thicknessToSensorMid;
95 }
96
97 public double getThicknessToSensitive()
98 {
99 return thicknessToSensor;
100 }
101
102 public LayerSlice getSlice(int idx)
103 {
104 return slices.get(idx);
105 }
106
107 public List<LayerSlice> getSlices()
108 {
109 return slices;
110 }
111
112 public int getNumberOfSlices()
113 {
114 return slices.size();
115 }
116
117 public double getSensorThickness()
118 {
119 return sensorThickness;
120 }
121
122 public int indexOfFirstSensor()
123 {
124 return indexOfFirstSensor;
125 }
126
127 private void computeThicknessToSensor()
128 {
129 int i = indexOfFirstSensor();
130
131 if ( i != -1)
132 {
133
134 for ( int ii = 0; ii < i; ii++)
135 {
136 thicknessToSensor += slices.get(ii).getThickness();
137 }
138 }
139 }
140
141 private void computeThickness()
142 {
143 thickness = 0;
144 for ( LayerSlice l : slices)
145 {
146 thickness += l.getThickness();
147 }
148 }
149
150 private void computeSensorThickness()
151 {
152 if (indexOfFirstSensor() != -1)
153 {
154 sensorThickness = slices.get(indexOfFirstSensor()).getThickness();
155 }
156 }
157
158 private void computeIndexOfFirstSensor()
159 {
160 int i = 0;
161 boolean fnd = false;
162 for ( LayerSlice l : slices )
163 {
164 if ( l.isSensitive() )
165 {
166 fnd = true;
167 break;
168 }
169 i++;
170 }
171
172 if (!fnd)
173 {
174 i = -1;
175 }
176
177 indexOfFirstSensor = i;
178 }
179
180 public double computeDistanceToSlice(int idx)
181 {
182 double d = 0;
183 for (int i=0; i<idx; i++)
184 {
185 d += getSlice(i).getThickness();
186 }
187 return d;
188 }
189
190 public double computeDistanceToSliceMid(int idx)
191 {
192 double d = computeDistanceToSlice(idx);
193 d += getSlice(idx).getThickness() / 2;
194 return d;
195 }
196
197 private void computeThicknessToSensorMid()
198 {
199 int i = indexOfFirstSensor();
200
201 if ( i != -1)
202 {
203
204 for ( int ii = 0; ii < i; ii++)
205 {
206 thicknessToSensorMid += slices.get(ii).getThickness();
207 }
208
209 thicknessToSensorMid += slices.get(i).getThickness()/2;
210 }
211 }
212
213 private void computeThicknessWithPreOffset()
214 {
215 thicknessWithPreOffset = getThickness() + getPreOffset();
216 }
217 }