Commit in lcsim/src/org/lcsim/digisim on MAIN
SimCalorimeterHitsDriver.java+162added 1.1
GL: New driver to create SimCalorimeterHits from RawCalorimeterHits

lcsim/src/org/lcsim/digisim
SimCalorimeterHitsDriver.java added at 1.1
diff -N SimCalorimeterHitsDriver.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ SimCalorimeterHitsDriver.java	20 Jan 2006 23:34:50 -0000	1.1
@@ -0,0 +1,162 @@
+package org.lcsim.digisim;
+
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.HashMap;
+import org.lcsim.util.Driver;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.EventHeader.LCMetaData;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.event.RawCalorimeterHit;
+import org.lcsim.event.LCRelation;
+import org.lcsim.event.MCParticle;
+import org.lcsim.util.lcio.LCIOConstants;
+import org.lcsim.geometry.IDDecoder;
+
+/**
+ * A driver to build SimCalorimeterHit collections from raw hit collections.
+ *
+ * @author Guilherme Lima
+ * @version $Id: SimCalorimeterHitsDriver.java,v 1.1 2006/01/20 23:34:50 lima Exp $
+ */
+public class SimCalorimeterHitsDriver extends Driver {
+
+    /** constructor */
+    public SimCalorimeterHitsDriver() {
+    }
+
+    /** Called at the before first event, or at rewind.
+     * Used to initialize the driver, e.g. book histograms.
+     */
+    public void startOfData() {
+    }
+
+//     /** Called for every run
+//      */
+//     public void processRunHeader( LCRunHeader run ) { }
+
+    /** Called for every event
+     */
+    protected void process( EventHeader event ) {
+      // get all raw hits in event
+      List<List<RawCalorimeterHit>> rawHitCollections
+	  = event.get(RawCalorimeterHit.class);
+
+      for( List<RawCalorimeterHit> rawCollection : rawHitCollections ) {
+	  processRawCollection(event, rawCollection);
+      }
+    }
+
+    /** 
+     * Create a collection of simulated hits, from a collection of raw
+     * hits into a collection of simulated hits, and appends the new
+     * collection to the event.  The new collection is named by
+     * replacing the string "Raw" in the input collection with "Digi"
+     * in the output collection.
+     * @param collection input collection of raw hits
+    */
+    private void processRawCollection(EventHeader event, List<RawCalorimeterHit> collection) {
+	// get metadata from simHits collection
+	String rawName = event.getMetaData(collection).getName();
+	String simName = rawName.replace("Raw", "");
+	String newName = rawName.replace("Raw", "Digi");
+
+        // get LCRelations collection
+	String linkName = rawName.replace("RawHits", "Raw2sim");
+	List<LCRelation> raw2SimLinks = event.get(LCRelation.class, linkName);
+
+	// build a map of raw -> sim hits
+	Map<RawCalorimeterHit,List<LCRelation>> linksMap;
+	linksMap = this.buildLinksMap(raw2SimLinks);
+
+	// create digitized SimCalHits collection from RawCalHits collection
+	List<SimCalorimeterHit> digiColl = new ArrayList<SimCalorimeterHit>();
+	for( RawCalorimeterHit rawhit : collection ) {
+	  // create a new (digitized) SimCalHit
+	  Raw2SimConverter raw2sim = new Raw2SimConverter( rawhit );
+
+	  // loop over raw2sim links to reconstruct MC info for a SimCalHit
+	  List<LCRelation> links = linksMap.get(rawhit);
+	  assert links!=null : "Are we re-digitizing a pre-digitized file?";
+	  double rawHitEne = rawhit.getAmplitude() * 1.0e-8;
+
+	  for( LCRelation link : links ) {
+	    SimCalorimeterHit simHit = (SimCalorimeterHit)link.getTo();
+	    double weight = link.getWeight();
+	    if(simHit!=null) {
+	      // a SimCalHit contributing to this RawCalHit (either
+	      // cell itself or a neighbor via crosstalk).
+	      // The energy contribution from this cell (weight*rawHitEne)
+	      // will be shared between the MCParticles according to their
+	      // contributions in the original SimCalHit.
+	      double eSimHit = simHit.getRawEnergy();
+
+	      // loop over contribs to simHit, adding contribs to rawHit
+	      int nmc = simHit.getMCParticleCount();
+// 	      if(nmc>1) System.out.println("*** new rawhit: nmc="+nmc+", E="+rawhit.getAmplitude()+", Time="+rawhit.getTimeStamp()+", simE="+simHit.getRawEnergy()*1e8);
+	      for(int imc=0; imc<nmc; ++imc) {
+		  MCParticle imcp = simHit.getMCParticle(imc);
+		  double imcEne = simHit.getContributedEnergy(imc);
+		  double imcTime = simHit.getContributedTime(imc);
+
+		  // add this MC contribution to the SimCalHit
+		  double eneContr = (imcEne/eSimHit) * weight * rawHitEne;
+		  raw2sim.addContribution(imcp, eneContr, imcTime);
+//  		  if(nmc>1) {
+// 		      System.out.println("mcp="+imcp.getType().getName()+", Econtr="+imcEne*1e8);
+// 		      if(imc==nmc-1) raw2sim.print();
+// 		  }
+	      }
+	    }
+	    else {
+	      // no valid simHit: noise
+	      double noiseE = weight*rawHitEne;
+	      double noiseT = rawhit.getTimeStamp()*1.0e-6;
+ 	      raw2sim.addContribution( null, noiseE, noiseT );
+	    }
+	  }
+	  SimCalorimeterHit newSimHit = raw2sim.convertToSimCalorimeterHit();
+	  digiColl.add( newSimHit );
+	}
+
+	// append collection to event
+	int flag = 1 << LCIOConstants.RCHBIT_ID1;
+	flag |= 1 << LCIOConstants.RCHBIT_TIME;
+	flag |= 1 << LCIOConstants.RCHBIT_NO_PTR;
+	event.put( newName, digiColl, SimCalorimeterHit.class, flag, simName );
+    }
+
+    /** Utility method to build a map of relationships between raw and
+     * simulated hits.
+     * @param links a collection of links
+     * @return a map of LCRelations, keyed by RawCalorimeterHits
+     */
+    public Map<RawCalorimeterHit,List<LCRelation>> buildLinksMap(final List<LCRelation> links)
+    {
+	// create map for output
+	Map<RawCalorimeterHit, List<LCRelation>> result
+	    = new HashMap<RawCalorimeterHit, List<LCRelation>>();
+
+	// loop over links
+	for( LCRelation rel : links ) {
+	    RawCalorimeterHit rawhit = (RawCalorimeterHit)rel.getFrom();
+
+	    List<LCRelation> relations = result.get(rawhit);
+	    if(relations==null) {
+		relations = new ArrayList<LCRelation>();
+		result.put(rawhit,relations);
+	    }
+	    relations.add( rel );
+	}
+
+	return result;
+    }
+
+
+    /** Called after data processing for clean up.
+     */
+    public void end() { }
+
+    //*** FIELDS ***
+}
CVSspam 0.2.8