MPF.multiHistDrawer module

This module tries to implement a TTree::Draw like functionality with the addition that multiple histograms can be filled at once.

Note

For most use cases it is better to use the functionality via HistProjector or ProcessProjector instead of directly invoking MultiHistDrawer

The code is rather experimental - it uses the TTree::MakeClass code generator and pastes in the nescessary additions to fill the required histograms. Everything from generating the code and executing it is done automatically.

Whatever, it runs fine for now. - So does a burning bus. [1]

Example:

d = MultiHistDrawer(path, treeName)
hist1 = d.addHist(varexp=var1, xmin=xmin1, xmax=xmax1, nbins=nbins1)
hist2 = d.addHist(varexp=var2, xmin=xmin2, xmax=xmax2, nbins=nbins2)
d.run()

# now hist1 and hist2 should be filled

It should be noted that there are no checks on the input performed. This makes code injection possible, so don’t pass arbitrary user input to addHist(). On the other hand it can also be used at your benefit:

d.addHist(varexp="""1);
line_10: printf("LOOK AROUND YOU ");
line_20: goto line_10;
//""", cut="1", weight="1")
[1]https://xkcd.com/1695/
class MPF.multiHistDrawer.MultiHistDrawer(path, treeName)[source]
addHist(cut, weight, **kwargs)[source]

Add a histogram to be filled later. Returns the histogram (still unfilled).

Parameters:
  • cut – selection expression
  • varexp – expression to be filled into the histogram, default “1”
  • weight – additional expression, to be multiplied with cut, default “1”
  • xmin – minimum value to be filled into the histogram, default 0.5
  • xmax – maximum value to be filled into the histogram, default 1.5
  • nbins – number of bins between xmin and xmax, default 1
  • binLowEdges – list of low edges for variable binning, the first value is the lower bound of the first bin, the last value the upper bound of the last bin. If given, xmin, xmax and nbins are ignored.
  • ymin – minimum y-value to be filled into the histogram, default 0.5
  • ymax – maximum y-value to be filled into the histogram, default 1.5
  • nbinsy – number of bins between ymin and ymax, default 1
  • yBinLowEdges – list of low edges for variable binning of the y-axis, the first value is the lower bound of the first bin, the last value the upper bound of the last bin. If given, ymin, ymax and nbinsy are ignored.
branchUsed(branchName)[source]

Determine wether a branch is used for any cut or varexp. This is not perfect - it might activate too many branches. But it shouldn’t miss any (i hope)

classdir = None

Directory to work in for the temporary classes. If None, the systems tmp directory is used

generate()[source]

Generate the c++ code

getOverallOR()[source]

Return an expression that evaluates if any of the used selections passed

get_2D_expr(varexp)[source]

Parse expressions like x:y for drawing 2D histograms, also enabling the possibility to have expressions containing “::”

run(compile=False)[source]

Finally generate and run the code which will fill the hists.

Parameters:compile – If True, use “++” for loading the code. Often the overhead for this is larger as the speed improvement.
skipCleanup = False

for debug purposes - set true to keep generated c files

MPF.multiHistDrawer.getHists(*args, **kwargs)[source]

Wrapper to get all hists for a like dict:

{
  "hist1" : {nbins=nbins1, xmin=xmin1, xmax=xmax1, varexp=var1, cut=cut1, weight=weight1},
  "hist2" : {nbins=nbins2, xmin=xmin2, xmax=xmax2, varexp=var2, cut=cut2, weight=weight2},
  ...
}

Returns a dict with histograms

MPF.multiHistDrawer.getHistsPaths(histConfDict, treeName, *paths, **kwargs)[source]

call getHists and merges the returned histsDicts for all given paths

MPF.multiHistDrawer.getYields(*args, **kwargs)[source]