ATLAS Offline Software
LArG4AODNtuplePlotter.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 == "hadrleak") :
14  return event.ethad1/event.et
15  elif (hname == "e237oe277") :
16  if (event.e277 > 0) :
17  return event.e237/event.e277
18  else :
19  return 0
20  elif (hname == "e2tsts1") :
21  return event.e2tsts1/(1000. + 0.009 * event.et)
22  elif (hname == "e2tsts1demins") :
23  return event.e2tsts1 - event.emins1
24  elif (hname == "erec") :
25  return event.energy
26  elif (hname == "erecegen") :
27  return event.energy / event.truth_energy
28  elif (hname == "eta") :
29  return event.eta
30  elif (hname == "pt") :
31  return event.pt
32  elif (hname == "egen") :
33  return event.truth_energy
34  else :
35  return "False"
36 
37 def varname(hname) :
38  if (hname == "hadrleak") :
39  return "Hadron Leak"
40  elif (hname == "e237oe277") :
41  return "#frac{E_{237}}{E_{277}}"
42  elif (hname == "e2tsts1") :
43  return "E_{2}tsts_{1}"
44  elif (hname == "e2tsts1demins") :
45  return "e2tsts1demins"
46  elif (hname == "erec") :
47  return "E_{rec}"
48  elif (hname == "erecegen") :
49  return "#frac{E_{rec}}{E_{gen}}"
50  elif (hname == "eta") :
51  return "#eta"
52  elif (hname == "pt") :
53  return "P_{t}"
54  elif (hname == "egen") :
55  return "E_{gen}"
56  else :
57  return ""
58 
59 import sys
60 from os.path import isfile
61 from optparse import OptionParser
62 
63 usage = "usage: %prog [options] root1 [root2 ...]"
64 
65 parser = OptionParser(usage=usage, version="%prog v1.0 $Id: LArG4AODNtuplePlotter.py 505706 2012-06-15 11:48:02Z gsedov $")
66 
67 parser.add_option("-s","--split",dest="split_canvas", help="Split canvas, format: 'HORIZ:VERT' (default = %default)")
68 parser.add_option("-p","--plots",dest="plots", help="input file with histogram parameters")
69 parser.add_option("-f","--files",dest="files", help="input file with files parameters")
70 parser.add_option("-o","--output",dest="outputfile", help="output ps file")
71 parser.add_option("-d","--divide",dest="divide",action="store_true", help="Calculate ratio between root files (divide all roots to first")
72 parser.add_option("-m","--mean",dest="mean",action="store_true",help="Print mean value in the legend")
73 parser.add_option("-z","--zero",dest="zero",action="store_true",help="Set minimums to zero")
74 
75 parser.set_defaults(split_canvas="1:1",plots="",files="",outputfile="DISPLAY",divide=False, zero=False)
76 
77 (options, args) = parser.parse_args()
78 
79 split_canv = options.split_canvas.split(":")
80 split_canv = map(int,split_canv)
81 
82 from LArG4Validation.LArG4PlottingScript import parseRoots, defaultRoots, \
83  parsePlots, fillPlots, dividePlots, savePlots, drawPlots, PlotEntry, \
84  createPlots
85 
86 if (len(split_canv) != 2) :
87  print ("ERROR: wrong split parameter")
88  sys.exit(1)
89 
90 if (options.divide and len(args) < 2) :
91  print ("ERROR: must be at least two input files for ratio calculation")
92  sys.exit(1)
93 
94 if isfile(options.files) :
95  print ("Parsing file with root parameters:",options.files)
96  parsedRoots = parseRoots(options.files)
97 else :
98  print ("No root parameters provided, using default")
99  parsedRoots = defaultRoots(args)
100 
101 if isfile(options.plots) :
102  print ("Parsing file with plots parameters:",options.plots)
103  parsedPlots = parsePlots(options.plots,varname)
104 else :
105  print ("No plots parameters provided, using default")
106  parsedPlots = []
107  parsedRestricts = []
108  listvars = ["hadrleak","e237oe277","weta2","f1","e2tsts1","e2tsts1demins","wtots1","fracs1","weta1"]
109  capvars = {}
110  for var in listvars :
111  capvars[var] = varname(var)
112  listmax = [0.02,1.0,0.015,0.7,3.0,1500.0,4.0,0.6,0.8]
113  listmin = [-0.007,0.84,0.005,0.0,0.0,0.0,1.0,0.0,0.4]
114  listlogy = [0,0,0,0,1,1,0,0,0]
115  for var,mmax,mmin,logy in zip(listvars,listmax,listmin,listlogy) :
116  pe = PlotEntry()
117  pe.vars_to_draw.append(var)
118  pe.axis_captions[var] = capvars[var]
119  pe.display_name = var
120  pe.givenmin = mmin
121  pe.givenmax = mmax
122  pe.logy = logy
123  parsedPlots.append(pe)
124  split_canv[0] = 3
125  split_canv[1] = 3
126 
127 #number of pads
128 maxperlist = split_canv[0]*split_canv[1]
129 
130 #can't make NewPage for display
131 numplots = len(parsedPlots)
132 
133 if (options.outputfile == "DISPLAY") and (numplots > maxperlist) :
134  print ("ERROR: too many hists to print to display")
135  sys.exit(1)
136 
137 from ROOT import TFile
138 
139 #opening root files
140 for rootopt in parsedRoots :
141  if not isfile(rootopt.filename) :
142  print ("ERROR: unexistent file:",rootopt.filename)
143  sys.exit(1)
144  root = TFile(rootopt.filename,"read")
145  if root.IsOpen() == 0 :
146  print ("ERROR: can't open the file:",rootopt.filename)
147  sys.exit(1)
148  rootopt.rootfile = root
149  rootopt.tree = root.Get("AOD")
150 
151 print ("Creating plots...")
152 plots = createPlots(parsedPlots,parsedRoots)
153 
154 print ("Filling plots...")
155 fillPlots(plots,parsedPlots,parsedRoots,eventext)
156 
157 
158 if (options.divide) :
159  print ("Calculating ratio")
160  rootopt1 = parsedRoots.pop(0)
161  dividePlots(plots,rootopt1)
162  for rootopt in parsedRoots :
163  rootopt.legendname += " / " + rootopt1.legendname
164 
165 print ("Done!")
166 
167 if options.outputfile.endswith(".root") :
168  savePlots(plots,options.outputfile)
169 else :
170  canv,leg = drawPlots(plots,parsedPlots,parsedRoots,options.outputfile,options.zero,options.mean,split_canv[0],split_canv[1])
171 
172 if (options.outputfile == "DISPLAY") :
173  from time import sleep
174  while not not(canv) :
175  sleep(1)
LArG4PlottingScript.drawPlots
def drawPlots(plots, plotopts, rootopts, output, optzero, optmean, opth, optw)
Definition: LArG4PlottingScript.py:427
LArG4AODNtuplePlotter.eventext
def eventext(event, hname)
Definition: LArG4AODNtuplePlotter.py:12
LArG4PlottingScript.fillPlots
def fillPlots(plots, plotopts, rootopts, eventVal)
Definition: LArG4PlottingScript.py:297
LArG4PlottingScript.savePlots
def savePlots(plots, output)
Definition: LArG4PlottingScript.py:414
LArG4PlottingScript.parsePlots
def parsePlots(filename, varCaption)
Definition: LArG4PlottingScript.py:153
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
LArG4AODNtuplePlotter.varname
def varname(hname)
Definition: LArG4AODNtuplePlotter.py:37
LArG4PlottingScript.parseRoots
def parseRoots(filename)
Definition: LArG4PlottingScript.py:18