ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Friis/TancNote/build_nn_observables.py
Revision: 1.1
Committed: Thu Apr 22 16:28:45 2010 UTC (15 years ago) by friis
Content type: text/x-python
Branch: MAIN
Log Message:
Cleanup some, (not all) of references/citations

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     r'$\tau^{-} \rightarrow \pi^{-}$',
10     'OneProngOnePiZero':
11     r'$\tau^{-} \rightarrow \pi^{-}\pi^0$',
12     'OneProngTwoPiZero':
13     r'$\tau^{-} \rightarrow \pi^{-}\pi^0\pi^0$',
14     'ThreeProngNoPiZero':
15     r'$\tau^{-} \rightarrow \pi^{-}\pi^{+}\pi^{-}$',
16     'ThreeProngOnePiZero':
17     r'$\tau^{-} \rightarrow \pi^{-}\pi^{+}\pi^{-}\pi^0$',
18     }
19    
20     PLOT_TEMPLATE = Template(
21     r'''\put($x, $y) {\mbox{\includegraphics*[height=60mm]{${file_location}}}}
22     \put(${letterx}, ${lettery}){\small ($letter)}
23     ''')
24    
25    
26     FIGURE_TEMPLATE = Template(
27     r'''
28     \begin{figure}[b]
29     \setlength{\unitlength}{1mm}
30     \begin{center}
31    
32     \begin{picture}($width, $height)(0,0)
33     ${figures}
34     \end{picture}
35    
36     \caption{
37     Training sample distributions of signal (red) and background (blue) for different observables used
38     (see section~\ref{sec:$decayModeSection}) in the neural network corresponding to the $decaymode decay mode.
39     }
40    
41     \label{fig:${label}}
42     \end{center}
43     \end{figure}
44     ''')
45    
46     VARIABLE_TEMPLATE = Template(
47     r'''
48     \begin{itemize}
49     $items
50     \end{itemize}
51     ''')
52    
53     VARIABLE_ITEM_TEMPLATE = Template(
54     r'''
55     \item $item (Figure~$description)
56     ''')
57    
58     #VARIABLE_TEMPLATE = Template(
59     #r'''
60     #\begin{table}[h]
61     # \centering
62     # \begin{tabular}{l|r}
63     # Input observable & Figure index \\
64     # \hline
65     # $items
66     # \end{tabular}
67     #\end{table}
68     #''')
69     #
70     #VARIABLE_ITEM_TEMPLATE = Template(
71     #r'''
72     #$item & $description \\
73     #''')
74    
75     DESCRIPTION_TEMPLATE = Template(
76     r'''
77     \begin{description}
78     $items
79     \end{description}
80    
81     ''')
82    
83     DESCRIPTION_ITEM_TEMPLATE = Template(
84     r'''
85     \item[$item] \hfill \\
86     $description
87     ''')
88    
89     VAR_DESCRIPTIONS = {
90     'ChargedOutlierAngleN':
91     r'''$\Delta R$ between the Nth charged object (ordered by $P_T$) in the isolation region
92     and the tau--candidate momentum axis. If the number of
93     isolation region objects is less than N, the input is set at one.''',
94    
95     'ChargedOutlierPtN': r'''Transverse momentum of the Nth charged object in the isolation region. If the number of
96     isolation region objects is less than N, the input is set at zero.''',
97    
98     'DalitzN': r''' Invariant mass of four vector sum of the ``main track'' and the Nth signal
99     region object ''',
100    
101     'Eta': r'''Pseudo-rapidity of the signal region objects ''',
102    
103     'InvariantMassOfSignal': r'''Invariant mass of the composite object formed by the signal region constituents''',
104    
105     'MainTrackAngle': r'''$\Delta R$ between the ``main track'' and the composite four--vector formed by the
106     signal region constituents''',
107    
108     'MainTrackPt': r'''Transverse momentum of the ``main track'' ''',
109    
110     'OutlierNCharged': r'''Number of charged objects in the isolation region''',
111    
112     'OutlierSumPt': r'''Sum of the transverse momentum of objects in the isolation region''',
113    
114     'PiZeroAngleN': r'''$\Delta R$ between the Nth $\pi^0$ object in the signal region (ordered by $P_T$) and
115     the tau--candidate momentum axis''',
116    
117     'PiZeroPtN': r'''Transverse momentum of the Nth $\pi^0$ object in the signal region.''',
118    
119     'TrackAngleN': r'''$\Delta R$ between the Nth charged object in the signal region (ordered by $P_T$) and
120     the tau--candidate momentum axis, exclusive of the main track.''',
121    
122     'TrackPtN': r'''Transverse momentum of the Nth charged object in the signal region, exclusive of the
123     main track''',
124     }
125    
126     def groups_of(group_size, iterable):
127     count = 0
128     output = []
129     for item in iterable:
130     output.append(item)
131     count += 1
132     if count == group_size:
133     count = 0
134     yield output
135     output = []
136     # Yield partial at end
137     if output:
138     yield output
139    
140     def put_eta_first(x, y):
141     if x == 'Eta':
142     return 1
143     if y == 'Eta':
144     return -1
145     return cmp(x,y)
146    
147     if __name__=="__main__":
148     # parse file names
149     file_name_matcher = re.compile(r'(?P<network>[^_]*)_(?P<variable>\w*).pdf')
150    
151     figures_list = glob.glob('figures/NeuralNetObservables/*.pdf')
152    
153     # Keep track of all our plots
154     info_dict = {}
155    
156     variable_list = []
157    
158     for figure_file in figures_list:
159     figure_file_name = os.path.basename(figure_file)
160     parse = file_name_matcher.match(figure_file_name)
161     network = parse.group('network')
162     variable = parse.group('variable')
163    
164     # skip correlation variable
165     if variable == 'correlation':
166     continue
167    
168     # Get the dict for this network, otherwise create a new one
169     network_dict = info_dict.setdefault(network, {})
170     network_dict[variable] = figure_file
171    
172     # Remove trailing indices from variables
173    
174     variable_list.append(re.sub("[0-9]+$", "N", variable))
175    
176     # Build description of variables
177     variable_set = list(set(variable_list))
178     variable_set.sort()
179     print variable_set
180    
181     description_output_file = open(os.path.join(
182     'note/observable_distributions/','var_descriptions.tex'), 'w')
183     description_items = ""
184     for variable in variable_set:
185     description_items += DESCRIPTION_ITEM_TEMPLATE.substitute(
186     item = variable, description=VAR_DESCRIPTIONS[variable]
187     )
188    
189     description_output_file.write(DESCRIPTION_TEMPLATE.substitute(
190     items = description_items))
191    
192     # Loop over neural nets and build each section
193     for network, network_info in info_dict.iteritems():
194     print ""
195     print "Building %s" % network
196     output_file = open(os.path.join(
197     'note/observable_distributions/', network+'.tex'), 'w')
198    
199     section_label = '%s_input_descriptions' % network
200     output = "\label{sec:%s}\n" % section_label
201    
202     # Count variables
203     variables = network_info.keys()
204     # Sort nicely
205     variables.sort(put_eta_first)
206     n_vars = len(variables)
207    
208    
209     variable_table_entries=""
210    
211     # max group size of 5
212     for figure_index, plot_group in enumerate(groups_of(6, variables)):
213     letter_values = list("abcdefghijklmnopqrstuv")
214     current_height = 0
215     figures_list = ""
216     figure_label = "%s_%i" % (network, figure_index)
217     for plot_row in groups_of(2, plot_group):
218    
219     letter = letter_values.pop(0)
220     figures_list += PLOT_TEMPLATE.substitute(
221     x = 0.5, y = current_height,
222     letterx=0.5+10, lettery=current_height+60, letter=letter,
223     file_location = network_info[plot_row[0]])
224    
225     # Update variable table
226     variable_table_entries += VARIABLE_ITEM_TEMPLATE.substitute(
227     item=plot_row[0],
228     description=r"\ref{fig:%s}%s"% (figure_label, letter))
229    
230     letter = letter_values.pop(0)
231     figures_list += PLOT_TEMPLATE.substitute(
232     x = 65, y = current_height,
233     letterx=65+10, lettery=current_height+60, letter=letter,
234     file_location = network_info[plot_row[1]])
235    
236     # Update variable table
237     variable_table_entries += VARIABLE_ITEM_TEMPLATE.substitute(
238     item=plot_row[1],
239     description=r"\ref{fig:%s}%s"% (figure_label, letter))
240    
241     # go to next row
242     current_height += 65
243    
244     output += FIGURE_TEMPLATE.substitute(
245     width=130, height=current_height,
246     figures = figures_list,
247     decaymode = LATEX_NAME_MAPPING[network],
248     decayModeSection=section_label, label = figure_label)
249    
250     variable_index = VARIABLE_TEMPLATE.substitute(
251     items = variable_table_entries)
252    
253     output_file.write(variable_index)
254    
255     output_file.write(output)
256    
257    
258    
259