From b20f41349c16606743e7c433d2f1b14242e34c56 Mon Sep 17 00:00:00 2001 From: Nathan Baltzell Date: Tue, 17 Dec 2024 17:08:34 -0500 Subject: [PATCH] rename, cleanup, add json --- bin/trutheff | 10 + .../org/jlab/analysis/efficiency/Truth.java | 182 ++++++++++++++++++ .../org/jlab/analysis/nabo/Efficiency.java | 124 ------------ 3 files changed, 192 insertions(+), 124 deletions(-) create mode 100755 bin/trutheff create mode 100644 common-tools/clas-analysis/src/main/java/org/jlab/analysis/efficiency/Truth.java delete mode 100644 common-tools/clas-analysis/src/main/java/org/jlab/analysis/nabo/Efficiency.java diff --git a/bin/trutheff b/bin/trutheff new file mode 100755 index 000000000..5ba7ba36a --- /dev/null +++ b/bin/trutheff @@ -0,0 +1,10 @@ +#!/bin/bash + +. `dirname $0`/../libexec/env.sh + +export MALLOC_ARENA_MAX=1 + +java -Xmx1536m -Xms1024m -XX:+UseSerialGC \ + -cp "$CLAS12DIR/lib/clas/*:$CLAS12DIR/lib/services/*:$CLAS12DIR/lib/utils/*" \ + org.jlab.analysis.efficiency.Truth \ + $* diff --git a/common-tools/clas-analysis/src/main/java/org/jlab/analysis/efficiency/Truth.java b/common-tools/clas-analysis/src/main/java/org/jlab/analysis/efficiency/Truth.java new file mode 100644 index 000000000..a439c60fa --- /dev/null +++ b/common-tools/clas-analysis/src/main/java/org/jlab/analysis/efficiency/Truth.java @@ -0,0 +1,182 @@ +package org.jlab.analysis.efficiency; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeMap; +import java.util.stream.LongStream; +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.jnp.hipo4.io.HipoReader; +import org.jlab.jnp.utils.json.JsonArray; +import org.jlab.jnp.utils.json.JsonObject; +import org.jlab.utils.options.OptionParser; + +/** + * Efficiency matrix calculator based solely on the MC::GenMatch truth-matching + * bank (which is purely hit-based), and a pid assignment match in MC::Particle + * and REC::Particle. + * + * @author baltzell + */ +public class Truth { + + static final int UDF = 0; + static final List NEGATIVES = Arrays.asList(11, -211, -321, -2212); + static final List POSITIVES = Arrays.asList(-11, 211, 321, 2212, 45); + static final List NEUTRALS = Arrays.asList(22, 2112); + static List PIDS; + + Schema mcGenMatch; + Schema mcParticle; + Schema recParticle; + long[][] tallies; + + public static void main(String[] args) { + OptionParser o = new OptionParser("trutheff"); + o.setRequiresInputList(true); + o.parse(args); + System.out.println(o.getInputList()); + Truth t = new Truth(o.getInputList().get(0)); + t.add(o.getInputList()); + System.out.println(t.toTable()); + System.out.println(t.toJson()); + } + + public Truth(SchemaFactory s) { + init(s); + } + + public Truth(HipoReader r) { + init(r.getSchemaFactory()); + } + + public Truth(String filename) { + HipoReader r = new HipoReader(); + r.open(filename); + init(r.getSchemaFactory()); + } + + private void init(SchemaFactory schema) { + PIDS = new ArrayList(NEGATIVES); + PIDS.addAll(POSITIVES); + PIDS.addAll(NEUTRALS); + PIDS.add(UDF); + tallies = new long[PIDS.size()][PIDS.size()]; + mcGenMatch = schema.getSchema("MC::GenMatch"); + mcParticle = schema.getSchema("MC::Particle"); + recParticle = schema.getSchema("REC::Particle"); + } + + /** + * Get one element of the efficiency matrix. + * @param truth true PID + * @param rec reconstructed PID + * @return probability + */ + public float get(int truth, int rec) { + long sum = LongStream.of(tallies[PIDS.indexOf(truth)]).sum(); + return sum>0 ? tallies[PIDS.indexOf(truth)][PIDS.indexOf(rec)]/sum : 0; + } + + /** + * Add an event in the form of truth and reconstructed particle species. + * @param truth truth PID + * @param rec reconstructed PID + */ + public void add(int truth, int rec) { + if (PIDS.contains(truth)) { + if (!PIDS.contains(rec)) add(truth, UDF); + else tallies[PIDS.indexOf(truth)][PIDS.indexOf(rec)]++; + } + } + + /** + * Add a HIPO event. + * @param e + */ + public void add(Event e) { + Bank bm = new Bank(mcParticle); + Bank br = new Bank(recParticle); + e.read(bm); + e.read(br); + TreeMap good = getMapping(e); + for (short row=0; row filenames) { + Event e = new Event(); + for (String f : filenames) { + HipoReader r = new HipoReader(); + r.open(f); + while (r.hasNext()) { + r.nextEvent(e); + add(e); + } + } + } + + /** + * Truth-matching banks contain pointers to MC::Particle and REC::Particle, + * and here we cache that mapping to avoid nested loops. + */ + private TreeMap getMapping(Event e) { + Bank b = new Bank(mcGenMatch); + e.read(b); + TreeMap m = new TreeMap<>(); + for (int row=0; row NEGATIVES = Arrays.asList(11, -211, -321, -2212); - static final List POSITIVES = Arrays.asList(11, 211, 321, 2212); - static final List NEUTRALS = Arrays.asList(22, 2112); - static List PIDS; - - Schema mcGenMatch; - Schema mcParticle; - Schema recParticle; - int[][] caches; - - public static void main(String[] args) { - Efficiency e = new Efficiency(args); - System.out.println(e); - } - - private void init(SchemaFactory schema) { - PIDS = new ArrayList(NEGATIVES); - PIDS.addAll(POSITIVES); - PIDS.addAll(NEUTRALS); - caches = new int[PIDS.size()][PIDS.size()]; - mcGenMatch = schema.getSchema("MC::GenMatch"); - mcParticle = schema.getSchema("MC::Particle"); - recParticle = schema.getSchema("REC::Particle"); - } - - public Efficiency(SchemaFactory schema) { - init(schema); - } - - public Efficiency(String... filenames) { - HipoReader r = new HipoReader(); - r.open(filenames[0]); - init(r.getSchemaFactory()); - r.close(); - add(filenames); - } - - public float get(int mc, int rec) { - long sum = IntStream.of(caches[PIDS.indexOf(mc)]).sum(); - return sum>0 ? caches[PIDS.indexOf(mc)][PIDS.indexOf(rec)]/sum : 0; - } - - public float get(int pid) { - return get(pid,pid); - } - - @Override - public String toString() { - StringBuilder s = new StringBuilder(); - s.append(" "); - for (int i=0; i good = getMapping(e); - for (short row=0; row m = new TreeMap<>(); - for (int row=0; row