11"""Small module containing helper functions to set the metadata on objects
12created for the validation correctly """
15from typing
import Optional, Union, List, Tuple
20from ROOT
import Belle2
23def file_description_set(
24 rootfile: Union[ROOT.TFile, str, pathlib.PurePath], description: str
27 Add file description validation metadata to a ROOT file.
30 rootfile (ROOT.TFile, str or pathlib.PurePath): Name of the root file
31 to open or an already open TFile instance
32 description (str): Common description/information of/about all plots
33 in this ROOT file (will be displayed above the plots)
39 if not isinstance(rootfile, ROOT.TFile):
40 if isinstance(rootfile, pathlib.PurePath):
41 rootfile = str(rootfile)
42 rootfile = ROOT.TFile.Open(rootfile,
"UPDATE")
44 if not rootfile.IsOpen()
or not rootfile.IsWritable():
46 f
"ROOT file {rootfile.GetName()} is not open for writing"
51 directory_guard = ROOT.TDirectory.TContext(rootfile)
52 desc = ROOT.TNamed(
"Description", description)
58def validation_metadata_set(
64 xlabel: Optional[str] =
None,
65 ylabel: Optional[str] =
None,
69 Set the validation metadata for a given object by setting the necessary
70 values. This function can be used on any object supported by the
71 Validation (histograms, profiles, ntuples)
74 obj: Instance of the object which should get the metadata
75 title (str): Title to use for the object
76 contact (str): Contact person, usually in the form "Name <email>"
77 description (str): Text description what can be seen in the plot
78 check (str): Text description what to look for in the validation for
79 shifters to easily see if the distribution looks ok
80 xlabel (str): If given try to set this as the label for the x axis
81 ylabel (str): If given try to set this as the label for the y axis
82 metaoptions (str): Metaoptions (additional options to influence the
83 comparison between revisions, styling of the plot, etc.)
87 Different ways to specify LaTeX for different arguments:
88 see `create_validation_histograms`
93 obj.SetAlias(
"Contact", contact)
94 obj.SetAlias(
"Description", description)
95 obj.SetAlias(
"Check", check)
96 obj.SetAlias(
"MetaOptions", metaoptions)
97 except AttributeError:
101 function_list = obj.GetListOfFunctions()
102 function_list.Add(ROOT.TNamed(
"Contact", contact))
103 function_list.Add(ROOT.TNamed(
"Description", description))
104 function_list.Add(ROOT.TNamed(
"Check", check))
105 function_list.Add(ROOT.TNamed(
"MetaOptions", metaoptions))
106 except AttributeError:
110 if xlabel
is not None:
112 obj.GetXaxis().SetTitle(xlabel)
113 except AttributeError:
116 if ylabel
is not None:
118 obj.GetYaxis().SetTitle(ylabel)
119 except AttributeError:
124def validation_metadata_update(
125 rootfile: Union[str, ROOT.TFile, pathlib.PurePath], name: str, *args, **argk
128 This is a convenience helper for `validation_metadata_set` in case the
129 objects have already been saved in a ROOT file before: It will open the
130 file (or use an existing TFile), extract the object, add the metadata and
131 save the new version to the file
134 rootfile (str, ROOT.TFile or pathlib.PurePath): Name of the root file
135 to open or an already open TFile instance
136 name (str): Name of the object in the file
137 title (str): Title to use for the object
138 contact (str): Contact person, usually in the form "Name <email>"
139 description (str): Text description what can be seen in the plot
140 check (str): Text description what to look for in the validation for
141 shifters to easily see if the distribution looks ok
142 xlabel (str): If given try to set this as the label for the x axis
143 ylabel (str): If given try to set this as the label for the y axis
144 metaoptions (str): Metaoptions (additional options to influence the
145 comparison between revisions, styling of the plot, etc.)
149 Different ways to specify LaTeX for different arguments:
150 see `create_validation_histograms`
154 if not isinstance(rootfile, ROOT.TFile):
155 if isinstance(rootfile, pathlib.PurePath):
156 rootfile = str(rootfile)
157 rootfile = ROOT.TFile.Open(rootfile,
"UPDATE")
159 if not rootfile.IsOpen()
or not rootfile.IsWritable():
161 f
"ROOT file {rootfile.GetName()} is not open for writing"
163 obj = rootfile.Get(name)
166 subdir = rootfile.GetDirectory(args[0])
168 obj = subdir.Get(name)
171 validation_metadata_set(obj, *(args[1:]), **argk)
172 subdir.WriteObject(obj, name,
"Overwrite")
175 f
"Cannot find object named {name} in {rootfile.GetName()}"
178 validation_metadata_set(obj, *args, **argk)
182 directory_guard = ROOT.TDirectory.TContext(rootfile)
183 obj.Write(
"", ROOT.TObject.kOverwrite)
190 Simple module to set the valdiation metadata for a given list of objects
191 automatically at the end of event processing
193 Just add this module **before** any
194 VariablesToNtuple/VariablesToHistogram modules and it will set the
195 correct validation metadata at the end of processing
198 The module needs to be before the modules creating the objects
199 as terminate() functions are executed in reverse order from last to
200 first module. If this module is after the creation modules the metadata
201 might not be set correctly
206 variables: List[Tuple[str]],
207 rootfile: Union[str, pathlib.PurePath],
211 Initialize ValidationMetadataSetter
214 variables (list(tuple(str))): List of objects to set the metadata
215 for. Each entry should be the name of an object followed by the
216 metadata values which will be forwarded to
217 `validation_metadata_set`:
218 ``(name, title, contact, description, check, xlabel, ylabel,
220 where ``xlabel``, ``ylabel`` and ``metaoptions`` are optional
221 rootfile (str or pathlib.PurePath): The name of the ROOT file where
222 the objects can be found
223 description (str): Common description/information of/about all plots
224 in this ROOT file (will be displayed above the plots)
229 if isinstance(rootfile, pathlib.PurePath):
230 rootfile = str(rootfile)
241 """Make sure we keep the file open"""
247 """And update the metadata at the end"""
250 validation_metadata_update(self.
_tfile, name, *metadata)
256def create_validation_histograms(
258 rootfile: Union[str, pathlib.PurePath],
260 variables_1d: Optional[List[Tuple]] =
None,
261 variables_2d: Optional[List[Tuple]] =
None,
265 Create histograms for all the variables and also label them to be useful
266 in validation plots in one go. This is similar to the
267 `modularAnalysis.variablesToHistogram` function but also sets the
268 metadata correctly to be used by the validation
271 path (basf2.Path): Path where to put the modules
272 rootfile (str or pathlib.PurePath): Name of the output root file
273 particlelist (str): Name of the particle list, can be empty for event
275 variables_1d: List of 1D histogram definitions of the form
276 ``var, bins, min, max, title, contact, description, check_for
277 [, xlabel [, ylabel [, metaoptions]]]``
278 variables_2d: List of 2D histogram definitions of the form
279 ``var1, bins1, min1, max1, var2, bins2, min2, max2, title, contact,
280 description, check_for [, xlabel [, ylabel [, metaoptions]]]``
281 description: Common description/information of/about all plots in this
282 ROOT file (will be displayed above the plots)
286 Sadly, there are two different ways to specify latex formulas.
288 1. The ROOT-style using ``#``:
289 ``"Einstein-Pythagoras a^{2} + b^{2} = #frac{E}{m}"``
291 This style should be used for histogram title and labels, that is the
292 ``title``, ``xlabel`` and ``ylabel`` arguments
294 2. The normal Latex style (with escaped backslashes or raw
296 ``"Einstein-Pythagoras $a^2 + b^2 = \\\\frac{E}{m}$"``.
298 This style should be used for all other fields like ``description``,
301 You can use the normal Latex style also for histogram title and descriptions
302 but the PDF version of the plots will still be buggy and not show all
305 if isinstance(rootfile, pathlib.PurePath):
306 rootfile = str(rootfile)
310 if variables_1d
is not None:
311 for var, nbins, vmin, vmax, *data
in variables_1d:
312 histograms_1d.append((var, nbins, vmin, vmax))
313 metadata.append([var] + data)
315 if variables_2d
is not None:
316 for row
in variables_2d:
319 histograms_2d.append(row[:8])
320 metadata.append([var1 + var2] + list(row[8:]))
327 "VariablesToHistogram",
328 particleList=particlelist,
329 variables=histograms_1d,
330 variables_2d=histograms_2d,
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
static RootFileCreationManager & getInstance()
Interface for the FileManager.