View Javadoc

1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
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   * @author Norman Graf
31   */
32  public class NNAlgoTest extends TestCase
33  {
34      private boolean debug = false;
35      /** Creates a new instance of NNAlgoTest */
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          //let's try something more creative
66          long[] ids =   { 0,  /**/  2,  3,  4,  5,  6,  7, /**/   9};
67          double[] vals ={.1,  /**/ .1, .3, .1, .4, .2, .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          // should give 3 clusters, since first cluster is below threshhold
77          assertEquals(clusters.size(), 3);
78          
79          // map should have one entry left
80          assertEquals(hitmap.size(), 1);
81          
82          //sort the list of clusters
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          // now let's mix it up a bit by increasing the neighborhood...
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         // should give 2 clusters, since first cell has same energy as second cell.
101         // TODO fix this anomaly
102         assertEquals(clusters.size(), 2);
103         
104         // map should have one entry left
105         assertEquals(hitmap.size(), 1);        
106         
107         //sort the list of clusters
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      * Raw energy deposited in Calorimeter Cell
159      */
160     public double getRawEnergy()
161     {
162         return _rawEnergy;
163     }
164     /**
165      * Corrected energy deposted in Calorimeter Cell.
166      */
167     public double getCorrectedEnergy()
168     {
169         return _correctedEnergy;
170     }
171     /**
172      * The ID of the cell. This can be converted to a physical
173      * position using a IDDecoder object obtained from the event
174      * meta-data or from this hit.
175      */
176     public long getCellID()
177     {
178         return _cellID;
179     }
180     
181     /**
182      * Get the ID decoder for this hit. Note that all hits in a collection are
183      * gauranteed to share the same id decoder, but once hits have been combined
184      * into clusters each hit may have its own id decoder.
185      */
186     public IDDecoder getIDDecoder()
187     {
188         return _decoder;
189     }
190     
191     /**
192      * The subdetector corresponding to this hit.
193      */
194     public Subdetector getSubdetector()
195     {
196         return null;
197     }
198     
199     public double getTime()
200     {
201         return _time;
202     }
203     
204     /**
205      * The position of the hit. If the hit position is stored in the source
206      * LCIO file this will be returned. Otherwise the IDDecoder is used to get
207      * the hit position from the hit ID.
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."); //To change body of generated methods, choose Tools | Templates.
223     }
224 
225     @Override
226     public int getSystemId()
227     {
228         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
229     }
230 
231     @Override
232     public BarrelEndcapFlag getBarrelEndcapFlag()
233     {
234         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
235     }
236 
237     @Override
238     public int getLayerNumber()
239     {
240         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
241     }
242 
243     @Override
244     public int getIdentifierFieldValue(String field)
245     {
246         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
247     }
248 
249     @Override
250     public EventHeader.LCMetaData getMetaData()
251     {
252         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
253     }
254 
255     public void setMetaData(EventHeader.LCMetaData meta)
256     {
257         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
258     }
259 
260     @Override
261     public IDetectorElement getDetectorElement()
262     {
263         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
264     }
265 
266     @Override
267     public void setDetectorElement(IDetectorElement de)
268     {
269         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
270     }
271 
272     @Override
273     public IIdentifier getIdentifier()
274     {
275         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
276     }
277 
278     @Override
279     public IExpandedIdentifier getExpandedIdentifier()
280     {
281         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
282     }
283 
284     @Override
285     public IIdentifierHelper getIdentifierHelper()
286     {
287         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
288     }
289 
290     @Override
291     public Hep3Vector getPositionVec()
292     {
293         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
294     }
295 }
296 
297 class Decoder implements IDDecoder
298 {
299     private long _id;
300     
301     
302     /** Load the decoder with a 64-bit id value from the hit. */
303     public void setID(long id)
304     {
305         _id = id;
306     }
307     
308     /** Get an expanded identifier that maps strings to integer values. */
309     public ExpandedIdentifier getExpandedIdentifier()
310     {
311         return null;
312     }
313     
314     /** Same as getIDExpanded() except sets id. */
315     public ExpandedIdentifier getExpandedIdentifier(long id)
316     {
317         return null;
318     }
319     /*/\/\/\ Access to field data /\/\/\ */
320     public int getValue(String field)
321     {
322         return 0;
323     }
324     public int getValue(int index)
325     {
326         return 0;
327     }
328     
329     /* /\/\/\ ID description /\/\/\ */
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     /** @return layer number */
352     public int getLayer()
353     {
354         return 0;
355     }
356     
357     /* /\/\/\ Position interface /\/\/\ */
358     
359     // FIXME: change to Hep3Vector and eliminate the X/Y/Z/theta/phi methods
360     
361     /** @return Hep3Vector representing the position of the current ID. */
362     public Hep3Vector getPositionVector()
363     {
364         return null;
365     }
366     
367     /** @return position as double array of length 3 */
368     public double[] getPosition()
369     {
370         return null;
371     }
372     
373     /** @return X coordinate */
374     public double getX()
375     {
376         return 0.;
377     }
378     
379     /** @return Y coordinate */
380     public double getY()
381     {
382         return 0.;
383     }
384     
385     /** @return Z coordinate */
386     public double getZ()
387     {
388         return 0.;
389     }
390     
391     /** @return phi angle */
392     public double getPhi()
393     {
394         return 0.;
395     }
396     
397     /** @return theta angle */
398     public double getTheta()
399     {
400         return 0.;
401     }
402     
403     /* /\/\/\ Reverse Decoding: Position to Cell /\/\/\ */
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     /* /\/\/\ Miscellaneous /\/\/\ */
419     
420     /** Get the flag that indicates barrel or endcap, i.e. the "barrel" field. */
421     public BarrelEndcapFlag getBarrelEndcapFlag()
422     {
423         return null;
424     }
425     
426     /** Get the system ID, i.e. the "system" field. */
427     public int getSystemID()
428     {
429         return 0;
430     }
431     
432     /** @Deprecated use getSystemID() instead */
433     public int getSystemNumber()
434     {
435         return 0;
436     }
437     
438     /** Get the Subdetector associated with this IDDecoder, or null if not applicable. */
439     public Subdetector getSubdetector()
440     {
441         return null;
442     }
443     
444     /* /\/\/\ Neighbours /\/\/\ */
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."); //To change body of generated methods, choose Tools | Templates.
473     }
474 
475     @Override
476     public int getVLayer()
477     {
478         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
479     }
480 }