ATLAS Offline Software
Loading...
Searching...
No Matches
LArG4ValidationPlotter.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3#
4# python script for drawing LArG4Validation ntuples
5#
6
7__version__ = '$Revision: 505706 $'
8__author__ = 'Radist Morse radist.morse@gmail.com'
9__doc__ = 'Script for drawing LArG4Validation N tuples'
10
11#list of accessible variables
12def eventext(event,hname) :
13 if (hname == "erec") :
14 return event.DepositedEnergy
15 elif (hname == "egen") :
16 return event.TrackEnergy
17 elif (hname == "cpu") :
18 return event.CPU
19 elif (hname == "eta") :
20 return event.Eta
21 elif (hname == "phi") :
22 return event.Phi
23 elif (hname == "pt") :
24 return event.Pt
25 elif (hname == "FC1_DeltaR") :
26 return sqrt(event.FC1_DeltaX*event.FC1_DeltaX+event.FC1_DeltaY*event.FC1_DeltaY)
27 if (hname == "erecegen") :
28 return event.DepositedEnergy / event.TrackEnergy
29 else :
30 return "False"
31
32def varname(hname) :
33 if (hname == "erec") :
34 return "E_{rec}"
35 elif (hname == "egen") :
36 return "E_{gen}"
37 elif (hname == "cpu") :
38 return "CPU Time"
39 elif (hname == "eta") :
40 return "#eta"
41 elif (hname == "phi") :
42 return "#phi"
43 elif (hname == "pt") :
44 return "P_{t}"
45 elif (hname == "FC1_DeltaR") :
46 return "FC1_DeltaR"
47 elif (hname == "erecegen") :
48 return "#frac{E_{rec}}{E_{gen}}"
49 else :
50 return ""
51
52import sys
53from os.path import isfile
54from optparse import OptionParser
55from math import sqrt
56
57usage = "usage: %prog [options] root1 [root2 ...]"
58
59parser = OptionParser(usage=usage, version="%prog v1.0 $Id: LArG4ValidationPlotter.py 505706 2012-06-15 11:48:02Z gsedov $")
60
61parser.add_option("-s","--split",dest="split_canvas", help="Split canvas, format: 'HORIZ:VERT' (default = %default)")
62parser.add_option("-p","--plots",dest="plots", help="input file with histogram parameters")
63parser.add_option("-f","--files",dest="files", help="input file with files parameters")
64parser.add_option("-o","--output",dest="outputfile", help="output ps file")
65parser.add_option("-d","--divide",dest="divide",action="store_true", help="Calculate ratio between root files (divide all roots to first")
66parser.add_option("-m","--mean",dest="mean",action="store_true",help="Print mean value in the legend")
67parser.add_option("-z","--zero",dest="zero",action="store_true",help="Set minimums to zero")
68
69parser.set_defaults(split_canvas="1:1",plots="",files="",outputfile="DISPLAY",divide=False, zero=False)
70
71(options, args) = parser.parse_args()
72
73split_canv = options.split_canvas.split(":")
74split_canv = map(int,split_canv)
75
76from LArG4Validation.LArG4PlottingScript import parseRoots, defaultRoots, \
77 parsePlots, fillPlots, dividePlots, savePlots, drawPlots, PlotEntry, \
78 createPlots
79
80if (len(split_canv) != 2) :
81 print ("ERROR: wrong split parameter")
82 sys.exit(1)
83
84if (options.divide and len(args) < 2) :
85 print ("ERROR: must be at least two input files for ratio calculation")
86 sys.exit(1)
87
88if isfile(options.files) :
89 print ("Parsing file with root parameters:",options.files)
90 parsedRoots = parseRoots(options.files)
91else :
92 print ("No root parameters provided, using default")
93 parsedRoots = defaultRoots(args)
94
95if isfile(options.plots) :
96 print ("Parsing file with plots parameters:",options.plots)
97 parsedPlots = parsePlots(options.plots,varname)
98else :
99 print ("No plots parameters provided, using default")
100 parsedPlots = []
101 parsedRestricts = []
102 for var in ["erec","cpu"] :
103 pe = PlotEntry()
104 pe.vars_to_draw = [var]
105 pe.axis_captions[var] = varname(var)
106 pe.display_name = var
107 parsedPlots.append(pe)
108 pe = PlotEntry()
109 pe.vars_to_draw = ["eta","erec"]
110 pe.axis_captions["eta"] = varname("eta")
111 pe.axis_captions["erec"] = varname("erec")
112 pe.display_name = "Energy vs Eta"
113 pe.profile = True
114 parsedPlots.append(pe)
115 pe = PlotEntry()
116 pe.vars_to_draw = ["egen","erec"]
117 pe.axis_captions["egen"] = varname("egen")
118 pe.axis_captions["erec"] = varname("erec")
119 pe.display_name = "Energy vs Truth Energy"
120 pe.profile = True
121 parsedPlots.append(pe)
122 split_canv[0] = 2
123 split_canv[1] = 2
124
125#number of pads
126maxperlist = split_canv[0]*split_canv[1]
127
128#can't make NewPage for display
129numplots = len(parsedPlots)
130
131if (options.outputfile == "DISPLAY") and (numplots > maxperlist) :
132 print ("ERROR: too many hists to print to display")
133 sys.exit(1)
134
135from ROOT import TFile
136
137#opening root files
138for rootopt in parsedRoots :
139 if not isfile(rootopt.filename) :
140 print ("ERROR: unexistent file:",rootopt.filename)
141 sys.exit(1)
142 root = TFile(rootopt.filename,"read")
143 if root.IsOpen() == 0 :
144 print ("ERROR: can't open the file:",rootopt.filename)
145 sys.exit(1)
146 rootopt.rootfile = root
147 rootopt.tree = root.Get("COL/1")
148
149print ("Creating plots...")
150plots = createPlots(parsedPlots,parsedRoots)
151
152print ("Filling plots...")
153fillPlots(plots,parsedPlots,parsedRoots,eventext)
154
155if (options.divide) :
156 print ("Calculating ratio")
157 rootopt1 = parsedRoots.pop(0)
158 dividePlots(plots,rootopt1)
159 for rootopt in parsedRoots :
160 rootopt.legendname += " / " + rootopt1.legendname
161
162print ("Done!")
163
164if options.outputfile.endswith(".root") :
165 savePlots(plots,options.outputfile)
166else :
167 canv,leg = drawPlots(plots,parsedPlots,parsedRoots,options.outputfile,options.zero,options.mean,split_canv[0],split_canv[1])
168
169if (options.outputfile == "DISPLAY") :
170 from time import sleep
171 while not not(canv) :
172 sleep(1)
STL class.