Author: [log in to unmask] Date: Wed Jul 29 17:51:28 2015 New Revision: 3652 Log: Add support for setting event header parameters. Also apply formatting. Modified: projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseLCSimEvent.java Modified: projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseLCSimEvent.java ============================================================================= --- projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseLCSimEvent.java (original) +++ projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseLCSimEvent.java Wed Jul 29 17:51:28 2015 @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.IdentityHashMap; @@ -26,207 +25,23 @@ /** * A base implementation for EventHeader + * * @author Tony Johnson */ public class BaseLCSimEvent extends BaseEvent implements EventHeader { - private String detectorName; - private final Map<List, LCMetaData> metaDataMap = new IdentityHashMap<List, LCMetaData>(); - private ConditionsManager conditionsManager = ConditionsManager.defaultInstance(); - private static final int NANO_SECONDS = 1000000; - public static final String READOUT_NAME = "ReadoutName"; - - /** Creates a new instance of BaseLCSimEvent */ - public BaseLCSimEvent(int run, int event, String detectorName) { - this(run, event, detectorName, System.currentTimeMillis() * NANO_SECONDS); - } - - public BaseLCSimEvent(int run, int event, String detectorName, long timeStamp) { - super(run, event, timeStamp); - this.detectorName = detectorName; - try { - conditionsManager.setDetector(detectorName, run); - } catch (ConditionsNotFoundException x) { - throw new RuntimeException(x); - } - } - - public BaseLCSimEvent(int run, int event, String detectorName, long timeStamp, boolean triggerConditionsUpdate) { - super(run, event, timeStamp); - this.detectorName = detectorName; - if (triggerConditionsUpdate) { - try { - conditionsManager.setDetector(detectorName, run); - } catch (ConditionsNotFoundException x) { - throw new RuntimeException(x); - } - } - } - - // public String getInputFile() - // { - // return (String)get("INPUT_FILE"); - // } - - public String toString() { - return "Run " + getRunNumber() + " Event " + getEventNumber() + " (" + new Date(getTimeStamp() / NANO_SECONDS) + ") Detector: " + detectorName; - } - - public Detector getDetector() { - return conditionsManager.getCachedConditions(Detector.class, "compact.xml").getCachedData(); - } - - public List<MCParticle> getMCParticles() { - return get(MCParticle.class, MC_PARTICLES); - } - - public <T> List<T> get(Class<T> type, String name) { - return (List<T>) get(name); - } - - public <T> List<List<T>> get(Class<T> type) { - List<List<T>> result = new ArrayList<List<T>>(); - for (Map.Entry<List, LCMetaData> entry : metaDataMap.entrySet()) { - if (type.isAssignableFrom(entry.getValue().getType())) { - result.add(entry.getKey()); - } - } - return result; - } - - public Collection<LCMetaData> getMetaData() { - return metaDataMap.values(); - } - - public Set<List> getLists() { - return metaDataMap.keySet(); - } - - public LCMetaData getMetaData(List x) { - return metaDataMap.get(x); - } - - public void put(String name, Object component) { - // Check if collection exists already which is an error. - if (hasItem(name)) { - throw new IllegalArgumentException("An item called " + name + " already exists in the event."); - } - super.put(name, component); - if (component instanceof List) { - List list = (List) component; - Class type = list.isEmpty() ? Object.class : list.get(0).getClass(); - for (Object o : list) { - if (!type.isAssignableFrom(o.getClass())) - type = Object.class; - } - metaDataMap.put(list, new MetaData(name, type, 0, null)); - } - } - - public void put(String name, List collection, Class type, int flags) { - put(name, collection, type, flags, null); - } - - public void put(String name, List collection, Class type, int flags, String readoutName) { - super.put(name, collection); - - LCMetaData meta = new MetaData(name, type, flags, readoutName); - metaDataMap.put(collection, meta); - - setCollectionMetaData(collection, type, meta); - } - - private void setCollectionMetaData(List collection, Class type, LCMetaData meta) { - // Set MetaData on collection objects if necessary. - if (Hit.class.isAssignableFrom(type)) { - for (Object o : collection) { - Hit hit = (Hit) o; - if (hit.getMetaData() == null) { - ((Hit) o).setMetaData(meta); - } - } - } - } - - public void put(String name, List collection, Class type, int flags, Map intMap, Map floatMap, Map stringMap) { - super.put(name, collection); - LCMetaData meta = new MetaData(name, type, flags, intMap, floatMap, stringMap); - metaDataMap.put(collection, meta); - } - - /** - * Removes a collection from the event. - */ - public void remove(String name) { - Object collection = get(name); - if (collection instanceof List) - metaDataMap.remove((List) collection); - super.keys().remove(name); - } - - public String getDetectorName() { - return detectorName; - } - - public boolean hasCollection(Class type, String name) { - if (!hasItem(name)) - return false; - Object collection = get(name); - if (!(collection instanceof List)) - return false; - return type.isAssignableFrom(metaDataMap.get(collection).getType()); - } - - public boolean hasCollection(Class type) { - for (LCMetaData meta : metaDataMap.values()) { - if (type.isAssignableFrom(meta.getType())) { - return true; - } - } - return false; - } - - public boolean hasItem(String name) { - return super.keys().contains(name); - } - - public Map<String, int[]> getIntegerParameters() { - return Collections.<String, int[]> emptyMap(); - - } - - public Map<String, float[]> getFloatParameters() { - return Collections.<String, float[]> emptyMap(); - - } - - public Map<String, String[]> getStringParameters() { - return Collections.<String, String[]> emptyMap(); - } - - public float getWeight() { - return 1.0f; - } - private class MetaData implements LCMetaData { + private transient IDDecoder dec; private int flags; + private Map<String, float[]> floatMap; + private Map<String, int[]> intMap; private final String name; + private Map<String, String[]> stringMap; private final Class type; - private Map<String, int[]> intMap; - private Map<String, float[]> floatMap; - private Map<String, String[]> stringMap; - private transient IDDecoder dec; - - MetaData(String name, Class type, int flags, String readoutName) { - this.name = name; - this.type = type; - this.flags = flags; - if (readoutName != null) - getStringParameters().put(READOUT_NAME, new String[] { readoutName }); - } - - MetaData(String name, Class type, int flags, Map intMap, Map floatMap, Map stringMap) { + + MetaData(final String name, final Class type, final int flags, final Map intMap, final Map floatMap, + final Map stringMap) { this.name = name; this.type = type; this.flags = flags; @@ -235,22 +50,33 @@ this.stringMap = stringMap; } - public int getFlags() { - return flags; - } - - public String getName() { - return name; - } - - public Class getType() { - return type; - } - - public org.lcsim.geometry.IDDecoder getIDDecoder() { - if (dec == null) - dec = findIDDecoder(); - return dec; + MetaData(final String name, final Class type, final int flags, final String readoutName) { + this.name = name; + this.type = type; + this.flags = flags; + if (readoutName != null) { + this.getStringParameters().put(READOUT_NAME, new String[] {readoutName}); + } + } + + /** + * Make an IDDecoder for this MetaData using the CellIDEncoding parameter. + * + * @return An IDDecoder made built from the CellIDEncoding. + */ + private IDDecoder createIDDecoderFromCellIDEncoding() { + final String[] cellIdEncoding = this.getStringParameters().get("CellIDEncoding"); + IDDecoder result = null; + if (cellIdEncoding != null) { + result = new BaseIDDecoder(); + try { + final IDDescriptor desc = new IDDescriptor(cellIdEncoding[0]); + result.setIDDescription(desc); + } catch (final IDDescriptor.IDException x) { + throw new RuntimeException(x); + } + } + return result; } public org.lcsim.geometry.IDDecoder findIDDecoder() { @@ -258,84 +84,308 @@ // use the name of the collection itself. String readoutName = name; if (stringMap != null) { - String[] names = stringMap.get(READOUT_NAME); - if (names != null && names.length >= 1) + final String[] names = stringMap.get(READOUT_NAME); + if (names != null && names.length >= 1) { readoutName = names[0]; + } } // Find the IDDecoder using the Detector. org.lcsim.geometry.IDDecoder result = null; try { - result = getDetector().getDecoder(readoutName); - } catch (RuntimeException x) { + result = BaseLCSimEvent.this.getDetector().getDecoder(readoutName); + } catch (final RuntimeException x) { } // Detector lookup failed. Attempt to use the CellIDEncoding collection parameter. - if (result == null) - result = createIDDecoderFromCellIDEncoding(); + if (result == null) { + result = this.createIDDecoderFromCellIDEncoding(); + } // If both methods failed, then there is a problem. - //if (result == null) - // throw new RuntimeException("Could not find or create an IDDecoder for the collection: " + name + ", readout: " + readoutName); + // if (result == null) + // throw new RuntimeException("Could not find or create an IDDecoder for the collection: " + name + + // ", readout: " + readoutName); return result; } - /** - * Make an IDDecoder for this MetaData using the CellIDEncoding parameter. - * @return An IDDecoder made built from the CellIDEncoding. - */ - private IDDecoder createIDDecoderFromCellIDEncoding() { - String[] cellIdEncoding = getStringParameters().get("CellIDEncoding"); - IDDecoder result = null; - if (cellIdEncoding != null) { - result = new BaseIDDecoder(); - try { - IDDescriptor desc = new IDDescriptor(cellIdEncoding[0]); - result.setIDDescription(desc); - } catch (IDDescriptor.IDException x) { - throw new RuntimeException(x); - } - } - return result; - } - - public Map<String, int[]> getIntegerParameters() { - if (intMap == null) - intMap = new HashMap<String, int[]>(); - return intMap; - } - - public Map<String, float[]> getFloatParameters() { - if (floatMap == null) - floatMap = new HashMap<String, float[]>(); - return floatMap; - } - - public Map<String, String[]> getStringParameters() { - if (stringMap == null) - stringMap = new HashMap<String, String[]>(); - return stringMap; - } - + @Override public EventHeader getEvent() { return BaseLCSimEvent.this; } - public void setSubset(boolean isSubset) { - flags = LCIOUtil.bitSet(flags, LCIOConstants.BITSubset, isSubset); - } - + @Override + public int getFlags() { + return flags; + } + + @Override + public Map<String, float[]> getFloatParameters() { + if (floatMap == null) { + floatMap = new HashMap<String, float[]>(); + } + return floatMap; + } + + @Override + public org.lcsim.geometry.IDDecoder getIDDecoder() { + if (dec == null) { + dec = this.findIDDecoder(); + } + return dec; + } + + @Override + public Map<String, int[]> getIntegerParameters() { + if (intMap == null) { + intMap = new HashMap<String, int[]>(); + } + return intMap; + } + + @Override + public String getName() { + return name; + } + + @Override + public Map<String, String[]> getStringParameters() { + if (stringMap == null) { + stringMap = new HashMap<String, String[]>(); + } + return stringMap; + } + + @Override + public Class getType() { + return type; + } + + @Override public boolean isSubset() { return LCIOUtil.bitTest(flags, LCIOConstants.BITSubset); } - public void setTransient(boolean isTransient) { - flags = LCIOUtil.bitSet(flags, LCIOConstants.BITTransient, isTransient); - } - + @Override public boolean isTransient() { return LCIOUtil.bitTest(flags, LCIOConstants.BITTransient); } + + @Override + public void setSubset(final boolean isSubset) { + flags = LCIOUtil.bitSet(flags, LCIOConstants.BITSubset, isSubset); + } + + @Override + public void setTransient(final boolean isTransient) { + flags = LCIOUtil.bitSet(flags, LCIOConstants.BITTransient, isTransient); + } + } + + private static final int NANO_SECONDS = 1000000; + public static final String READOUT_NAME = "ReadoutName"; + private final ConditionsManager conditionsManager = ConditionsManager.defaultInstance(); + + private final String detectorName; + private final Map<String, float[]> floatParameters = new HashMap<String, float[]>(); + private final Map<String, int[]> intParameters = new HashMap<String, int[]>(); + private final Map<String, String[]> stringParameters = new HashMap<String, String[]>(); + + private final Map<List, LCMetaData> metaDataMap = new IdentityHashMap<List, LCMetaData>(); + + /** Creates a new instance of BaseLCSimEvent */ + public BaseLCSimEvent(final int run, final int event, final String detectorName) { + this(run, event, detectorName, System.currentTimeMillis() * NANO_SECONDS); + } + + public BaseLCSimEvent(final int run, final int event, final String detectorName, final long timeStamp) { + super(run, event, timeStamp); + this.detectorName = detectorName; + try { + conditionsManager.setDetector(detectorName, run); + } catch (final ConditionsNotFoundException x) { + throw new RuntimeException(x); + } + } + + public BaseLCSimEvent(final int run, final int event, final String detectorName, final long timeStamp, + final boolean triggerConditionsUpdate) { + super(run, event, timeStamp); + this.detectorName = detectorName; + if (triggerConditionsUpdate) { + try { + conditionsManager.setDetector(detectorName, run); + } catch (final ConditionsNotFoundException x) { + throw new RuntimeException(x); + } + } + } + + @Override + public <T> List<List<T>> get(final Class<T> type) { + final List<List<T>> result = new ArrayList<List<T>>(); + for (final Map.Entry<List, LCMetaData> entry : metaDataMap.entrySet()) { + if (type.isAssignableFrom(entry.getValue().getType())) { + result.add(entry.getKey()); + } + } + return result; + } + + @Override + public <T> List<T> get(final Class<T> type, final String name) { + return (List<T>) this.get(name); + } + + @Override + public Detector getDetector() { + return conditionsManager.getCachedConditions(Detector.class, "compact.xml").getCachedData(); + } + + @Override + public String getDetectorName() { + return detectorName; + } + + @Override + public Map<String, float[]> getFloatParameters() { + return floatParameters; + } + + @Override + public Map<String, int[]> getIntegerParameters() { + return intParameters; + + } + + @Override + public Set<List> getLists() { + return metaDataMap.keySet(); + } + + @Override + public List<MCParticle> getMCParticles() { + return this.get(MCParticle.class, MC_PARTICLES); + } + + @Override + public Collection<LCMetaData> getMetaData() { + return metaDataMap.values(); + } + + @Override + public LCMetaData getMetaData(final List x) { + return metaDataMap.get(x); + } + + @Override + public Map<String, String[]> getStringParameters() { + return stringParameters; + } + + @Override + public float getWeight() { + return 1.0f; + } + + @Override + public boolean hasCollection(final Class type) { + for (final LCMetaData meta : metaDataMap.values()) { + if (type.isAssignableFrom(meta.getType())) { + return true; + } + } + return false; + } + + @Override + public boolean hasCollection(final Class type, final String name) { + if (!this.hasItem(name)) { + return false; + } + final Object collection = this.get(name); + if (!(collection instanceof List)) { + return false; + } + return type.isAssignableFrom(metaDataMap.get(collection).getType()); + } + + @Override + public boolean hasItem(final String name) { + return super.keys().contains(name); + } + + @Override + public void put(final String name, final List collection, final Class type, final int flags) { + this.put(name, collection, type, flags, null); + } + + @Override + public void put(final String name, final List collection, final Class type, final int flags, final Map intMap, + final Map floatMap, final Map stringMap) { + super.put(name, collection); + final LCMetaData meta = new MetaData(name, type, flags, intMap, floatMap, stringMap); + metaDataMap.put(collection, meta); + } + + @Override + public void put(final String name, final List collection, final Class type, final int flags, + final String readoutName) { + super.put(name, collection); + + final LCMetaData meta = new MetaData(name, type, flags, readoutName); + metaDataMap.put(collection, meta); + + this.setCollectionMetaData(collection, type, meta); + } + + @Override + public void put(final String name, final Object component) { + // Check if collection exists already which is an error. + if (this.hasItem(name)) { + throw new IllegalArgumentException("An item called " + name + " already exists in the event."); + } + super.put(name, component); + if (component instanceof List) { + final List list = (List) component; + Class type = list.isEmpty() ? Object.class : list.get(0).getClass(); + for (final Object o : list) { + if (!type.isAssignableFrom(o.getClass())) { + type = Object.class; + } + } + metaDataMap.put(list, new MetaData(name, type, 0, null)); + } + } + + /** + * Removes a collection from the event. + */ + @Override + public void remove(final String name) { + final Object collection = this.get(name); + if (collection instanceof List) { + metaDataMap.remove(collection); + } + super.keys().remove(name); + } + + private void setCollectionMetaData(final List collection, final Class type, final LCMetaData meta) { + // Set MetaData on collection objects if necessary. + if (Hit.class.isAssignableFrom(type)) { + for (final Object o : collection) { + final Hit hit = (Hit) o; + if (hit.getMetaData() == null) { + ((Hit) o).setMetaData(meta); + } + } + } + } + + @Override + public String toString() { + return "Run " + this.getRunNumber() + " Event " + this.getEventNumber() + " (" + + new Date(this.getTimeStamp() / NANO_SECONDS) + ") Detector: " + detectorName; } } ######################################################################## Use REPLY-ALL to reply to list To unsubscribe from the LCDET-SVN list, click the following link: https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCDET-SVN&A=1