Print

Print


Commit in lcsim/src/org/lcsim/recon/cluster/util on MAIN
CalHitMapDriver.java+57added 1.1
CalHitMapMgr.java+157added 1.1
+214
2 added files
GL: General utilities, moved from digisim to recon.cluster.util

lcsim/src/org/lcsim/recon/cluster/util
CalHitMapDriver.java added at 1.1
diff -N CalHitMapDriver.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CalHitMapDriver.java	7 Dec 2005 16:56:02 -0000	1.1
@@ -0,0 +1,57 @@
+package org.lcsim.recon.cluster.util;
+
+import org.lcsim.util.Driver;
+import org.lcsim.event.EventHeader;
+
+/**
+ * A helper driver to build calorimeter hit maps.
+ * This class uses CalHitMapMgr for filling and delivering the hit maps,
+ * so this class is basically a Driver for the org.lcsim framework.
+ *
+ * @author Guilherme Lima
+ * @version $Id: CalHitMapDriver.java,v 1.1 2005/12/07 16:56:02 lima Exp $
+ * @see CalHitMapMgr
+ */
+public class CalHitMapDriver extends Driver {
+
+    /** constructor */
+    public CalHitMapDriver() {
+//    _description=" A singleton providing fast data access to cal hits via maps:\n no need to fill those maps yourself.";
+
+      _mgr = CalHitMapMgr.getInstance();
+      assert _mgr!=null : "";
+    }
+
+    /** Called at the begin of job before anything is read.
+     * Used to initialize the driver, e.g. book histograms.
+     */
+    public void init() {
+	System.out.println("CalHitMapDriver.init() called");
+	assert CalHitMapMgr.getInstance() != null
+	    : "Error: No CalHitMapMgr instance available.";
+    }
+
+//     /** Called for every run.
+//      */
+//     public void processRunHeader( LCRunHeader run ) {};
+
+    /** Called for every event - the working horse.
+     */
+    protected void process( EventHeader evt ) {
+      System.out.println("CalHitMapDriver.process() called");
+      _mgr.setEvent(evt);
+    }
+
+    /** Called after data processing for clean up.
+     */
+    public void end() { }
+
+    //*** FIELDS ***
+
+    /** Run number */
+    private int _nRun;
+    /** Event number */
+    private int _nEvt;
+    /** Reference to the singleton class CalHitMapMgr */
+    private CalHitMapMgr _mgr;
+}

lcsim/src/org/lcsim/recon/cluster/util
CalHitMapMgr.java added at 1.1
diff -N CalHitMapMgr.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CalHitMapMgr.java	7 Dec 2005 16:56:02 -0000	1.1
@@ -0,0 +1,157 @@
+package org.lcsim.recon.cluster.util;
+
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.geometry.Detector;
+import org.lcsim.geometry.compact.Subdetector;
+import org.lcsim.geometry.compact.Readout;
+
+/**
+ * Arranges calorimeter hits in maps keyed by cellID.  Hits can then
+ * be randomly accessed from the cellID.  Once filled, the maps can be
+ * accessed from *any* class, using e.g.<p>
+ *
+ *   <code>Map&lt;Long,SimCalorimeterHit&gt; hitmap = CalHitMapMgr.getInstance().getCollHitMap("EcalBarrHits");</code>
+ * 
+ * <p><em>Note:</em> <code>CalHitMapDriver</code> needs to be
+ * processed before the data is available to other classes.
+ *
+ * @author Guilherme Lima
+ * @version $Id: CalHitMapMgr.java,v 1.1 2005/12/07 16:56:02 lima Exp $
+ * @see CalHitMapDriver
+ */
+public class CalHitMapMgr {
+
+    /**
+     * Return the instance of CalHitMapMgr (to be used instead of
+     * constructor)
+     */
+    public static CalHitMapMgr getInstance() {
+	if( _me == null ) _me = new CalHitMapMgr();
+	assert _me != null : "Problem constructing CalHitMapMgr.";
+	return _me;
+    }
+
+    /**
+     * Sets current event and fill subdetectors map.  This should be
+     * always called first, before any hit map can be requested.  A
+     * driver exists for this purpose, called CalHitMapDriver.
+     * @see CalHitMapDriver
+     */
+    public void setEvent(EventHeader event) {
+      if( _event!=event || event.getEventNumber()!=_evtno
+	  || event.getRunNumber()!=_runno ) {
+	_me.reset();
+	_me._event = event;
+	_me._runno = event.getRunNumber();
+	_me._evtno = event.getEventNumber();
+
+	// fill subdetectors map
+	Detector det = _event.getDetector();
+	for( Subdetector idet : det.getSubdetectors().values() ) {
+	  Readout ro = idet.getReadout();
+	  if( ro != null ) _subdets.put( ro.getName(), idet );
+	}
+      }
+    }
+
+    public EventHeader getEvent() {
+	return _event;
+    }
+
+    public Detector getDetector() {
+	return _event.getDetector();
+    }
+
+    public Subdetector getSubdetector(String readout) {
+	return _subdets.get(readout);
+    }
+
+    /**
+     * Returns a hit map, with all hits contained in a given collection.
+     * @param colName name of collection with data to be returned
+     */
+    public final Map<Long,SimCalorimeterHit> getCollHitMap(final String colName) {
+	Map<Long,SimCalorimeterHit> retColl = _collMap.get( colName );
+	if(retColl==null) {
+	    // if it does not exist, create it now
+	    retColl = new HashMap<Long,SimCalorimeterHit>();
+	    _collMap.put(colName, retColl);
+	}
+	assert retColl!=null : "retColl is empty for colName="+colName;
+	if( retColl.size()==0 ) fillHitMap( colName, retColl );
+	return retColl;
+    }
+
+    /** Private constructor */
+    protected CalHitMapMgr() {
+	_collMap = new HashMap< String, Map<Long,SimCalorimeterHit> >();
+    }
+
+    /** Clear hit maps */
+    private void reset() {
+	// Loop over all existing hit collections
+	for( String key : _collMap.keySet() ) {
+	    // clear hit maps for each subdetector collection
+	    Map<Long,SimCalorimeterHit> subDetHitMap = _collMap.get(key);
+	    subDetHitMap.clear();
+	}
+	_collMap.clear();
+	_subdets.clear();
+    }
+
+    /**
+     * Fills hit maps using data from current event.
+     * @param colName name of hit collection to extract the hits from
+     * @param hitmap output map containing
+     * @see CalHitMapMgr.setEvent
+     */
+    private void fillHitMap( final String colName,
+			     Map<Long,SimCalorimeterHit> hitmap )  {
+
+ 	assert _event!=null : "No event seems to be available.";
+
+	// First fill the hit map without defining densities,
+	// as the map may be used for calculating densities
+	List<SimCalorimeterHit> hits = null;
+	try {
+	    hits = _event.get( SimCalorimeterHit.class, colName );
+	}
+// 	catch(DataNotAvailableException e) {
+	catch(Exception e) {
+// 	    System.out.println(" Evt "+_event.getEventNumber()
+// 			       +": No data in collection "+colName);
+	    // This happens quite frequently with tail catcher,
+	    // when no energy leaks into it
+	}
+	// return if no collection in event
+	if(hits==null) return;
+
+	for( int i = 0; i<hits.size(); ++i ) {
+	    SimCalorimeterHit ihit = hits.get(i);
+	    long cellid = ihit.getCellID();
+
+	    // fill hit map
+	    hitmap.put( cellid, ihit );
+	}
+    }
+
+    //=== FIELDS ===
+    /** Singleton instance */
+    private static CalHitMapMgr _me = new CalHitMapMgr();
+    /** Current event */
+    private EventHeader _event;
+    /** Run number */
+    private int _runno;
+    /** Event number */
+    private int _evtno;
+    /** Map of hit maps, keyed by collection name where hits come from */
+    private Map< String, Map<Long, SimCalorimeterHit> > _collMap;
+    /** Map of subdetectors keyed by collection names */
+    private Map<String,Subdetector> _subdets = new HashMap<String,Subdetector>();
+    private Map<Integer,List<SimCalorimeterHit>> _layerHitmap;
+}
CVSspam 0.2.8