Beispiel #1
0
def make_dirs(dir_to_make):
    """
    Helper function to checking and making a directory
    """
    dir_to_make = expanduser(dir_to_make)
    if not exists(dir_to_make):
        get_logger().info("Make directory %s", dir_to_make)
        makedirs(dir_to_make)
Beispiel #2
0
def parse_yaml(filepath):
    """
    Parse a YAML file and return dictionary
    Args:
        filepath: Path to the YAML file to be parsed.
    """
    if not os.path.isfile(filepath):
        get_logger().critical("YAML file %s does not exist.", filepath)
    with open(filepath) as f:
        return yaml.safe_load(f)
Beispiel #3
0
def parallelizer(function, argument_list, maxperchunk, max_n_procs=2):
    """
    A centralized version for quickly parallelizing a function.
    """
    get_logger().info("Parallelizing function %s", function.__name__)
    chunks = [argument_list[x:x+maxperchunk] \
              for x in range(0, len(argument_list), maxperchunk)]
    for chunk in chunks:
        get_logger().info("Processing new chunck size = %i", maxperchunk)
        pool = mp.Pool(max_n_procs)
        _ = [
            pool.apply_async(function, args=chunk[i])
            for i in range(len(chunk))
        ]
        pool.close()
        pool.join()
Beispiel #4
0
    def __init__(self, config):

        self.logger = get_logger()

        self.config = config
        self.data = {}

        self.read()
    def __init__(self, dimensions):
        
        self.logger = get_logger()

        self.coord_columns = range(dimensions + 1)
        # TODO Add errors
        #self.error_columns = range(dimensions + 1, 3 * (dimensions + 1))
        self.dimensions = dimensions

        self.df = pd.DataFrame(columns=self.coord_columns)
Beispiel #6
0
    def __init__(self, config):
        self.data_manager = DataManager(config)
        self.config = config

        self.logger = get_logger()

        # TODO Put defaults in a dictionary
        self.default_font_size = 50
        self.default_figure_width = 40
        self.default_figure_height = 30
        self.default_dpi = 100

        self.default_x_axis_label = "x_axis"
        self.default_y_axis_label = "y_axis"
    def __init__(self, config: dict):

        self.logger = get_logger()

        # Static config variables
        self.delim = "::"
        self.this_flag = "THIS"
        self.var_flag = "VAR"
        self.sources_flag = "SOURCES"

        # The raw config dictionary
        self.config_raw = parse_yaml(config)

        # The digested configuration
        self.variables = None
        self.sources = None
        self.plots = None

        self.check_config()
        self.digest()
def digest_root_source(**kwargs):
    # NOTE TODO Can for now only extract TH1 and TH2

    logger = get_logger()

    def digest_1d(histo):
        to_be_added = []
        axis = histo.GetXaxis()

        get_coord = None

        if axis.IsAlphanumeric():
            get_coord = axis.GetBinLabel
        else:
            get_coord = axis.getBinCenter

        for ibin in range(1, axis.GetNbins() + 1):
            coord = get_coord(ibin)
            if coord == "":
                continue
            to_be_added.append(((coord,), histo.GetBinContent(ibin)))

        return to_be_added

    def digest_2d(histo):
        to_be_added = []
        x_axis = histo.GetXaxis()
        y_axis = histo.GetYaxis()

        get_coord_x = None
        get_coord_y = None

        if x_axis.IsAlphanumeric():
            get_coord_x = x_axis.GetBinLabel
        else:
            get_coord_x = x_axis.getBinCenter
        if y_axis.IsAlphanumeric():
            get_coord_y = y_axis.GetBinLabel
        else:
            get_coord_y = y_axis.getBinCenter

        for ibin in range(1, x_axis.GetNbins() + 1):
            for jbin in range(1, y_axis.GetNbins() + 1):
                coord_x = get_coord_x(ibin)
                coord_y = get_coord_y(ibin)
                if coord_x == "" or coord_y == "":
                    continue
                to_be_added.append(((coord_x, coord_y,), histo.GetBinContent(ibin, jbin)))

        return to_be_added


    files = kwargs.pop("files", None)
    in_root_dir = kwargs.pop("dir", None)
    name = kwargs.pop("name", None)

    if None in [files, in_root_dir, name]:
        logger.fatal("Not all fields provided")

    if kwargs:
        logger.warning("There are unknown arguments when digesting ROOT file which will be ignored")
        print_dict(kwargs)

    # TODO For now treating everything as 1d histogram
    data = None
    for f in files:
        root_file = TFile.Open(f, "READ")
        root_dir = root_file.Get(in_root_dir)
        histo = root_dir.Get(name)
        if isinstance(histo, TH2):
            if data is None:
                data = DataSource(2)
            to_be_added = digest_2d(histo)
        else:
            if data is None:
                data = DataSource(1)
            to_be_added = digest_1d(histo)
        data.extend(to_be_added)

    return data