Ejemplo n.º 1
0
def post_projection_processing_for_2d_correlation(
        hist: Hist,
        normalization_factor: float,
        title_label: str,
        jet_pt: analysis_objects.JetPtBin,
        track_pt: analysis_objects.TrackPtBin,
        rebin_factors: Optional[Tuple[int, int]] = None) -> None:
    """ Basic post processing tasks for a new 2D correlation observable.

    Args:
        hist: Histogram to be post processed.
        normalization_factor: Factor by which the hist should be scaled.
        title_label: Histogram title label.
        jet_pt: Jet pt bin.
        track_pt: Track pt bin.
        rebin_factors: (x rebin factor, y rebin factor). Both values must be specified (can set to 1 if you
            don't want to rebin a particular axis). Default: None.
    Returns:
        None. The histogram is modified in place.
    """
    # If we specify a rebin factor, then rebin.
    if rebin_factors is not None:
        hist.Rebin2D(*rebin_factors)

    # Scale
    hist.Scale(1.0 / normalization_factor)

    # Set title, axis labels
    jet_pt_bins_title = labels.jet_pt_range_string(jet_pt)
    track_pt_bins_title = labels.track_pt_range_string(track_pt)
    hist.SetTitle(
        rf"{title_label}\:\mathrm{{with}}\:{jet_pt_bins_title} \mathrm{{,}} {track_pt_bins_title}"
    )
    hist.GetXaxis().SetTitle(r"$\Delta\varphi$")
    hist.GetYaxis().SetTitle(r"$\Delta\eta$")
Ejemplo n.º 2
0
def compare_mixed_event_normalization_options(
        mixed_event: Hist, eta_limits: Tuple[float, float]
) -> Tuple[Hist,
           # Basic data
           np.ndarray, np.ndarray, np.ndarray, np.ndarray,
           # CWT
           np.ndarray, np.ndarray,
           # Moving Average
           float, float,
           # Smoothed gaussian
           np.ndarray, np.ndarray, float,
           # Linear fits
           float, float, float, float]:
    """ Compare mixed event normalization options.

    The large window over which the normalization is extracted seems to be important to avoid fluctatuions.

    Also allows for comparison of:
        - Continuous wave transform with width ~ pi
        - Smoothing data assuming the points are distributed as a gaussian with options of:
            - Max of smoothed function
            - Moving average over pi of smoothed function
        - Moving average over pi
        - Linear 1D fit
        - Linear 2D fit

    All of the above were also performed over a 2 bin rebin except for the gaussian smoothed function.

    Args:
        mixed_event: The 2D mixed event histogram.
        eta_limits: Eta limits of which the mixed event should be projection into 1D.
    Returns:
        A very complicated tuple containing all of the various compared options. See the code.
    """
    # Create projected histograms
    peak_finding_hist, peak_finding_hist_array = _peak_finding_objects_from_mixed_event(
        mixed_event=mixed_event, eta_limits=eta_limits)
    # Determine max via the moving average
    # This is what is implemented in the mixed event normalization, but in principle, this could change.
    # Since it's easy to calculate, we do it by hand again here.
    max_moving_avg = np.max(utils.moving_average(peak_finding_hist_array,
                                                 n=36))

    # Create rebinned hist
    # The rebinned hist may be less susceptible to noise, so it should be compared.
    # Only rebin the 2D in delta phi because otherwise the delta eta bins will not align with the limits
    # NOTE: By passing a new name, it will create a new rebinned histogram.
    mixed_event_rebin = mixed_event.Rebin2D(2, 1,
                                            f"{mixed_event.GetName()}Rebin")
    # Scale back down to account for the rebin.
    mixed_event_rebin.Scale(1. / 2.)
    peak_finding_hist_rebin = peak_finding_hist.Rebin(
        2,
        peak_finding_hist.GetName() + "Rebin")
    # Scale back down to account for the rebin.
    peak_finding_hist_rebin.Scale(1. / 2.)
    # Note that peak finding will only be performed on the 1D hist
    peak_finding_hist_array_rebin = histogram.Histogram1D.from_existing_hist(
        peak_finding_hist_rebin).y

    # Define points where the plots and functions can be evaluted
    lin_space = np.linspace(-0.5 * np.pi, 3. / 2 * np.pi,
                            len(peak_finding_hist_array))
    lin_space_rebin = np.linspace(-0.5 * np.pi, 3. / 2 * np.pi,
                                  len(peak_finding_hist_array_rebin))

    # Using CWT
    # See: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.find_peaks_cwt.html
    # and: https://stackoverflow.com/a/42285002
    peak_locations: np.ndarray = scipy.signal.find_peaks_cwt(
        peak_finding_hist_array, widths=np.arange(20, 50, .1))
    peak_locations_rebin: np.ndarray = scipy.signal.find_peaks_cwt(
        peak_finding_hist_array_rebin, widths=np.arange(10, 25, .05))
    logger.info(
        f"peak_locations: {peak_locations}, values: {peak_finding_hist_array[peak_locations]}"
    )

    # Using gaussian smoothing
    # See: https://stackoverflow.com/a/22291860
    f = scipy.interpolate.interp1d(lin_space, peak_finding_hist_array)
    # Resample for higher resolution
    lin_space_resample = np.linspace(-0.5 * np.pi, 3. / 2 * np.pi, 7200)
    f_resample = f(lin_space_resample)
    # Gaussian
    # std deviation is in x!
    window = scipy.signal.gaussian(1000, 300)
    smoothed_array = scipy.signal.convolve(f_resample,
                                           window / window.sum(),
                                           mode="same")
    #max_smoothed = np.amax(smoothed_array)
    #logger.debug("max_smoothed: {}".format(max_smoothed))
    # Moving average on smoothed curve
    smoothed_moving_avg = utils.moving_average(smoothed_array,
                                               n=int(len(smoothed_array) // 2))
    max_smoothed_moving_avg = np.max(smoothed_moving_avg)

    # Moving average with rebin
    moving_avg_rebin = utils.moving_average(peak_finding_hist_array_rebin,
                                            n=18)
    max_moving_avg_rebin = np.max(moving_avg_rebin)

    # Fit using TF1 over some range
    # Fit the deltaPhi away side
    fit1D = fitting.fit_1d_mixed_event_normalization(
        peak_finding_hist, [1. / 2. * np.pi, 3. / 2. * np.pi])
    max_linear_fit1D = fit1D.GetParameter(0)
    fit1D_rebin = fitting.fit_1d_mixed_event_normalization(
        peak_finding_hist_rebin, [1. / 2. * np.pi, 3. / 2. * np.pi])
    max_linear_fit1D_rebin = fit1D_rebin.GetParameter(0)
    fit2D = fitting.fit_2d_mixed_event_normalization(
        mixed_event, [1. / 2. * np.pi, 3. / 2. * np.pi], eta_limits)
    max_linear_fit2D = fit2D.GetParameter(0)
    fit2D_rebin = fitting.fit_2d_mixed_event_normalization(
        mixed_event_rebin, [1. / 2. * np.pi, 3. / 2. * np.pi], eta_limits)
    max_linear_fit2D_rebin = fit2D_rebin.GetParameter(0)

    logger.debug(
        f"linear1D: {max_linear_fit1D}, linear1D_rebin: {max_linear_fit1D_rebin}"
    )
    logger.debug(
        f"linear2D: {max_linear_fit2D}, linear2D_rebin: {max_linear_fit2D_rebin}"
    )

    return (
        peak_finding_hist,
        # Basic data
        lin_space,
        peak_finding_hist_array,
        lin_space_rebin,
        peak_finding_hist_array_rebin,
        # CWT
        peak_locations,
        peak_locations_rebin,
        # Moving Average
        max_moving_avg,
        max_moving_avg_rebin,
        # Smoothed gaussian
        lin_space_resample,
        smoothed_array,
        max_smoothed_moving_avg,
        # Linear fits
        max_linear_fit1D,
        max_linear_fit1D_rebin,
        max_linear_fit2D,
        max_linear_fit2D_rebin,
    )