Commit in lcsim/src/org/lcsim/recon/pfa/cheat on MAIN
PerfectIdentifier.java+156added 1.1
Make particles out of clusters with cheating

lcsim/src/org/lcsim/recon/pfa/cheat
PerfectIdentifier.java added at 1.1
diff -N PerfectIdentifier.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PerfectIdentifier.java	22 Jun 2006 01:53:10 -0000	1.1
@@ -0,0 +1,156 @@
+package org.lcsim.recon.pfa.cheat;
+
+import java.util.*;
+
+import hep.physics.particle.Particle;
+import hep.physics.vec.Hep3Vector;
+
+import org.lcsim.util.*;
+import org.lcsim.event.*;
+import org.lcsim.event.Track;
+import org.lcsim.mc.fast.tracking.ReconTrack;
+import org.lcsim.event.base.BaseReconstructedParticle;
+
+public class PerfectIdentifier extends Driver
+{
+    public PerfectIdentifier() {
+    }
+    
+    public void process(EventHeader event)
+    {
+	List<Cluster> inputClusterList = event.get(Cluster.class, m_inputClusterListName);
+	List<ReconstructedParticle> outputParticleList = new Vector<ReconstructedParticle>();
+	List<MCParticle> mcList = event.get(MCParticle.class, m_mcName);
+	List<Track> trackList = event.get(Track.class, m_inputTrackListName);
+	
+	for (Cluster clus : inputClusterList) {
+	    // Identify
+	    MCParticle truthID = findTruthID(clus, mcList);
+	    // Add to output list
+	    LocalReconstructedParticle part = new LocalReconstructedParticle();
+	    part.addCluster(clus);
+	    part.setEnergy(truthID.getEnergy());
+	    // Match track(s):
+	    for (Track currentTrack : trackList) {
+		if (currentTrack instanceof ReconTrack) {
+		    ReconTrack cheatTrack = (ReconTrack)(currentTrack);
+		    Particle cheatTrackParticle = cheatTrack.getMCParticle();
+		    if (cheatTrackParticle == truthID) {
+			part.addTrack(currentTrack);
+		    }
+		}
+	    }
+	    // Set the other particle properties that are needed to render
+	    // properly in the event display.
+	    part.setMomentum(truthID.getMomentum());
+	    part.setReferencePoint(truthID.getOrigin());
+	    part.setCharge(truthID.getCharge());
+	    // Add to the output list
+	    outputParticleList.add(part);
+	}
+	event.put(m_outputParticleListName, outputParticleList);
+    }
+    
+    protected MCParticle findTruthID(Cluster clus, List<MCParticle> mcList) {
+	// Find the dominant MC particle in some way.
+	// For now, do something ugly
+	Map<MCParticle, Integer> tmpMap = new HashMap<MCParticle, Integer> ();
+	for (CalorimeterHit hit : clus.getCalorimeterHits()) {
+	    Set<MCParticle> particles = findMCParticles(hit, mcList);
+	    for (MCParticle part : particles) {
+		Integer oldValue = tmpMap.get(part);
+		if (oldValue==null) {
+		    oldValue = new Integer(0);
+		}
+		Integer newValue = new Integer(oldValue + 1);
+		tmpMap.put(part, newValue);
+	    }
+	}
+	
+	// Which particle contributed the most?
+	int max = 0;
+	MCParticle best = null;
+	for (MCParticle part : tmpMap.keySet()) {
+	    int numHits = tmpMap.get(part).intValue();
+	    if (numHits > max || best==null) {
+		max = numHits;
+		best = part;
+	    }
+	}
+	return best;
+    }
+
+    // Duplicate code w.r.t. PerfectClusterer
+    protected Set<MCParticle> findMCParticles(CalorimeterHit hit, List<MCParticle> mcList)
+    {
+	if ( ! (hit instanceof SimCalorimeterHit) ) {
+	    throw new AssertionError("Non-simulated hit!");
+	} else {
+	    SimCalorimeterHit simHit = (SimCalorimeterHit) (hit);
+	    Set<MCParticle> contributingParticlesFromList = new HashSet<MCParticle>();
+	    int nContributingParticles = simHit.getMCParticleCount();
+	    for (int i=0; i<nContributingParticles; i++) {
+		MCParticle part = simHit.getMCParticle(i);
+		List<MCParticle> parentsInList = findParentsInList(part, mcList);
+		contributingParticlesFromList.addAll(parentsInList);
+	    }
+	    return contributingParticlesFromList;
+	}
+    }
+    
+    protected List<MCParticle> findParentsInList(MCParticle part, List<MCParticle> mcList) 
+    {
+	List<MCParticle> outputList = new Vector<MCParticle>();
+	if (mcList.contains(part)) {
+	    // Already in there
+	    outputList.add(part);
+	} else {
+	    // Not in there -- recurse up through parents
+	    List<MCParticle> parents = part.getParents();
+	    if (parents.size()==0) {
+		// Ran out of options -- add nothing and return below
+	    } else {
+		for (MCParticle parent : parents) {
+		    List<MCParticle> ancestorsInList = findParentsInList(parent, mcList);
+		    outputList.addAll(ancestorsInList);
+		}
+	    }
+	}
+	return outputList;
+    }   
+    
+    public void setInputClusterList(String name) {
+	m_inputClusterListName = name;
+    }
+    public void setOutputParticleList(String name) {
+	m_outputParticleListName = name;
+    }
+    public void setMCParticleList(String name) {
+	m_mcName = name;
+    }
+    public void setInputTrackList(String name) {
+	m_inputTrackListName = name;
+    }
+    
+    String m_inputTrackListName;
+    String m_inputClusterListName;
+    String m_outputParticleListName;
+    String m_mcName;
+
+    // This should be replaced by a central implementation
+    private class LocalReconstructedParticle extends BaseReconstructedParticle
+    {
+	public void setEnergy(double e) {
+	    _fourVec.setT(e);
+	}
+	public void setMomentum(Hep3Vector p3) {
+	    _fourVec.setV3(_fourVec.t(), p3);
+	}
+	public void setReferencePoint(Hep3Vector p3) {
+	    _referencePoint = p3;
+	}
+	public void setCharge(double q) {
+	    _charge = q;
+	}
+    }
+}
CVSspam 0.2.8