diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/OccupanceTable.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/OccupanceTable.java new file mode 100644 index 0000000000..be1fe74ed2 --- /dev/null +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/OccupanceTable.java @@ -0,0 +1,123 @@ +package org.jlab.detector.calib.utils; + +import java.util.Map; +import org.jlab.io.base.DataBank; +import org.jlab.io.base.DataEvent; +import org.jlab.utils.groups.IndexedTable; +import org.jlab.utils.groups.IndexedTable.IndexedEntry; +import org.jlab.detector.banks.RawDataBank; + +/** + * Occupancy bookkeeper based on IndexedTable, with I/O helpers for indexed banks. + * + * @author baltzell + */ +public class OccupanceTable { + + String hitBank; + String occBank; + IndexedTable table; + + /** + * A 3-index table, e.g., sector/layer/component. + * @param hitBank name of the hit bank + */ + public OccupanceTable(String hitBank) { + this.hitBank = hitBank; + occBank = "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) { + this.hitBank = hitBank; + occBank = "OCC::" + hitBank; + table = new IndexedTable(indexCount, new String[]{"occ/F"}); + } + + public String getHitBank() { return hitBank; } + public String getOccBank() { return occBank; } + 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(); + for (long hash : m.keySet()) { + int[] idx = IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize()); + for (int j=0; jclas-reco 14.2.0-SNAPSHOT - + + + org.jlab.clas + clas-utils + 14.2.0-SNAPSHOT + + diff --git a/reconstruction/calib/src/main/java/org/jlab/calibration/service/OccupanceEngine.java b/reconstruction/calib/src/main/java/org/jlab/calibration/service/OccupanceEngine.java new file mode 100644 index 0000000000..8b000fa2a9 --- /dev/null +++ b/reconstruction/calib/src/main/java/org/jlab/calibration/service/OccupanceEngine.java @@ -0,0 +1,68 @@ +package org.jlab.calibration.service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.jlab.io.base.DataEvent; +import org.jlab.clas.reco.ReconstructionEngine; +import org.jlab.detector.banks.RawBank.OrderGroups; +import org.jlab.detector.banks.RawDataBank; +import org.jlab.detector.calib.utils.OccupanceTable; +import org.jlab.utils.system.ClasUtilsFile; + +public class OccupanceEngine extends ReconstructionEngine { + + static final String BANKDIR = ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4/singles/occupancy"); + + int events; + int prescale; + OccupanceTable[] tables; + + public OccupanceEngine() { + super("Occupance", "baltzell","0.1"); + } + + @Override + public boolean processDataEventUser(DataEvent event) { + for (OccupanceTable t : tables) { + RawDataBank b = new RawDataBank(t.getHitBank(), 1000, OrderGroups.NOMINAL); + b.read(event); + t.fill(b, false); + } + if (++events % prescale == 0) { + for (OccupanceTable t : tables) { + if (t.getTable().getRowCount() > 0) + event.appendBank(t.create(events, event)); + t.reset(); + } + events = 0; + } + return true; + } + + @Override + public boolean init() { + prescale = Integer.parseInt(getEngineConfigString("occupancyPrescale","100")); + try { + tables = 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) + .toArray(OccupanceTable[]::new); + } catch (IOException ex) { + System.getLogger(OccupanceEngine.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); + return false; + } + + return true; + } + + @Override + public void detectorChanged(int runNumber) { + for (OccupanceTable table : tables) table.reset(); + events = 0; + } + +} \ No newline at end of file diff --git a/reconstruction/uber/src/main/java/org/jlab/service/uber/Uber.java b/reconstruction/uber/src/main/java/org/jlab/service/uber/Uber.java index e3aac26011..9f5c82fbf2 100644 --- a/reconstruction/uber/src/main/java/org/jlab/service/uber/Uber.java +++ b/reconstruction/uber/src/main/java/org/jlab/service/uber/Uber.java @@ -33,6 +33,7 @@ import org.jlab.service.dc.DCTBEngineAI; import org.jlab.service.eb.EBHBAIEngine; import org.jlab.service.eb.EBTBAIEngine; +import org.jlab.calibration.service.OccupanceEngine; /** * A container of engine sequences for shorter YAMLs. @@ -144,7 +145,8 @@ public Last() { add(new RICHEBEngine(), new RTPCEngine(), new VTXEngine(), - new CalibBanksEngine()); + new CalibBanksEngine(), + new OccupanceEngine()); } } }