import torch 
import numpy as np

def compute_r68( pred, true):
    """
    Compute the energy resolution as r68. 

    The r68 is defined as the value of |dE|/E_true that contains 68% of the lowest values.

    Parameters:
    pred (torch.Tensor): The predicted values. These values are inverse log transformed within the function.
    true (torch.Tensor): The true values. These values are inverse log transformed within the function.

    Returns:
    float: The computed r68 value.
    """
    
    deltae =torch.abs((pred - true) / true) #dE/E_true
    deltae = deltae.detach().cpu().numpy()#convert to numpy
    r68 = np.percentile(deltae, 68.27) #compute r68
    
    return r68