OscarLoader

class OscarLoader.OscarLoader(OSCAR_FILE: str)[source]

OscarLoader is a class for loading and processing OSCAR format data files.

This class extends the BaseLoader and provides functionality to load, filter, and process data from files in the OSCAR format. It supports different versions of the OSCAR format and allows for event-based data loading with optional filtering.

Attributes:
PATH_OSCAR_str

The path to the OSCAR data file.

oscar_format_str

The format of the OSCAR data file.

optional_arguments_dict

A dictionary of optional arguments passed to the load method.

event_end_lines_list

A list to store the end lines of events in the OSCAR data file.

num_events_int

The number of events in the OSCAR data file.

num_output_per_event_numpy.ndarray

An array containing the number of particles per event in the OSCAR data file.

Methods

__init__(OSCAR_FILE)

Initializes the OscarLoader with the provided OSCAR file path.

load(**kwargs)

Loads the OSCAR data with optional event ranges and filters.

_get_num_skip_lines()

Calculates the number of initial lines to skip in the OSCAR file.

set_num_events()

Sets the number of events in the OSCAR data file.

set_oscar_format()

Determines and sets the format of the OSCAR data file.

oscar_format()

Returns the OSCAR format of the data file.

impact_parameter()

Returns the impact parameter of the events in the OSCAR data file.

event_end_lines()

Returns the event end lines in the OSCAR data file.

__get_num_read_lines()

Calculates the number of lines to read based on the specified events.

__apply_kwargs_filters(event, filters_dict)

Applies filters to the event data based on the provided filter dictionary.

set_particle_list(kwargs)

Sets the list of particles from the OSCAR data file.

set_num_output_per_event_and_event_footers()

Determines the number of output lines per event and the event footers in the OSCAR data file.

event_end_lines() List[str][source]

Returns the list of event end lines.

This method returns the list of strings that mark the end of events in the OSCAR data.

Returns:
List[str]

A list of strings that mark the end of events in the OSCAR data.

impact_parameter() List[float][source]

Returns the impact parameter of the events.

This method extracts the impact parameter of the collision from the last line of each event in the OSCAR data file.

Returns:
List[float]

The impact parameter of the collisions.

load(**kwargs: Dict[str, Any]) Tuple[List[List[Particle]], int, ndarray, List[str]][source]

Loads the OSCAR data from the specified file.

This method accepts optional arguments that specify which events to load and any filters to apply. The ‘events’ argument can be either a tuple specifying a range of events or an integer specifying a single event. If the ‘events’ argument is provided and is not a tuple or an integer, or if any of the event numbers are negative, a ValueError is raised.

Parameters:
**kwargsdict, optional

A dictionary of optional arguments. The following keys are recognized:

  • ‘events’: Either a tuple of two integers specifying the range of events to load, or a single integer specifying a single event to load.

  • ‘filters’: A list of filters to apply to the data.

Returns:
tuple

A tuple containing the following elements:

  • A list of particles loaded from the OSCAR data.

  • The number of events in the OSCAR data.

  • The number of particles in each event in the OSCAR data.

Raises:
ValueError

If an unrecognized keyword argument is used in the constructor, or if the ‘events’ argument is not a tuple or an integer, or if any of the event numbers are negative.

oscar_format() str | None[source]

Returns the OSCAR format string.

This method returns the OSCAR format string that specifies the format of the OSCAR data being loaded.

Returns:
Optional[str]

The OSCAR format string, or None if the format is not set.

set_num_events() None[source]

Sets the number of events in the OSCAR data file.

This method reads the file in binary mode to search for the last line. This approach avoids the need to loop through the entire file, which can be time-consuming for large files. It then checks if the last line starts with a ‘#’ and contains the word ‘event’. If it does, it sets the number of events to the integer value in the third position of the last line. If the last line does not meet these conditions, it raises a TypeError.

Parameters:
None
Returns:
None
Raises:
TypeError

If the last line of the file does not start with a ‘#’ and contain the word ‘event’.

set_num_output_per_event_and_event_footers() None[source]

Sets the number of output lines per event and the event footers in the OSCAR data file.

This method reads the OSCAR data file line by line and determines the number of output lines for each event and the event footers. The method behaves differently depending on the OSCAR format of the data file. If the format is ‘Oscar2013Extended_IC’ or ‘Oscar2013Extended_Photons’, it counts the number of lines between ‘in’ and ‘end’ lines. Otherwise, it counts the number of lines between ‘out’ and ‘end’ lines.

Parameters:
None
Returns:
None
Raises:
None
set_oscar_format() None[source]

Sets the number of events in the OSCAR data file.

This method reads the file in binary mode to search for the last line. This approach avoids the need to loop through the entire file, which can be time-consuming for large files. It then checks if the last line starts with a ‘#’ and contains the word ‘event’. If it does, it sets the number of events to the integer value in the third position of the last line. If the last line does not meet these conditions, it raises a TypeError.

Parameters:
None
Returns:
None
Raises:
TypeError

If the last line of the file does not start with a ‘#’ and contain the word ‘event’.

set_particle_list(kwargs: Dict[str, Any]) List[List[Particle]][source]

Sets the list of particles from the OSCAR data file.

This method reads the OSCAR data file line by line and creates a list of Particle objects. It also applies any filters specified in the ‘filters’ key of the kwargs dictionary. If the ‘events’ key is specified in the kwargs dictionary, it adjusts the number of events and the number of output lines per event accordingly.

Parameters:
kwargsdict

A dictionary of optional arguments. The following keys are recognized:

  • ‘events’: Either a tuple of two integers specifying the range of events to load, or a single integer specifying a single event to load.

  • ‘filters’: A list of filters to apply to the data.

Returns:
particle_listlist

A list of Particle objects loaded from the OSCAR data file.

Raises:
IndexError

If the number of events in the OSCAR file does not match the number of events specified by the comments in the OSCAR file, or if the index is out of range of the OSCAR file.

ValueError

If the first line of the event is not a comment line or does not contain “out”, or if a comment line is unexpectedly found.