Skip to content

Commit bdb04f3

Browse files
committed
add sorting
1 parent 4aa7a01 commit bdb04f3

File tree

1 file changed

+126
-83
lines changed

1 file changed

+126
-83
lines changed

common-tools/clas-io/src/main/java/org/jlab/utils/HipoDiff.java

Lines changed: 126 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,47 @@
1212

1313
public class HipoDiff {
1414

15+
static class SortedBank extends Bank {
16+
SortedBank(Schema s) { super(s); }
17+
int[] getSorted(int... index) {
18+
int[] rows = new int[getRows()];
19+
for (int row = 0; row < rows.length; row++) rows[row] = row;
20+
for (int i = 0; i < rows.length - 1; i++) {
21+
for (int j = 0; j < rows.length - i - 1; j++) {
22+
for (int idx : index) {
23+
int x1 = getInt(idx, rows[j]);
24+
int x2 = getInt(idx, rows[j + 1]);
25+
if (x1 > x2) {
26+
int tmp = rows[j];
27+
rows[j] = rows[j + 1];
28+
rows[j + 1] = tmp;
29+
break;
30+
}
31+
else if (x1 < x2) break;
32+
}
33+
}
34+
}
35+
return rows;
36+
}
37+
}
38+
1539
static int nrow = 0;
1640
static int nevent = -1;
1741
static int nentry = 0;
1842
static int nbadevent = 0;
1943
static int nbadrow = 0;
2044
static int nbadentry = 0;
45+
static int[] sortIndex = null;
46+
47+
static int nmax;
2148
static double tolerance;
22-
static boolean verboseMode = false;
23-
static boolean quietMode = false;
24-
static Bank runConfigBank = null;
25-
static SchemaFactory schemaFactory = null;
26-
static ArrayList<Bank> banksA = new ArrayList<>();
27-
static ArrayList<Bank> banksB = new ArrayList<>();
28-
static HashMap<String, HashMap<String,Integer>> badEntries = new HashMap<>();
49+
static boolean verboseMode;
50+
static boolean quietMode;
51+
static Bank runConfigBank;
52+
53+
static ArrayList<SortedBank> banksA = new ArrayList<>();
54+
static ArrayList<SortedBank> banksB = new ArrayList<>();
55+
static HashMap<String, HashMap<String, Integer>> badEntries = new HashMap<>();
2956

3057
public static void main(String args[]) {
3158

@@ -34,7 +61,8 @@ public static void main(String args[]) {
3461
op.addOption("-n", "-1", "number of events");
3562
op.addOption("-q", null, "quiet mode");
3663
op.addOption("-Q", null, "verbose mode");
37-
op.addOption("-b", null,"name of bank to diff");
64+
op.addOption("-b", null, "name of bank to diff");
65+
op.addOption("-s", null, "sort on column index");
3866
op.setRequiresInputList(true);
3967
op.parse(args);
4068
if (op.getInputList().size() != 2) {
@@ -43,35 +71,43 @@ public static void main(String args[]) {
4371
System.exit(1);
4472
}
4573

74+
if (op.getOption("-s").stringValue() != null) {
75+
String[] stmp = op.getOption("-s").stringValue().split(",");
76+
sortIndex = new int[stmp.length];
77+
for (int i = 0; i < stmp.length; i++) sortIndex[i] = Integer.parseInt(stmp[i]);
78+
}
4679
verboseMode = op.getOption("-Q").stringValue() != null;
4780
quietMode = op.getOption("-q").stringValue() != null;
48-
final int nmax = op.getOption("-n").intValue();
81+
nmax = op.getOption("-n").intValue();
4982
tolerance = op.getOption("-t").doubleValue();
5083

5184
HipoReader readerA = new HipoReader();
5285
HipoReader readerB = new HipoReader();
5386
readerA.open(op.getInputList().get(0));
5487
readerB.open(op.getInputList().get(1));
55-
Event eventA = new Event();
56-
Event eventB = new Event();
88+
SchemaFactory sf = readerA.getSchemaFactory();
89+
runConfigBank = new Bank(sf.getSchema("RUN::config"));
5790

58-
schemaFactory = readerA.getSchemaFactory();
59-
runConfigBank = new Bank(schemaFactory.getSchema("RUN::config"));
6091
if (op.getOption("-b").stringValue() == null) {
61-
for (Schema s : schemaFactory.getSchemaList()) {
62-
banksA.add(new Bank(s));
63-
banksB.add(new Bank(s));
92+
for (Schema s : sf.getSchemaList()) {
93+
banksA.add(new SortedBank(s));
94+
banksB.add(new SortedBank(s));
6495
}
96+
} else {
97+
banksA.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
98+
banksB.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
6599
}
66-
else {
67-
banksA.add(new Bank(schemaFactory.getSchema(op.getOption("-b").stringValue())));
68-
banksB.add(new Bank(schemaFactory.getSchema(op.getOption("-b").stringValue())));
69-
}
70100

71-
while (readerA.hasNext() && readerB.hasNext() && (nmax < 1 || nevent < nmax)) {
101+
compare(readerA, readerB);
102+
}
103+
104+
public static void compare(HipoReader a, HipoReader b) {
105+
Event eventA = new Event();
106+
Event eventB = new Event();
107+
while (a.hasNext() && b.hasNext() && (nmax < 1 || nevent < nmax)) {
72108
if (++nevent % 10000 == 0) System.out.println("Analyzed " + nevent + " events");
73-
readerA.nextEvent(eventA);
74-
readerB.nextEvent(eventB);
109+
a.nextEvent(eventA);
110+
b.nextEvent(eventB);
75111
eventA.read(runConfigBank);
76112
compare(eventA, eventB);
77113
}
@@ -85,84 +121,91 @@ public static void main(String args[]) {
85121
}
86122

87123
public static void compare(Event a, Event b) {
88-
for (int i=0; i<banksA.size(); i++) {
124+
for (int i = 0; i < banksA.size(); i++) {
89125
a.read(banksA.get(i));
90126
b.read(banksB.get(i));
91-
compare(banksA.get(i),banksB.get(i));
127+
compare(banksA.get(i), banksB.get(i));
92128
}
93129
}
94-
95-
public static void compare(Bank a, Bank b) {
96130

131+
public static void compare(SortedBank a, SortedBank b) {
97132
if (a.getRows() != b.getRows()) {
98133
System.out.println("========================= Different number of rows:");
99134
runConfigBank.show();
100135
a.show();
101136
b.show();
102137
nbadevent++;
103138
System.out.println("=========================");
139+
return;
104140
}
105-
else {
106-
for (int i = 0; i < a.getRows(); i++) {
107-
boolean mismatch = false;
108-
nrow++;
109-
for (int j = 0; j < a.getSchema().getElements(); j++) {
110-
final int type = a.getSchema().getType(j);
111-
final String name = a.getSchema().getElementName(j);
112-
int element = -1;
113-
String values = "";
114-
nentry++;
115-
switch (type) {
116-
case 1:
117-
if (a.getByte(name, i) != b.getByte(name, i)) {
118-
element = j;
119-
values += a.getByte(name, i) + "/" + b.getByte(name, i);
120-
}
121-
break;
122-
case 2:
123-
if (a.getShort(name, i) != b.getShort(name, i)) {
124-
element = j;
125-
values += a.getShort(name, i) + "/" + b.getShort(name, i);
126-
}
127-
break;
128-
case 3:
129-
if (a.getInt(name, i) != b.getInt(name, i)) {
130-
element = j;
131-
values += a.getInt(name, i) + "/" + b.getInt(name, i);
132-
}
133-
break;
134-
case 4:
135-
if ((!Double.isNaN(a.getFloat(name, i)) || !Double.isNaN(b.getFloat(name, i)))
136-
&& (!Double.isInfinite(a.getFloat(name, i)) || !Double.isInfinite(b.getFloat(name, i)))
137-
&& Math.abs(a.getFloat(name, i) - b.getFloat(name, i)) > tolerance) {
138-
element = j;
139-
values += a.getFloat(name, i) + "/" + b.getFloat(name, i);
140-
}
141-
break;
142-
}
143-
if (element >= 0) {
144-
if (verboseMode) {
145-
System.out.println("Bank.show "+a.getSchema().getName());
146-
a.show();
147-
b.show();
141+
int[] rowsA = sortIndex == null ? null : a.getSorted(sortIndex);
142+
int[] rowsB = sortIndex == null ? null : b.getSorted(sortIndex);
143+
for (int row = 0; row < a.getRows(); row++) {
144+
boolean mismatch = false;
145+
nrow++;
146+
int rowA = sortIndex == null ? row : rowsA[row];
147+
int rowB = sortIndex == null ? row : rowsB[row];
148+
for (int j = 0; j < a.getSchema().getElements(); j++) {
149+
final int type = a.getSchema().getType(j);
150+
final String name = a.getSchema().getElementName(j);
151+
int element = -1;
152+
String values = "";
153+
nentry++;
154+
switch (type) {
155+
case 1:
156+
if (a.getByte(name, rowA) != b.getByte(name, rowB)) {
157+
element = j;
158+
values += a.getByte(name, rowA) + "/" + b.getByte(name, rowB);
159+
}
160+
break;
161+
case 2:
162+
if (a.getShort(name, rowA) != b.getShort(name, rowB)) {
163+
element = j;
164+
values += a.getShort(name, rowA) + "/" + b.getShort(name, rowB);
148165
}
149-
if (!quietMode) {
150-
System.out.println(a.getSchema().getName()+" mismatch at event " + runConfigBank.getInt("event", 0)
151-
+ " in row " + i + " for variable " + name + " with values " + values);
166+
break;
167+
case 3:
168+
if (a.getInt(name, rowA) != b.getInt(name, rowB)) {
169+
element = j;
170+
values += a.getInt(name, rowA) + "/" + b.getInt(name, rowB);
152171
}
153-
mismatch = true;
154-
nbadentry++;
155-
String bankName = a.getSchema().getName();
156-
String elementName = a.getSchema().getElementName(element);
157-
if (!badEntries.containsKey(bankName)) badEntries.put(bankName, new HashMap<>());
158-
Map<String,Integer> m = badEntries.get(bankName);
159-
if (!m.containsKey(elementName)) m.put(elementName, 0);
160-
m.put(elementName, m.get(elementName)+1);
172+
break;
173+
case 4:
174+
if ((!Double.isNaN(a.getFloat(name, rowA)) || !Double.isNaN(b.getFloat(name, rowB)))
175+
&& (!Double.isInfinite(a.getFloat(name, rowA)) || !Double.isInfinite(b.getFloat(name, rowB)))
176+
&& Math.abs(a.getFloat(name, rowA) - b.getFloat(name, rowB)) > tolerance) {
177+
element = j;
178+
values += a.getFloat(name, rowA) + "/" + b.getFloat(name, rowB);
179+
}
180+
break;
181+
}
182+
if (element >= 0) {
183+
if (verboseMode) {
184+
System.out.println("Bank.show " + a.getSchema().getName());
185+
a.show();
186+
b.show();
187+
}
188+
if (!quietMode) {
189+
System.out.println(a.getSchema().getName() + " mismatch at event " + runConfigBank.getInt("event", 0)
190+
+ " in row " + row + " for variable " + name + " with values " + values);
191+
}
192+
mismatch = true;
193+
nbadentry++;
194+
String bankName = a.getSchema().getName();
195+
String elementName = a.getSchema().getElementName(element);
196+
if (!badEntries.containsKey(bankName)) {
197+
badEntries.put(bankName, new HashMap<>());
161198
}
199+
Map<String, Integer> m = badEntries.get(bankName);
200+
if (!m.containsKey(elementName)) {
201+
m.put(elementName, 0);
202+
}
203+
m.put(elementName, m.get(elementName) + 1);
162204
}
163-
if (mismatch) nbadrow++;
205+
}
206+
if (mismatch) {
207+
nbadrow++;
164208
}
165209
}
166210
}
167-
168211
}

0 commit comments

Comments
 (0)