def __init__(self, config_filename: str,
                 selected_analysis_options: params.SelectedAnalysisOptions,
                 manager_task_name: str, **kwargs: Any):
        self.config_filename = config_filename
        self.selected_analysis_options = selected_analysis_options
        self.task_name = manager_task_name

        # Retrieve YAML config for manager configuration
        # NOTE: We don't store the overridden selected_analysis_options because in principle they depend
        #       on the selected task. In practice, such options are unlikely to vary between the manager
        #       and the analysis tasks. However, the validation cannot handle the overridden options
        #       (because the leading hadron bias enum is converting into the object). So we just use
        #       the overridden option in formatting the output prefix (where it is required to determine
        #       the right path), and then passed the non-overridden values to the analysis objects.
        self.config, overridden_selected_analysis_options = analysis_config.read_config_using_selected_options(
            task_name=self.task_name,
            config_filename=self.config_filename,
            selected_analysis_options=self.selected_analysis_options)
        # Determine the formatting options needed for the output prefix
        formatting_options = analysis_config.determine_formatting_options(
            task_name=self.task_name,
            config=self.config,
            selected_analysis_options=overridden_selected_analysis_options)
        # Additional helper variables
        self.task_config = self.config[self.task_name]
        self.output_info = analysis_objects.PlottingOutputWrapper(
            # Format to ensure that the selected analysis options are filled in.
            output_prefix=self.config["outputPrefix"].format(
                **formatting_options),
            printing_extensions=self.config["printingExtensions"],
        )

        # Monitor the progress of the analysis.
        self._progress_manager = enlighten.get_manager()
Пример #2
0
def plot_RPF_regions(input_file: str, hist_name: str, output_prefix: str = ".", printing_extensions: Optional[List[str]] = None) -> None:
    """ Main entry point for stand-alone highlight plotting functionality.

    If this is being used as a library, call ``plot_RPF_fit_regions(...)`` directly instead.

    Args:
        input_file (str): Path to the input file.
        hist_name (str): Name of the histogram to be highlighted.
        output_prefix (str): Directory where the output file should be stored. Default: "."
        printing_extensions (list): Printing extensions to be used. Default: None, which corresponds
            to printing to ``.pdf``.
    Returns:
        None.
    """
    # Argument validation
    if printing_extensions is None:
        printing_extensions = [".pdf"]

    # Basic setup
    # Create logger
    logging.basicConfig(level=logging.DEBUG)
    # Quiet down the matplotlib logging
    logging.getLogger("matplotlib").setLevel(logging.INFO)

    # Retrieve hist
    with histogram.RootOpen(filename = input_file, mode = "READ") as f:
        hist = f.Get(hist_name)
        hist.SetDirectory(0)

    # Increase the size of the fonts, etc
    with sns.plotting_context(context = "notebook", font_scale = 1.5):
        # See the possible arguments to highlight_region_of_surface(...)
        # For example, to turn on the color overlay, it would be:
        #highlight_args = {"use_color_screen" : True}
        highlight_args: Dict[str, bool] = {}
        # Call plotting functions
        fig, ax = plot_RPF_fit_regions(
            histogram.get_array_from_hist2D(hist),
            highlight_regions = define_highlight_regions(),
            colormap = "ROOT_kBird", view_angle = (35, 225),
            **highlight_args,
        )

        # Modify axis labels
        # Set the distance from axis to label in pixels.
        # Having to set this by hand isn't ideal, but tight_layout doesn't work as well for 3D plots.
        ax.xaxis.labelpad = 12
        # Visually, delta eta looks closer
        ax.yaxis.labelpad = 15

        # If desired, add additional labeling here.
        # Probably want to use, for examxple, `ax.text2D(0.1, 0.9, "label text", transform = ax.transAxes)`
        # This would place the text in the upper left corner (it's like NDC)

        # Save and finish up
        # The figure will be saved at ``output_prefix/output_path.printing_extension``
        output_wrapper = analysis_objects.PlottingOutputWrapper(output_prefix = output_prefix, printing_extensions = printing_extensions)
        plot_base.save_plot(output_wrapper, fig, output_name = "highlightRPFRegions")
        plt.close(fig)
Пример #3
0
    def __init__(self,
                 generator: generator.Generator,
                 identifier: str,
                 jet_radius: float = 0.4,
                 output_prefix: str = "."):
        self.generator = generator
        self.identifier = identifier
        self.jet_radius = jet_radius

        # Output variables
        # They start out as lists, but will be converted to numpy arrays when finalized.
        self.jets: np.ndarray = []
        self.events: np.ndarray = []

        # Output info
        self.output_info = analysis_objects.PlottingOutputWrapper(
            output_prefix=output_prefix,
            printing_extensions=["pdf"],
        )
Пример #4
0
    def __init__(self, event_activity: params.EventActivity, *args: Any,
                 **kwargs: Any):
        # Setup base class
        super().__init__(*args, **kwargs)

        # Efficiency hists
        self.event_activity = event_activity
        self.efficiency_hists: List[np.ndarray] = []
        self.efficiency_sampling: List[int] = []

        # Output
        # Start with the output_prefix from the base class
        output_prefix = self.output_info.output_prefix.format(
            collision_energy_in_GeV=self.generator.sqrt_s)
        self.output_info = analysis_objects.PlottingOutputWrapper(
            output_prefix=os.path.join(output_prefix, str(self.identifier)),
            printing_extensions=["pdf"],
        )
        if not os.path.exists(self.output_info.output_prefix):
            os.makedirs(self.output_info.output_prefix)
def characterize_tracking_efficiency(period: str, system: params.CollisionSystem) -> None:
    """ Characterize the tracking efficiency.

    Args:
        period: Period for calculating the efficiencies.
        system: Collision system.
    Returns:
        Calculated efficiencies for the same range as the JetHUtils implementation.
    """
    # Setup
    logger.warning(f"Plotting efficiencies for {period}, system {system}")
    efficiency_function, efficiency_period, PublicUtils = setup_AliPhysics(period)
    # Setting up evaluation ranges.
    pt_values, eta_values, n_cent_bins, centrality_ranges = generate_parameters(system)
    # Plotting output location
    output_info = analysis_objects.PlottingOutputWrapper(
        output_prefix = f"output/{system}/{period}/trackingEfficiency",
        printing_extensions = ["pdf"],
    )

    # Calculate the efficiency.
    efficiencies = calculate_efficiencies(
        n_cent_bins = n_cent_bins, pt_values = pt_values, eta_values = eta_values,
        efficiency_period = efficiency_period, efficiency_function = efficiency_function,
    )

    # Calculate and check efficiency properties.
    result = efficiency_properties(
        n_cent_bins = n_cent_bins, efficiencies = efficiencies, PublicUtils = PublicUtils
    )
    if not result:
        raise RuntimeError("Failed to calculate all efficiency properties. Check the logs!")

    # Comparison to previous task
    if period in ["LHC11a", "LHC11h"]:
        try:
            comparison_efficiencies = jetH_task_efficiency_for_comparison(period, system, pt_values, eta_values)
            np.testing.assert_allclose(efficiencies, comparison_efficiencies)
            logger.info("Efficiencies agree!")
        except AttributeError as e:
            logger.warning(f"{e.args[0]}. Skipping!")
    else:
        logger.info("Skipping efficiencies comparison because no other implementation exists.")

    # Done checking the efficiency parametrization, so now plot it.
    for centrality_bin in range(n_cent_bins):
        plot_tracking_efficiency_parametrization(
            efficiencies[centrality_bin], centrality_ranges[centrality_bin],
            period, system, output_info
        )

    # Next, compare to the actual efficiency data.
    # We only have the LHC15o data easily available.
    if period == "LHC15o":
        # First, retrieve efficiency data
        efficiency_data_2D, efficiency_data_1D_pt, efficiency_data_1D_eta = \
            retrieve_efficiency_data(n_cent_bins, centrality_ranges)

        # Calculate residuals.
        logger.info("Calculating and plotting residuals")
        # Setup
        residuals: np.ndarray = None
        for centrality_bin, centrality_range in centrality_ranges.items():
            # Finish the setup
            if residuals is None:
                residuals = np.zeros(
                    shape = (n_cent_bins, efficiency_data_2D[centrality_bin].GetXaxis().GetNbins(),
                             efficiency_data_2D[centrality_bin].GetYaxis().GetNbins())
                )
            logger.debug(f"residuals shape: {residuals.shape}")

            # Calculate the residuals.
            residuals[centrality_bin], pts, etas, = calculate_residual_2D(
                efficiency_data_2D[centrality_bin], efficiency_function, efficiency_period, centrality_bin
            )

            # Plot the residuals.
            plot_residual(residuals[centrality_bin], pts, etas, period, centrality_bin, centrality_ranges, output_info)

            # Plot the 2D efficiency data
            plot_2D_efficiency_data(efficiency_data_2D[centrality_bin],
                                    centrality_ranges[centrality_bin], output_info)

            # 1D efficiency comparison
            # NOTE: Since we are using the 2D efficiency parametrization, the 1D fits aren't going to be quite right.
            #       This is basically because we can't integrate an efficiency (it's not well defined).
            #       It will be fairly close, but not exactly right.
            plot_1D_pt_efficiency(efficiency_data_1D_pt[centrality_bin],
                                  PublicUtils, efficiency_period,
                                  centrality_bin, centrality_ranges[centrality_bin],
                                  output_info)
            plot_1D_eta_efficiency(efficiency_data_1D_eta[centrality_bin],
                                   PublicUtils, efficiency_period,
                                   centrality_bin, centrality_ranges[centrality_bin],
                                   output_info)