PyRDataFrame

pydiana.tools.pyRDataFrame.clean_colnames(colnames)[source]

Function (internal) that returns only the useful part of a variable name

Parameters:

(list) (colnames) --

Returns:

cnames2 (list): list of str, output colnames

pydiana.tools.pyRDataFrame.convert2pandas(df, colnames, sep_field='', eventtype2string=True)[source]

Function that checks if the inserted dataframe is a Pandas DataFrame and returns it. If not, one implemented option is that df is a RDataFrame: in this case this function uses the provided array colnames (considering only the not None elements in it) to build a Pandas DataFrame (needed in our analisys setup). For graph_tools, can be also needed to save the column from which there is the separation of plots, so along with the requested columns, also the sep_field column (if not exists return an error) will be saved in the output Pandas dataframe. Upgradable with other types of dataframe in the future. N.B.: this function automatically create a map frame_name --> diana_name; the inserted colnames list can contain be provided in frame_name, diana_name or even both format mixed inside the list.

Parameters:
  • df (inserted dataframe to be converted (if needed) in a Pandas DataFrame) --

  • (list) (colnames) --

  • (str) (sep_field) -- (only used if df is not a Pandas DataFrame)

  • (bool) (eventtype2string) -- using as blueprint the enumerator in QSampleInfo

Returns:

res_df: resulting Pandas DataFrame (equal to df if df is a Pandas DataFrame)

pydiana.tools.pyRDataFrame.eval_alias(alias, expr, rdataframe, redefine=False, verbose=False)[source]

Function for adding an alias to the dataframe.

Parameters:
  • alias (string with the name of the alias to use) --

  • expr (string with the expression used to define the alias) --

  • rdataframe (RDataFrame instance to be aliased) --

  • redefine (boolean for enabling redefining of previously existing aliases) --

  • verbose (boolean for enabling message printing) --

Returns:

returns RDataFrame with implemented aliases. If aliasing was not successful returns RDataFrame

pydiana.tools.pyRDataFrame.eval_aliases(rdataframe, aliases=None, redefine=False, verbose=False)[source]

Function for defining multiple aliases from dictionary.

Parameters:
  • rdataframe (RDataFrame instance to be aliased) --

  • aliases (dictionary of the aliases used in the notebook. The keys are the aliases and the values are the expression to alias (using the default c++ aliases is allowed)) --

  • redefine (boolean for enabling redefining of previously existing aliases) --

  • verbose (boolean for enabling message printing) --

Return type:

RDataFrame with implemented aliases. If no aliases are provided then returns same RDataFrame.

pydiana.tools.pyRDataFrame.extract_aliases(datachain)[source]

Function for extracting aliases directly from QChain instead of reading alias file. :param QChain object with data:

Return type:

alias dictionary with alias name as key and alias path as value

pydiana.tools.pyRDataFrame.findVar(expr, rdataframe)[source]

Function for finding variable in the rdataframe :param var: :type var: stirng with the variable to find :type rdataframe: :param rdataframe: :type rdataframe: RDataFrame in which to search

Return type:

list with the variables in the expression

pydiana.tools.pyRDataFrame.make_expr(string, rdataframe, returnval='None', init_classes=False, conv_spaces=True)[source]

Function made to convert a string in a usable expression for the rdataframe :type string: str :param string: :type string: expression to evaluate :type rdataframe: :param rdataframe: :type rdataframe: RDataFrame instance to be used to check column names :type returnval: :param returnval: :type returnval: modes of the return val, it can either return a None value (if set to 'None') or just the converted string (if set to 'best') :type init_classes: bool :param init_classes: :type init_classes: boolean for enabling the initialization of the classes used in the expression :param conv_spaces (bool): :type conv_spaces (bool): boolean for toggling on and of the conversion of the spaces in the expression

Return type:

string with the expression or None based on returnval

pydiana.tools.pyRDataFrame.name2var(diana_name, conv_operations=True, conv_spaces=True)[source]

Function that converts variables names from Diana ROOT files to acceptable variables for RDataFrames. Since "@", "." and other characters present in Diana ROOT files cannot be used in RDataFrames column names, they are redefined.

Parameters:

(str) (diana_name) --

Return type:

str

Returns:

frame_name (str): name of the same variable (column) in the RDataFrame

pydiana.tools.pyRDataFrame.open_rdataframe(datachain, local_aliases=None, redefine=False, verbose=False)[source]

Function that opens the RDataFrame starting from QChain

Parameters:
  • datachain (QChain with data) --

  • local_aliases (dictionary of the aliases used in the notebook. The keys are the aliases and the values are the expression to alias (using the default c++ aliases is allowed)) --

  • redefine (boolean for enabling redefining of previously existing aliases) --

  • verbose (boolean for enabling message printing) --

Returns:

aliasDF: Opened RDataFrame (with implemented aliases from Diana)

pydiana.tools.pyRDataFrame.read_varsRDataFrame(variables, datafile, cuts=None)[source]

Function for reading a tuple for a diana root file passing through an RDataFrame (retrocompatibility)

Parameters:
  • variables (array-like of strings containing the name of the variables to fetch. If dictionary, the values must be the names of the variables to search and the keys the name to be shown in the resulting dataframe) --

  • datafile (QChain instance of the file/files to read) --

  • cuts (String containing the cuts to select the wanted variables) --

Return type:

Pandas dataframe with all the requested variables, or None if no events pass the cuts.

pydiana.tools.pyRDataFrame.rfilter(rdataframe, cuts=None, name='MyCut', verbose=False)[source]

Function for evaluating a filtering expression on an rdataframe :type cuts: Optional[str] :param cuts: :type cuts: string with the cuts expression in root style :type rdataframe: :param rdataframe: :type rdataframe: RDataFrame to filter :param name (str): :type name (str): name of the cut using when referrency to it inside ROOT :param verbose (bool): :type verbose (bool): print information while applying cuts

Returns:

filtered rdataframe if successul otherwise returns same dataframe

pydiana.tools.pyRDataFrame.var2name(frame_name, conv_operations=True)[source]

Function that converts back the variables names used for RDataFrames in the real names from Diana ROOT files. Since "@", "." and other characters present in Diana ROOT files cannot be used in RDataFrames column names, they are redefined.

Parameters:

(str) (frame_name) --

Return type:

str

Returns:

diana_name (str): real name of the same variable from Diana ROOT files