-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
192 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \ | ||
$* |
182 changes: 182 additions & 0 deletions
182
common-tools/clas-analysis/src/main/java/org/jlab/analysis/efficiency/Truth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
124 changes: 0 additions & 124 deletions
124
common-tools/clas-analysis/src/main/java/org/jlab/analysis/nabo/Efficiency.java
This file was deleted.
Oops, something went wrong.