Print

Print


Commit in lcsim/src/org/lcsim/recon/pfa/structural on MAIN
HitBookKeeper.java+159added 1.1
Tool for book-keeping: make sure that the number of hits is consistent throughout a multistep PFA algorithm.

lcsim/src/org/lcsim/recon/pfa/structural
HitBookKeeper.java added at 1.1
diff -N HitBookKeeper.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ HitBookKeeper.java	5 Jul 2006 00:17:24 -0000	1.1
@@ -0,0 +1,159 @@
+package org.lcsim.recon.pfa.structural;
+
+import java.util.List;
+import java.util.Vector;
+import java.util.Set;
+import java.util.Map;
+import java.util.HashSet;
+import java.lang.String;
+import org.lcsim.event.EventHeader;
+import org.lcsim.util.Driver;
+
+import org.lcsim.event.ReconstructedParticle;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.Cluster;
+import org.lcsim.util.hitmap.HitMap;
+
+/**
+ * Utility class for PFAs:
+ * Make sure that the book-keeping is OK for hits.
+ * The user supplies groups of named lists, such that
+ * each list should contain the same number of hits.
+ * For example, the first group might be HitMaps of all hits in
+ * the calorimeter before any clustering, and the
+ * second group might be a list of clusters plus a
+ * HitMap of left-over hits.
+ *
+ * @version $Id: HitBookKeeper.java,v 1.1 2006/07/05 00:17:24 mcharles Exp $
+ */
+
+public class HitBookKeeper extends Driver
+{
+    boolean m_debug = false;
+    public void setDebug(boolean debug) { m_debug = debug; }
+
+    boolean m_requireAllHitsMatch = true;
+    boolean m_requireUniqueHitsMatch = true;
+    public void requireAllHitsMatch(boolean b) { m_requireAllHitsMatch = b; }
+    public void requireUniqueHitsMatch(boolean b) { m_requireUniqueHitsMatch = b; }
+
+    public HitBookKeeper() {
+	m_namedLists = new Vector<List<String>>();
+    }
+
+    public void addListOfNamedLists(String[] list) {
+	List<String> newListOfNamedLists = new Vector<String> ();
+	for (String item : list) { 
+	    newListOfNamedLists.add(item);
+	}
+	m_namedLists.add(newListOfNamedLists);
+    }
+
+    /** Check that lists are consistent in this event. */
+    public void process(EventHeader event)
+    {
+	List<Integer> hitCountInList = new Vector<Integer>();
+	List<Integer> uniqueHitCountInList = new Vector<Integer>();
+	for (List<String> listOfNamedLists : m_namedLists) {
+	    // How many hits in these lists in total?
+	    Set<CalorimeterHit> hitSet = new HashSet<CalorimeterHit>();
+	    List<CalorimeterHit> hitList = new Vector<CalorimeterHit>();
+	    for (String nameOfList : listOfNamedLists) {
+		List<CalorimeterHit> hitsInThisList = findHits(event, nameOfList);
+		hitList.addAll(hitsInThisList);
+		hitSet.addAll(hitsInThisList);
+	    }
+	    hitCountInList.add(new Integer(hitList.size()));
+	    uniqueHitCountInList.add(new Integer(hitSet.size()));
+	    if (m_debug) {
+		System.out.println("DEBUG: In this group, there were "+hitList.size()+" hits ("+hitSet.size()+" unique)");
+	    }
+	}
+
+	assert(hitCountInList.size() == uniqueHitCountInList.size());
+
+	if (hitCountInList.size()<2) {
+	    // No possibility of disagreement
+	    if (m_debug) {
+		System.out.println("DEBUG: Only "+hitCountInList.size()+" group(s) => not doing consistency check");
+	    }
+	    return;
+	} else {
+	    // Check.
+	    if (m_requireAllHitsMatch) {
+		int hitCount = hitCountInList.get(0);
+		for (Integer tmp : hitCountInList) {
+		    if (tmp != hitCount) {
+			throw new AssertionError("Total hit count in group is "+tmp+" but hit count at start was "+hitCount+" -- mismatch.");
+		    }
+		}
+		if (m_debug) { System.out.println("DEBUG: Total hit count consistently "+hitCount+" over "+hitCountInList.size()+" groups."); }
+	    } else {
+		if (m_debug) { System.out.println("DEBUG: Ignoring total hit counts."); }
+	    }
+	    if (m_requireAllHitsMatch) {
+		int uniqueHitCount = uniqueHitCountInList.get(0);
+		for (Integer tmp : uniqueHitCountInList) {
+		    if (tmp != uniqueHitCount) {
+			throw new AssertionError("Unique hit count in group is "+tmp+" but unique hit count at start was "+uniqueHitCount+" -- mismatch.");
+		    }
+		}
+		if (m_debug) { System.out.println("DEBUG: Total hit count consistently "+uniqueHitCount+" over "+uniqueHitCountInList.size()+" groups."); }
+	    } else {
+		if (m_debug) { System.out.println("DEBUG: Ignoring unique hit counts."); }
+	    }
+	    boolean m_requireAllHitsMatch = true;
+	    boolean m_requireUniqueHitsMatch = true;
+	}
+    }
+
+    protected List<CalorimeterHit> findHits(EventHeader event, String name) 
+    {
+	// First, try a List:
+	Object mainObj = event.get(name);
+	if (mainObj instanceof List) {
+	    List<CalorimeterHit> output = new Vector<CalorimeterHit>();
+	    List rawList = (List) (mainObj);
+	    for (Object obj : rawList) {
+		// Try <CalorimeterHit>
+		if (obj instanceof CalorimeterHit) {
+		    CalorimeterHit hit = (CalorimeterHit) (obj);
+		    output.add(hit);
+		} else if (obj instanceof Cluster) {
+		    Cluster clus = (Cluster) (obj);
+		    output.addAll(clus.getCalorimeterHits());
+		} else if (obj instanceof ReconstructedParticle) {
+		    ReconstructedParticle part = (ReconstructedParticle)(obj);
+		    for (Cluster clus : part.getClusters()) {
+			output.addAll(clus.getCalorimeterHits());
+		    }
+		} else {
+		    throw new AssertionError("I don't know how to handle an object of class '"+obj.getClass().getName()+"' named "+name);
+		}
+	    }
+	    if (m_debug) {
+		Set tmpSet = new HashSet();
+		tmpSet.addAll(output);
+		System.out.println("DEBUG:     In the list of hits named '"+name+"', I found "+output.size()+" hits ("+tmpSet.size()+" unique)");
+	    }
+	    return output;
+	} else if (mainObj instanceof HitMap) {
+	    List<CalorimeterHit> output = new Vector<CalorimeterHit>();
+	    HitMap map = (HitMap) (mainObj);
+	    output.addAll(map.values());
+	    if (m_debug) {
+		Set tmpSet = new HashSet();
+		tmpSet.addAll(output);
+		System.out.println("DEBUG:     In the hitmap named '"+name+"', I found "+output.size()+" hits ("+tmpSet.size()+" unique)");
+	    }
+	    return output;
+	} else {
+	    throw new AssertionError("Couldn't handle an object of type "+mainObj.getClass().getName()+" named "+name);
+	}
+    }
+
+
+    // We have several lists of named lists
+    // Each list of named lists should have the same # hits
+    List<List<String>> m_namedLists;
+}
CVSspam 0.2.8