Commit in hps-java on MAIN | |||
sandbox/MCEventBuilder.java | +284 | added 1.1 | |
/MCRawDataToEvio4Converter.java | +211 | added 1.1 | |
src/main/java/org/lcsim/hps/evio/MCEventBuilder.java | -284 | 1.2 removed | |
/MCRawDataToEvio4Converter.java | -211 | 1.4 removed | |
+495 | -495 |
move old bogus evio converters to sandbox
diff -N MCEventBuilder.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ MCEventBuilder.java 29 Apr 2012 23:12:19 -0000 1.1 @@ -0,0 +1,284 @@
+package org.lcsim.hps.evio; + +import java.util.ArrayList; +import java.util.List; + +import org.jlab.coda.jevio.BaseStructure; +import org.jlab.coda.jevio.BaseStructureHeader; +import org.jlab.coda.jevio.CompositeData; +import org.jlab.coda.jevio.EvioEvent; +import org.jlab.coda.jevio.EvioException; +import org.lcsim.detector.DetectorElementStore; +import org.lcsim.detector.IDetectorElement; +import org.lcsim.detector.identifier.IExpandedIdentifier; +import org.lcsim.detector.identifier.IIdentifier; +import org.lcsim.detector.identifier.IIdentifierDictionary; +import org.lcsim.detector.tracker.silicon.SiSensor; +import org.lcsim.event.EventHeader; +import org.lcsim.event.RawCalorimeterHit; +import org.lcsim.event.RawTrackerHit; +import org.lcsim.event.base.BaseLCSimEvent; +import org.lcsim.event.base.BaseRawCalorimeterHit; +import org.lcsim.event.base.BaseRawTrackerHit; +import org.lcsim.geometry.Detector; +import org.lcsim.geometry.Subdetector; +import org.lcsim.geometry.subdetector.HPSEcal3; +import org.lcsim.geometry.subdetector.HPSTracker; +import org.lcsim.util.lcio.LCIOConstants; + +/** + * Build LCSim events from EVIO data. + * + * @author Jeremy McCormick <[log in to unmask]> + * + */ +public final class MCEventBuilder implements LCSimEventBuilder { + + // Names of subdetectors. + private String trackerName; + private String calorimeterName; + + // Names of raw data collections with default settings. + private String rawTrackerHitCollectionName = "RawTrackerHitMaker_RawTrackerHits"; + private String rawCalorimeterHitCollectionName = "EcalRawHits"; + + // Detector conditions object. + private Detector detector; + + // Debug flag. + private boolean debug = false; + + /* + public LCSimEventBuilder(String detectorName) { + + // Make a dummy event to setup the conditions system. + EventHeader dummyEvent = new BaseLCSimEvent(0, 0, detectorName); + detector = dummyEvent.getDetector(); + + // Set default detector names by looking for HPS detector types. + for (Subdetector subdet : detector.getSubdetectorList()) { + if (subdet instanceof HPSTracker) { + trackerName = subdet.getName(); + System.out.println("trackerName = " + trackerName); + } else if (subdet instanceof HPSEcal3) { + calorimeterName = subdet.getName(); + System.out.println("calorimeterName = " + calorimeterName); + } + } + } + */ + + public MCEventBuilder() + {} + + public void setDetectorName(String detectorName) { + + // Make a dummy event to setup the conditions system. + EventHeader dummyEvent = new BaseLCSimEvent(0, 0, detectorName); + detector = dummyEvent.getDetector(); + + // Set default detector names by looking for HPS detector types. + for (Subdetector subdet : detector.getSubdetectorList()) { + if (subdet instanceof HPSTracker) { + trackerName = subdet.getName(); + System.out.println("trackerName = " + trackerName); + } else if (subdet instanceof HPSEcal3) { + calorimeterName = subdet.getName(); + System.out.println("calorimeterName = " + calorimeterName); + } + } + } + + public void setDebug(boolean debug) { + this.debug = debug; + } + + public String getRawTrackerHitCollectionName() { + return rawTrackerHitCollectionName; + } + + public void setRawTrackerHitCollectionName(String rawTrackerHitCollectionName) { + this.rawTrackerHitCollectionName = rawTrackerHitCollectionName; + } + + public String getRawCalorimeterHitCollectionName() { + return rawCalorimeterHitCollectionName; + } + + public void setRawCalorimeterHitCollectionName(String rawCalorimeterHitCollectionName) { + this.rawCalorimeterHitCollectionName = rawCalorimeterHitCollectionName; + } + + public String getTrackerName() { + return trackerName; + } + + public void setTrackerName(String trackerName) { + this.trackerName = trackerName; + } + + public String getTrackerReadoutName() { + return detector.getSubdetector(trackerName).getReadout().getName(); + } + + public String getCalorimeterName() { + return calorimeterName; + } + + public void setCalorimeterName(String calorimeterName) { + this.calorimeterName = calorimeterName; + } + + public String getCalorimeterReadoutName() { + return detector.getSubdetector(calorimeterName).getReadout().getName(); + } + + public Detector getDetector() { + return detector; + } + + public EventHeader makeLCSimEvent(EvioEvent evioEvent) { + + // Make RawTrackerHit collection. + List<RawTrackerHit> rawTrackerHits = makeRawTrackerHits(evioEvent); + + // Make RawCalorimeterHit collection, combining top and bottom section of ECal into one list. + List<RawCalorimeterHit> rawCalorimeterHits = makeRawCalorimeterHits(evioEvent); + + // Create a new LCSimEvent. + //EventHeader lcsimEvent = new BaseLCSimEvent(0, evioEvent.getHeader().getNumber(), detectorName); + EventHeader lcsimEvent = new BaseLCSimEvent(0, evioEvent.getHeader().getNumber(), detector.getDetectorName()); + + // Add the hit collections. + lcsimEvent.put(rawTrackerHitCollectionName, rawTrackerHits, RawTrackerHit.class, (1 << LCIOConstants.TRAWBIT_ID1), getTrackerReadoutName()); + lcsimEvent.put(rawCalorimeterHitCollectionName, rawCalorimeterHits, RawCalorimeterHit.class, 0, getCalorimeterReadoutName()); + + return lcsimEvent; + } + + + private List<RawTrackerHit> makeRawTrackerHits(EvioEvent event) { + List<RawTrackerHit> rawTrackerHits = null; + for (BaseStructure bank : event.getChildren()) { + if (bank.getHeader().getTag() == MCRawDataToEvio4Converter.trackerBankTag) { + //System.out.println("found SVT bank; tag = " + MCRawDataToEvio4Converter.trackerBankTag); + CompositeData cdata = null; + try { + cdata = bank.getCompositeData(); + } + catch (EvioException e) { + throw new RuntimeException(e); + } + rawTrackerHits = makeRawTrackerHits(cdata); + } + } + return rawTrackerHits; + } + + private List<RawTrackerHit> makeRawTrackerHits(CompositeData cdata) { + + // Get some ID info before looping in order to strip out irrelevant fields. + // TODO Next three should be cached so as to avoid calling every event. + IIdentifierDictionary dict = detector.getSubdetector(trackerName).getDetectorElement().getIdentifierHelper().getIdentifierDictionary(); + int fieldIdx = dict.getFieldIndex("side"); + int sideIdx = dict.getFieldIndex("strip"); + + // List of hits to return. + List<RawTrackerHit> hits = new ArrayList<RawTrackerHit>(); + + // Loop over the items in the CompositeData. + int n = cdata.getNValue(); + if (debug) + System.out.println("RawTrackerHit.N = " + n); + for (int i=0; i<n; i++) { + + // Get values for hit from composite data. + Long id = cdata.getLong(); + int time = cdata.getInt(); + int adcValue = cdata.getInt(); + + // Make the new hit. + RawTrackerHit hit = new BaseRawTrackerHit(id, time, new short[] {(short)adcValue}); + + // The "side" and "strip" fields needs to be stripped from the ID for sensor lookup. + IExpandedIdentifier expId = dict.unpack(hit.getIdentifier()); + expId.setValue(fieldIdx, 0); + expId.setValue(sideIdx, 0); + IIdentifier strippedId = dict.pack(expId); + + // Find the sensor for this hit using the stripped ID. + SiSensor sensor = findSensor(strippedId); + + // Assign sensor to hit. + hit.setDetectorElement(sensor); + + // Add this hit to the list. + hits.add(hit); + } + + if (debug) + System.out.println("makeRawTrackerHits created " + hits.size() + " hits"); + + return hits; + } + + private SiSensor findSensor(IIdentifier id) { + List<IDetectorElement> des = DetectorElementStore.getInstance().find(id); + SiSensor sensor = null; + if (des == null || des.size() == 0) { + throw new RuntimeException("Failed to find any DetectorElements with ID <0x" + Long.toHexString(id.getValue()) + ">."); + } + else if (des.size() == 1) { + sensor = (SiSensor)des.get(0); + } + else { + for (IDetectorElement de : des) { + if (de instanceof SiSensor) { + sensor = (SiSensor)de; + break; + } + } + } + if (sensor == null) { + throw new RuntimeException("No sensor was found with ID <0x" + Long.toHexString(id.getValue()) + ">."); + } + return sensor; + } + + private List<RawCalorimeterHit> makeRawCalorimeterHits(EvioEvent event) { + List<RawCalorimeterHit> hits = new ArrayList<RawCalorimeterHit>(); + for (BaseStructure bank : event.getChildren()) { + BaseStructureHeader header = bank.getHeader(); + if (header.getTag() == MCRawDataToEvio4Converter.ecalBottomBankTag || header.getTag() == MCRawDataToEvio4Converter.ecalTopBankTag) { + CompositeData cdata = null; + try { + cdata = bank.getCompositeData(); + } + catch (EvioException e) { + throw new RuntimeException(e); + } + List<RawCalorimeterHit> bankHits = makeRawCalorimeterHits(cdata); + hits.addAll(bankHits); + } + } + return hits; + } + + private List<RawCalorimeterHit> makeRawCalorimeterHits(CompositeData cdata) { + List<RawCalorimeterHit> hits = new ArrayList<RawCalorimeterHit>(); + List<Object> items = cdata.getItems(); + int n = cdata.getNValue(); + for (int i=0; i<n; i++) { + long id = cdata.getLong(); + int amplitude = cdata.getInt(); + int timestamp = cdata.getInt(); + hits.add(new BaseRawCalorimeterHit(id, amplitude, timestamp)); + } + return hits; + } + + @Override + public boolean isPhysicsEvent(EvioEvent evioEvent) { + return true; + } +}
\ No newline at end of file
diff -N MCRawDataToEvio4Converter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ MCRawDataToEvio4Converter.java 29 Apr 2012 23:12:19 -0000 1.1 @@ -0,0 +1,211 @@
+package org.lcsim.hps.evio; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.jlab.coda.jevio.CompositeData; +import org.jlab.coda.jevio.DataType; +import org.jlab.coda.jevio.EventBuilder; +import org.jlab.coda.jevio.EventWriter; +import org.jlab.coda.jevio.EvioBank; +import org.jlab.coda.jevio.EvioException; +import org.lcsim.event.EventHeader; +import org.lcsim.event.EventHeader.LCMetaData; +import org.lcsim.event.RawCalorimeterHit; +import org.lcsim.event.RawTrackerHit; +import org.lcsim.geometry.IDDecoder; +import org.lcsim.util.Driver; + +/** + * This Driver creates an EVIO data file using raw data generated from HPS MC data, + * e.g. output from slic simulation in LCIO format. + * + * The EVIO event structure is as follows, and subject to change. + * + * event + * bank (ecal) + * composite data + * bank (ecal) + * composite data + * bank (svt) + * composite data + * + * The tracker data bank has number = 1 and the ecal data bank has number = 2. + * + * For now the data formats do not match raw data that would come off the ET ring + * but I plan to add this soon. + * + * @author Jeremy McCormick <[log in to unmask]> + */ +public class MCRawDataToEvio4Converter extends Driver { + + // These correspond to ROC (readout crate) IDs from the DAQ system. + public static final int ecalTopBankTag = 0x1; + public static final int ecalBottomBankTag = 0x2; + public static final int trackerBankTag = 0x3; + + // CompositeData formats. + public static final String trackerFormat = "N(L,I,I)"; + public static final String ecalFormat = "N(L,I,I)"; + + //public static final int ecalBankTag = 0xe103; + public static final int trackerBankNumber = 1; + public static final int ecalBankNumber = 2; + + String evioOutputFile = "MCRawData.evio"; + EventWriter writer; + + String rawTrackerHitCollectionName = "RawTrackerHitMaker_RawTrackerHits"; + String rawCalorimeterHitCollectionName = "EcalRawHits"; + + private int eventsWritten = 0; + + public MCRawDataToEvio4Converter() + {} + + public void setEvioOutputFile(String evioOutputFile) { + this.evioOutputFile = evioOutputFile; + } + + public void setRawTrackerHitCollectionName(String rawTrackerHitCollectionName) { + this.rawTrackerHitCollectionName = rawTrackerHitCollectionName; + } + + public void setRawCalorimeterHitCollectionName(String rawCalorimeterHitCollectionName) { + this.rawCalorimeterHitCollectionName = rawCalorimeterHitCollectionName; + } + + public void startOfData() { + try { + writer = new EventWriter(evioOutputFile); + } + catch (EvioException e) { + throw new RuntimeException(e); + } + } + + public void endOfData() { + System.out.println("wrote " + eventsWritten + " events"); + try { + writer.close(); + } catch (EvioException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void process(EventHeader event) { + + // Make a new EVIO event. + EventBuilder builder = new EventBuilder(0, DataType.BANK, event.getEventNumber()); + + // Write RawTrackerHit collection. + writeRawTrackerHits(event.get(RawTrackerHit.class, rawTrackerHitCollectionName), builder); + + // Write RawCalorimeterHit collection. + List<RawCalorimeterHit> rawCalorimeterHits = event.get(RawCalorimeterHit.class, rawCalorimeterHitCollectionName); + LCMetaData meta = event.getMetaData(rawCalorimeterHits); + writeRawCalorimeterHits(meta, rawCalorimeterHits, builder); + + // Write this EVIO event. + builder.setAllHeaderLengths(); + try { + writer.writeEvent(builder.getEvent()); + ++eventsWritten; + } catch (EvioException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void writeRawTrackerHits(List<RawTrackerHit> rawTrackerHits, EventBuilder builder) { + + int dataSize = rawTrackerHits.size(); + + //System.out.println("writeRawTrackerHits will write " + dataSize + " hits"); + + CompositeData.Data data = new CompositeData.Data(); + data.addN(dataSize); + for (RawTrackerHit hit : rawTrackerHits) { + data.addLong(hit.getCellID()); + data.addInt(hit.getTime()); + data.addInt(hit.getADCValues()[0]); + } + CompositeData cdata = null; + try { + cdata = new CompositeData(trackerFormat, 1, data, 0 ,0); + } + catch (EvioException e) { + throw new RuntimeException(e); + } + EvioBank bank = new EvioBank(trackerBankTag, DataType.COMPOSITE, trackerBankNumber); + try { + bank.appendCompositeData(cdata); + } catch (EvioException e) { + throw new RuntimeException(e); + } + bank.setAllHeaderLengths(); + try { + builder.addChild(builder.getEvent(), bank); + } catch (EvioException e) { + throw new RuntimeException(e); + } + } + + public void writeRawCalorimeterHitCollection(List<RawCalorimeterHit> hits, int bankTag, EventBuilder builder) { + CompositeData.Data data = new CompositeData.Data(); + int nTopHits = hits.size(); + data.addN(nTopHits); + for (RawCalorimeterHit hit : hits) { + data.addLong(hit.getCellID()); + data.addInt(hit.getAmplitude()); + data.addInt(hit.getTimeStamp()); + } + CompositeData cdata = null; + try { + cdata = new CompositeData(ecalFormat, 1, data, 0, 0); + } + catch (EvioException e) { + throw new RuntimeException(e); + } + EvioBank bank = new EvioBank(bankTag, DataType.COMPOSITE, ecalBankNumber); + try { + bank.appendCompositeData(cdata); + } catch (EvioException e) { + throw new RuntimeException(e); + } + bank.setAllHeaderLengths(); + try { + builder.addChild(builder.getEvent(), bank); + } catch (EvioException e) { + throw new RuntimeException(e); + } + } + + public void writeRawCalorimeterHits(LCMetaData meta, List<RawCalorimeterHit> rawCalorimeterHits, EventBuilder builder) { + + // Make two lists containing the hits from top and bottom sections, which go into separate EVIO data banks. + IDDecoder dec = meta.getIDDecoder(); + List<RawCalorimeterHit> topHits = new ArrayList<RawCalorimeterHit>(); + List<RawCalorimeterHit> bottomHits = new ArrayList<RawCalorimeterHit>(); + for (RawCalorimeterHit hit : rawCalorimeterHits) { + dec.setID(hit.getCellID()); + int iy = dec.getValue("iy"); + // Negative iy should be bottom section. + if (iy < 0) { + bottomHits.add(hit); + } + // Positive iy should be top section. + else { + topHits.add(hit); + } + } + + // Write the two collections for top and bottom hits to separate EVIO banks. + writeRawCalorimeterHitCollection(topHits, ecalTopBankTag, builder); + writeRawCalorimeterHitCollection(bottomHits, ecalBottomBankTag, builder); + } +}
\ No newline at end of file
diff -N MCEventBuilder.java --- MCEventBuilder.java 26 Apr 2012 21:16:50 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,284 +0,0 @@
-package org.lcsim.hps.evio; - -import java.util.ArrayList; -import java.util.List; - -import org.jlab.coda.jevio.BaseStructure; -import org.jlab.coda.jevio.BaseStructureHeader; -import org.jlab.coda.jevio.CompositeData; -import org.jlab.coda.jevio.EvioEvent; -import org.jlab.coda.jevio.EvioException; -import org.lcsim.detector.DetectorElementStore; -import org.lcsim.detector.IDetectorElement; -import org.lcsim.detector.identifier.IExpandedIdentifier; -import org.lcsim.detector.identifier.IIdentifier; -import org.lcsim.detector.identifier.IIdentifierDictionary; -import org.lcsim.detector.tracker.silicon.SiSensor; -import org.lcsim.event.EventHeader; -import org.lcsim.event.RawCalorimeterHit; -import org.lcsim.event.RawTrackerHit; -import org.lcsim.event.base.BaseLCSimEvent; -import org.lcsim.event.base.BaseRawCalorimeterHit; -import org.lcsim.event.base.BaseRawTrackerHit; -import org.lcsim.geometry.Detector; -import org.lcsim.geometry.Subdetector; -import org.lcsim.geometry.subdetector.HPSEcal3; -import org.lcsim.geometry.subdetector.HPSTracker; -import org.lcsim.util.lcio.LCIOConstants; - -/** - * Build LCSim events from EVIO data. - * - * @author Jeremy McCormick <[log in to unmask]> - * - */ -public final class MCEventBuilder implements LCSimEventBuilder { - - // Names of subdetectors. - private String trackerName; - private String calorimeterName; - - // Names of raw data collections with default settings. - private String rawTrackerHitCollectionName = "RawTrackerHitMaker_RawTrackerHits"; - private String rawCalorimeterHitCollectionName = "EcalRawHits"; - - // Detector conditions object. - private Detector detector; - - // Debug flag. - private boolean debug = false; - - /* - public LCSimEventBuilder(String detectorName) { - - // Make a dummy event to setup the conditions system. - EventHeader dummyEvent = new BaseLCSimEvent(0, 0, detectorName); - detector = dummyEvent.getDetector(); - - // Set default detector names by looking for HPS detector types. - for (Subdetector subdet : detector.getSubdetectorList()) { - if (subdet instanceof HPSTracker) { - trackerName = subdet.getName(); - System.out.println("trackerName = " + trackerName); - } else if (subdet instanceof HPSEcal3) { - calorimeterName = subdet.getName(); - System.out.println("calorimeterName = " + calorimeterName); - } - } - } - */ - - public MCEventBuilder() - {} - - public void setDetectorName(String detectorName) { - - // Make a dummy event to setup the conditions system. - EventHeader dummyEvent = new BaseLCSimEvent(0, 0, detectorName); - detector = dummyEvent.getDetector(); - - // Set default detector names by looking for HPS detector types. - for (Subdetector subdet : detector.getSubdetectorList()) { - if (subdet instanceof HPSTracker) { - trackerName = subdet.getName(); - System.out.println("trackerName = " + trackerName); - } else if (subdet instanceof HPSEcal3) { - calorimeterName = subdet.getName(); - System.out.println("calorimeterName = " + calorimeterName); - } - } - } - - public void setDebug(boolean debug) { - this.debug = debug; - } - - public String getRawTrackerHitCollectionName() { - return rawTrackerHitCollectionName; - } - - public void setRawTrackerHitCollectionName(String rawTrackerHitCollectionName) { - this.rawTrackerHitCollectionName = rawTrackerHitCollectionName; - } - - public String getRawCalorimeterHitCollectionName() { - return rawCalorimeterHitCollectionName; - } - - public void setRawCalorimeterHitCollectionName(String rawCalorimeterHitCollectionName) { - this.rawCalorimeterHitCollectionName = rawCalorimeterHitCollectionName; - } - - public String getTrackerName() { - return trackerName; - } - - public void setTrackerName(String trackerName) { - this.trackerName = trackerName; - } - - public String getTrackerReadoutName() { - return detector.getSubdetector(trackerName).getReadout().getName(); - } - - public String getCalorimeterName() { - return calorimeterName; - } - - public void setCalorimeterName(String calorimeterName) { - this.calorimeterName = calorimeterName; - } - - public String getCalorimeterReadoutName() { - return detector.getSubdetector(calorimeterName).getReadout().getName(); - } - - public Detector getDetector() { - return detector; - } - - public EventHeader makeLCSimEvent(EvioEvent evioEvent) { - - // Make RawTrackerHit collection. - List<RawTrackerHit> rawTrackerHits = makeRawTrackerHits(evioEvent); - - // Make RawCalorimeterHit collection, combining top and bottom section of ECal into one list. - List<RawCalorimeterHit> rawCalorimeterHits = makeRawCalorimeterHits(evioEvent); - - // Create a new LCSimEvent. - //EventHeader lcsimEvent = new BaseLCSimEvent(0, evioEvent.getHeader().getNumber(), detectorName); - EventHeader lcsimEvent = new BaseLCSimEvent(0, evioEvent.getHeader().getNumber(), detector.getDetectorName()); - - // Add the hit collections. - lcsimEvent.put(rawTrackerHitCollectionName, rawTrackerHits, RawTrackerHit.class, (1 << LCIOConstants.TRAWBIT_ID1), getTrackerReadoutName()); - lcsimEvent.put(rawCalorimeterHitCollectionName, rawCalorimeterHits, RawCalorimeterHit.class, 0, getCalorimeterReadoutName()); - - return lcsimEvent; - } - - - private List<RawTrackerHit> makeRawTrackerHits(EvioEvent event) { - List<RawTrackerHit> rawTrackerHits = null; - for (BaseStructure bank : event.getChildren()) { - if (bank.getHeader().getTag() == MCRawDataToEvio4Converter.trackerBankTag) { - //System.out.println("found SVT bank; tag = " + MCRawDataToEvio4Converter.trackerBankTag); - CompositeData cdata = null; - try { - cdata = bank.getCompositeData(); - } - catch (EvioException e) { - throw new RuntimeException(e); - } - rawTrackerHits = makeRawTrackerHits(cdata); - } - } - return rawTrackerHits; - } - - private List<RawTrackerHit> makeRawTrackerHits(CompositeData cdata) { - - // Get some ID info before looping in order to strip out irrelevant fields. - // TODO Next three should be cached so as to avoid calling every event. - IIdentifierDictionary dict = detector.getSubdetector(trackerName).getDetectorElement().getIdentifierHelper().getIdentifierDictionary(); - int fieldIdx = dict.getFieldIndex("side"); - int sideIdx = dict.getFieldIndex("strip"); - - // List of hits to return. - List<RawTrackerHit> hits = new ArrayList<RawTrackerHit>(); - - // Loop over the items in the CompositeData. - int n = cdata.getNValue(); - if (debug) - System.out.println("RawTrackerHit.N = " + n); - for (int i=0; i<n; i++) { - - // Get values for hit from composite data. - Long id = cdata.getLong(); - int time = cdata.getInt(); - int adcValue = cdata.getInt(); - - // Make the new hit. - RawTrackerHit hit = new BaseRawTrackerHit(id, time, new short[] {(short)adcValue}); - - // The "side" and "strip" fields needs to be stripped from the ID for sensor lookup. - IExpandedIdentifier expId = dict.unpack(hit.getIdentifier()); - expId.setValue(fieldIdx, 0); - expId.setValue(sideIdx, 0); - IIdentifier strippedId = dict.pack(expId); - - // Find the sensor for this hit using the stripped ID. - SiSensor sensor = findSensor(strippedId); - - // Assign sensor to hit. - hit.setDetectorElement(sensor); - - // Add this hit to the list. - hits.add(hit); - } - - if (debug) - System.out.println("makeRawTrackerHits created " + hits.size() + " hits"); - - return hits; - } - - private SiSensor findSensor(IIdentifier id) { - List<IDetectorElement> des = DetectorElementStore.getInstance().find(id); - SiSensor sensor = null; - if (des == null || des.size() == 0) { - throw new RuntimeException("Failed to find any DetectorElements with ID <0x" + Long.toHexString(id.getValue()) + ">."); - } - else if (des.size() == 1) { - sensor = (SiSensor)des.get(0); - } - else { - for (IDetectorElement de : des) { - if (de instanceof SiSensor) { - sensor = (SiSensor)de; - break; - } - } - } - if (sensor == null) { - throw new RuntimeException("No sensor was found with ID <0x" + Long.toHexString(id.getValue()) + ">."); - } - return sensor; - } - - private List<RawCalorimeterHit> makeRawCalorimeterHits(EvioEvent event) { - List<RawCalorimeterHit> hits = new ArrayList<RawCalorimeterHit>(); - for (BaseStructure bank : event.getChildren()) { - BaseStructureHeader header = bank.getHeader(); - if (header.getTag() == MCRawDataToEvio4Converter.ecalBottomBankTag || header.getTag() == MCRawDataToEvio4Converter.ecalTopBankTag) { - CompositeData cdata = null; - try { - cdata = bank.getCompositeData(); - } - catch (EvioException e) { - throw new RuntimeException(e); - } - List<RawCalorimeterHit> bankHits = makeRawCalorimeterHits(cdata); - hits.addAll(bankHits); - } - } - return hits; - } - - private List<RawCalorimeterHit> makeRawCalorimeterHits(CompositeData cdata) { - List<RawCalorimeterHit> hits = new ArrayList<RawCalorimeterHit>(); - List<Object> items = cdata.getItems(); - int n = cdata.getNValue(); - for (int i=0; i<n; i++) { - long id = cdata.getLong(); - int amplitude = cdata.getInt(); - int timestamp = cdata.getInt(); - hits.add(new BaseRawCalorimeterHit(id, amplitude, timestamp)); - } - return hits; - } - - @Override - public boolean isPhysicsEvent(EvioEvent evioEvent) { - return true; - } -}
\ No newline at end of file
diff -N MCRawDataToEvio4Converter.java --- MCRawDataToEvio4Converter.java 25 Mar 2012 07:26:45 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,211 +0,0 @@
-package org.lcsim.hps.evio; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.jlab.coda.jevio.CompositeData; -import org.jlab.coda.jevio.DataType; -import org.jlab.coda.jevio.EventBuilder; -import org.jlab.coda.jevio.EventWriter; -import org.jlab.coda.jevio.EvioBank; -import org.jlab.coda.jevio.EvioException; -import org.lcsim.event.EventHeader; -import org.lcsim.event.EventHeader.LCMetaData; -import org.lcsim.event.RawCalorimeterHit; -import org.lcsim.event.RawTrackerHit; -import org.lcsim.geometry.IDDecoder; -import org.lcsim.util.Driver; - -/** - * This Driver creates an EVIO data file using raw data generated from HPS MC data, - * e.g. output from slic simulation in LCIO format. - * - * The EVIO event structure is as follows, and subject to change. - * - * event - * bank (ecal) - * composite data - * bank (ecal) - * composite data - * bank (svt) - * composite data - * - * The tracker data bank has number = 1 and the ecal data bank has number = 2. - * - * For now the data formats do not match raw data that would come off the ET ring - * but I plan to add this soon. - * - * @author Jeremy McCormick <[log in to unmask]> - */ -public class MCRawDataToEvio4Converter extends Driver { - - // These correspond to ROC (readout crate) IDs from the DAQ system. - public static final int ecalTopBankTag = 0x1; - public static final int ecalBottomBankTag = 0x2; - public static final int trackerBankTag = 0x3; - - // CompositeData formats. - public static final String trackerFormat = "N(L,I,I)"; - public static final String ecalFormat = "N(L,I,I)"; - - //public static final int ecalBankTag = 0xe103; - public static final int trackerBankNumber = 1; - public static final int ecalBankNumber = 2; - - String evioOutputFile = "MCRawData.evio"; - EventWriter writer; - - String rawTrackerHitCollectionName = "RawTrackerHitMaker_RawTrackerHits"; - String rawCalorimeterHitCollectionName = "EcalRawHits"; - - private int eventsWritten = 0; - - public MCRawDataToEvio4Converter() - {} - - public void setEvioOutputFile(String evioOutputFile) { - this.evioOutputFile = evioOutputFile; - } - - public void setRawTrackerHitCollectionName(String rawTrackerHitCollectionName) { - this.rawTrackerHitCollectionName = rawTrackerHitCollectionName; - } - - public void setRawCalorimeterHitCollectionName(String rawCalorimeterHitCollectionName) { - this.rawCalorimeterHitCollectionName = rawCalorimeterHitCollectionName; - } - - public void startOfData() { - try { - writer = new EventWriter(evioOutputFile); - } - catch (EvioException e) { - throw new RuntimeException(e); - } - } - - public void endOfData() { - System.out.println("wrote " + eventsWritten + " events"); - try { - writer.close(); - } catch (EvioException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public void process(EventHeader event) { - - // Make a new EVIO event. - EventBuilder builder = new EventBuilder(0, DataType.BANK, event.getEventNumber()); - - // Write RawTrackerHit collection. - writeRawTrackerHits(event.get(RawTrackerHit.class, rawTrackerHitCollectionName), builder); - - // Write RawCalorimeterHit collection. - List<RawCalorimeterHit> rawCalorimeterHits = event.get(RawCalorimeterHit.class, rawCalorimeterHitCollectionName); - LCMetaData meta = event.getMetaData(rawCalorimeterHits); - writeRawCalorimeterHits(meta, rawCalorimeterHits, builder); - - // Write this EVIO event. - builder.setAllHeaderLengths(); - try { - writer.writeEvent(builder.getEvent()); - ++eventsWritten; - } catch (EvioException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public void writeRawTrackerHits(List<RawTrackerHit> rawTrackerHits, EventBuilder builder) { - - int dataSize = rawTrackerHits.size(); - - //System.out.println("writeRawTrackerHits will write " + dataSize + " hits"); - - CompositeData.Data data = new CompositeData.Data(); - data.addN(dataSize); - for (RawTrackerHit hit : rawTrackerHits) { - data.addLong(hit.getCellID()); - data.addInt(hit.getTime()); - data.addInt(hit.getADCValues()[0]); - } - CompositeData cdata = null; - try { - cdata = new CompositeData(trackerFormat, 1, data, 0 ,0); - } - catch (EvioException e) { - throw new RuntimeException(e); - } - EvioBank bank = new EvioBank(trackerBankTag, DataType.COMPOSITE, trackerBankNumber); - try { - bank.appendCompositeData(cdata); - } catch (EvioException e) { - throw new RuntimeException(e); - } - bank.setAllHeaderLengths(); - try { - builder.addChild(builder.getEvent(), bank); - } catch (EvioException e) { - throw new RuntimeException(e); - } - } - - public void writeRawCalorimeterHitCollection(List<RawCalorimeterHit> hits, int bankTag, EventBuilder builder) { - CompositeData.Data data = new CompositeData.Data(); - int nTopHits = hits.size(); - data.addN(nTopHits); - for (RawCalorimeterHit hit : hits) { - data.addLong(hit.getCellID()); - data.addInt(hit.getAmplitude()); - data.addInt(hit.getTimeStamp()); - } - CompositeData cdata = null; - try { - cdata = new CompositeData(ecalFormat, 1, data, 0, 0); - } - catch (EvioException e) { - throw new RuntimeException(e); - } - EvioBank bank = new EvioBank(bankTag, DataType.COMPOSITE, ecalBankNumber); - try { - bank.appendCompositeData(cdata); - } catch (EvioException e) { - throw new RuntimeException(e); - } - bank.setAllHeaderLengths(); - try { - builder.addChild(builder.getEvent(), bank); - } catch (EvioException e) { - throw new RuntimeException(e); - } - } - - public void writeRawCalorimeterHits(LCMetaData meta, List<RawCalorimeterHit> rawCalorimeterHits, EventBuilder builder) { - - // Make two lists containing the hits from top and bottom sections, which go into separate EVIO data banks. - IDDecoder dec = meta.getIDDecoder(); - List<RawCalorimeterHit> topHits = new ArrayList<RawCalorimeterHit>(); - List<RawCalorimeterHit> bottomHits = new ArrayList<RawCalorimeterHit>(); - for (RawCalorimeterHit hit : rawCalorimeterHits) { - dec.setID(hit.getCellID()); - int iy = dec.getValue("iy"); - // Negative iy should be bottom section. - if (iy < 0) { - bottomHits.add(hit); - } - // Positive iy should be top section. - else { - topHits.add(hit); - } - } - - // Write the two collections for top and bottom hits to separate EVIO banks. - writeRawCalorimeterHitCollection(topHits, ecalTopBankTag, builder); - writeRawCalorimeterHitCollection(bottomHits, ecalBottomBankTag, builder); - } -}
\ No newline at end of file
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1