/usr/bin/aida2root is in rivet-root-converter 1.8.3-1.3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | #! /usr/bin/env python
"""\
%prog aidafile [aidafile2 ...]
Verify in the ROOT user manual what needs to be setup for use of ROOT with python
For example, additional setup steps such as this may be required:
setup setenv PYTHONDIR /usr
setenv PATH $ROOTSYS/bin:$PYTHONDIR/bin:$PATH
setenv LD_LIBRARY_PATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.6:$LD_LIBRARY_PATH
setenv PYTHONPATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.6
"""
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
import sys
if sys.version_info[:3] < (2,4,0):
print "rivet scripts require Python version >= 2.4.0... exiting"
sys.exit(1)
import os, array
try:
from ROOT import ROOT, TGraphAsymmErrors, TGraph2DErrors, TH1F, TH2F, TFile
except:
sys.stderr.write("Couldn't find PyROOT module. If ROOT is installed, PyROOT probably is too, but it's not currently accessible\n")
foundpyroot = False
if os.environ.has_key("ROOTSYS"):
ROOTSYS = os.environ["ROOTSYS"]
ROOTLIBDIR = os.path.join("ROOTSYS", "lib")
if os.path.exists(ROOTLIBDIR):
from glob import glob
if glob(os.path.join(ROOTLIBDIR, "libPyROOT.*")) and glob(os.path.join(ROOTLIBDIR, "ROOT.py")):
foundpyroot = True
if foundpyroot:
sys.stderr.write("It looks like you want to put %s in your LD_LIBRARY_PATH and PYTHONPATH shell variables.\n" % ROOTLIBDIR)
else:
sys.stderr.write("You should make sure that the directory containing libPyROOT and ROOT.py is in your LD_LIBRARY_PATH and PYTHONPATH shell variables.\n")
sys.stderr.write("You can test if PyROOT is properly set up by calling 'import ROOT' at the interactive Python shell\n")
sys.exit(1)
## Try to load faster but non-standard cElementTree module
try:
import xml.etree.cElementTree as ET
except ImportError:
try:
import cElementTree as ET
except ImportError:
try:
import xml.etree.ElementTree as ET
except:
sys.stderr.write("Can't load the ElementTree XML parser: please install it!\n")
sys.exit(1)
class Histo:
def __init__(self, nDim):
self._points = []
self.name = ""
self.title = ""
self._nDim = nDim
def addPoint(self, dp):
if dp.dimensionality() != self._nDim:
er = "Tried to add a datapoint of dimensionality " + str(dp.dimensionality()) + " to a histogram of dimensionality " + str(self._nDim)
sys.stderr.write(er)
sys.exit(1)
self._points.append(dp)
def numPts(self):
return len(self._points)
def asTGraph(self):
tg = TGraph()
tg.SetName(self.name)
tg.SetTitle(self.title)
return tg
def asHisto(self):
tg = self.asTGraph()
histo = tg.Histogram().Clone()
return histo
@staticmethod
def equalFloats(left, right, precision=1.e-6):
try:
test = abs((left - right) / (left + right))
return test < precision
except ZeroDivisionError:
if left * right < 0.:
return False
else:
return True
return False
class Histo2D(Histo):
def __init__(self):
Histo.__init__(self,3)
def asTGraph(self):
xs = array.array("d", [])
ex = array.array("d", [])
ys = array.array("d", [])
ey = array.array("d", [])
zs = array.array("d", [])
ez = array.array("d", [])
for pt in self._points:
x = pt.mean(0)
erx = pt.er(0)
y = pt.mean(1)
ery = pt.er(1)
z = pt.mean(2)
erz = pt.er(2)
xs.append(x)
ex.append(erx)
ys.append(y)
ey.append(ery)
zs.append(z)
ez.append(erz)
if self.numPts() == 0:
tg = TGraph2DErrors()
er = "Tried to create TGraph2DErrors called " + self.name + " with zero datapoints"
else:
tg = TGraph2DErrors(self.numPts(), xs, ys, zs, ex, ey, ez)
tg.SetTitle(self.title)
tg.SetName(self.name.replace("-", "_"))
return tg
def asTHisto(self):
if self.numPts() == 0:
histo = TH2F()
histo.SetName(self.name)
return histo
tmpXEdges = []
tmpYEdges = []
for pt in self._points:
tmpXEdges.append(pt.lowEdge(0))
tmpXEdges.append(pt.highEdge(0))
tmpYEdges.append(pt.lowEdge(1))
tmpYEdges.append(pt.highEdge(1))
sortedX = sorted(tmpXEdges)
sortedY = sorted(tmpYEdges)
xBinEdges = array.array("d", [sortedX[0]])
yBinEdges = array.array("d", [sortedY[0]])
for edge in sortedX:
if not Histo.equalFloats(edge, xBinEdges[-1]):
xBinEdges.append(edge)
for edge in sortedY:
if not Histo.equalFloats(edge, yBinEdges[-1]):
yBinEdges.append(edge)
histo = TH2F(self.name, self.title, len(xBinEdges)-1, xBinEdges, len(yBinEdges)-1, yBinEdges)
histo.Sumw2()
for pt in self._points:
bin = histo.FindBin(pt.value(0), pt.value(1))
histo.SetBinContent(bin, pt.value(2))
histo.SetBinError(bin, pt.er(2))
return histo
class Histo1D(Histo):
def __init__(self):
Histo.__init__(self,2)
def asTGraph(self):
xerrminus = array.array("d", [])
xerrplus = array.array("d", [])
xval = array.array("d", [])
yval = array.array("d", [])
yerrminus = array.array("d", [])
yerrplus = array.array("d", [])
for pt in self._points:
x = pt.value(0)
xplus = pt.erUp(0)
xminus = pt.erDn(0)
y = pt.value(1)
yplus = pt.erUp(1)
yminus = pt.erDn(1)
xval.append(x)
xerrminus.append(xminus)
xerrplus.append(xplus)
yval.append(y)
yerrminus.append(yminus)
yerrplus.append(yplus)
tg = TGraphAsymmErrors(self.numPts(), xval, yval, xerrminus, xerrplus, yerrminus, yerrplus)
tg.SetTitle(self.title)
tg.SetName(self.name.replace("-", "_"))
return tg
def asTHisto(self):
if self.numPts() == 0:
histo = TH1F()
histo.SetName(self.name)
return histo
binEdges = array.array("d", [])
binEdges.append(self._points[0].lowEdge(0))
bin = 0
binNumbers = []
for pt in self._points:
lowEdge = pt.lowEdge(0)
highEdge = pt.highEdge(0)
if not Histo1D.equalFloats(lowEdge, binEdges[-1]):
binEdges.append(lowEdge)
bin = bin + 1
bin = bin + 1
binEdges.append(highEdge)
binNumbers.append(bin)
histo = TH1F(self.name, self.title, self.numPts(), binEdges)
histo.Sumw2()
for i, pt in enumerate(self._points):
histo.SetBinContent(binNumbers[i], pt.value(1))
histo.SetBinError(binNumbers[i], pt.er(1))
return histo
class DataPoint:
def __init__(self):
self._dims = 0
self._coords = []
self._erUps = []
self._erDns = []
def setCoord(self, val, up, down):
self._dims = self._dims + 1
self._coords.append(val)
self._erUps.append(up)
self._erDns.append(down)
def dimensionality(self):
return self._dims
def th(self, dim):
th = "th"
if dim == 1:
th = "st"
elif dim == 2:
th = "nd"
elif dim == 3:
th = "rd"
return th
def checkDimensionality(self, dim):
if dim >= self.dimensionality():
er = "Tried to obtain the " + str(dim) + self.th(dim) + " dimension of a " + str(self.dimensionality()) + " dimension DataPoint"
sys.stderr.write(er)
sys.exit(1)
def value(self, dim):
self.checkDimensionality(dim)
return self._coords[dim]
def erUp(self, dim):
self.checkDimensionality(dim)
return self._erUps[dim]
def erDn(self, dim):
self.checkDimensionality(dim)
return self._erDns[dim]
def mean(self, dim):
val = self.value(dim) + 0.5 * (self.erUp(dim) - self.erDn(dim))
return val
def er(self, dim):
ee = 0.5 * (self.erUp(dim) + self.erDn(dim))
return ee
def lowEdge(self, dim):
return self.value(dim) - self.erDn(dim)
def highEdge(self, dim):
return self.value(dim) + self.erUp(dim)
def mkHistoFromDPS(dps):
dim = dps.get("dimension")
is3D = False
if dim == "3":
myhist = Histo2D()
is3D = True
else:
myhist = Histo1D()
myhist.name = dps.get("name")
myhist.title = dps.get("title")
myhist.path = dps.get("path")
points = dps.findall("dataPoint")
numbins = len(points)
for ptNum, point in enumerate(points):
dp = DataPoint()
for d, m in enumerate(point.findall("measurement")):
val = float(m.get("value"))
down = float(m.get("errorMinus"))
up = float(m.get("errorPlus"))
dp.setCoord(val, up, down)
myhist.addPoint(dp)
return myhist
##################################
from optparse import OptionParser
parser = OptionParser(usage=__doc__)
parser.add_option("-s", "--smart-output", action="store_true", default=True,
help="Write to output files with names based on the corresponding input filename",
dest="SMARTOUTPUT")
parser.add_option("-m", "--match", action="append",
help="Only write out histograms whose $path/$name string matches these regexes",
dest="PATHPATTERNS")
parser.add_option("-g", "--tgraph", action="store_true", default=False,
help="Store output as ROOT TGraphAsymmErrors or TGraph2DErrors",
dest="TGRAPH")
parser.add_option("-t", "--thisto", action="store_false",
help="Store output as ROOT TH1 or TH2",
dest="TGRAPH")
opts, args = parser.parse_args()
if opts.PATHPATTERNS is None:
opts.PATHPATTERNS = []
if len(args) < 1:
sys.stderr.write("Must specify at least one AIDA histogram file\n")
sys.exit(1)
for aidafile in args:
tree = ET.parse(aidafile)
histos = []
for dps in tree.findall("dataPointSet"):
useThisDps = True
if len(opts.PATHPATTERNS) > 0:
useThisDps = False
dpspath = os.path.join(dps.get("path"), dps.get("name"))
for regex in opts.PATHPATTERNS:
if re.compile(regex).search(dpspath):
useThisDps = True
break
if useThisDps:
histos.append(mkHistoFromDPS(dps))
if len(histos) > 0:
if opts.SMARTOUTPUT:
outfile = os.path.basename(aidafile).replace(".aida", ".root")
out = TFile(outfile,"RECREATE")
for h in sorted(histos):
if opts.TGRAPH:
h.asTGraph().Write()
else:
h.asTHisto().Write()
out.Close()
else:
sys.stderr.write("ROOT objects must be written to a file")
|