diff --git a/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Writer.java b/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Writer.java index dabcdb132c..a7a84868a9 100644 --- a/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Writer.java +++ b/common-tools/clara-io/src/main/java/org/jlab/io/clara/Clas12Writer.java @@ -8,6 +8,7 @@ import org.jlab.detector.helicity.HelicitySequenceDelayed; import org.jlab.detector.serial.SerialHoncho; import org.jlab.detector.serial.PostProcessor; +import org.jlab.detector.serial.Occupancer; import org.jlab.jnp.hipo4.data.Bank; import org.jlab.jnp.hipo4.data.Event; import org.jlab.jnp.hipo4.data.SchemaFactory; @@ -28,6 +29,7 @@ */ public class Clas12Writer extends HipoToHipoWriter { + Occupancer occupancer; SerialHoncho serial; Bank runConfig; ConstantsManager conman; @@ -35,6 +37,7 @@ public class Clas12Writer extends HipoToHipoWriter { boolean postprocess; private void init(JSONObject opts) { + occupancer = new Occupancer(1000); fullSchema = new SchemaFactory(); fullSchema.initFromDirectory(FileUtils.getEnvironmentPath("CLAS12DIR","etc/bankdefs/hipo4")); serial = new SerialHoncho(fullSchema); @@ -63,6 +66,7 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event protected void writeEvent(Object event) throws EventWriterException { Event t = serial.read((Event)event); if (!t.isEmpty()) writer.addEvent(t, 1); + occupancer.process(t); super.writeEvent(event); } diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/banks/FilteredBank.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/banks/FilteredBank.java index 99b9171e7d..2775f9399c 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/banks/FilteredBank.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/banks/FilteredBank.java @@ -60,7 +60,7 @@ public final void setFilter(int... values){ * @param evt */ public void read(Event evt){ - evt.read(bank); + evt.read(bank); this.notifyRead(); } diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/serial/Occupancer.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/serial/Occupancer.java new file mode 100644 index 0000000000..29dcc07ec6 --- /dev/null +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/serial/Occupancer.java @@ -0,0 +1,174 @@ +package org.jlab.detector.serial; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Map; +import org.jlab.detector.banks.RawBank; +import org.jlab.utils.groups.IndexedTable; +import org.jlab.utils.groups.IndexedTable.IndexedEntry; +import org.jlab.detector.banks.RawBank.OrderGroups; +import org.jlab.detector.banks.RawDataBank; +import org.jlab.jnp.hipo4.data.Bank; +import org.jlab.jnp.hipo4.data.Event; +import org.jlab.jnp.hipo4.data.Schema; +import org.jlab.jnp.hipo4.data.SchemaFactory; +import org.jlab.utils.system.ClasUtilsFile; + +/** + * Occupancy bookkeeper based on IndexedTable, with I/O helpers for indexed banks. + * + * @author baltzell + */ +public class Occupancer extends ArrayList { + + static final String BANKDIR = ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4/singles/occupancy"); + static final SchemaFactory schema = new SchemaFactory(); + static { schema.initFromDirectory(ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4")); } + + int nevents; + int prescale; + + public Occupancer(int prescale) { + super(); + this.prescale = prescale; + this.nevents = 0; + try { + addAll(Files.list(Paths.get(BANKDIR)) + .filter(Files::isRegularFile) + .map(p -> p.getFileName().toString()) + .map(s -> s.substring(0, s.length()-5)) + .map(s -> s.substring(5, s.length())) + .map(OccupanceTable::new).toList()); + } catch (IOException ex) { + System.getLogger(Occupancer.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); + } + } + + public boolean process(Event event) { + forEach(t -> { t.fill(event, false); }); + if (++nevents % prescale == 0) { + forEach(t -> { + if (t.getTable().getRowCount() > 0) { + event.write(t.create(nevents, event)); + } + t.reset(); + }); + nevents = 0; + } + return true; + } + + public void reset() { + forEach(t -> { t.reset(); nevents = 0; }); + } + + public static final class OccupanceTable { + + Schema occSchema; + Schema hitSchema; + IndexedTable table; + + /** + * A 3-index table, e.g., sector/layer/component. + * @param hitBank name of the hit bank + */ + public OccupanceTable(String hitBank) { + hitSchema = schema.getSchema(hitBank); + occSchema = schema.getSchema("OCC::" + hitBank); + table = new IndexedTable(3, new String[]{"occ/F"}); + } + + /** + * An N-index table. + * @param hitBank name of the hit bank + * @param indexCount number of inidices in the hit bank + */ + public OccupanceTable(String hitBank, int indexCount) { + hitSchema = schema.getSchema(hitBank); + occSchema = schema.getSchema("OCC::" + hitBank); + table = new IndexedTable(indexCount, new String[]{"occ/F"}); + } + + public final IndexedTable getTable() { return table; } + + /** + * Zero the occupancy table. + */ + public final void reset() { + table = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"}); + } + + /** + * Get the occupancy table, normalized by number of events. + * @param events + * @return + */ + public final IndexedTable getOccupancy(long events) { + IndexedTable t = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"}); + for (long hash : ((Map)table.getList().getMap()).keySet()) { + t.addEntry(IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize())); + t.setDoubleValueByHash((table.getDoubleValueByHash(0, hash))/events, 0, hash); + } + return t; + } + + /** + * Fill the occupancy table. + * @param weight + * @param index + */ + public synchronized final void fill(float weight, int... index) { + for (int i=0; i m = table.getList().getMap(); + int i = 0; + for (long hash : m.keySet()) { + int[] idx = IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize()); + for (int j=0; jclas-reco 14.2.1-SNAPSHOT - +