def apply_if_valid(name, trial, callback=None, raise_if_not=True):
    """Detect a parameter in trial and call a callback on it if provided

    Parameters
    ----------
    name: str
        Name of the param to look for
    trial: `orion.core.worker.trial.Trial`
        Instance of trial to investigate
    callback: None of callable
        Function to call with (trial, param) with a parameter is found.
        Defaults to None
    raise_if_not: bool
        raises RuntimeError if no parameter is found.
        Defaults to True.

    Returns
    -------
    bool
        False if parameter is not found and `raise_if_not is False`.
        True if parameter is found and callback is None.
        Else, output of callback(trial, item).

    """
    for param in trial.params:
        if param.name == name:
            return callback is None or callback(trial, param)

    if raise_if_not:
        raise RuntimeError("Provided trial does not have a compatible configuration. "
                           "A dimension named '%s' should be present.\n %s" %
                           (name, trial))

    return False