1
2
3
4
5
6
7
8
9 package org.lcsim.recon.emid.hmatrix;
10
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.TreeMap;
15 import org.lcsim.conditions.ConditionsManager;
16 import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
17 import org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException;
18 import org.lcsim.conditions.ConditionsSet;
19 import org.lcsim.math.interpolation.BilinearInterpolator;
20
21
22
23
24
25
26
27
28 public class InterpolatedHMatrixBuilder
29 {
30 static final boolean debug = false;
31
32
33
34
35
36
37
38
39
40 public static InterpolatedHMatrix build(String detectorName)
41 {
42 String conditionsSetName = detectorName+"_HMatrices";
43 ConditionsManager mgr = ConditionsManager.defaultInstance();
44 try
45 {
46 mgr.setDetector(detectorName, 0);
47 }
48 catch( ConditionsNotFoundException e)
49 {
50 System.out.println("Conditions not found for detector "+mgr.getDetector());
51 System.out.println("Please check that this properties file exists for this detector ");
52 throw new RuntimeException("Conditions not found for detector "+mgr.getDetector());
53 }
54 ConditionsSet cs = null;
55 try
56 {
57 cs = mgr.getConditions(conditionsSetName);
58 }
59 catch(ConditionsSetNotFoundException e)
60 {
61 System.out.println("ConditionSet "+conditionsSetName+" not found for detector "+mgr.getDetector());
62 System.out.println("Please check that this properties file exists for this detector ");
63 throw new RuntimeException("ConditionSet "+conditionsSetName+" not found for detector "+mgr.getDetector());
64
65 }
66
67 int dim = cs.getInt("Dimensionality");
68 double[] energies = cs.getDoubleArray("EnergyVals");
69 int numEnergies = energies.length;
70 if(debug) System.out.println("energies: "+Arrays.toString(energies));
71 double[] angles = cs.getDoubleArray("ThetaVals");
72 int numAngles = angles.length;
73 if(debug) System.out.println("angles: "+Arrays.toString(angles));
74
75
76 Map<String, double[]> avMap = new TreeMap<String, double[]>();
77 Map<String, double[]> covMap = new HashMap<String, double[]>();
78
79 for(double theta : angles)
80 {
81 for( double energy : energies)
82 {
83 String key = "Theta_"+theta+"_Energy_"+energy;
84 avMap.put(key, cs.getDoubleArray(key+"_vals"));
85 covMap.put(key, cs.getDoubleArray(key+"_covs"));
86 if(debug) System.out.println(key);
87 }
88 }
89
90
91
92
93
94
95
96
97 double[][] tmp = new double[numAngles][numEnergies];
98 BilinearInterpolator[] valInterpolators = new BilinearInterpolator[dim];
99
100
101 for (int iVal=0; iVal<dim; ++iVal)
102 {
103 for(int i=0; i<numAngles; ++i)
104 {
105 for(int j=0; j<numEnergies; ++j)
106 {
107 String key = "Theta_"+angles[i]+"_Energy_"+energies[j];
108 tmp[i][j] = avMap.get(key)[iVal];
109 if(debug) System.out.println(key +"_"+iVal+" "+ tmp[i][j]);
110 }
111 }
112 valInterpolators[iVal] = new BilinearInterpolator(angles, energies, tmp);
113 }
114
115
116 int covDim = (dim*(dim+1))/2;
117 BilinearInterpolator[] covInterpolators = new BilinearInterpolator[(dim*(dim+1))/2];
118
119 for (int iVal=0; iVal<covDim; ++iVal)
120 {
121 for(int i=0; i<numAngles; ++i)
122 {
123 for(int j=0; j<numEnergies; ++j)
124 {
125 String key = "Theta_"+angles[i]+"_Energy_"+energies[j];
126 tmp[i][j] = covMap.get(key)[iVal];
127 if(debug) System.out.println(key +"_"+iVal+" "+ tmp[i][j]);
128 }
129 }
130 covInterpolators[iVal] = new BilinearInterpolator(angles, energies, tmp);
131 }
132
133 return new InterpolatedHMatrix(dim, valInterpolators, covInterpolators);
134 }
135 }