Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
rootdrawtree.py
Go to the documentation of this file.
1#!/usr/bin/env @python@
2
3# ROOT command line tools: rootdrawtree
4# Author: Luca Giommi
5# Mail: luca.giommi2@studio.unibo.it
6# Date: 15/09/16
7
8import sys
9import argparse
10import ROOT
11from sys import stderr
12import textwrap
13
15 parser = argparse.ArgumentParser(
16 formatter_class=argparse.RawDescriptionHelpFormatter, prog='rootdrawtree',
17 description=textwrap.dedent('''\
18Python script that loops over a chain to create histograms.
19There are two ways to do it: one is parsing the name of the configuration file as argument,
20that must have a proper syntax as shown in the class documentation of TSimpleAnalysis.
21
22Example:
23user@users-desktop:~$ rootdrawtree input_file.txt # input_file.txt is the configuration file
24
25The other way is to pass as arguments the name of the output file, the name of the .root input
26files, the expressions (that will be shown in the histograms) and the name of the tree (that
27is optional if there is only one tree inside the first .root input file).
28
29Examples:
30user@users-desktop:~$ rootdrawtree --output output.root --input hsimple.root --tree ntuple --histo 'hpxpy=px:py if px>2'
31user@users-desktop:~$ rootdrawtree --output output.root --input hsimple.root hsimple2.root --histo 'hpx=px' 'hpxpy=px:py if px>2'
32
33 '''))
34
35 parser.add_argument('configFile', nargs='?', default='', help = "Configuration file")
36 parser.add_argument('-o', '--output', default='', action='store', dest='output', help='Name of the output file in which will be stored the histograms')
37 parser.add_argument('-i', '--input', default=[], nargs='*', dest='inputFiles', help='.root input files')
38 parser.add_argument('-t', '--tree', default='', action='store', dest='tree', help='Name of the tree')
39 parser.add_argument('-hs', '--histo', default=[], nargs='*', dest='histoExpr', help='Expressions to build the histograms in the form "NAME = EXPRESSION if CUT"')
40 return parser
41
42if __name__ == "__main__":
43 parser = get_argparse()
44
45 args = parser.parse_args()
46 sys.argv = []
47
48 if (args.configFile != '' and (args.output != '' or args.inputFiles != [] or args.histoExpr != [] or args.tree != '')):
49 stderr.write("Error: both configuration file and options are provided \n")
50 sys.exit(1)
51 if (args.configFile != ''):
52 a = ROOT.TSimpleAnalysis(args.configFile)
53 if a.Configure():
54 a.Run()
55 else:
56 if len(args.inputFiles) == 0:
57 stderr.write("Error: neither configuration file nor input files are provided\n")
58 sys.exit(1)
59 inputfile = ROOT.vector("string")(len(args.inputFiles))
60 for i,s in enumerate(args.inputFiles):
61 inputfile[i] = s
62 if len(args.histoExpr) == 0:
63 stderr.write("Error: neither configuration file nor expression is provided\n")
64 sys.exit(1)
65 expr = ROOT.vector("string")(len(args.histoExpr))
66 for k,l in enumerate(args.histoExpr):
67 expr[k]=l
68 a = ROOT.TSimpleAnalysis(args.output, inputfile, expr, args.tree)
69 a.Run()