|
| 1 | +package org.jlab.rec.cvt.banks; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import org.junit.AfterClass; |
| 5 | +import org.junit.BeforeClass; |
| 6 | +import org.junit.Test; |
| 7 | +import static org.junit.Assert.*; |
| 8 | + |
| 9 | +import org.jlab.detector.banks.RawBank.OrderType; |
| 10 | +import org.jlab.detector.base.DetectorType; |
| 11 | +import org.jlab.io.base.DataBank; |
| 12 | +import org.jlab.io.base.DataEvent; |
| 13 | +import org.jlab.analysis.physics.TestEvent; |
| 14 | +import org.jlab.rec.cvt.bmt.BMTType; |
| 15 | +import org.jlab.rec.cvt.cluster.Cluster; |
| 16 | +import org.jlab.rec.cvt.hit.Hit; |
| 17 | +import org.jlab.rec.cvt.services.CVTEngine; |
| 18 | +import org.jlab.utils.groups.IndexedTable; |
| 19 | + |
| 20 | +public class BSTHitReaderTest { |
| 21 | + |
| 22 | + private static final int NOMINAL = OrderType.NOMINAL.getTypeId(); |
| 23 | + private static final int BACKGROUND = OrderType.BGADDED_NOMINAL.getTypeId(); |
| 24 | + private static final int REMOVED = OrderType.BGREMOVED.getTypeId(); |
| 25 | + private static String previousClas12Dir; |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void initializeGeometry() { |
| 29 | + previousClas12Dir = System.getProperty("CLAS12DIR"); |
| 30 | + System.setProperty("CLAS12DIR", "../../"); |
| 31 | + CVTEngine engine = new CVTEngine(); |
| 32 | + assertTrue(engine.init()); |
| 33 | + engine.detectorChanged(11); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterClass |
| 37 | + public static void restoreResourcePath() { |
| 38 | + if (previousClas12Dir == null) { |
| 39 | + System.clearProperty("CLAS12DIR"); |
| 40 | + } else { |
| 41 | + System.setProperty("CLAS12DIR", previousClas12Dir); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void leadingOrderFilteredRowPreservesOriginalIds() { |
| 47 | + assertHitReferences(new int[]{REMOVED, NOMINAL, BACKGROUND}, |
| 48 | + new int[]{2, 3, 4}, new int[]{2, 3}); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void interiorOrderFilteredRowPreservesOriginalIds() { |
| 53 | + assertHitReferences(new int[]{NOMINAL, REMOVED, NOMINAL}, |
| 54 | + new int[]{2, 3, 4}, new int[]{1, 3}); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void unfilteredRowsKeepTheirIds() { |
| 59 | + assertHitReferences(new int[]{NOMINAL, NOMINAL, BACKGROUND}, |
| 60 | + new int[]{2, 3, 4}, new int[]{1, 2, 3}); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void timingRowDoesNotRenumberTheFollowingHit() { |
| 65 | + assertHitReferences(new int[]{NOMINAL, NOMINAL}, |
| 66 | + new int[]{-1, 3}, new int[]{2}); |
| 67 | + } |
| 68 | + |
| 69 | + private static void assertHitReferences(int[] orders, int[] adcs, int[] expectedRawRows) { |
| 70 | + // Reuse the repository's event/schema factory, retaining no fixture hits. |
| 71 | + DataEvent event = TestEvent.get(DetectorType.CVT); |
| 72 | + event.removeBanks(event.getBankList()); |
| 73 | + assertEquals(0, event.getBankList().length); |
| 74 | + DataBank raw = event.createBank("BST::adc", orders.length); |
| 75 | + IndexedTable status = new IndexedTable(3, "status/I"); |
| 76 | + for (int row = 0; row < orders.length; row++) { |
| 77 | + int strip = adcs[row] < 0 ? 1 : 10 + row; |
| 78 | + raw.setByte("sector", row, (byte) 1); |
| 79 | + raw.setByte("layer", row, (byte) 1); |
| 80 | + raw.setShort("component", row, (short) strip); |
| 81 | + raw.setInt("ADC", row, adcs[row]); |
| 82 | + raw.setByte("order", row, (byte) orders[row]); |
| 83 | + raw.setFloat("time", row, 10 + row); |
| 84 | + status.addEntry(1, 1, strip); |
| 85 | + status.setIntValue(0, "status", 1, 1, strip); |
| 86 | + } |
| 87 | + event.appendBank(raw); |
| 88 | + IndexedTable adcStatus = new IndexedTable(3, "adcstatus/I"); |
| 89 | + adcStatus.addEntry(0, 0, 0); |
| 90 | + adcStatus.setIntValue(0, "adcstatus", 0, 0, 0); |
| 91 | + |
| 92 | + HitReader reader = new HitReader(); |
| 93 | + reader.fetch_SVTHits(event, -1, -1, status, adcStatus); |
| 94 | + List<Hit> hits = reader.getSVTHits(); |
| 95 | + assertEquals(expectedRawRows.length, hits.size()); |
| 96 | + |
| 97 | + // Assemble one synthetic cluster to check the bank writer's references. |
| 98 | + Cluster cluster = new Cluster(DetectorType.BST, BMTType.UNDEFINED, 1, 1, 1); |
| 99 | + cluster.addAll(hits); |
| 100 | + cluster.calc_CentroidParams(); |
| 101 | + DataBank hitBank = RecoBankWriter.fillSVTHitBank(event, hits, "BST::Hits"); |
| 102 | + DataBank clusterBank = RecoBankWriter.fillSVTClusterBank(event, |
| 103 | + List.of(cluster), "BST::Clusters"); |
| 104 | + assertNotNull(hitBank); |
| 105 | + assertNotNull(clusterBank); |
| 106 | + assertEquals(expectedRawRows.length, hitBank.rows()); |
| 107 | + assertEquals(1, clusterBank.rows()); |
| 108 | + assertEquals(expectedRawRows.length, clusterBank.getShort("size", 0)); |
| 109 | + for (int index = 0; index < expectedRawRows.length; index++) { |
| 110 | + int rawRow = expectedRawRows[index] - 1; |
| 111 | + // The physical strip independently identifies its original ADC row. |
| 112 | + assertEquals(raw.getShort("component", rawRow), hits.get(index).getStrip().getStrip()); |
| 113 | + assertEquals("Hit ID must reference the original ADC row", |
| 114 | + expectedRawRows[index], hits.get(index).getId()); |
| 115 | + assertEquals(expectedRawRows[index], hitBank.getShort("ID", index)); |
| 116 | + assertEquals("Cluster reference must use the original ADC row ID", |
| 117 | + expectedRawRows[index], clusterBank.getShort("Hit" + (index + 1) + "_ID", 0)); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments