示例#1
0
    def run(self):
        """Add all requested processors, then reparse all command line arguments.
		   Finally prepare and run all processors.
		"""
        # Detect all valid processors
        self._detect_available_processors()

        json_default_initialisation = None
        if self.args["json_defaults"] is not None:
            json_default_initialisation = self.args["json_defaults"]
            json_defaults = JsonDict(
                self.args["json_defaults"]).doIncludes().doComments()
            #set_defaults will overwrite/ignore the json_default argument. Cannot be used.
            no_default_args = dict(
                (k, v) for (k, v) in self.args.items()
                if not self.parser.get_default(k) == self.args[k])
            self.args.update(
                dict(json_defaults.items() + no_default_args.items()))

        # replace 'json_defaults' from imported json file to actual name of imported json file
        if json_default_initialisation != None:
            self.args["json_defaults"] = json_default_initialisation

        if self.args["list_available_modules"]:
            self._print_available_modules()
            return

        # handle input modules (first)
        if isinstance(self.args["input_modules"], basestring):
            self.args["input_modules"] = [self.args["input_modules"]]
        for module in self.args["input_modules"]:
            if self._isvalid_processor(module, processor_type=InputBase):
                self.processors.append(self.available_processors[module]())
            else:
                log.info(
                    "Provide a valid input module or use the default. Default is \"{0}\"!"
                    .format(self.parser.get_default("input_modules")))
                log.critical(
                    "Input module \"{0}\" cannot be not found or imported!".
                    format(module))
                sys.exit(1)

        # handle analysis modules (second)
        if self.args["analysis_modules"] is None:
            self.args["analysis_modules"] = []

        for module in self.args["analysis_modules"]:
            if self._isvalid_processor(module, processor_type=AnalysisBase):
                self.processors.append(self.available_processors[module]())
            else:
                log.critical(
                    "Analysis module \"{0}\" cannot be not found or imported!".
                    format(module))
                sys.exit(1)

        # handle plot modules (third)
        if isinstance(self.args["plot_modules"], basestring):
            self.args["plot_modules"] = [self.args["plot_modules"]]
        if not self.args["plot_modules"]:
            log.critical("Empty list of plot modules supplied!")

        for module in self.args["plot_modules"]:
            if self._isvalid_processor(module, processor_type=PlotBase):
                self.processors.append(self.available_processors[module]())
            else:
                log.critical(
                    "Plot module \"{0}\" cannot be not found or imported!".
                    format(module))
                sys.exit(1)

        # let processors modify the parser and then parse the arguments again
        for processor in self.processors:
            processor.modify_argument_parser(self.parser, self.args)

        # overwrite defaults by defaults from json files
        if self.args["json_defaults"] != None:
            self.parser.set_defaults(**(JsonDict(
                self.args["json_defaults"]).doIncludes().doComments()))

        self.args = vars(self.parser.parse_args(self._args_from_script))
        plotData = plotdata.PlotData(self.args)

        # print the final processor chain
        log.debug('Processors will be run in the following order')
        log.debug(' => '.join(
            [processor.name() for processor in self.processors]))

        if not plotData.plotdict['no_logo']:
            self._logo()

        # general ROOT settings
        log.debug("Setting ROOT TH1 DefaultSumw2 to True.")
        ROOT.TH1.SetDefaultSumw2(True)
        ROOT.gROOT.SetBatch(True)

        # export arguments into JSON file (1)
        # remove entries from dictionary that are not meant to be exported
        export_args = JsonDict(copy.deepcopy(plotData.plotdict))
        for arg in [
                "quantities", "export_json", "live", "userpc", "json_defaults"
        ]:
            export_args.pop(arg, None)
        # remove defaults
        for key in export_args.keys():
            if (key in self.args
                    and self.parser.get_default(key) == export_args[key] and
                (self.args.get("json_defaults", None) is None
                 or key not in JsonDict(
                     self.args["json_defaults"]).doIncludes().doComments())):
                export_args.pop(key, None)

        if plotData.plotdict["export_json"] == "update":
            plotData.plotdict["export_json"] = "default" if plotData.plotdict[
                "json_defaults"] is None else plotData.plotdict[
                    "json_defaults"][0]

        # prepare aguments for all processors before running them
        for processor in self.processors:
            processor.prepare_args(self.parser, plotData)
            processor.run(plotData)

        # export arguments into JSON file
        if plotData.plotdict["export_json"] != "default" and plotData.plotdict[
                "export_json"] not in [False, "False", None, "None"]:
            export_args.save(plotData.plotdict["export_json"], indent=4)
        else:
            plotData.plotdict["export_json"] = None

        # save plots
        output_filenames = plotData.save()

        del (plotData)
        return output_filenames
示例#2
0
	def run(self):
		"""Add all requested processors, then reparse all command line arguments.
		   Finally prepare and run all processors.
		"""
		# Detect all valid processors
		self._detect_available_processors()

		json_default_initialisation = None
		if self.args["json_defaults"] is not None:
			json_default_initialisation = self.args["json_defaults"]
			json_defaults = JsonDict(self.args["json_defaults"]).doIncludes().doComments()
			#set_defaults will overwrite/ignore the json_default argument. Cannot be used.
			no_default_args = dict((k,v) for (k,v) in self.args.items() if not self.parser.get_default(k) == self.args[k] )
			self.args.update(dict(json_defaults.items() + no_default_args.items()))

		# replace 'json_defaults' from imported json file to actual name of imported json file
		if json_default_initialisation != None:
			self.args["json_defaults"] = json_default_initialisation

		if self.args["list_available_modules"]:
			self._print_available_modules()
			return
		
		# handle input modules (first)
		if isinstance(self.args["input_modules"], basestring):
			self.args["input_modules"] = [self.args["input_modules"]]
		for module in self.args["input_modules"]:
			if self._isvalid_processor(module, processor_type=InputBase):
				self.processors.append(self.available_processors[module]())
			else:
				log.info("Provide a valid input module or use the default. Default is \"{0}\"!".format(self.parser.get_default("input_modules")))
				log.critical("Input module \"{0}\" cannot be not found or imported!".format(module))
				sys.exit(1)

		# handle analysis modules (second)
		if self.args["analysis_modules"] is None:
			self.args["analysis_modules"] = []
		
		for module in self.args["analysis_modules"]:
			if self._isvalid_processor(module, processor_type=AnalysisBase):
				self.processors.append(self.available_processors[module]())
			else:
				log.critical("Analysis module \"{0}\" cannot be not found or imported!".format(module))
				sys.exit(1)

		# handle plot modules (third)
		if isinstance(self.args["plot_modules"], basestring):
			self.args["plot_modules"] = [self.args["plot_modules"]]
		if not self.args["plot_modules"]:
			log.critical("Empty list of plot modules supplied!")

		for module in self.args["plot_modules"]:
			if self._isvalid_processor(module, processor_type=PlotBase):
				self.processors.append(self.available_processors[module]())
			else:
				log.critical("Plot module \"{0}\" cannot be not found or imported!".format(module))
				sys.exit(1)

		# let processors modify the parser and then parse the arguments again
		for processor in self.processors:
			processor.modify_argument_parser(self.parser, self.args)
		
		# overwrite defaults by defaults from json files
		if self.args["json_defaults"] != None:
			self.parser.set_defaults(**(JsonDict(self.args["json_defaults"]).doIncludes().doComments()))
		
		self.args = vars(self.parser.parse_args(self._args_from_script))
		plotData = plotdata.PlotData(self.args)
		
		# print the final processor chain
		log.debug('Processors will be run in the following order')
		log.debug(' => '.join([processor.name() for processor in self.processors]))

		if not plotData.plotdict['no_logo']:
			self._logo()

		# general ROOT settings
		log.debug("Setting ROOT TH1 DefaultSumw2 to True.")
		ROOT.TH1.SetDefaultSumw2(True)
		ROOT.gROOT.SetBatch(True)
		
		# export arguments into JSON file (1)
		# remove entries from dictionary that are not meant to be exported
		export_args = JsonDict(copy.deepcopy(plotData.plotdict))
		for arg in ["quantities", "export_json", "live", "userpc", "json_defaults"]:
			export_args.pop(arg, None)
		# remove defaults
		for key in export_args.keys():
			if (key in self.args and self.parser.get_default(key) == export_args[key]
						and (self.args.get("json_defaults", None) is None or
						key not in JsonDict(self.args["json_defaults"]).doIncludes().doComments())):
				export_args.pop(key, None)

		if plotData.plotdict["export_json"] == "update":
			plotData.plotdict["export_json"] = "default" if plotData.plotdict["json_defaults"] is None else plotData.plotdict["json_defaults"][0]
		
		# prepare aguments for all processors before running them
		for processor in self.processors:
			processor.prepare_args(self.parser, plotData)
			processor.run(plotData)
		
		# export arguments into JSON file
		if plotData.plotdict["export_json"] != "default" and plotData.plotdict["export_json"] not in [False, "False", None, "None"]:
			export_args.save(plotData.plotdict["export_json"], indent=4)
		
		# save plots
		output_filenames = plotData.save()
		
		del(plotData)
		return output_filenames