2 import common_tools
as tools
4 from BatchLog
import BatchLog
12 from ROOT
import TH1D, TH1F, TH2F, TH2D, TProfile2D
18 def __init__(self, controller, verbose=False, testing=False):
23 self.
plot_base =
"%s/figures/%s/v%s/"%(os.environ[
"NOVAANAVALID_GRID_TEST"],
25 self.controller.version)
27 self.
plot_base =
"%s/figures/%s/v%s/"%(os.environ[
"NOVAANAVALID_GRID"],
29 self.controller.version)
41 This will compose a view of each component of the controller 44 These plots are order by category (reco, true, mixed), with 45 one page provided for each dataset and one page provided 46 overlaying all datasets. 48 Components that are not complete are skipped 51 if self.
verbose:
print "view : component view has already been run" 59 print "view : Component type: %s, has %i instances"%(component_type, len(self.
component_types[component_type]))
70 if component_type
in self.plot_groups.keys():
71 if self.
verbose:
print "view : plot groups found: %s"%repr(self.
plot_groups[component_type])
82 plots[component.dataset] = {}
83 directories = hasattr(component,
"directories")
and component.directories
84 plot_dict, open_root_file = tools.openPlots(component.root_file, directories=directories)
85 if self.
verbose:
print "view : component %s, dataset: %s, opened %i histograms"%(component.name, component.dataset, len(plot_dict.keys()))
86 open_files.append(open_root_file)
88 component.plot_dir =
"%s/%s/v%s/%s"%(self.
plot_base, component.name, component.version, component.dataset)
89 tools.mkdir(component.plot_dir)
92 if "sensitivity" in component.name:
96 component.pot = self.
get_pot(plot_dict)
98 if self.
verbose:
print "view : PoT: %.2e"%component.pot
101 for plot_name
in plot_dict.keys():
104 plot_names.append(plot_name)
105 plots[component.dataset][plot_name] = plot_dict[plot_name]
107 if (tools.getHistogramTrueLevel(plot_name) !=
""):
continue 109 if self.
verbose:
print "view : plot name: %s"%(plot_name)
111 if plot_name.count(
"_vs_") > 1:
112 this_area_norm =
False 113 this_pot_norm =
False 115 this_area_norm = area_norm
116 this_pot_norm = component.pot
119 path = self.
draw_plots( [plot_dict[plot_name]], component.plot_dir, plot_name, [component], canvas)
121 self.
draw_plots( [plot_dict[plot_name]], component.plot_dir, plot_name, [component], canvas, normalisation=
"area")
123 self.
draw_plots( [plot_dict[plot_name]], component.plot_dir, plot_name, [component], canvas, normalisation=
"pot")
131 if self.
verbose:
print "view : - has standard truth components" 138 sub_plots.append(plot_dict[
"%s%s"%(plot_name,suffix)])
139 sub_comps.append(component)
140 fraction_of.append(0)
141 plot_name =
"%s-truth_components"%plot_name
142 plots[component.dataset][plot_name] = sub_plots
144 self.
draw_plots( sub_plots, component.plot_dir, plot_name, sub_comps, canvas)
146 self.
draw_plots( sub_plots, component.plot_dir, plot_name, sub_comps, canvas, normalisation=
"area", fraction_of=fraction_of)
148 self.
draw_plots( sub_plots, component.plot_dir, plot_name, sub_comps, canvas, normalisation=
"pot")
150 plot_names =
list(set(plot_names))
153 for plot_name
in plot_names:
156 for component
in component_group:
157 if (plot_name
not in plots[component.dataset].
keys()):
continue 159 components.append(component)
160 if len(components) < 2:
continue 162 to_show = [plots[c.dataset][plot_name]
for c
in components]
163 joined_name =
"_".join([c.dataset
for c
in components])
164 if len(joined_name) > 25: joined_name = joined_name[:25]
166 overlay_dir =
"%s/%s/v%s/%s"%(self.
plot_base, component.name, component.version, joined_name)
167 tools.mkdir(overlay_dir)
169 path = self.
draw_plots( to_show, overlay_dir, plot_name, [c
for c
in components], canvas, ratios=
True)
170 self.
draw_plots( to_show, overlay_dir, plot_name, [c
for c
in components], canvas, normalisation=
"area", ratios=
True)
172 self.
draw_plots( to_show, overlay_dir, plot_name, [c
for c
in components], canvas, normalisation=
"pot", ratios=
True)
183 for i,component
in enumerate(components):
184 component = copy.copy(component)
185 if (
"%s-truth_components"%plot_name
in plots[component.dataset].
keys()):
187 if self.
verbose:
print "view : plot: %s, component: %s has truth components"%(plot_name, component.dataset)
189 truth_plots = plots[component.dataset][
"%s-truth_components"%plot_name]
190 component.true_components =
True 191 for j,plot
in enumerate(truth_plots):
192 truth_to_show.append(plot)
193 fraction_of.append(i)
194 comps.append(component)
196 truth_to_show.append(plots[component.dataset][plot_name])
197 fraction_of.append(i)
198 comps.append(component)
202 plot_name =
"%s-truth_components"%(plot_name)
203 self.
draw_plots( truth_to_show, overlay_dir, plot_name, comps, canvas)
204 self.
draw_plots( truth_to_show, overlay_dir, plot_name, comps, canvas, normalisation=
"area", fraction_of=fraction_of)
206 self.
draw_plots( truth_to_show, overlay_dir, plot_name, comps, canvas, normalisation=
"pot")
209 for open_file
in open_files:
214 if "%s%s"%(name,suffix)
not in all_names:
return False 218 info = tools.getPlotStats(plots, area_norm=area_norm)
220 info.datasets = [c.dataset
for c
in components]
222 info.component_name = components[0].name
223 info.component_version = components[0].version
224 info.controller_name = self.controller.name
225 info.controller_version = self.controller.version
226 if len(components) ==1:
228 elif "_nd_" in components[0].dataset:
229 info.group =
"ND overlays" 231 info.group =
"FD overlays" 233 if components[0].pot
and pot_norm: info.normalisations.append((
"PoT",
"pot_norm_"))
237 plots, output_dir, plot_name, components, canvas, \
238 normalisation=
False, fraction_of=
False, \
240 ratio_ranges=[ (
"",[]), (
"zoom_",[0.81,1.19]) ], \
248 if (normalisation ==
"area"):
250 for i,plot
in enumerate(plots):
251 if (type(plot)
not in [TH1F, TH1D]):
return 255 area = base_area * (plot.GetSumOfWeights() / plots[fraction_of[i]].GetSumOfWeights())
256 except ZeroDivisionError:
262 draw_config.axis_labels =
"%s;Normalised Entries [%%]"%(draw_config.axis_labels.split(
";")[0])
263 plot_name =
"area_norm_%s"%(plot_name)
265 elif (normalisation ==
"pot"):
267 for i,plot
in enumerate(plots):
268 if (type(plot)
not in [TH1F, TH1D]):
return 270 area = plot.GetSumOfWeights() * (tools.STANDARD_POT / components[i].pot)
271 except ZeroDivisionError:
275 draw_config.axis_labels =
"%s;Entries / %.0e PoT"%(draw_config.axis_labels.split(
";")[0],tools.STANDARD_POT)
276 plot_name =
"pot_norm_%s"%(plot_name)
278 elif (normalisation):
279 print "view : Unknown normalisation: %s"%repr(normalisation)
282 name =
"%s/%s"%(output_dir,plot_name)
286 draw_config.axis_labels,
287 colours=draw_config.colours,
288 lines=draw_config.lines,
289 markers=draw_config.markers,
290 draw_options=draw_config.draw_options,
291 latex_label=latex_label,
295 log_name =
"%s/log_%s"%(output_dir,plot_name)
298 draw_config.axis_labels,
299 colours=draw_config.colours,
300 lines=draw_config.lines,
301 markers=draw_config.markers,
302 draw_options=draw_config.draw_options,
303 latex_label=latex_label,
307 if ratios
and (len(plots) > 1)
and (type(plots[0])
in [TH1F, TH1D, TH2D, TH2F, TProfile2D]):
309 if (normalisation ==
"area"): prefix =
"Area normalised ratio" 310 elif (normalisation ==
"pot"): prefix =
"PoT normalised ratio" 311 else: prefix =
"Ratio" 312 ratio_axis_labels =
"%s;%s to %s"%(draw_config.axis_labels.split(
";")[0], prefix, draw_config.labels[0])
314 for ratio_range
in ratio_ranges:
315 prefix = ratio_range[0]
316 if type(plots[0])
in [TH1F, TH1D]:
317 y_range = ratio_range[1]
321 z_range = ratio_range[1]
323 if (len(ratios) == 1)
and (type(ratios[0])
in [TH2D, TH2F, TProfile2D]):
324 draw_config.draw_options[0] =
"COLZ" 326 ratio_name =
"%s/ratio_%s%s"%(output_dir,prefix,plot_name)
328 draw_config.labels[1:],
332 colours=draw_config.colours[1:],
333 lines=draw_config.lines[1:],
334 markers=draw_config.markers[1:],
335 draw_options=draw_config.draw_options[1:],
336 latex_label=latex_label,
342 pot_name =
"meta-TotalPOT" 343 if pot_name
in plot_dict.keys():
344 return plot_dict[pot_name].GetBinContent(1)
349 if self.
verbose:
print "view : [%i]: dataset: %s"%(i_c, component.dataset)
357 if self.
verbose:
print "view : [%i]: dataset: %s"%(i_c, component.dataset)
365 self.complete_components.append(component)
369 root_files = tools.findRootOutput(component, testing=self.
testing)
370 if len(root_files) != 1:
371 print "view: component %s, has %i root files"%(component.name, len(root_files))
372 tools.findRootOutput(component, verbose=
True, testing=self.
testing)
375 component.root_file = root_files[0]
379 job_sub_id = tools.getJobID(component.lines)
380 logs =
BatchLog(tools.fetchLogs(job_sub_id, component.run_time))
382 if (logs.log_status() !=
"completed")
or (logs.err_status() !=
"no error"):
384 print "view : - log status: ",logs.log_status()
385 print "view : - err status: ",logs.err_status()
392 if (
not hasattr(component,
"sam_view")):
393 component.setup_sam_view()
395 sam_status = component.get_sam_status(self.
sam)
396 if (sam_status !=
"complete")
and (
"ended complete" not in sam_status):
397 component.sam_view.print_summary(self.
sam)
398 print "view : SAM status: ",sam_status
402 if self.
verbose:
print "view : Merge status: %s"%component.get_sam_merge_status()
403 if component.get_sam_merge_status() ==
"complete":
410 return a mapping of component types to components 414 for component
in self.controller.components:
415 if component.name
in [
"hello_world"]:
continue 416 if component.name
not in self.component_types.keys():
419 if hasattr(component,
"sam")
and component.sam
and (
not self.
sam):
420 self.
sam = samweb_client.SAMWebClient(
"nova")
423 self.component_type_keys.sort()
427 this looks at the current complete components and groups them 432 if len(nd_components): self.grouped_components.append(nd_components)
435 if len(fd_components): self.grouped_components.append(fd_components)
439 plots can have different truth levels and different reco levels, group them here 446 category = tools.getHistogramCategory(plot)
447 observable = tools.getHistogramObservable(plot)
448 plot_type =
"%s-%s"%(category,observable)
449 if (plot_type
not in self.
plot_types): self.plot_types.append(plot_type)
450 reco_level = tools.getHistogramRecoLevel(plot)
451 if (reco_level
not in self.
reco_levels): self.reco_levels.append(reco_level)
452 true_level = tools.getHistogramTrueLevel(plot)
453 if (true_level
not in self.
true_levels): self.true_levels.append(true_level)
458 print "view : Saving as: %s"%self.
file_name
def get_plot_information(self, plots, components, area_norm=False, pot_norm=True)
def component_status(self, component_type)
def component_view(self, canvas)
def __init__(self, controller, verbose=False, testing=False)
def draw_component_plots(self, canvas, component_type)
def find_plot_types(self, plots)
def check_root_files(self, component)
def check_sam_status(self, component)
def has_standard_truth_types(self, name, all_names)
def parseDrawingConfig(plot_name, components, verbose=False)
parse drawing configuration options from the name of a histogram and a list of components ...
def write(self, output_name)
def datasetToLabel(name)
convert the dataset name to a label
def check_log_status(self, component)
procfile open("FD_BRL_v0.txt")
def group_components(self)
def draw_plots(self, plots, output_dir, plot_name, components, canvas, normalisation=False, fraction_of=False, ratios=False, ratio_ranges=[("",[]), zoom_, log=True)
def find_complete_components(self, component_type)
def get_pot(self, plot_dict)
def recoLevelName(name)
get a reconstructed level
def find_component_types(self)