3 from array
import array
8 ROOT.PyConfig.IgnoreCommandLineOptions =
True
11 ROOT.gSystem.Load(
'libeicsmear')
12 ROOT.gROOT.SetBatch(
True)
15 """Parse command line arguments and return in an argparse.Namespace."""
16 from argparse
import ArgumentParser, ArgumentDefaultsHelpFormatter
17 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
18 parser.add_argument(
'file', nargs=
'+', help=
'input ASCII file')
19 parser.add_argument(
'-o',
'--outdir', default=
'.',
20 help=
'output directory')
21 parser.add_argument(
'-f',
'--format', default=
'pdf', choices=[
'ps',
'pdf'],
22 help=
'output file format')
23 parser.add_argument(
'-n',
'--nevents', default=-1, type=int,
24 help=
'number of events per file')
25 return parser.parse_args()
27 def build(filename, outdir='.', nevents=-1):
28 """Build a ROOT tree from the named text file.
30 Returns the name of the resultant ROOT file.
33 base, extension = os.path.splitext(filename)
34 ROOT.BuildTree(filename, outdir, nevents)
35 return '.'.join([base,
'root'])
38 """Return a histogram of the requested type.
40 args -- normal histogram constructor arguments
41 kwargs -- additional attributes to attach to the histogram
43 e.g. to create a TH1D with a 'cut' attribute:
45 histogram(ROOT.TH1D, 'myhist', '', 10, 0, 10, cut='x>10')
49 attributes = {
'select': h.GetName(),
'cut':
'',
'log':
''}
50 attributes.update(kwargs)
51 for name, value
in attributes.iteritems():
52 setattr(h, name, value)
53 h.SetOption(attributes.get(
'opt',
''))
58 return histogram(ROOT.TH1D, *args, **kwargs)
62 return histogram(ROOT.TH2D, *args, **kwargs)
65 """Fill a histogram from a tree.
67 Uses the histogram's 'select' and 'cut' attributes to determine
72 counts = tree.Project(self.GetName(), self.select, self.cut)
74 print 'Error filling histogram', self.GetName(),\
75 'from tree, histogram will not be filled'
78 ROOT.TH1.FillTree = fill_histogram_from_tree
81 """A collection of basic histograms."""
84 """Return the string prepended by this object's file basename."""
85 return '_'.join([self.base, string])
88 """Initialise histograms and associate them with a ROOT file."""
91 self.base, extension = os.path.splitext(file.GetName())
92 self.
out =
'.'.join([self.base,
'pdf'])
94 self.
window = ROOT.TCanvas(
name(
'canvas'),
'', 1, 1, 800, 800)
98 th1d(
name(
'beamlepton'),
'Beam lepton ID', 300, -3000., 3000.,
99 select=
'BeamLepton().Id().Code()'),
100 th1d(
name(
'beamhadron'),
'Beam hadron ID', 300, -3000., 3000.,
101 select=
'BeamHadron().Id().Code()'),
102 th1d(
name(
'scattered'),
'Scattered lepton ID', 300, -3000., 3000.,
103 select=
'ScatteredLepton().Id().Code()'),
104 th1d(
name(
'boson'),
'Exchange boson ID', 300, -3000., 3000.,
105 select=
'ExchangeBoson().Id().Code()'),
106 th1d(
name(
'nu'),
'nu', 25, 1., 5., select=
'log10(nu)'),
107 th2d(
name(
'Q2x'),
'$Q^{2}$ vs. $x_{Bj}$', 25, -5., 0., 25, -1., 4.,
108 select=
'log10(QSquared):log10(x)', opt=
'colz'),
109 th1d(
name(
'xf'),
'$x_{F}$', 55, -1.1, 1.1, select=
'xFeynman',
110 cut=
'KS==1 && id!=ScatteredLepton().Id().Code()'),
111 th1d(
name(
'z'),
'$z$', 60, -0.1, 1.1, select=
'z',
112 cut=
'KS==1 && id!=ScatteredLepton().Id().Code()', log=
'y'),
113 th1d(
name(
'pt'),
'$p_{T}$ vs. virtual photon', 60, 0., 3.,
114 select=
'ptVsGamma', log=
'y'),
115 th1d(
name(
'ptot'),
'Total final-state momentum', 100, 0., 300.,
116 select=
'FinalStateMomentum().P()'),
117 th1d(
name(
'ctot'),
'Total final-state charge', 100, -2., 2.,
118 select=
'FinalStateCharge()'),
119 th2d(
name(
'pthb'),
'Get4VectorInHadronBosonFrame().Pt():ptVsGamma',
120 50, 0., 10., 50, 0., 10.,
121 select=
'Get4VectorInHadronBosonFrame().Pt():ptVsGamma',
123 th2d(
name(
'mass'),
'Photon mass$^{2}$ vs. $Q^{2}$',
124 25, -1., 4., 25, -1., 4.,
125 select=
'log10(-(ExchangeBoson().Get4Vector().M2())):'
130 for method
in [
'',
'JB',
'DA']:
132 """Insert method into a string."""
134 return string.format(method)
136 return ''.join([string, method])
138 th1d(
name(var(
'x')), var(
'$x_{{Bj}}{}$'), 25, -5., 0.,
139 select=var(
'log10(x{})')),
140 th1d(
name(var(
'Q2')), var(
'$Q^{{2}}{}$'), 25, -1., 4.,
141 select=var(
'log10(QSquared)'), log=
'y'),
142 th1d(
name(var(
'y')), var(
'$y{}$'), 25, -0.1, 1.1,
144 th1d(
name(var(
'W2')), var(
'$W^{{2}}{}$'), 25, 1., 6.,
145 select=var(
'log10(WSquared)'))
149 th1d(
name(
'number'),
'Event number', 100, 0.,
150 file.EICTree.GetEntries(), select=
'number'),
151 th1d(
name(
'process'),
'Process', 200, 0., 200., select=
'process'),
152 th1d(
name(
'nTracks'),
'Number of tracks', 200, 0., 200.,
154 th2d(
name(
'tracks'),
'particles.GetEntries() vs. nTracks',
155 200, 0., 200., 200, 0., 200.,
156 select=
'@particles.GetEntries():nTracks', opt=
'colz'),
157 th1d(
name(
'Ein'),
'ELeptonInNucl', 25, 0., 12000.,
158 select=
'ELeptonInNucl'),
159 th1d(
name(
'Eout'),
'ELeptonOutNucl', 25, 0., 12000.,
160 select=
'ELeptonOutNucl'),
162 'HadronicFinalStateMomentum().M2():WSquared',
163 25, 1., 6., 25, 1., 6.,
164 select=
'log10(HadronicFinalStateMomentum().M2()):'
165 'log10(WSquared)', opt=
'colz')
168 def page(self, open=False, close=False, log=''):
169 """Print the current contents of the window."""
171 self.window.Print(self.
out +
'[')
173 self.window.Print(self.
out +
']')
175 self.window.SetLogx(
'x' in log)
176 self.window.SetLogy(
'y' in log)
177 self.window.SetLogz(
'z' in log)
178 self.window.Print(self.
out)
181 """Fill and print all histograms."""
184 histogram.FillTree(self.file.EICTree)
187 self.
page(log=histogram.log)
190 """Cloes the ROOT file and image file."""
192 self.
page(close=
True)
196 """PYHTIA-specific histograms"""
199 super(PythiaHistograms, self).
__init__(file)
202 th2d(
name(
'trueX'),
'$x_{Bj}$', 25, -5., 0., 25, -5., 0.,
203 select=
'log10(x):log10(trueX)', opt=
'colz'),
204 th2d(
name(
'trueQ2'),
'$Q^{2}$', 25, -1., 4., 25, -1., 4.,
205 select=
'log10(QSquared):log10(trueQ2)', opt=
'colz'),
206 th2d(
name(
'trueY'),
'$y$', 25, -0.1, 1.1, 25, -0.1, 1.1,
207 select=
'y:trueY', opt=
'colz'),
208 th2d(
name(
'trueW2'),
'$W^{2}$', 25, 1., 6., 25, 1., 6.,
209 select=
'log10(WSquared):log10(trueW2)', opt=
'colz')
214 """DJANGOH-specific histograms"""
217 super(DjangohHistograms, self).
__init__(file)
220 th2d(
name(
'dtrueX'),
'$x_{Bj}$', 25, -5., 0., 25, -5., 0.,
221 select=
'log10(x):log10(dtrueX)', opt=
'colz'),
222 th2d(
name(
'dtrueQ2'),
'$Q^{2}$', 25, -1., 4., 25, -1., 4.,
223 select=
'log10(QSquared):log10(dtrueQ2)', opt=
'colz'),
224 th2d(
name(
'dtrueY'),
'$y$', 25, -0.1, 1.1, 25, -0.1, 1.1,
225 select=
'y:dtrueY', opt=
'colz'),
226 th2d(
name(
'dtrueW2'),
'$W^{2}$', 25, 1., 6., 25, 1., 6.,
227 select=
'log10(WSquared):log10(dtrueW2)', opt=
'colz')
232 HISTOGRAMS = {
'pythia': PythiaHistograms,
235 'djangoh': DjangohHistograms,
237 'gmc_trans': Histograms}
240 """Return the event type for a ROOT file.
242 e.g. 'pythia', 'lepto'.
245 file.EICTree.GetEntry(0)
246 return file.EICTree.event.ClassName().lstrip(
'erhic::Event').lower()
248 if __name__ ==
'__main__':
251 names = [
build(name, options.outdir, options.nevents)
252 for name
in options.file]
254 root_files = [ROOT.TFile(name,
'update')
for name
in names]
256 histograms = [HISTOGRAMS[
event_type(file)](file)
for file
in root_files]
258 for histogram
in histograms: