Print

Print


Commit in lcsim/src/org/lcsim/contrib/proulx/eventoverlay on MAIN
EventOverlay.java+355added 1.1
GuineapigOverlayDriver.java+120added 1.1
+475
2 added files
code to merge and remove mc information from simulated guineapig events; and to overlay these events with other events -- rough draft

lcsim/src/org/lcsim/contrib/proulx/eventoverlay
EventOverlay.java added at 1.1
diff -N EventOverlay.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ EventOverlay.java	21 Feb 2007 18:20:53 -0000	1.1
@@ -0,0 +1,355 @@
+package org.lcsim.contrib.proulx.eventoverlay;
+
+import java.io.*;
+import java.lang.*;
+import java.util.*;
+import org.lcsim.util.lcio.*;
+import org.lcsim.event.*;
+import org.lcsim.geometry.*;
+
+public class EventOverlay
+{
+  private HashMap<String,HitCollectionOverlay> map
+    = new HashMap<String,HitCollectionOverlay>();
+
+  public static void main(String [] args) throws Exception
+  {
+    LCIOWriter writer
+      = new LCIOWriter(new File("eventoverlay/testEventOverlay.slcio"));
+    for (int i=0; i<args.length; ++i) {
+      EventOverlay overlay = new EventOverlay();
+      EventHeader e = overlay.getGuineapigEvent(args[i]);
+      //(new TestDriver()).process(e);
+      writer.write(e);
+    }
+    writer.close();
+    /* -- debug code to test storage of calorimeter hit ids
+    LCIOReader reader
+      = new LCIOReader(new File("eventoverlay/testEventOverlay.slcio"));
+    EventHeader eNew = reader.read();
+    (new TestDriver()).process(eNew);
+    */
+  }
+
+  public EventHeader getGuineapigEvent(String filename) throws Exception
+  {
+    LCIOReader reader = new LCIOReader(new File(filename));
+    EventHeader e = null;
+    boolean eof = false;
+    int iEvent = 0;
+    System.out.println("\nProcessing file: "+filename);
+    while (!eof) {
+      try {
+	EventHeader currentEvent = reader.read();
+	System.out.println("  reading event "+(iEvent++));
+	updateLists(currentEvent);
+	if (e==null) e = currentEvent;
+      }
+      catch (IOException ioe) {
+	eof = true;
+      }
+    }
+
+    // replace the calorimeter hits
+    for (String collectionName : map.keySet()) {
+      e.remove(collectionName);
+      e.put(collectionName,
+	    map.get(collectionName).getHits(),
+	    map.get(collectionName).meta.getType(),
+	    map.get(collectionName).meta.getFlags(),
+	    collectionName);
+    }
+    // remove the mc particles
+    for (List<MCParticle> list : e.get(MCParticle.class)) {
+      e.remove(e.getMetaData(list).getName());
+    }
+    
+    return e;
+  }
+
+  public void overlayGuineapigEvent(EventHeader event,
+				    EventHeader gpEvent)
+  {
+    map.clear();
+    updateLists(event, true);
+    updateLists(gpEvent, false);
+    
+    // replace the calorimeter hits
+    for (String collectionName : map.keySet()) {
+      //event.remove(collectionName);
+      event.put(collectionName+"Overlay",
+		map.get(collectionName).getHits(),
+		map.get(collectionName).meta.getType(),
+		map.get(collectionName).meta.getFlags(),
+		collectionName+"Overlay");
+    }
+
+    map.clear();
+    updateLists(gpEvent, false);
+    for (String collectionName : map.keySet()) {
+      //event.remove(collectionName);
+      event.put(collectionName+"Guineapig",
+		map.get(collectionName).getHits(),
+		map.get(collectionName).meta.getType(),
+		map.get(collectionName).meta.getFlags(),
+		collectionName+"Guineapig");
+    }
+    //return event;
+  }
+
+  private void updateLists(EventHeader e)
+  {
+    this.updateLists(e, false);
+  }
+
+  private void updateLists(EventHeader e,
+			   boolean retainMCInformation)
+  {
+    for (List<SimCalorimeterHit> hitList : e.get(SimCalorimeterHit.class)) {
+      EventHeader.LCMetaData meta = e.getMetaData(hitList);
+      String name = meta.getName();
+      if (name.equals("LuminosityMonitorHits")) {
+	System.out.println("    Updating hit collection "+name);
+	System.out.println("      SIZE  =  "+hitList.size());
+      }
+      if (! map.containsKey(name)) {
+	map.put(name,new HitCollectionOverlay());
+	map.get(name).meta = meta;
+      }
+      int sizeBefore = map.get(name).getNHits();
+      map.get(name).addHits(hitList, retainMCInformation);
+      int sizeAfter = map.get(name).getNHits();
+      if (name.equals("LuminosityMonitorHits")) {
+	System.out.println("      nNew/nHits = "+(sizeAfter-sizeBefore)+
+			   "/"+sizeAfter);
+      }
+    }
+  }
+}
+
+class HitCollectionOverlay
+{
+  public EventHeader.LCMetaData meta;
+
+  private int NMAXHITS = 10000;
+  private Vector<SimCalorimeterHitOverlay> hits
+    = new Vector<SimCalorimeterHitOverlay>(NMAXHITS);
+  //private double [] energy = new double[NMAXHITS];
+  private long [] ids = new long[NMAXHITS];
+  private int nHits = 0;
+
+  //public HitCollectionOverlay()
+  //{
+  //for (int i=0; i<NMAXHITS; ++i) {
+  //ids[i] = 0;
+  //}
+  //}
+
+  public void addHits(List<SimCalorimeterHit> newHits,
+		      boolean retainMCInformation)
+  {
+    int nNew = 0;
+    for (SimCalorimeterHit h : newHits) {
+      //System.out.println("hit");
+      
+      long id = h.getCellID();
+      int i;
+      // find the index of this hit (or whether it's new)
+      for (i=0; i<nHits && id != ids[i]; ++i) ;
+      if (i==nHits) {
+	hits.add(new SimCalorimeterHitOverlay(h,retainMCInformation));
+	ids[i] = id;
+	nHits++;
+	nNew++;
+	if (nHits == NMAXHITS) { resizeIDArray(); }
+      } else {
+	hits.get(i).overlayHit(h);
+      }
+      //energy[i] += h.getRawEnergy();
+    }
+    //System.out.println("  nNew/nHits = "+nNew+"/"+nHits);
+  }
+
+  private void resizeIDArray()
+  {
+    NMAXHITS *= 2;
+    long [] newIDArray = new long[NMAXHITS];
+    for (int i=0; i<nHits; ++i) {
+      newIDArray[i] = ids[i];
+    }
+    ids = newIDArray;
+  }
+  
+  public int getNHits()
+  {
+    return nHits;
+  }
+
+  public List<SimCalorimeterHit> getHits()
+  {
+    Vector<SimCalorimeterHit> simHits
+      = new Vector<SimCalorimeterHit>(hits.size());
+    simHits.addAll(hits);
+    return simHits;
+  }
+}
+
+class SimCalorimeterHitOverlay implements SimCalorimeterHit
+{
+  // fields for SimCalorimeterHit interface
+  private double [] contributedEnergy = null;
+  private double [] contributedTime = null;
+  private MCParticle [] mcParticles = null;
+  private int mcParticleCount = 0;
+
+  // fields for CalorimeterHit interface
+  private long cellID = -1;
+  private double correctedEnergy = 0;
+  private IDDecoder decoder = null;
+  private EventHeader.LCMetaData meta = null;
+  private double [] position = new double[3];
+  private double rawEnergy = 0;
+  private Subdetector sub = null;
+  private double time = 0;
+
+
+  private SimCalorimeterHitOverlay()
+  {
+  }
+
+  public SimCalorimeterHitOverlay(SimCalorimeterHit hit1,
+				  SimCalorimeterHit hit2)
+  {
+    this(hit1, hit2, true);
+  }
+
+  public SimCalorimeterHitOverlay(SimCalorimeterHit hit1,
+				  SimCalorimeterHit hit2,
+				  boolean retainMCInformation)
+  {
+    this(hit1, retainMCInformation);
+    this.overlayHit(hit2);
+  }
+
+  public SimCalorimeterHitOverlay(SimCalorimeterHit hit)
+  {
+    this(hit, true);
+  }
+
+  public SimCalorimeterHitOverlay(SimCalorimeterHit hit,
+				  boolean retainMCInformation)
+  {
+    if (retainMCInformation) {
+      setMCInformation(hit);
+    }
+
+    cellID          = hit.getCellID();
+    //correctedEnergy = hit.getCorrectedEnergy();
+    correctedEnergy = hit.getRawEnergy();
+    decoder         = hit.getIDDecoder();
+    meta            = hit.getLCMetaData();
+    position[0]     = hit.getPosition()[0];
+    position[1]     = hit.getPosition()[1];
+    position[2]     = hit.getPosition()[2];
+    rawEnergy       = hit.getRawEnergy();
+    sub             = hit.getSubdetector();
+    time            = hit.getTime();
+  }
+
+  private void setMCInformation(SimCalorimeterHit hit)
+  {
+    mcParticleCount = hit.getMCParticleCount();
+
+    contributedEnergy = new double[mcParticleCount];
+    contributedTime = new double[mcParticleCount];
+    mcParticles = new MCParticle[mcParticleCount];
+
+    for (int i=0; i<mcParticleCount; ++i) {
+      contributedEnergy[i] = hit.getContributedEnergy(i);
+      contributedTime[i]   = hit.getContributedTime(i);
+      mcParticles[i]       = hit.getMCParticle(i);
+    }
+  }
+
+  public void overlayHit(SimCalorimeterHit hit)
+  {
+    // will NOT 
+    // time averaged over contributing hits, weighted by energy (not important)
+    time = hit.getTime()*hit.getRawEnergy() + this.getTime()*this.getRawEnergy();
+    // increment energy
+    //correctedEnergy += hit.getCorrectedEnergy();
+    correctedEnergy += hit.getRawEnergy();
+    rawEnergy       += hit.getRawEnergy();
+
+    if (this.getRawEnergy() != 0) {
+      time /= this.getRawEnergy();
+    }
+  }
+
+  // methods required by SimCalorimeterHit
+  public double getContributedEnergy(int index)
+  {
+    return contributedEnergy[index];
+  }
+
+  public double getContributedTime(int index)
+  {
+    return contributedTime[index];
+  }
+
+  public MCParticle getMCParticle(int index)
+  {
+    return mcParticles[index];
+  }
+
+  public int getMCParticleCount()
+  {
+    return mcParticleCount;
+  }
+
+  public int getPDG(int index) 
+  {
+    return mcParticles[index].getPDGID();
+  }
+
+  // methods required by CalorimeterHit
+  public long getCellID()
+  {
+    return cellID;
+  }
+
+  public double getCorrectedEnergy()
+  {
+    return correctedEnergy;
+  }
+
+  public IDDecoder getIDDecoder()
+  {
+    return decoder;
+  }
+
+  public EventHeader.LCMetaData getLCMetaData()
+  {
+    return meta;
+  }
+
+  public double[] getPosition()
+  {
+    return position;
+  }
+
+  public double getRawEnergy()
+  {
+    return rawEnergy;
+  }
+
+  public Subdetector getSubdetector()
+  {
+    return sub;
+  }
+
+  public double getTime()
+  {
+    return time;
+  }
+}

lcsim/src/org/lcsim/contrib/proulx/eventoverlay
GuineapigOverlayDriver.java added at 1.1
diff -N GuineapigOverlayDriver.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ GuineapigOverlayDriver.java	21 Feb 2007 18:20:53 -0000	1.1
@@ -0,0 +1,120 @@
+package org.lcsim.contrib.proulx.eventoverlay;
+
+import java.io.*;
+import java.lang.*;
+import java.util.*;
+import org.lcsim.util.lcio.*;
+import org.lcsim.event.*;
+import org.lcsim.util.*;
+import org.lcsim.geometry.*;
+
+public class GuineapigOverlayDriver extends Driver
+{
+  private static Random rand = new Random();
+  private EventOverlay overlay = new EventOverlay();
+  private int nevents = 0;
+  private String gpFileName;
+
+  public static void main(String [] args) throws Exception
+  {
+    LCIOWriter writer
+      = new LCIOWriter(new File("eventoverlay/test_BDK_GP_overlay.slcio"));
+    GuineapigOverlayDriver d = new GuineapigOverlayDriver();
+    for (int iFile = 0; iFile < args.length; ++iFile) {
+      System.out.println("Reading events from file "+args[iFile]);
+
+      // count the events in the file
+      LCIOReader reader = new LCIOReader(new File(args[iFile]));
+      boolean eof = false;
+      int nevents = 0;
+      while (!eof) {
+	try {
+	  reader.skipEvents(1);
+	  nevents++;
+	}
+	catch (IOException ioe) {
+	  eof = true;
+	}
+      }
+
+      //
+      reader = new LCIOReader(new File(args[iFile]));
+      eof = false;
+      int ievent = 0;
+      while (!eof) {
+	try {
+	  if (ievent<nevents) {
+	    System.out.println("  Overlaying event "+(ievent++)+
+			       " of "+nevents);
+	  }
+	  EventHeader e = reader.read();
+	  d.process(e);
+	  writer.write(e);
+	}
+	catch (IOException ioe) {
+	  eof = true;
+	}
+      }
+    }
+  }
+
+  public GuineapigOverlayDriver() throws Exception
+  {
+    this("/nfs/data18/nlc/proulx/java/eventoverlay/testEventOverlay.slcio");
+  }
+
+  public GuineapigOverlayDriver(String filename, int nEvents)
+  {
+    gpFileName = filename;
+    nevents = nEvents;
+  }
+
+  public GuineapigOverlayDriver(String filename) throws Exception
+  {
+    gpFileName = filename;
+    nevents = 0;
+
+    System.out.println("initializing number of events");
+    LCIOReader reader = new LCIOReader(new File(gpFileName));
+    boolean eof = false;
+    while (!eof) {
+      try {
+	reader.skipEvents(1);
+	nevents++;
+      }
+      catch (IOException ioe) {
+	eof = true;
+      }
+    }
+    System.out.println("nevents = "+nevents);
+    System.out.println("done with GuineapigOverlayDriver initialization.");
+  }
+
+  public void process(EventHeader event)
+  {
+    EventHeader gpEvent = getGPEvent(rand.nextInt(nevents));
+    if (gpEvent != null) {
+      overlay.overlayGuineapigEvent(event,gpEvent);
+    }
+  }
+
+  private EventHeader getGPEvent(int iEvent)
+  {
+    try {
+      LCIOReader reader = new LCIOReader(new File(gpFileName));
+      reader.skipEvents(iEvent);
+      return reader.read();
+    }
+    catch (Exception e) {
+      System.err.println("Exception while looking up guinea pig file:");
+      System.err.println(e.toString());
+      e.printStackTrace(System.err);
+    }
+    return null;
+  }
+
+  public static void setRandomNumberSeed(long seed)
+  {
+    rand.setSeed(seed);
+  }
+}
CVSspam 0.2.8