1
2
3
4
5
6
7 package org.lcsim.recon.cluster.localequivalence;
8
9 import hep.physics.vec.Hep3Vector;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import junit.framework.TestCase;
15 import org.lcsim.detector.DetectorIdentifierHelper;
16 import org.lcsim.detector.IDetectorElement;
17 import org.lcsim.detector.identifier.ExpandedIdentifier;
18 import org.lcsim.detector.identifier.IExpandedIdentifier;
19 import org.lcsim.detector.identifier.IIdentifier;
20 import org.lcsim.detector.identifier.IIdentifierHelper;
21 import org.lcsim.event.CalorimeterHit;
22 import org.lcsim.event.EventHeader;
23 import org.lcsim.geometry.IDDecoder;
24 import org.lcsim.geometry.Subdetector;
25 import org.lcsim.geometry.util.IDDescriptor;
26 import org.lcsim.geometry.subdetector.BarrelEndcapFlag;
27
28
29
30
31
32 public class NNAlgoTest extends TestCase
33 {
34 private boolean debug = false;
35
36 public void testNNAlgo()
37 {
38 Map<Long, CalorimeterHit> hitmap = new HashMap<Long, CalorimeterHit>();
39
40 double[] pos = {0.,0.,0.};
41 for(int i=0; i<10; ++i)
42 {
43 double d = i+.1;
44 long l = (long) i;
45 hitmap.put(l, new CalHit(d, d, l, d, pos));
46 }
47
48 if(debug) System.out.println(hitmap);
49
50 double minValue = 0.15;
51 NNAlgo alg = new NNAlgo(minValue);
52 if(debug) System.out.println(alg);
53 List<NNCluster> clusters = alg.cluster(hitmap);
54 if(debug) System.out.println("found "+clusters.size()+ " clusters");
55 assertEquals(clusters.size(), 1);
56 for(NNCluster clus : clusters)
57 {
58 if(debug) System.out.println(clus);
59 assertEquals(clus.size(), 10);
60 }
61
62 if(debug) System.out.println("hitmap left with "+hitmap.size());
63 assertEquals(hitmap.size(), 0);
64
65
66 long[] ids = { 0, 9};
67 double[] vals ={.1, .5};
68 hitmap.clear();
69 double rE = .1;
70 double t = 137.;
71 for(int i=0; i<ids.length; ++i)
72 {
73 hitmap.put(ids[i], new CalHit(rE, vals[i], ids[i], t, pos));
74 }
75 clusters = alg.cluster(hitmap);
76
77 assertEquals(clusters.size(), 3);
78
79
80 assertEquals(hitmap.size(), 1);
81
82
83 Collections.sort(clusters);
84 Collections.reverse(clusters);
85 if(debug) System.out.println(clusters.get(0).value());
86 assertEquals(clusters.get(0).value(), 0.8);
87 assertEquals(clusters.get(1).value(), 0.5);
88 assertEquals(clusters.get(2).value(), 0.4);
89
90
91
92 NNAlgo alg2 = new NNAlgo(minValue,2,2,2);
93
94 hitmap.clear();
95 for(int i=0; i<ids.length; ++i)
96 {
97 hitmap.put(ids[i], new CalHit(rE, vals[i], ids[i], t, pos));
98 }
99 clusters = alg2.cluster(hitmap);
100
101
102 assertEquals(clusters.size(), 2);
103
104
105 assertEquals(hitmap.size(), 1);
106
107
108 Collections.sort(clusters);
109 Collections.reverse(clusters);
110 if(debug) System.out.println(clusters.get(0).value());
111 assertEquals(clusters.get(0).value(), 1.1);
112 assertEquals(clusters.get(1).value(), 0.6);
113 if(debug)
114 {
115 for(NNCluster clus : clusters)
116 {
117 System.out.println(clus.value());
118 }
119 }
120
121
122 }
123
124 }
125
126 class CalHit implements CalorimeterHit
127 {
128
129 @Override
130 public double getEnergyError()
131 {
132 return 0.;
133 }
134
135 @Override
136 public int getType()
137 {
138 return 0;
139 }
140 private double _rawEnergy;
141 private double _correctedEnergy;
142 private long _cellID;
143 private double _time;
144 private double[] _pos;
145
146 private Decoder _decoder = new Decoder();
147
148 public CalHit(double rE, double cE, long id, double t, double[] pos)
149 {
150 _rawEnergy = rE;
151 _correctedEnergy = cE;
152 _cellID = id;
153 _time = t;
154 _pos = pos;
155 _decoder.setID(id);
156 }
157
158
159
160 public double getRawEnergy()
161 {
162 return _rawEnergy;
163 }
164
165
166
167 public double getCorrectedEnergy()
168 {
169 return _correctedEnergy;
170 }
171
172
173
174
175
176 public long getCellID()
177 {
178 return _cellID;
179 }
180
181
182
183
184
185
186 public IDDecoder getIDDecoder()
187 {
188 return _decoder;
189 }
190
191
192
193
194 public Subdetector getSubdetector()
195 {
196 return null;
197 }
198
199 public double getTime()
200 {
201 return _time;
202 }
203
204
205
206
207
208
209 public double[] getPosition()
210 {
211 return _pos;
212 }
213
214 public String toString()
215 {
216 return "CalHit: value= "+ _correctedEnergy;
217 }
218
219 @Override
220 public DetectorIdentifierHelper getDetectorIdentifierHelper()
221 {
222 throw new UnsupportedOperationException("Not supported yet.");
223 }
224
225 @Override
226 public int getSystemId()
227 {
228 throw new UnsupportedOperationException("Not supported yet.");
229 }
230
231 @Override
232 public BarrelEndcapFlag getBarrelEndcapFlag()
233 {
234 throw new UnsupportedOperationException("Not supported yet.");
235 }
236
237 @Override
238 public int getLayerNumber()
239 {
240 throw new UnsupportedOperationException("Not supported yet.");
241 }
242
243 @Override
244 public int getIdentifierFieldValue(String field)
245 {
246 throw new UnsupportedOperationException("Not supported yet.");
247 }
248
249 @Override
250 public EventHeader.LCMetaData getMetaData()
251 {
252 throw new UnsupportedOperationException("Not supported yet.");
253 }
254
255 public void setMetaData(EventHeader.LCMetaData meta)
256 {
257 throw new UnsupportedOperationException("Not supported yet.");
258 }
259
260 @Override
261 public IDetectorElement getDetectorElement()
262 {
263 throw new UnsupportedOperationException("Not supported yet.");
264 }
265
266 @Override
267 public void setDetectorElement(IDetectorElement de)
268 {
269 throw new UnsupportedOperationException("Not supported yet.");
270 }
271
272 @Override
273 public IIdentifier getIdentifier()
274 {
275 throw new UnsupportedOperationException("Not supported yet.");
276 }
277
278 @Override
279 public IExpandedIdentifier getExpandedIdentifier()
280 {
281 throw new UnsupportedOperationException("Not supported yet.");
282 }
283
284 @Override
285 public IIdentifierHelper getIdentifierHelper()
286 {
287 throw new UnsupportedOperationException("Not supported yet.");
288 }
289
290 @Override
291 public Hep3Vector getPositionVec()
292 {
293 throw new UnsupportedOperationException("Not supported yet.");
294 }
295 }
296
297 class Decoder implements IDDecoder
298 {
299 private long _id;
300
301
302
303 public void setID(long id)
304 {
305 _id = id;
306 }
307
308
309 public ExpandedIdentifier getExpandedIdentifier()
310 {
311 return null;
312 }
313
314
315 public ExpandedIdentifier getExpandedIdentifier(long id)
316 {
317 return null;
318 }
319
320 public int getValue(String field)
321 {
322 return 0;
323 }
324 public int getValue(int index)
325 {
326 return 0;
327 }
328
329
330 public int getFieldCount()
331 {
332 return 0;
333 }
334 public String getFieldName(int index)
335 {
336 return null;
337 }
338 public int getFieldIndex(String name)
339 {
340 return 0;
341 }
342 public void setIDDescription(IDDescriptor id)
343 {
344
345 }
346 public IDDescriptor getIDDescription()
347 {
348 return null;
349 }
350
351
352 public int getLayer()
353 {
354 return 0;
355 }
356
357
358
359
360
361
362 public Hep3Vector getPositionVector()
363 {
364 return null;
365 }
366
367
368 public double[] getPosition()
369 {
370 return null;
371 }
372
373
374 public double getX()
375 {
376 return 0.;
377 }
378
379
380 public double getY()
381 {
382 return 0.;
383 }
384
385
386 public double getZ()
387 {
388 return 0.;
389 }
390
391
392 public double getPhi()
393 {
394 return 0.;
395 }
396
397
398 public double getTheta()
399 {
400 return 0.;
401 }
402
403
404
405 public long findCellContainingXYZ(Hep3Vector pos)
406 {
407 return 0;
408 }
409 public long findCellContainingXYZ(double[] pos)
410 {
411 return 0;
412 }
413 public long findCellContainingXYZ(double x, double y, double z)
414 {
415 return 0;
416 }
417
418
419
420
421 public BarrelEndcapFlag getBarrelEndcapFlag()
422 {
423 return null;
424 }
425
426
427 public int getSystemID()
428 {
429 return 0;
430 }
431
432
433 public int getSystemNumber()
434 {
435 return 0;
436 }
437
438
439 public Subdetector getSubdetector()
440 {
441 return null;
442 }
443
444
445
446 public boolean supportsNeighbours()
447 {
448 return true;
449 }
450 public long[] getNeighbourIDs()
451 {
452 long[] n = new long[2];
453 n[0] = _id-1;
454 n[1] = _id+1;
455 return n;
456 }
457 public long[] getNeighbourIDs(int deltaLayer, int deltaTheta, int deltaPhi)
458 {
459 long[] n = new long[2*deltaLayer];
460 for(int i=0; i< deltaLayer; ++i)
461 {
462 n[i] = _id+i-deltaLayer;
463 n[deltaLayer+i] = _id+i+1;
464 }
465 return n;
466
467 }
468
469 @Override
470 public int[] getValues(int[] buffer)
471 {
472 throw new UnsupportedOperationException("Not supported yet.");
473 }
474
475 @Override
476 public int getVLayer()
477 {
478 throw new UnsupportedOperationException("Not supported yet.");
479 }
480 }