scikit-hep / uproot5

ROOT I/O in pure Python and NumPy.
https://uproot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
239 stars 76 forks source link

feat: implement TGraph-writing #1256

Closed Pepesob closed 4 months ago

Pepesob commented 4 months ago

Implemented function as_TGraph() that gives to user possibilty to save DataFrame (or any dict like object) as TGraph. Example of usage:

import uproot
f = uproot.recreate("example.root")
x = np.array([-50,0,1, 2, 3, 4, 5, 17, 100,110], dtype='float64')
y = np.array([-50,0,1, 2, 3, 4, 5, 10, 100,120], dtype='float64')
d = {'x': x, 'y': y}
df = pd.DataFrame(d)
f["mytgraph"] = uproot.as_TGraph(df)
f.close()

List of arguments:


- df (DataFrame or and dict like object): DataFrame object with column names as follows:
     x (float): x values of TGraph.
     y (float): y values of TGraph.
     x_errors (float or left unspecified): Symethrical error values for corresponding x value
     y_errors (float or left unspecified): Symethrical error values for corresponding y value
     x_errors_low (float or left unspecified): Asymmetrical lower error values for corresponding x value
     x_errors_high (float or left unspecified): Asymmetrical upper error values for corresponding x value
     y_errors_low (float or left unspecified): Asymmetrical lower error values for corresponding y value
     y_errors_high (float or left unspecified): Asymmetrical upper error values for corresponding y value
     (other column names will be ignored!)
- title (str): Title of the histogram.
- xAxisLabel (str): Label of the X axis.
- yAxisLabel (str): Label of the Y axis.
- minY (None or float): Minimum value on the Y axis to be shown, if set to None then minY=min(y)
- maxY (None or float): Maximum value on the Y axis to be shown, if set to None then maxY=max(y)
- lineColor (int): Line color. (https://root.cern.ch/doc/master/classTAttLine.html)
- lineStyle (int): Line style.
- lineWidth (int): Line width.
- markerColor (int): Marker color. (https://root.cern.ch/doc/master/classTAttMarker.html)
- markerStyle (int): Marker style.
- markerSize (float): Marker size.

How does it work:

  Converts arguments into TGraph, TGraphErrors or TGraphAsymmErros based on the given arguments.
  When all errors are unspecified, detected object is TGraph.
  When x_errors, y_errors are specified, detected object is TGraphErrors.
  When x_errors_low, x_errors_high, y_errors_low, y_errors_high are specified, detected object is TGraphAsymmErrors.
  Note that both x_errors, y_errors need to be specified or set to None.
  The same rule applies to x_errors_low, x_errors_high, y_errors_low, y_errors_high.
  Also can't specify x_errors, y_errors and x_errors_low, x_errors_high, y_errors_low, y_errors_high at the same time.
  All rules are designed to remove any ambiguity.
grzanka commented 4 months ago

This is a nice design ! Can it be extended (maybe in future PR) to handle TMultiGraph as well ? I would like to save plots with different data series. Each serie would be separate collection of x,y pairs. Each serie may have different number of points.

jpivarski commented 4 months ago

@all-contributors please add @Pepesob for code

allcontributors[bot] commented 4 months ago

@jpivarski

I've put up a pull request to add @Pepesob! :tada: