Skip to content

Instantly share code, notes, and snippets.

@lmoneta
Last active October 4, 2022 16:06
Show Gist options
  • Save lmoneta/31912af47c65bb7549bdc7c765e75c02 to your computer and use it in GitHub Desktop.
Save lmoneta/31912af47c65bb7549bdc7c765e75c02 to your computer and use it in GitHub Desktop.

TMVA Pythonizations

  • TMVA::Factory constructor
  • TMVA::Factory::BookMethod
  • TMVA::DataLoader::PrepareTrainingAndTestTree
  • TMVA::CrossValidation constructor. See tutorial TMVA_CrossValidation.C
  • Pythonizations of GUI funcitons. TMVA GUI is composed of several functions implemented in the TMVAGUI library. See all available functions in tmva/tmvagui/inc/TMVA
  • Add converters in PYROOT numpy arrays to RTensor objects to convert RTensor from and to Numpy arrays

Hist Pythonizations

  • Add functions to create Histograms objects (TH1, TH2, TH3) from numpy arrays.

    • h1 = ROOT.TH1.FromNumpy(x, weights = None, name='h1', title='h1',nbins=100,xmin=0,xmax=10) where x is a numpy array of x values used to fill the histogram.
    • In case of weighted histogram the function should take the weights h1 = ROOT.TH1.FromNumpy(x, weights = w, name='h1', title='h1',nbins=100,xmin=0,xmax=10) where w is a numpy array of weights.
  • Add function to fill histogram from a numpy array (this is not really needed. we will not implement!)

    • x = np.random.normal(size=1000)
    • h1.Fill(x)
    • and in case of weights:
      • w = np.random.uniform(0,2,size=1000)
      • h.Fill(x,w)
  • Add function to retrieve Numpy arrays from histograms. For example after having created an histogram as above, one should be able to do:

    • y = h.GetContent(firstBin=1, lastBin=nbins, retW2=false) return the bin content and optionally the bin sum of weight square

      • the function should be implemented using TH1::GetArray() and TH1::GetSumw2(). In C++ the arrays contain underflow and overflow content, so they are of shape (nbinsX+2, nbinsY+2, nbinsZ+2). One can do the histogram slicing using numpy and return the corresponding array
    • err = h.GetErrors(firstBin=1, lastBin=nbins) to return the histogram errors in a Numpy array

    • x = h.GetBinEdges(axis=1) to return the bin histogram edges for the given axis (default x axis).

      • It should be implemented using TH1::GetXaxis()::GetXbins() for irregular binning or TH1::GetXaxis() get number of bins , minimum and maximum.
      • x = h.GetBinLowEdges() and h.GetBinUpEdges() and h.GetBinCenters() could be impelmented, but maybe in a later stage

TGraph Pythonizations

  • Add functions ro create TGraph's object from numpy. Here we might be able to use directly the TGraph constrcuctor

    • x = np.array[[1,2,3,4,5]]
    • y = np.array[[3,4,6,7,8]]
    • graph = ROOT.TGraph(x,y)
  • Similar constructors for

    • TGraphErrors: graph = TGraphErrors(x,y,ex,ey)
    • TgraphAsymErrors : graph = TGraphErrors(x,y,exlow,exhigh,eylow, eyhigh)
    • TGraph2D : graph = TGraph2D(x,y,z)
    • TGraph2DErrors: graph = TGraph2D(x,y,z,ex,ey,ez)
    • TGraph2DAsymErrors: graph = TGraph2D(x,y,z,exl,exh,eyl,eyh,ezl,ezh)
    • note some of the errors passed could be optional, as is done in teh C++ API
  • for TGraph we could have x = g.GetX() and y = g.GetY(), but also x,y = g.Get()

  • adding also errors in case of TGraphErrors, ex = g.GetErrorX() and ey = g.GetErrorY() and ex,ey = g.GetErrors()

  • for the asymmetric errors exl,exh = g.GetErrors()

Histogram slicing

  • slice histogram using Python syntax.
    • h1[1:10] is an histogram where one calls SetRange(1,10) and returns a new histogram. It requires a TH1 * TH1::Slice(int first, int last) function in C++ returning a new copy of the histogram that is missing
  • Implement UHI indexing as described here: https://uhi.readthedocs.io/en/latest/indexing.html#syntax
    • one examople is projections using operator[]
      • for example doing a ProjectionX from ybin1 to ybin2, h2.ProjectionX("_py",y1,y2) can be done in Python with: h1 = h2[:,y1:y2:sum]
      • rebinning can be done as h1[:rebin(2)] which is equivalent to h1.Rebin(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment