Skip to content

Commit

Permalink
rename, cleanup, add json
Browse files Browse the repository at this point in the history
  • Loading branch information
baltzell committed Dec 17, 2024
1 parent 14d6d47 commit b20f413
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 124 deletions.
10 changes: 10 additions & 0 deletions bin/trutheff
Original file line number Diff line number Diff line change
@@ -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 \
$*
Original file line number Diff line number Diff line change
@@ -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<Integer> NEGATIVES = Arrays.asList(11, -211, -321, -2212);
static final List<Integer> POSITIVES = Arrays.asList(-11, 211, 321, 2212, 45);
static final List<Integer> NEUTRALS = Arrays.asList(22, 2112);
static List<Integer> 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<Short,Short> good = getMapping(e);
for (short row=0; row<bm.getRows(); ++row) {
if (!good.containsKey(row)) add(bm.getInt("pid",row), UDF);
else add(bm.getInt("pid",row), br.getInt("pid",good.get(row)));
}
}

/**
* Add input HIPO files by path.
* @param filenames
*/
public void add(List<String> 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<Short,Short> m = new TreeMap<>();
for (int row=0; row<b.getRows(); ++row)
m.put(b.getShort("mcindex", row), b.getShort("pindex",row));
return m;
}

/**
* Get efficiencies as a human-readable table.
* @return
*/
public String toTable() {
StringBuilder s = new StringBuilder();
s.append(" ");
for (int i=0; i<PIDS.size(); ++i) {
s.append(String.format("%7d",PIDS.get(i)));
if (PIDS.size()==i+1) s.append("\n");
}
for (int i=0; i<PIDS.size(); ++i) {
s.append(String.format("%6d",PIDS.get(i)));
for (int j=0; j<PIDS.size(); ++j) {
s.append(String.format("%7.4f",get(PIDS.get(i),PIDS.get(j))));
if (PIDS.size()==j+1) s.append("\n");
}
}
return s.toString();
}

/**
* Get efficiencies as a JSON object.
* @return
*/
public JsonObject toJson() {
JsonObject ret = new JsonObject();
JsonArray pids = new JsonArray();
JsonArray effs = new JsonArray();
for (int i=0; i<PIDS.size(); ++i) {
pids.add(PIDS.get(i));
JsonArray a = new JsonArray();
for (int j=0; j<PIDS.size(); ++j)
a.add(get(PIDS.get(i),PIDS.get(j)));
effs.add(a);
}
ret.add("pids", pids);
ret.add("effs", effs);
return ret;
}

}

This file was deleted.

0 comments on commit b20f413

Please sign in to comment.