basf2_helper package#

class b2luigi.basf2_helper.targets.ROOTLocalTarget(*args, **kwargs)[source]#

Bases: LocalTarget

class ROOTLocalTarget[source]#

Bases: object

A custom target class that extends b2luigi.LocalTarget to handle ROOT files.

This class overrides the exists method to not only check for the existence of the file but also verify that the ROOT file contains at least one key.

exists()[source]#

Checks if the target file exists and contains at least one key.

This method first verifies the existence of the file using the parent class’s exists method. If the file exists, it opens the file using ROOT’s TFile, retrieves the list of keys, and checks if the list contains any entries.

Returns:

True if the file exists and contains at least one key, False otherwise.

Return type:

bool

class b2luigi.basf2_helper.tasks.Basf2Task(*args, **kwargs)[source]#

Bases: DispatchableTask

class Basf2Task[source]#

Bases: object

A custom task class for handling basf2-related tasks in the b2luigi framework. This class extends b2luigi.DispatchableTask and provides additional functionality for managing output file targets and serialized parameters.

git_hash#

A parameter representing the basf2 git hash. It is set to the current basf2 git hash (see: get_basf2_git_hash) by default and marked as non-significant to avoid affecting the task’s unique ID.

Type:

b2luigi.Parameter

git_hash = <luigi.parameter.Parameter object>#
get_output_file_target(*args, **kwargs)[source]#

Determines the appropriate output file target based on the file extension.

If the output file has a “.root” extension, it returns a b2luigi.basf2_helper.ROOTLocalTarget for the specified file. Otherwise, it delegates to the superclass implementation of b2luigi.Task.get_output_file_target().

Parameters:
  • *args – Positional arguments passed to get_output_file_name and the superclass method.

  • **kwargs – Keyword arguments passed to get_output_file_name and the superclass method.

Returns:

A b2luigi.basf2_helper.ROOTLocalTarget if the file extension is “.root”, otherwise

the result of the superclass’s get_output_file_target method.

Return type:

Target

get_serialized_parameters()[source]#

Retrieve the serialized parameters of the current task. (see get_serialized_parameters)

Returns:

A dictionary containing the serialized parameters of the task.

Return type:

dict

class b2luigi.basf2_helper.tasks.Basf2PathTask(*args, **kwargs)[source]#

Bases: Basf2Task

A task for running a basf2 processing path within the b2luigi framework.

In contrast to the normal (b2)luigi tasks, the execution logic of a Basf2PathTask is not defined in a run method but in create_path. The create_path method needs to return the basf2 path that is created in the steering file. Furthermore, the Progress module is automatically added and print(b2.statistics) is called after the path is processed.

Warning

Due to technical reasons, the path needs to be created within the create_path method. The path can be used in further objects, however, it is not possible for it to originate from an outer scope.

num_processes = <luigi.parameter.IntParameter object>#
max_event = <luigi.parameter.IntParameter object>#
create_path()[source]#
process()[source]#

Executes the processing task using the basf2 framework.

It sets the number of processes for basf2 if self.num_processes is specified. Then, it creates a processing path, adds the basf2.Progress module to the path, and prints the path configuration.

Finally, it processes the path with the specified maximum number of events (self.max_event). If self.max_event is not set, it defaults to 0 (process all events). After processing, it prints the basf2 statistics.

Raises:

ImportError – If the basf2 module cannot be found.

class b2luigi.basf2_helper.tasks.SimplifiedOutputBasf2Task(*args, **kwargs)[source]#

Bases: Basf2PathTask

A task that simplifies the handling of output files in a basf2 processing path.

This class is intended to be subclassed (see Basf2PathTask), and the create_path method must be implemented to define the basf2 processing path. The output method identifies and collects output file targets from the modules in the path.

create_path()[source]#
output()[source]#

Generates the output targets for the task by inspecting the modules in the created path.

Returns:

A list of ROOTLocalTarget objects corresponding to the output file names

specified in the “RootOutput” modules of the path.

Return type:

list

class b2luigi.basf2_helper.tasks.MergerTask(*args, **kwargs)[source]#

Bases: Basf2Task

A task class for merging input files using a specified command.

cmd = []#
output()[source]#

Generates the output for the task by iterating over input file names and applying filters based on the keys attribute.

Yields:

The result of b2luigi.Task.add_to_output() for each key that passes the filtering conditions.

Filtering Conditions:
  • If the task has a keys attribute, only keys present in self.keys are processed.

process()[source]#

Processes input files and generates output files by executing a command.

  1. Creates necessary output directories using create_output_dirs.

  2. Iterates over the input file names grouped by keys.

  3. Skips processing for keys not specified in self.keys (if self.keys exists).

  4. Constructs a command by appending the output file name and input file list to self.cmd.

  5. Executes the constructed command using subprocess.check_call.

class b2luigi.basf2_helper.tasks.HaddTask(*args, **kwargs)[source]#

Bases: MergerTask

HaddTask is a subclass of MergerTask that represents a task for merging ROOT files using the hadd command-line tool.

cmd = ['hadd', '-f']#
class b2luigi.basf2_helper.tasks.Basf2FileMergeTask(*args, **kwargs)[source]#

Bases: MergerTask

Basf2FileMergeTask is a subclass of MergerTask that represents a task for merging basf2 ROOT files using the b2file-merge command-line tool.

cmd = ['b2file-merge', '-f']#
class b2luigi.basf2_helper.tasks.Basf2nTupleMergeTask(*args, **kwargs)[source]#

Bases: MergerTask

property cmd#

Command to use to merge basf2 tuple files.

b2luigi.basf2_helper.utils.get_basf2_git_hash()[source]#

Retrieve the version or git hash of the basf2 release being used.

This function determines the version of the basf2 framework in use. If the environment variable BELLE2_RELEASE is set to "head" or is not defined, it attempts to import the basf2.version module to retrieve the version information. If the import fails, a warning is issued, and the version is set to "not_set".

Returns:

The basf2 release name, its version hash, or “not_set” if basf2 cannot be imported or no release is configured.

Return type:

str

Warning

ImportWarning: Raised if the basf2.version module cannot be imported.