View Javadoc

1   /*
2    * InterpolatedHMatrixBuilder.java
3    *
4    * Created on June 5, 2008, 11:54 PM
5    *
6    * $Id: InterpolatedHMatrixBuilder.java,v 1.1 2008/06/06 07:27:32 ngraf Exp $
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   * This class uses data stored in a properties file to create an InterpolatedHMatrix
23   * which is able to calculate a cluster "chi-squared" for arbitrary values of theta and 
24   * energy
25   *
26   * @author Norman Graf
27   */
28  public class InterpolatedHMatrixBuilder
29  {
30      static final boolean debug = false;
31      
32      /**
33       * Given a detectorname, this method will locate the appropriate
34       * properties file and create an HMatrix which interpolates between 
35       * the discrete points in theta and energy.
36       *
37       * @param detectorName the name of a supported detector
38       * @return an InterpolatedHMatrix
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          // Maps to hold all the arrays of numbers keyed on theta and energy
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          // for each theta and energy point, we have:
91          // n           average values
92          // n*(n+1)/2   covariance values (packed)
93          //
94          // will need to create a bilinearInterpolator for each of these (n^2+3n)/2 parameters
95          
96          // order is theta, energy
97          double[][] tmp = new double[numAngles][numEnergies];
98          BilinearInterpolator[] valInterpolators = new BilinearInterpolator[dim];
99          
100         // mean vector [dim]
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         // cov matrix elements [(dim*(dim+1))/2]
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 }