lcsim/src/org/lcsim/contrib/uiowa/util
diff -u -r1.2 -r1.3
--- EnergyAssociator.java 14 Oct 2005 17:38:19 -0000 1.2
+++ EnergyAssociator.java 16 Dec 2005 21:13:54 -0000 1.3
@@ -5,6 +5,8 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
import java.util.Comparator;
import java.util.Collections;
@@ -16,6 +18,8 @@
import org.lcsim.event.SimCalorimeterHit;
import org.lcsim.event.Track;
import org.lcsim.event.EventHeader;
+import org.lcsim.util.event.BaseCalorimeterHit;
+import org.lcsim.recon.cluster.util.BasicCluster;
/**
* A truth associator that works by comparing energy
@@ -90,23 +94,25 @@
*/
public List<MCParticle> associateHitToMCParticles(SimCalorimeterHit hit)
{
- SortMCList comp = new SortMCList(hit);
- List<MCParticle> output = comp.contributingMCParticles();
- Collections.sort(output, comp);
- return output;
+ LocalSortedMap<MCParticle> map = new LocalSortedMap<MCParticle>();
+ int nContributingParticles = hit.getMCParticleCount();
+ for (int i=0; i<nContributingParticles; i++) {
+ map.put(hit.getMCParticle(i), hit.getContributedEnergy(i));
+ }
+ return map.getSortedList();
}
/**
* Given an MCParticle, returns a list of all SimCalorimeterHits to
- * which it contributes, sorted by purity.
+ * which it contributes. Order is arbitrary.
*/
public List<SimCalorimeterHit> associateMCParticleToHits(MCParticle part)
{
- if (m_mapMCParticlesToHits == null) { makeMapMCParticlesToHits(); }
+ if (m_mapMCParticlesToHits == null) {
+ makeMapMCParticlesToHits();
+ }
// Clone it, to make sure our internals don't get messed up by an enthusiastic user
ArrayList<SimCalorimeterHit> internalHits = m_mapMCParticlesToHits.get(part);
ArrayList<SimCalorimeterHit> hits = new ArrayList<SimCalorimeterHit>(internalHits);
- SortHitList comp = new SortHitList(part);
- Collections.sort(hits, comp);
return hits;
}
@@ -116,27 +122,44 @@
*/
public List<MCParticle> associateClusterToMCParticles(Cluster clus)
{
- // Use hits. They have to be cast to SimCalorimeterHit objects first...
+ Set<MCParticle> contributingParticles = new HashSet<MCParticle>();
List<CalorimeterHit> hits = clus.getCalorimeterHits();
- List<SimCalorimeterHit> simHits = new Vector<SimCalorimeterHit>();
for (CalorimeterHit hit : hits) {
SimCalorimeterHit simHit = (SimCalorimeterHit) hit;
- simHits.add(simHit);
+ int nContributingParticles = simHit.getMCParticleCount();
+ for (int i=0; i<nContributingParticles; i++) {
+ contributingParticles.add(simHit.getMCParticle(i));
+ }
+ }
+ LocalSortedMap<MCParticle> map = new LocalSortedMap<MCParticle>();
+ for (MCParticle part : contributingParticles) {
+ map.put(part, getEnergyContributedToCluster(part, clus));
}
- // Now do the sort/extraction
- SortMCList comp = new SortMCList(simHits);
- List<MCParticle> output = comp.contributingMCParticles();
- Collections.sort(output, comp);
- return output;
+ return map.getSortedList();
}
/**
* Given an MCParticle, returns a list of all Clusters to which it
* contributes, sorted by amount of energy contributed.
*/
- public List<Cluster> associateMCParticleToClusters(MCParticle part)
+ public List<Cluster> associateMCParticleToClusters(MCParticle part)
{
- throw new AssertionError("Not yet implemented");
+ // Map from cluster to amount of enery contributed
+ LocalSortedMap<Cluster> map = new LocalSortedMap<Cluster>();
+ for (Cluster clus : m_clusters) {
+ double energy = getEnergyContributedToCluster(part, clus);
+ if (energy > 0.0) {
+ map.put(clus, new Double(energy));
+ }
+ }
+ //{
+ //// debug stuff
+ //List<Cluster> outputList = map.getSortedList();
+ //for (Cluster clus : outputList) {
+ //System.out.println("DEBUG: Association list: "+part.getType().getName()+" maps to cluster with "+clus.getCalorimeterHits().size()+" hits contributing "+map.get(clus));
+ //}
+ //}
+ return map.getSortedList();
}
public List<MCParticle> associateTrackToMCParticles(Track track)
@@ -158,6 +181,7 @@
throw new AssertionError("Not yet implemented");
}
+ /*
class SortHitList implements Comparator<SimCalorimeterHit>
{
MCParticle m_part;
@@ -250,6 +274,7 @@
return (m_mapParticleToEnergy.get(part)).doubleValue();
}
}
+ */
protected Map<MCParticle, ArrayList<SimCalorimeterHit> > m_mapMCParticlesToHits;
@@ -270,4 +295,87 @@
}
}
}
+
+ protected double getEnergyContributedToCluster(MCParticle part, Cluster clus) {
+ RonEnergyCalibration cor = new RonEnergyCalibration(m_event);
+ {
+ // debug stuff
+ //System.out.println("DEBUG: EnergyAssociator.getEnergyContributedToCluster(): corrected energy contributed by particle "+part.getType().getName()+" to cluster with "+clus.getCalorimeterHits().size()+" hits = "+cor.energyContributedByParticle(clus, part));
+ }
+ return cor.energyContributedByParticle(clus, part);
+ }
+
+ protected class CompareMapping<T> implements Comparator<T> {
+ public CompareMapping(Map<T,Double> map) {
+ m_map = map;
+ }
+ public int compare(Object o1, Object o2) {
+ T c1 = (T) o1;
+ T c2 = (T) o2;
+ Double D1 = m_map.get(c1);
+ Double D2 = m_map.get(c2);
+ if (D1.equals(D2)) {
+ // Equal
+ //System.out.println("DEBUG: Running compare of ["+o1+"]==("+D1+") and ["+o1+"]==("+D2+")... equal");
+ return 0;
+ } else if (D1.doubleValue() < D2.doubleValue()) {
+ //System.out.println("DEBUG: Running compare of ["+o1+"]==("+D1+") and ["+o1+"]==("+D2+")... first < second");
+ return +1;
+ } else {
+ //System.out.println("DEBUG: Running compare of ["+o1+"]==("+D1+") and ["+o1+"]==("+D2+")... first > second");
+ return -1;
+ }
+ }
+ Map<T,Double> m_map;
+ }
+
+ class LocalSortedMap<T>
+ {
+ public LocalSortedMap() {
+ m_list = new Vector<T>();
+ m_map = new HashMap<T,Double>();
+ }
+ public Double put(T t, double d) {
+ return this.put(t, new Double(d));
+ }
+ public Double put(T t, Double d) {
+ m_list.add(t);
+ return m_map.put(t,d);
+ }
+ public Double get(T t) {
+ return m_map.get(t);
+ }
+ public List<T> getSortedList() {
+ Comparator<T> comp = new CompareMapping<T>(m_map);
+ /*
+ {
+ String blah = new String("DEBUG: Dumping list before sort: ");
+ for (T t : m_list) {
+ if (t instanceof MCParticle) {
+ MCParticle part = (MCParticle) t;
+ blah += "["+part+"]==("+part.getType().getName()+")==("+part.getEnergy()+") ";
+ }
+ }
+ System.out.println(blah);
+ }
+ */
+ Collections.sort(m_list, comp);
+ /*
+ {
+ String blah = new String("DEBUG: Dumping list after sort: ");
+ for (T t : m_list) {
+ if (t instanceof MCParticle) {
+ MCParticle part = (MCParticle) t;
+ blah += "["+part+"]==("+part.getType().getName()+")==("+part.getEnergy()+") ";
+ }
+ }
+ System.out.println(blah);
+ }
+ */
+ return m_list;
+ }
+ public Map<T,Double> getMap() { return m_map; }
+ protected Map<T,Double> m_map;
+ protected List<T> m_list;
+ }
}