ATLAS Offline Software
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
12 def 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 
32 def 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 
52 import sys
53 from os.path import isfile
54 from optparse import OptionParser
55 from math import sqrt
56 
57 usage = "usage: %prog [options] root1 [root2 ...]"
58 
59 parser = OptionParser(usage=usage, version="%prog v1.0 $Id: LArG4ValidationPlotter.py 505706 2012-06-15 11:48:02Z gsedov $")
60 
61 parser.add_option("-s","--split",dest="split_canvas", help="Split canvas, format: 'HORIZ:VERT' (default = %default)")
62 parser.add_option("-p","--plots",dest="plots", help="input file with histogram parameters")
63 parser.add_option("-f","--files",dest="files", help="input file with files parameters")
64 parser.add_option("-o","--output",dest="outputfile", help="output ps file")
65 parser.add_option("-d","--divide",dest="divide",action="store_true", help="Calculate ratio between root files (divide all roots to first")
66 parser.add_option("-m","--mean",dest="mean",action="store_true",help="Print mean value in the legend")
67 parser.add_option("-z","--zero",dest="zero",action="store_true",help="Set minimums to zero")
68 
69 parser.set_defaults(split_canvas="1:1",plots="",files="",outputfile="DISPLAY",divide=False, zero=False)
70 
71 (options, args) = parser.parse_args()
72 
73 split_canv = options.split_canvas.split(":")
74 split_canv = map(int,split_canv)
75 
76 from LArG4Validation.LArG4PlottingScript import parseRoots, defaultRoots, \
77  parsePlots, fillPlots, dividePlots, savePlots, drawPlots, PlotEntry, \
78  createPlots
79 
80 if (len(split_canv) != 2) :
81  print ("ERROR: wrong split parameter")
82  sys.exit(1)
83 
84 if (options.divide and len(args) < 2) :
85  print ("ERROR: must be at least two input files for ratio calculation")
86  sys.exit(1)
87 
88 if isfile(options.files) :
89  print ("Parsing file with root parameters:",options.files)
90  parsedRoots = parseRoots(options.files)
91 else :
92  print ("No root parameters provided, using default")
93  parsedRoots = defaultRoots(args)
94 
95 if isfile(options.plots) :
96  print ("Parsing file with plots parameters:",options.plots)
97  parsedPlots = parsePlots(options.plots,varname)
98 else :
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
126 maxperlist = split_canv[0]*split_canv[1]
127 
128 #can't make NewPage for display
129 numplots = len(parsedPlots)
130 
131 if (options.outputfile == "DISPLAY") and (numplots > maxperlist) :
132  print ("ERROR: too many hists to print to display")
133  sys.exit(1)
134 
135 from ROOT import TFile
136 
137 #opening root files
138 for 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 
149 print ("Creating plots...")
150 plots = createPlots(parsedPlots,parsedRoots)
151 
152 print ("Filling plots...")
153 fillPlots(plots,parsedPlots,parsedRoots,eventext)
154 
155 if (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 
162 print ("Done!")
163 
164 if options.outputfile.endswith(".root") :
165  savePlots(plots,options.outputfile)
166 else :
167  canv,leg = drawPlots(plots,parsedPlots,parsedRoots,options.outputfile,options.zero,options.mean,split_canv[0],split_canv[1])
168 
169 if (options.outputfile == "DISPLAY") :
170  from time import sleep
171  while not not(canv) :
172  sleep(1)
LArG4PlottingScript.drawPlots
def drawPlots(plots, plotopts, rootopts, output, optzero, optmean, opth, optw)
Definition: LArG4PlottingScript.py:427
LArG4PlottingScript.fillPlots
def fillPlots(plots, plotopts, rootopts, eventVal)
Definition: LArG4PlottingScript.py:297
LArG4PlottingScript.savePlots
def savePlots(plots, output)
Definition: LArG4PlottingScript.py:414
LArG4ValidationPlotter.eventext
def eventext(event, hname)
Definition: LArG4ValidationPlotter.py:12
LArG4PlottingScript.parsePlots
def parsePlots(filename, varCaption)
Definition: LArG4PlottingScript.py:153
LArG4ValidationPlotter.varname
def varname(hname)
Definition: LArG4ValidationPlotter.py:32
LArG4PlottingScript.dividePlots
def dividePlots(plots, rootopt1)
Definition: LArG4PlottingScript.py:405
LArG4PlottingScript.createPlots
def createPlots(plotopts, rootopts)
Definition: LArG4PlottingScript.py:276
LArG4PlottingScript.defaultRoots
def defaultRoots(args)
Definition: LArG4PlottingScript.py:78
LArG4PlottingScript.parseRoots
def parseRoots(filename)
Definition: LArG4PlottingScript.py:18