1
2
3
4
5
6
7
8
9 package org.lcsim.cal.calib;
10
11 import java.io.BufferedReader;
12 import java.io.BufferedWriter;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter;
18 import java.io.Writer;
19 import java.text.DecimalFormat;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.SortedSet;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
32 import org.lcsim.recon.emid.hmatrix.HMatrix;
33
34
35
36
37
38
39
40 public class InterpolatedHMatrixProcessor
41 {
42 public static void main(String[] args) throws Exception
43 {
44 if(args.length<2)
45 {
46 usage();
47 return ;
48 }
49
50 String listOfFiles = args[0];
51 String detectorName = args[1];
52
53 List<File> filesToProcess = filesToProcess(listOfFiles);
54 int numToProcess = filesToProcess.size();
55 double energy = 0.;
56 double theta = 0.;
57
58
59 SortedSet<Double> angles = new TreeSet<Double>();
60 SortedSet<Double> energies = new TreeSet<Double>();
61
62
63 Map<String, double[]> avMap = new TreeMap<String, double[]>();
64 Map<String, double[]> covMap = new HashMap<String, double[]>();
65
66 int hMatrixDimensionality = 0;
67 for(File f : filesToProcess)
68 {
69 String[] parts = f.getName().split("_");
70 for(String s : parts)
71 {
72 if(s.startsWith("Theta"))
73 {
74 theta = Double.parseDouble(s.substring(5));
75 }
76 if(s.contains("GeV"))
77 {
78 energy = Double.parseDouble(s.substring(0,s.length()-3));
79 }
80 }
81 angles.add(theta);
82 energies.add(energy);
83 String key = "Theta_"+theta+"_Energy_"+energy;
84
85 HMatrix h = HMatrix.read(f.getName());
86 hMatrixDimensionality = h.averageVector().length;
87 avMap.put(key, h.averageVector());
88 covMap.put(key, h.packedInverseCovarianceMatrix());
89
90 }
91
92
93 FileOutputStream fos = new FileOutputStream(detectorName+"_HMatrices.properties");
94 Writer w = new BufferedWriter(new OutputStreamWriter(fos));
95
96
97 w.write("# "+detectorName+" "+comment()+"\n");
98
99 w.write("Dimensionality = "+hMatrixDimensionality+"\n");
100
101 w.write("ThetaVals = "+stripBrackets(angles.toString())+"\n");
102 w.write("EnergyVals = "+stripBrackets(energies.toString())+"\n");
103
104
105 Set<String> keys = avMap.keySet();
106 for(String s : keys)
107 {
108 double[] vals = avMap.get(s);
109 w.write(s+"_vals = "+stripBrackets(Arrays.toString(vals))+"\n");
110 }
111
112 keys = covMap.keySet();
113 for(String s : keys)
114 {
115 double[] covs = covMap.get(s);
116 w.write(s+"_covs = "+stripBrackets(Arrays.toString(covs))+"\n");
117 }
118 w.flush();
119 w.close();
120 }
121
122 static void usage()
123 {
124 System.out.println("This is InterpolatedHMatrixProcessor");
125 System.out.println("usage:");
126 System.out.println("java InterpolatedHMatrixProcessor listOfInputHMatrixFiles detectorname");
127 System.out.println(" files in the list should have the form: ");
128 System.out.println(" detectorName_ThetaX_YGeV_Z.hmx");
129 }
130
131 static List<File> filesToProcess(String listOfFiles) throws Exception
132 {
133 List<File> filesToProcess = new ArrayList<File>();
134 FileInputStream fin = new FileInputStream(listOfFiles);
135 BufferedReader br = new BufferedReader(new InputStreamReader(fin));
136 String line;
137
138 while ( (line = br.readLine()) != null)
139 {
140 File f = new File(line.trim());
141 if(!f.exists()) throw new RuntimeException("Input file "+f+ " does not exist!");
142 filesToProcess.add(f);
143 }
144
145 return filesToProcess;
146 }
147
148 static String stripBrackets(String s)
149 {
150 String bad = "[]";
151 String result = "";
152 for ( int i = 0; i < s.length(); i++ )
153 {
154 if ( bad.indexOf(s.charAt(i)) < 0 )
155 result += s.charAt(i);
156 }
157 return result;
158 }
159
160 static String comment()
161 {
162 Calendar cal = new GregorianCalendar();
163 Date date = new Date();
164 cal.setTime(date);
165 DecimalFormat formatter = new DecimalFormat("00");
166 String day = formatter.format(cal.get(Calendar.DAY_OF_MONTH));
167 String month = formatter.format(cal.get(Calendar.MONTH)+1);
168 String myDate =cal.get(Calendar.YEAR)+month+day;
169 return myDate+" "+System.getProperty("user.name");
170 }
171
172 }