diff --git a/main/python/cmdLineUtils.py b/main/python/cmdLineUtils.py index c05c44dcf7f4b..ac6dd7e115e13 100644 --- a/main/python/cmdLineUtils.py +++ b/main/python/cmdLineUtils.py @@ -1064,13 +1064,24 @@ def rootMv(sourceList, destFileName, destPathSplit, compress=None, interactive=F # ROOTPRINT -def _keyListExtended(rootFile, pathSplitList): +def _keyListExtended(rootFile, pathSplitList, recursive = False): + prefixList = [] keyList, dirList = keyClassSplitter(rootFile, pathSplitList) for pathSplit in dirList: keyList.extend(getKeyList(rootFile, pathSplit)) + subList = [key for key in keyList if isDirectoryKey(key)] keyList = [key for key in keyList if not isDirectoryKey(key)] - keyListSort(keyList) - return keyList + prefixList = ["" for key in keyList] + if recursive: + for subdir in subList: + subkeyList, subprefixList = _keyListExtended(ROOT.gDirectory.Get(subdir.GetName()), pathSplitList, recursive) + keyList.extend(subkeyList) + prefixList.extend([subdir.GetName() + "_" + prefix for prefix in subprefixList]) + if recursive: + keyList, prefixList = (list(t) for t in zip(*sorted(zip(keyList, prefixList), key=lambda x: x[0].GetName().lower()))) + else: + keyListSort(keyList) + return keyList, prefixList def rootPrint( @@ -1083,6 +1094,7 @@ def rootPrint( sizeOption=None, styleOption=None, verboseOption=False, + recursiveOption=False, ): # Check arguments if sourceList == []: @@ -1162,8 +1174,8 @@ def rootPrint( continue openRootFiles.append(rootFile) # Fill the key list (almost the same as in root) - keyList = _keyListExtended(rootFile, pathSplitList) - for key in keyList: + keyList, prefixList = _keyListExtended(rootFile, pathSplitList, recursiveOption) + for k, key in enumerate(keyList): if isTreeKey(key): pass else: @@ -1182,8 +1194,9 @@ def rootPrint( canvas.Clear() canvas.Divide(x, y) else: + prefix = prefixList[k] if not outputOption: - outputFileName = key.GetName() + "." + formatOption + outputFileName = prefix + key.GetName() + "." + formatOption if directoryOption: outputFileName = os.path.join(directoryOption, outputFileName) if outputOption or formatOption == "pdf": diff --git a/main/python/rootprint.py b/main/python/rootprint.py index ac84b0b145a66..21de181ec1225 100755 --- a/main/python/rootprint.py +++ b/main/python/rootprint.py @@ -21,6 +21,7 @@ SIZE_HELP = "specify canvas size on the format 'width'x'height' (ex: 600x400)" STYLE_HELP = "specify a C file name which define a style" VERBOSE_HELP = "print informations about the running" +RECURSIVE_HELP = "Traverse file recursively entering any TDirectory." EPILOG = """Examples: - rootprint example.root:hist @@ -48,6 +49,7 @@ def get_argparse(): parser.add_argument("-s", "--size", help=SIZE_HELP) parser.add_argument("-S", "--style", help=STYLE_HELP) parser.add_argument("-v", "--verbose", action="store_true", help=VERBOSE_HELP) + parser.add_argument("-r", "--recursive", action="store_true", help=RECURSIVE_HELP) return parser def execute(): @@ -61,6 +63,7 @@ def execute(): divideOption = optDict["divide"], drawOption = optDict["draw"], \ formatOption = optDict["format"], \ outputOption = optDict["output"], sizeOption = optDict["size"], \ - styleOption = optDict["style"], verboseOption = optDict["verbose"]) + styleOption = optDict["style"], verboseOption = optDict["verbose"], \ + recursiveOption = optDict["recursive"]) if __name__ == "__main__": sys.exit(execute())