ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Friis/TancNote/build_nn_observables.py
Revision: 1.3
Committed: Wed Apr 28 02:59:27 2010 UTC (15 years ago) by friis
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +16 -5 lines
Log Message:
Really almost done

File Contents

# User Rev Content
1 friis 1.1 #!/usr/bin/env python
2     import os
3     import glob
4     import re
5     from string import Template
6    
7     LATEX_NAME_MAPPING = {
8     'OneProngNoPiZero':
9 friis 1.2 r'$\tau^{-} \rightarrow \pi^{-}\nu_\tau$',
10 friis 1.1 'OneProngOnePiZero':
11 friis 1.2 r'$\tau^{-} \rightarrow \pi^{-}\pi^0\nu_\tau$',
12 friis 1.1 'OneProngTwoPiZero':
13 friis 1.2 r'$\tau^{-} \rightarrow \pi^{-}\pi^0\pi^0\nu_\tau$',
14 friis 1.1 'ThreeProngNoPiZero':
15 friis 1.2 r'$\tau^{-} \rightarrow \pi^{-}\pi^{+}\pi^{-}\nu_\tau$',
16 friis 1.1 'ThreeProngOnePiZero':
17 friis 1.2 r'$\tau^{-} \rightarrow \pi^{-}\pi^{+}\pi^{-}\pi^0\nu_\tau$',
18     }
19    
20     LATEX_NAME_MAPPING_NO_TAU = {
21     'OneProngNoPiZero':
22     r'$\pi^{-}\nu_\tau$',
23     'OneProngOnePiZero':
24     r'$\pi^{-}\pi^0\nu_\tau$',
25     'OneProngTwoPiZero':
26     r'$\pi^{-}\pi^0\pi^0\nu_\tau$',
27     'ThreeProngNoPiZero':
28     r'$\pi^{-}\pi^{+}\pi^{-}\nu_\tau$',
29     'ThreeProngOnePiZero':
30     r'$\pi^{-}\pi^{+}\pi^{-}\pi^0\nu_\tau$',
31 friis 1.1 }
32    
33     PLOT_TEMPLATE = Template(
34     r'''\put($x, $y) {\mbox{\includegraphics*[height=60mm]{${file_location}}}}
35     \put(${letterx}, ${lettery}){\small ($letter)}
36     ''')
37    
38    
39     FIGURE_TEMPLATE = Template(
40     r'''
41 friis 1.3 \begin{figure}[h!]
42 friis 1.1 \setlength{\unitlength}{1mm}
43     \begin{center}
44    
45     \begin{picture}($width, $height)(0,0)
46     ${figures}
47     \end{picture}
48    
49     \caption{
50 friis 1.3 Distributions of the input observables $observList for tau--candidates from the signal (red) and background (blue) neural network training samples
51     (see section~\ref{sec:$decayModeSection}) corresponding to the $decaymode decay mode.
52 friis 1.1 }
53    
54     \label{fig:${label}}
55     \end{center}
56     \end{figure}
57     ''')
58    
59     VARIABLE_TEMPLATE = Template(
60     r'''
61     \begin{itemize}
62     $items
63     \end{itemize}
64     ''')
65    
66     VARIABLE_ITEM_TEMPLATE = Template(
67     r'''
68     \item $item (Figure~$description)
69     ''')
70    
71     #VARIABLE_TEMPLATE = Template(
72     #r'''
73     #\begin{table}[h]
74     # \centering
75     # \begin{tabular}{l|r}
76     # Input observable & Figure index \\
77     # \hline
78     # $items
79     # \end{tabular}
80     #\end{table}
81     #''')
82     #
83     #VARIABLE_ITEM_TEMPLATE = Template(
84     #r'''
85     #$item & $description \\
86     #''')
87    
88     DESCRIPTION_TEMPLATE = Template(
89     r'''
90     \begin{description}
91     $items
92     \end{description}
93    
94     ''')
95    
96     DESCRIPTION_ITEM_TEMPLATE = Template(
97     r'''
98     \item[$item] \hfill \\
99     $description
100     ''')
101    
102     VAR_DESCRIPTIONS = {
103     'ChargedOutlierAngleN':
104     r'''$\Delta R$ between the Nth charged object (ordered by $P_T$) in the isolation region
105     and the tau--candidate momentum axis. If the number of
106     isolation region objects is less than N, the input is set at one.''',
107    
108     'ChargedOutlierPtN': r'''Transverse momentum of the Nth charged object in the isolation region. If the number of
109     isolation region objects is less than N, the input is set at zero.''',
110    
111     'DalitzN': r''' Invariant mass of four vector sum of the ``main track'' and the Nth signal
112     region object ''',
113    
114     'Eta': r'''Pseudo-rapidity of the signal region objects ''',
115    
116     'InvariantMassOfSignal': r'''Invariant mass of the composite object formed by the signal region constituents''',
117    
118     'MainTrackAngle': r'''$\Delta R$ between the ``main track'' and the composite four--vector formed by the
119     signal region constituents''',
120    
121     'MainTrackPt': r'''Transverse momentum of the ``main track'' ''',
122    
123     'OutlierNCharged': r'''Number of charged objects in the isolation region''',
124    
125     'OutlierSumPt': r'''Sum of the transverse momentum of objects in the isolation region''',
126    
127     'PiZeroAngleN': r'''$\Delta R$ between the Nth $\pi^0$ object in the signal region (ordered by $P_T$) and
128     the tau--candidate momentum axis''',
129    
130     'PiZeroPtN': r'''Transverse momentum of the Nth $\pi^0$ object in the signal region.''',
131    
132     'TrackAngleN': r'''$\Delta R$ between the Nth charged object in the signal region (ordered by $P_T$) and
133     the tau--candidate momentum axis, exclusive of the main track.''',
134    
135     'TrackPtN': r'''Transverse momentum of the Nth charged object in the signal region, exclusive of the
136     main track''',
137     }
138    
139     def groups_of(group_size, iterable):
140     count = 0
141     output = []
142     for item in iterable:
143     output.append(item)
144     count += 1
145     if count == group_size:
146     count = 0
147     yield output
148     output = []
149     # Yield partial at end
150     if output:
151     yield output
152    
153     def put_eta_first(x, y):
154     if x == 'Eta':
155     return 1
156     if y == 'Eta':
157     return -1
158     return cmp(x,y)
159    
160     if __name__=="__main__":
161     # parse file names
162     file_name_matcher = re.compile(r'(?P<network>[^_]*)_(?P<variable>\w*).pdf')
163    
164     figures_list = glob.glob('figures/NeuralNetObservables/*.pdf')
165    
166     # Keep track of all our plots
167     info_dict = {}
168    
169     variable_list = []
170    
171 friis 1.2 # Keep track of full variable names
172     variable_list_raw = []
173    
174 friis 1.1 for figure_file in figures_list:
175     figure_file_name = os.path.basename(figure_file)
176     parse = file_name_matcher.match(figure_file_name)
177     network = parse.group('network')
178     variable = parse.group('variable')
179    
180     # skip correlation variable
181     if variable == 'correlation':
182     continue
183    
184     # Get the dict for this network, otherwise create a new one
185     network_dict = info_dict.setdefault(network, {})
186     network_dict[variable] = figure_file
187    
188     # Remove trailing indices from variables
189    
190 friis 1.2 variable_list_raw.append(variable)
191 friis 1.1 variable_list.append(re.sub("[0-9]+$", "N", variable))
192    
193     # Build description of variables
194     variable_set = list(set(variable_list))
195     variable_set.sort()
196     print variable_set
197    
198 friis 1.2 variable_set_raw = list(set(variable_list_raw))
199     variable_set_raw.sort()
200    
201 friis 1.1 description_output_file = open(os.path.join(
202     'note/observable_distributions/','var_descriptions.tex'), 'w')
203     description_items = ""
204     for variable in variable_set:
205     description_items += DESCRIPTION_ITEM_TEMPLATE.substitute(
206     item = variable, description=VAR_DESCRIPTIONS[variable]
207     )
208    
209     description_output_file.write(DESCRIPTION_TEMPLATE.substitute(
210     items = description_items))
211    
212     # Loop over neural nets and build each section
213     for network, network_info in info_dict.iteritems():
214     print ""
215     print "Building %s" % network
216     output_file = open(os.path.join(
217     'note/observable_distributions/', network+'.tex'), 'w')
218    
219     section_label = '%s_input_descriptions' % network
220     output = "\label{sec:%s}\n" % section_label
221    
222     # Count variables
223     variables = network_info.keys()
224     # Sort nicely
225     variables.sort(put_eta_first)
226     n_vars = len(variables)
227    
228 friis 1.3 print network_info
229 friis 1.1
230     variable_table_entries=""
231    
232     # max group size of 5
233     for figure_index, plot_group in enumerate(groups_of(6, variables)):
234     letter_values = list("abcdefghijklmnopqrstuv")
235     current_height = 0
236     figures_list = ""
237     figure_label = "%s_%i" % (network, figure_index)
238 friis 1.3 observables_list = []
239 friis 1.1 for plot_row in groups_of(2, plot_group):
240    
241     letter = letter_values.pop(0)
242     figures_list += PLOT_TEMPLATE.substitute(
243     x = 0.5, y = current_height,
244     letterx=0.5+10, lettery=current_height+60, letter=letter,
245     file_location = network_info[plot_row[0]])
246    
247 friis 1.3 observables_list.append("%s (%s)" % (plot_row[0], letter))
248    
249 friis 1.1 # Update variable table
250     variable_table_entries += VARIABLE_ITEM_TEMPLATE.substitute(
251     item=plot_row[0],
252     description=r"\ref{fig:%s}%s"% (figure_label, letter))
253    
254     letter = letter_values.pop(0)
255     figures_list += PLOT_TEMPLATE.substitute(
256     x = 65, y = current_height,
257     letterx=65+10, lettery=current_height+60, letter=letter,
258     file_location = network_info[plot_row[1]])
259    
260 friis 1.3 observables_list.append("%s(%s)" % (plot_row[1], letter))
261    
262 friis 1.1 # Update variable table
263     variable_table_entries += VARIABLE_ITEM_TEMPLATE.substitute(
264     item=plot_row[1],
265     description=r"\ref{fig:%s}%s"% (figure_label, letter))
266    
267     # go to next row
268     current_height += 65
269    
270 friis 1.3 # build observables list
271     observables_string = ",".join(observables_list[:-1])
272     observables_string += " and %s" % observables_list[-1]
273    
274 friis 1.1 output += FIGURE_TEMPLATE.substitute(
275     width=130, height=current_height,
276     figures = figures_list,
277 friis 1.3 observList = observables_string,
278 friis 1.1 decaymode = LATEX_NAME_MAPPING[network],
279     decayModeSection=section_label, label = figure_label)
280    
281     variable_index = VARIABLE_TEMPLATE.substitute(
282     items = variable_table_entries)
283    
284 friis 1.3 #output_file.write(variable_index)
285 friis 1.1
286     output_file.write(output)
287    
288 friis 1.2 # Build table
289     table_file = open("note/observable_distributions/nn_var_table.tex", "w")
290    
291     table_file.write(r'''
292     \begin{table}[h]
293     \centering
294     ''')
295     networks = [
296     'OneProngNoPiZero',
297     'OneProngOnePiZero',
298     'OneProngTwoPiZero',
299     'ThreeProngNoPiZero',
300     'ThreeProngOnePiZero',
301     ]
302    
303     # Write column defintion
304     table_file.write(
305     r"\begin{tabular}{l|"
306     + "|".join(
307     ["c" for network in networks])
308     + r"|}")
309     table_file.write("\n")
310    
311     # Write header
312     table_file.write(
313     r"\multirow{2}{*}{Input observable} & \multicolumn{%i}{c}{Neural network} \\"
314     % len(networks))
315     table_file.write("\n")
316    
317     table_file.write(
318     " & "
319     + " & ".join(
320     [r"%s" % LATEX_NAME_MAPPING_NO_TAU[network] for network in networks])
321     + r"\\")
322     table_file.write("\n")
323    
324     table_file.write(r"\hline")
325     table_file.write("\n")
326    
327     # build each row
328     for variable in variable_set_raw:
329     row_output = variable + "&"
330     # list that contains a flag if a given network has this variable
331     def has_it(network):
332     if variable in info_dict[network]:
333     return r"$\bullet$"
334     else:
335     return ""
336     row_output += " & ".join([
337     has_it(network) for network in networks])
338     row_output += r"\\"
339     table_file.write(row_output)
340     table_file.write("\n")
341    
342     table_file.write(r"\end{tabular}")
343    
344     table_file.write(
345     r'''
346     \caption{
347 friis 1.3 Input observables used for each of the neural networks implemented by the Tau Neural Classifier.
348 friis 1.2 The columns represents the neural networks associated to various decay modes and the rows represent
349     the superset of input observables(see section~\ref{sec:tanc_nn_discriminants}) used in the neural networks.
350     A dot in a given row and column indicates that the observable in that row is used in the neural network corresponding
351     to that column.
352     }
353     \label{tab:nn_var_table}
354     ''')
355    
356     table_file.write("\n")
357     table_file.write(r"\end{table}")
358    
359    
360    
361    
362    
363    
364    
365 friis 1.1
366    
367