Esempio n. 1
0
    def __init__(self):
        """
        """

        mon = CRMonitor()
        mon.message(self, "INFO: Initializing ROOT Style.")

        self.root_style_class = CRTStyle()

        # create fallbacks
        self.fill_colors        = dict()
        self.pretty_names       = dict()
        self.stacking_order     = []
        self.decorator_modules  = []
        self.main_settings      = None

        # directory names
        self.DIR_FILESERVICE  = "outputFileService/"
        self.DIR_LOGS         = "outputLogs/"
        self.DIR_CONFS        = "outputConfs/"
        self.DIR_PLOTS        = "outputPlots/"
        self.DIR_JOBINFO     = ".jobInfo/"

        # max num procs
        import multiprocessing as mp
        self.max_num_processes = mp.cpu_count()

        # postfixes for canvas saving
        self.canvas_postfixes = ['.root']
Esempio n. 2
0
class CRCentralHistoDispatch(object):
    """
    """

    __metaclass__ = CRSingleton

    class ParseError(Exception): pass

    def __init__(self):
        super(CRCentralHistoDispatch, self).__init__()
        self.open_root_files = {}
        self.service_files = {}
        self.message = CRMonitor().get_message_signal()


    def __del__(self):
        for tuple in self.open_root_files.items():
            tuple[1].Close()
        del self.open_root_files


    # TODO: give main_settings at constrction.
    def build_worker(self, worker_request, processes, main_settings):
        """
        """

        self.main_settings = main_settings

        # make dict of service files
        for proc in processes:
            self.service_files[proc.name] = proc.service_filename

        worker_request.samples = self.check_samples(worker_request.samples)

        # make worker
        worker = worker_request.class_ref(main_settings)
        worker.worker_request = worker_request
        worker.settings_group = worker_request.settings_group

        # loop over histogram lists
        worker_members = getmembers(worker_request)
        for member in worker_members:

            # if wrappers have to be build
            if member[0][:5] == "HIST_":
                var_name = member[0][5:]
                histo_token_list = member[1]

                # loop over histograms
                wrappers = self.get_histo_wrappers(
                    histo_token_list,
                    worker_request.samples
                )

                setattr(worker, var_name, wrappers)

            # if wrappers are delivered
            if member[0][:6] == "READY_":
                var_name = member[0][6:]
                wrappers = member[1]
                setattr(worker, var_name, wrappers)

        # add decorators
        for dec_name in worker_request.decorators:
            if dec_name == "None":
                continue
            decorator = root_style.import_decorator(dec_name)
            if decorator:
                worker = decorator(worker)
            else:
                self.message.emit(self, "WARNING I cannot find a decorator named '"+dec_name+"'")

        return worker


    def fill_input_streams(self, in_streams, processes, main_settings):
        """
        """

        self.main_settings = main_settings

        # make dict of service files
        for proc in processes:
            self.service_files[proc.name] = proc.service_filename

        for in_stream in in_streams:
            in_stream.samples = self.check_samples(in_stream.samples)
            wrappers = self.get_histo_wrappers(
                in_stream.histos,
                in_stream.samples
            )
            in_stream.histo_wrappers = wrappers


    def check_samples(self, samples):
        """
        """

        if samples == ["None"] or samples == [''] or samples == ['All']:
            samples = self.service_files.keys()
        else:
            for sample in samples[:]:
                if not self.service_files.has_key(sample):
                    samples.remove(sample)

        return samples


    def parse_histo_token(self, token):
        """
        Returns 'filepath, [in_file_folders,histoname]', where filename maybe
        None.
        """

        filename = None
        if token.count(":"):
            # special file mode
            filename, token = token.split(":")
            #filename = root_style.DIR_PLOTS + "/" + filename

        if token.count("/"):
            token = token.split("/")
        else:
            token = [token]

        return filename, token


    def get_root_file(self, filename):
        """
        """

        if self.open_root_files.has_key(filename):
            file = self.open_root_files[filename]
        else:
            file = TFile.Open(filename)
            self.open_root_files[filename] = file
        return file


    def get_histo(self, filename, in_file_path):
        """
        """

        file = self.get_root_file(filename)

        # browse to histogram
        obj = file
        for name in in_file_path:
            obj_key = obj.GetKey(name)
            if not obj_key:
                self.message.emit(self,
                    "WARNING I cannot find '"
                    + name
                    + "' in root file '"
                    + filename
                    + "'"
                )
                return None
            obj = obj_key.ReadObj()

        histo = obj
        if hasattr(histo, "Sumw2"):
            histo.Sumw2()
        return histo


    def get_histo_wrappers(self, histo_token_list, samples):
        """
        """

        wrappers = []
        for histo_token in histo_token_list:

            # parse histo_token
            filename, in_file_path = self.parse_histo_token(
                histo_token
            )

            if filename:
                # if special file stated
                histo = self.get_histo(filename, in_file_path)
                if histo:
                    wrappers.append(CRHistoWrapper(histo))

            else:
                # walk over service files otherwise
                for sample in samples:
                    filename = self.service_files[sample]
                    histo = self.get_histo(filename, in_file_path)
                    if not histo: continue
                    wrapper = CRHistoWrapper(
                        histo, sample, in_file_path[0]
                    )
                    self.collect_histogram_info(wrapper)
                    wrappers.append(wrapper)

        return wrappers


    def collect_histogram_info(self, wrapper):
        """
        Takes infos from main_settings object and puts into histogram wrappers.
        """

        qset = self.main_settings
        old_group = qset.group()
        qset.endGroup()
        qset.beginGroup(wrapper.sample)


        # no lumi, no histogram
        wrapper.lumi, parse_ok = qset.value("lumi", 0.).toDouble()
        if wrapper.lumi == 0. or not parse_ok:
            raise self.ParseError,\
            "Parsing of lumi value failed for " + wrapper.sample

        # data or not?
        wrapper.is_data = qset.value("isData", False).toBool()

        # mc to be overlayed?
        wrapper.is_overlay_mc = qset.value("isOverlayMc", False).toBool()

        # legend entry (if not given, takes wrapper.sample)
        legend_entry = str(qset.value("legend", wrapper.sample).toString())
        wrapper.legend = legend_entry

        qset.endGroup() # wrapper.sample
        qset.beginGroup(old_group)
Esempio n. 3
0
 def __init__(self):
     super(CRCentralHistoDispatch, self).__init__()
     self.open_root_files = {}
     self.service_files = {}
     self.message = CRMonitor().get_message_signal()
Esempio n. 4
0
 def __init__(self):
     super(CRPostProcTool, self).__init__()
     crm = CRMonitor()
     crm.connect_post_processing_tool(self)