With Dataset(*dataset_files) as dataset, canvas('canvas', 'plot. This defines the underlying metric, or ground distance, by giving the pairwise distances between the histogram bins. distancematrix (np.ndarray): A 2D array of np.float64, of size at least N × N. secondhistogram (np.ndarray): A 1D array of np.float64 of length N. finally: here to avoid generating a file if the canvas was not properly drawn. firsthistogram (np.ndarray): A 1D array of type np.float64 of length N.
Either by writing a class like you do (but checking the presence of an exception in the _exit_ method before drawing) or by using the decorator: canvas(name, filename, idunno, width, height):Ĭanvas = ROOT.TCanvas(name, idunno, width, height) The choice of KeyError as a base is a bit arbitrary, but I feel it fits nicely.Ī last thing, if you intend to build a lot of canvas to draw on, you may also be interested in wrapping that in a context manager.
So I’d rather write: class HistogramNotFoundError(KeyError): Non-error exceptions that are used for non-local flow control or other forms of signaling need no special suffix. Aim to answer the question "What went wrong?" programmatically, rather than only stating that "A problem occurred" (see PEP 3151 for an example of this lesson being learned for the builtin exception hierarchy)Ĭlass naming conventions apply here, although you should add the suffix "Error" to your exception classes if the exception is an error.
Direct inheritance from BaseException is reserved for exceptions where catching them is almost always the wrong thing to do.ĭesign exception hierarchies based on the distinctions that code catching the exceptions is likely to need, rather than the locations where the exceptions are raised. Why raise a generic purpose RuntimeError? If anyone wants to use your code and handle failures, they may catch more than it should.ĭerive exceptions from Exception rather than BaseException. Raise RuntimeError('Unable to retrieve histogram named '.format(name, self.filename))
# The TFile::Get() method returns a pointer to an object stored in a ROOT file. """Return the histogram identified by name from the file. Self.file = (self.filename, 'read')ĭef _exit_(self, exception_type, exception_value, traceback): Since a ROOT file is the fundamental unit of our datasets, I wrote a simple context manager to facilitate the common task of opening a ROOT file, retrieving some histograms, and then closing the file. We use the C++ framework ROOT to generate and store histograms and I am writing my code in Python to take advantage of its Python bindings ( PyROOT). My daily workflow largely consists of producing, styling, and circulating plots from a dataset to my advisor and collaborators.