Skip to content

Commit 2a1a7af

Browse files
baltzelltongtongcao
authored andcommitted
Speedup DetectorType lookup (#1013)
1 parent 0e535a8 commit 2a1a7af

File tree

4 files changed

+24
-42
lines changed

4 files changed

+24
-42
lines changed

common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorCollection.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package org.jlab.detector.base;
72

83
import java.util.ArrayList;
94
import java.util.Collection;
105
import java.util.HashSet;
116
import java.util.List;
127
import java.util.Set;
13-
import org.jlab.detector.base.DetectorDescriptor;
148
import org.jlab.utils.groups.IndexedList;
159

1610
/**
@@ -19,7 +13,7 @@
1913
*/
2014
public class DetectorCollection<T> {
2115

22-
private IndexedList<T> collection = new IndexedList<T>(3);
16+
private IndexedList<T> collection = new IndexedList<>(3);
2317
private String collectionName = "undefined";
2418

2519
public void setName(String name){
@@ -67,7 +61,7 @@ public T getObjectForKey(Long key){
6761
*/
6862
public List<T> getList(){
6963
Collection<T> vc = this.collection.getMap().values();
70-
List<T> list = new ArrayList<T>();
64+
List<T> list = new ArrayList<>();
7165
for(T c : vc){
7266
list.add(c);
7367
}
@@ -79,7 +73,7 @@ public List<T> getList(){
7973
*/
8074
public Set<Integer> getSectors(){
8175
Set<Long> list = this.collection.getMap().keySet();
82-
Set<Integer> sectors = new HashSet<Integer>();
76+
Set<Integer> sectors = new HashSet<>();
8377

8478
for(Long item : list){
8579
int sect = this.collection.getIndexGenerator().getIndex(item, 0);
@@ -95,7 +89,7 @@ public Set<Integer> getSectors(){
9589
*/
9690
public Set<Integer> getLayers(int sector){
9791
Set<Long> list = this.collection.getMap().keySet();
98-
Set<Integer> layers = new HashSet<Integer>();
92+
Set<Integer> layers = new HashSet<>();
9993
for(Long item : list){
10094
int sect = this.collection.getIndexGenerator().getIndex(item, 0);
10195
if(sect==sector){
@@ -113,7 +107,7 @@ public Set<Integer> getLayers(int sector){
113107
*/
114108
public Set<Integer> getComponents(int sector, int layer){
115109
Set<Long> list = this.collection.getMap().keySet();
116-
Set<Integer> components = new HashSet<Integer>();
110+
Set<Integer> components = new HashSet<>();
117111
for(Long item : list){
118112
int sect = this.collection.getIndexGenerator().getIndex(item, 0);
119113
int lay = this.collection.getIndexGenerator().getIndex(item, 1);

common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorDescriptor.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package org.jlab.detector.base;
72

8-
93
/**
104
*
115
* @author gavalian
@@ -158,7 +152,5 @@ public int compareTo(DetectorDescriptor o) {
158152
} else {
159153
return 1;
160154
}
161-
//return 0;
162-
//return 1;
163155
}
164156
}

common-tools/clas-detector/src/main/java/org/jlab/detector/base/DetectorType.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jlab.detector.base;
22

3+
import java.util.HashMap;
4+
35
/**
46
*
57
* @author gavalian
@@ -37,6 +39,16 @@ public enum DetectorType {
3739

3840
private final int detectorId;
3941
private final String detectorName;
42+
43+
private static final HashMap<String,DetectorType> stringLookup = new HashMap<>();
44+
private static final HashMap<Integer,DetectorType> intLookup = new HashMap<>();
45+
46+
static {
47+
for (DetectorType t : values()) {
48+
stringLookup.put(t.getName(), t);
49+
intLookup.put(t.getDetectorId(), t);
50+
}
51+
}
4052

4153
DetectorType(){
4254
detectorId = 0;
@@ -70,11 +82,7 @@ public int getDetectorId() {
7082
* @return
7183
*/
7284
public static DetectorType getType(String name) {
73-
name = name.trim();
74-
for(DetectorType id: DetectorType.values())
75-
if (id.getName().equalsIgnoreCase(name))
76-
return id;
77-
return UNDEFINED;
85+
return stringLookup.getOrDefault(name.trim(), UNDEFINED);
7886
}
7987

8088
/**
@@ -83,10 +91,6 @@ public static DetectorType getType(String name) {
8391
* @return
8492
*/
8593
public static DetectorType getType(Integer detId) {
86-
87-
for(DetectorType id: DetectorType.values())
88-
if (id.getDetectorId() == detId)
89-
return id;
90-
return UNDEFINED;
94+
return intLookup.getOrDefault(detId, UNDEFINED);
9195
}
9296
}

common-tools/clas-detector/src/main/java/org/jlab/detector/base/GeometryFactory.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package org.jlab.detector.base;
72

83
import org.jlab.detector.calib.utils.DatabaseConstantProvider;
@@ -24,12 +19,7 @@ public class GeometryFactory {
2419
public static int SYSTEM_TILTED = 2;
2520
public static int SYSTEM_CLAS = 3;
2621

27-
//private volatile
28-
29-
public GeometryFactory(){
30-
31-
}
32-
22+
public GeometryFactory(){}
3323

3424
/**
3525
* Load constants for given detector, with RUN and VARIATION specified
@@ -39,16 +29,18 @@ public GeometryFactory(){
3929
* @return
4030
*/
4131
public static ConstantProvider getConstants(DetectorType type, int run, String variation){
32+
4233
DatabaseConstantProvider provider = new DatabaseConstantProvider(run,variation);
34+
4335
if(type==DetectorType.DC){
4436
provider.loadTable("/geometry/dc/dc");
4537
provider.loadTable("/geometry/dc/region");
4638
provider.loadTable("/geometry/dc/superlayer");
4739
provider.loadTable("/geometry/dc/layer");
4840
provider.loadTable("/geometry/dc/alignment");
4941
provider.loadTable("/geometry/dc/ministagger");
50-
provider.loadTable("/geometry/dc/endplatesbow");
51-
provider.loadTable("/geometry/dc/feedthroughs");
42+
provider.loadTable("/geometry/dc/endplatesbow");
43+
provider.loadTable("/geometry/dc/feedthroughs");
5244
}
5345

5446
if(type==DetectorType.ECAL){

0 commit comments

Comments
 (0)