def get_binning_with_equal_entries():
	"""Get binning with roughly equal number of entries per bin
	
		Example usage: get_binning_with_equal_entries.py artus_output.root  folder/ntuple  quantityname  --n-bins 10
	"""

	parser = argparse.ArgumentParser(description="Print binning of histograms", parents=[logger.loggingParser])

	parser.add_argument("root_file", help="Input ROOT file")
	parser.add_argument("folder", help="Folder to TTree")
	parser.add_argument("quantity", help="Quantity for which binning should be determined")
	parser.add_argument("--n-bins", type=int, help="Number of bins. [Default: %(default)s]", default=25)
	parser.add_argument("--selection", type=str, help="Optional selection", default="1")

	args = parser.parse_args()
	logger.initLogger(args)
	
	with TFileContextManager(args.root_file, "READ") as root_file:
		tree = root_file.Get(args.folder)

		events_list = sorted([e[0] for e in eventselectionoverlap.EventSelectionOverlap.get_events_list_from_tree(tree, args.quantity, args.selection)])
		assert(len(events_list) > args.n_bins)

		# slice events list
		bin_border_list = []
		for i_bin in range(args.n_bins):
			bin_border_list.append(events_list[int(round(i_bin/float(args.n_bins)*len(events_list)))])
		bin_border_list.append(events_list[-1])
		
		log.info("Bin borders for {} bins:".format(args.n_bins))
		log.info(" ".join([str(i) for i in bin_border_list]))
示例#2
0
	def check_type(root_file_names, path_to_objects, print_quantities=False):
		if isinstance(root_file_names, basestring):
			root_file_names = [root_file_names]
		if isinstance(path_to_objects, basestring):
			path_to_objects = [path_to_objects]
		
		with TFileContextManager(root_file_names[0], "READ") as root_file: 
			root_object = root_file.Get(str(path_to_objects[0])) if str(path_to_objects[0]) != "" else root_file
			if root_object:
				if isinstance(root_object, ROOT.TTree):
					if print_quantities:
						log.info("List of all tree quantities (in the first file):")
						for leaf in sorted(root_object.GetListOfLeaves(), key=lambda leaf: leaf.GetName()):
							log.info("\t%s (%s)" % (RootTools.full_leaf_name(leaf), leaf.GetTypeName()))
					return "TTree"
				elif isinstance(root_object, ROOT.TDirectory):
					if print_quantities:
						log.info("List of all histogram/graph/function quantities (in the first file):")
						for key, path in sorted(RootTools.walk_root_directory(root_object), key=lambda element: element[1]):
							if key.GetClassName().startswith("TH") or key.GetClassName().startswith("TF") or key.GetClassName().startswith("Roo") or "Graph" in key.GetClassName():
								log.info("\t%s (%s)" % (path, key.GetClassName()))
					return "TDirectory"
				else:
					log.error("Usage of ROOT objects of Type \"" + root_object.ClassName() + "\" is not yet implemented!")
					return None
			else:
				log.error("Could not find ROOT object \"" + path_to_objects[0] + "\" in file \"" + root_file_names[0] + "\"!")
				return None
示例#3
0
def runtime(args=None, additional_dictionary=None):
	"""Make performance plots (processor runtime).

	To create the 'runTime'-ntuple in the excalibur output, add 'RunTimeConsumer'
	to the list of consumers in the config.
	"""

	only_producers = True  # dont plot filters

	plots = []
	for label, corr in zip(
		['Data', 'MC'],
		['Res', ''],
	):
		filename = '{}_runtime.root'.format(label.lower())
		folder = 'finalcuts_AK5PFJetsCHSL1L2L3{}/runTime'.format(corr)
		processors = []
		# get list of processors
		with TFileContextManager(filename, "READ") as rootfile:
			ntuple = rootfile.Get(folder)
			try:
				for leaf in ntuple.GetListOfLeaves():
					if ('Producer' in leaf.GetName()) or not only_producers:
						processors.append(leaf.GetName())
			except AttributeError:
				print "Could not find {}! Did you let the RunTimeConsumer run?".format(folder)
				exit(1)
		d = {
			# input
			'files': [filename],
			'folders': [folder],
			'x_expressions': processors,
			'nicks': processors,
			'x_bins': ['100,0,500'],
			'no_weight': True,
			# analysis
			'analysis_modules': ['NormalizeToUnity', 'HistogramFromMeanValues'],
			# formatting
			'nicks_whitelist': ['mean'],
			'markers': ['fill'],
			'legend': None,
			'x_label': ' ',
			'y_label': 'runtime',
			'energies': None,
			'title': label,
			'no_energy_label': True,
			# output
			'filename':  label.lower() + '_runtime',
		}
		plots.append(d)
	harryinterface.harry_interface(plots, args)
示例#4
0
	def read_input_json_dicts(self, plotData):
		"""If Artus config dict is present in root file -> append to plotdict"""
		for root_files in plotData.plotdict["files"]:
			# TODO: make TChain instead of using only first file?
			with TFileContextManager(root_files[0], "READ") as tfile:
				keys, names = zip(*roottools.RootTools.walk_root_directory(tfile))
			if jsonTools.JsonDict.PATH_TO_ROOT_CONFIG in names:
				input_json_dict = jsonTools.JsonDict(root_files)
			else:
				input_json_dict = {}
			plotData.input_json_dicts.append(input_json_dict)

		# Raise warning if config dict could be read out for some, but not for all files
		if ({} in plotData.input_json_dicts and not all([i == {} for i in plotData.input_json_dicts])):
			log.warning("'config' dict could not be read for all input files! (ignore this warning if you're not using Artus output files)")
示例#5
0
        "--elements",
        nargs="+",
        help="Regexes for elements for which the code should be executed.",
        default=[])
    parser.add_argument(
        "-c",
        "--codes",
        nargs="+",
        help=
        "Codes to be executed for matching elements. \"element\" is replaced by the matching element.",
        default=[])

    args = parser.parse_args()
    logger.initLogger(args)

    with TFileContextManager(args.root_file, "READ") as root_file:
        elements = roottools.RootTools.walk_root_directory(root_file)
        for index, (key, path) in enumerate(elements):
            class_name = key.GetClassName()
            log.info("%s (%s)" % (path, class_name))
            for regex, code in zip(args.elements, args.codes):
                if re.match(regex, path):
                    root_object = root_file.Get(path)
                    result = eval(code.replace("element", "root_object"))
                    if result:
                        log.info(code.replace("element", path))
                        log.info(result)
                    if index < len(elements) - 1:
                        log.info("\n" + (100 * "-") + "\n")
            if log.isEnabledFor(logging.DEBUG):
                root_object = root_file.Get(path)
示例#6
0
	def histogram_from_file(root_file_names, path_to_histograms, x_bins=None, y_bins=None, z_bins=None, name=None):
		"""
		Read histograms from files
	
		root_file_names: string (or list of strings)
		path_to_histograms: string (or list of strings) of path to root histogram in root file
	
		This function looks for the same histograms in all files and sums them up
		The name (string) of the resulting histogram can be passed as a parameter
		"""

		if isinstance(root_file_names, basestring):
			root_file_names = [root_file_names]
		if isinstance(path_to_histograms, basestring):
			path_to_histograms = [path_to_histograms]
	
		# prepare unique histogram name
		if name == None:
			name = "histogram_{0}.json".format(hashlib.md5("_".join([str(root_file_names),
				                                                     str(path_to_histograms)])).hexdigest())
	
		# loop over files and try to read histograms
		root_histogram = None
		for root_file_name in root_file_names:
			with TFileContextManager(root_file_name, "READ") as root_file:
				for path_to_histogram in path_to_histograms:
					tmp_root_histogram = root_file.Get(str(path_to_histogram))
					if tmp_root_histogram == None:
						log.critical("Cannot find histogram \"%s\" in file \"%s\"!" % (path_to_histogram, root_file_name))
						sys.exit(1)
				
					if tmp_root_histogram is None:
						log.error("Could not find histogram \"" + path_to_histogram + "\" in file \"" + root_file_name + "\"!")
					else:
						if isinstance(tmp_root_histogram, ROOT.TH1):
							tmp_root_histogram.SetDirectory(0)
					
						if root_histogram == None:
							root_histogram = tmp_root_histogram.Clone(name)
						else:
							root_histogram.Add(tmp_root_histogram)
					
						if isinstance(root_histogram, ROOT.TH1):
							root_histogram.SetDirectory(0)

		# rebinning
		if isinstance(root_histogram, ROOT.TH1) and root_histogram.GetNbinsX()*root_histogram.GetNbinsY()*root_histogram.GetNbinsZ() > 1:
			rebinning_x = 1
			if not x_bins is None:
				x_binning_string, x_bin_edges = RootTools.prepare_binning(x_bins)
				rebinning_x = x_bin_edges
				if x_bin_edges is None:
					rebinning_x = int(root_histogram.GetNbinsX() / int(x_bins[0]))
				else:
					rebinning_x = x_bin_edges
			
			rebinning_y = 1
			if not y_bins is None:
				y_binning_string, y_bin_edges = RootTools.prepare_binning(y_bins)
				rebinning_y = y_bin_edges
				if y_bin_edges is None:
					rebinning_y = int(root_histogram.GetNbinsY() / int(y_bins[0]))
				else:
					rebinning_y = y_bin_edges
			
			rebinning_z = 1
			if not z_bins is None:
				z_binning_string, z_bin_edges = RootTools.prepare_binning(z_bins)
				rebinning_z = z_bin_edges
				if z_bin_edges is None:
					rebinning_z = int(root_histogram.GetNbinsZ() / int(z_bins[0]))
				else:
					rebinning_z = z_bin_edges
			
			root_histogram = RootTools.rebin_root_histogram(
					root_histogram,
					rebinningX=rebinning_x,
					rebinningY=rebinning_y,
					rebinningZ=rebinning_z,
					name=name
			)
			
		return root_histogram