File indexing completed on 2024-11-26 02:34:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 """
0020 cuy
0021
0022 A very simple way to make plots with ROOT via an XML file.
0023
0024 usage: %prog -x <XML configuration file>
0025 -b, --batch : run in batch mode without graphics.
0026 -c, --create = CREATE: create XML configuration file from a ROOT file.
0027 -e, --example = EXAMPLE: generate an example xml file.
0028 -f, --flag = FLAG: create a baneer
0029 -l, --list = LIST: list of objects in the ROOT file.
0030 -p, --prt = PRT: print canvas in the format specified png, ps, eps, pdf, etc.
0031 -t, --tag = TAG: tag name for XML configuration file.
0032 -v, --verbose : verbose output.
0033 -w, --wait : Pause script after plotting a new superposition of histograms.
0034 -x, --xml = XML: xml configuration file.
0035
0036 Francisco Yumiceva (yumiceva@fnal.gov)
0037 Fermilab 2008
0038
0039 """
0040
0041
0042 import os, string, re, sys, math
0043
0044 try:
0045 import ROOT
0046 except:
0047 print("\nCannot load PYROOT, make sure you have setup ROOT in the path")
0048 print("and pyroot library is also defined in the variable PYTHONPATH, try:\n")
0049 if (os.getenv("PYTHONPATH")):
0050 print(" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
0051 else:
0052 print(" setenv PYTHONPATH $ROOTSYS/lib\n")
0053 sys.exit()
0054
0055 from ROOT import TFile
0056 from ROOT import TCanvas
0057 from ROOT import TLegend
0058 from ROOT import SetOwnership
0059 from ROOT import THStack
0060 from ROOT import TLatex
0061 from ROOT import TH1
0062 from ROOT import TH1F
0063 from ROOT import TGraphErrors
0064 from ROOT import TVectorD
0065 from ROOT import std
0066
0067 from xml.sax import saxutils, make_parser, handler
0068 from xml.sax.handler import feature_namespaces
0069
0070 import Inspector
0071 import Style
0072
0073
0074 import optparse
0075
0076 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
0077
0078 def nonzero(self):
0079 "True if options were given"
0080 for v in self.__dict__.values():
0081 if v is not None: return True
0082 return False
0083
0084 optparse.Values.__nonzero__ = nonzero
0085
0086 class ParsingError(Exception): pass
0087
0088 optionstring=""
0089
0090 def exit(msg=""):
0091 raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
0092
0093 def parse(docstring, arglist=None):
0094 global optionstring
0095 optionstring = docstring
0096 match = USAGE.search(optionstring)
0097 if not match: raise ParsingError("Cannot find the option string")
0098 optlines = match.group(1).splitlines()
0099 try:
0100 p = optparse.OptionParser(optlines[0])
0101 for line in optlines[1:]:
0102 opt, help=line.split(':')[:2]
0103 short,long=opt.split(',')[:2]
0104 if '=' in opt:
0105 action='store'
0106 long=long.split('=')[0]
0107 else:
0108 action='store_true'
0109 p.add_option(short.strip(),long.strip(),
0110 action = action, help = help.strip())
0111 except (IndexError,ValueError):
0112 raise ParsingError("Cannot parse the option string correctly")
0113 return p.parse_args(arglist)
0114
0115
0116
0117 class ValElement:
0118 def __init__(self):
0119 self.type = ""
0120 self.filename = ""
0121 self.release = ""
0122 self.histos = {}
0123 self.TH1s = {}
0124 self.weight = None
0125
0126 class divideElement:
0127 def __init__(self):
0128 self.name = ""
0129 self.numerator = None
0130 self.denominator = None
0131
0132 class plotElement:
0133 def __init__(self):
0134 self.name = ""
0135 self.title = ""
0136 self.color = ""
0137
0138 class additionElement:
0139 def __init__(self):
0140 self.name = ""
0141 self.title = ""
0142 self.SetLogy = ""
0143 self.SetGrid = ""
0144 self.histos = []
0145 self.weight = []
0146
0147 class superimposeElement:
0148 def __init__(self):
0149 self.name = ""
0150 self.title = ""
0151 self.SetLogy = ""
0152 self.SetGrid = ""
0153 self.histos = []
0154 self.color = []
0155 self.marker = []
0156 self.legend = []
0157 self.weight = []
0158
0159
0160 class graphElement:
0161 def __init__(self):
0162 self.name = ""
0163 self.title = ""
0164 self.SetLogy = ""
0165 self.SetGrid = ""
0166 self.histos = []
0167 self.color = []
0168 self.marker = []
0169 self.legend = []
0170 self.weight = []
0171 self.flavour = []
0172
0173
0174 class FindIssue(handler.ContentHandler):
0175 def __init__(self):
0176 self.data = {}
0177 self.divide = {}
0178 self.addition = {}
0179 self.superimpose = {}
0180 self.graph = {}
0181 self.tmpaddname = ""
0182 self.plot = {}
0183 self.size = 0
0184 self.atype = ""
0185 self.tmpsupername = ""
0186 self.tmpgraphname = ""
0187
0188 def startElement(self, name, attrs):
0189 if name == 'validation':
0190 self.size = self.size + 1
0191 self.atype = attrs.get('type',None)
0192 self.data[self.atype] = ValElement()
0193 self.data[self.atype].type = attrs.get('type',None)
0194 self.data[self.atype].filename = attrs.get('file',None)
0195 self.data[self.atype].release = attrs.get('release',None)
0196 self.data[self.atype].weight = attrs.get('weight','')
0197 if name == 'TH1':
0198 self.data[self.atype].histos[attrs.get('name',None)] = attrs.get('source',None)
0199
0200
0201 if name == 'divide':
0202 aname = attrs.get('name',None)
0203 self.divide[aname] = divideElement()
0204 self.divide[aname].name = aname
0205 self.divide[aname].numerator = attrs.get('numerator',None)
0206 self.divide[aname].denominator = attrs.get('denominator',None)
0207 self.divide[aname].DivideOption = attrs.get('DivideOption',None)
0208 self.divide[aname].Option = attrs.get('Option',None)
0209 if name == 'addition':
0210 aname = attrs.get('name',None)
0211 self.addition[aname] = additionElement()
0212 self.tmpaddname = aname
0213 self.addition[aname].name = aname
0214 self.addition[aname].title = attrs.get('title',None)
0215 self.addition[aname].YTitle = attrs.get('YTitle',None)
0216 self.addition[aname].XTitle = attrs.get('XTitle',None)
0217 self.addition[aname].Option = attrs.get('Option',None)
0218 self.addition[aname].Weight = attrs.get('Wight',None)
0219 self.addition[aname].Normalize = attrs.get('Normalize',None)
0220 self.addition[aname].SetGrid = attrs.get('SetGrid',None)
0221 if name == 'additionItem':
0222
0223 self.addition[self.tmpaddname].histos.append(attrs.get('name',None))
0224 self.addition[self.tmpaddname].weight.append(attrs.get('weight',None))
0225 if name == 'superimpose':
0226 aname = attrs.get('name',None)
0227 self.superimpose[aname] = superimposeElement()
0228 self.superimpose[aname].name = aname
0229 self.superimpose[aname].title = attrs.get('title',None)
0230 self.superimpose[aname].SetLogy = attrs.get('SetLogy',None)
0231 self.superimpose[aname].SetGrid = attrs.get('SetGrid',None)
0232 self.superimpose[aname].Normalize = attrs.get('Normalize',None)
0233 self.superimpose[aname].Stack = attrs.get('Stack',None)
0234 self.superimpose[aname].YTitle = attrs.get('YTitle',None)
0235 self.superimpose[aname].XTitle = attrs.get('XTitle',None)
0236 self.superimpose[aname].projection = attrs.get('Projection',None)
0237 self.superimpose[aname].bin = attrs.get('bin',None)
0238 self.superimpose[aname].profile = attrs.get('Profile',None)
0239 self.superimpose[aname].Fill = attrs.get('Fill',None)
0240 self.superimpose[aname].Option = attrs.get('Option',None)
0241 self.superimpose[aname].Weight = attrs.get('Weight',None)
0242 self.superimpose[aname].Maximum = attrs.get('Maximum',None)
0243 self.superimpose[aname].Minimum = attrs.get('Minimum',None)
0244 self.superimpose[aname].Labels = attrs.get('Labels',None)
0245 self.superimpose[aname].Rebin = attrs.get('Rebin',None)
0246 self.tmpsupername = aname
0247 if name == 'graph':
0248 aname = attrs.get('name',None)
0249 self.graph[aname] = graphElement()
0250 self.graph[aname].name = aname
0251 self.graph[aname].title = attrs.get('title',None)
0252 self.graph[aname].SetLogy = attrs.get('SetLogy',None)
0253 self.graph[aname].SetGrid = attrs.get('SetGrid',None)
0254 self.graph[aname].Normalize = attrs.get('Normalize',None)
0255 self.graph[aname].Stack = attrs.get('Stack',None)
0256 self.graph[aname].YTitle = attrs.get('YTitle',None)
0257 self.graph[aname].XTitle = attrs.get('XTitle',None)
0258 self.graph[aname].projection = attrs.get('Projection',None)
0259 self.graph[aname].bin = attrs.get('bin',None)
0260 self.graph[aname].profile = attrs.get('Profile',None)
0261 self.graph[aname].Fill = attrs.get('Fill',None)
0262 self.graph[aname].Option = attrs.get('Option',None)
0263 self.graph[aname].Weight = attrs.get('Weight',None)
0264 self.graph[aname].Maximum = attrs.get('Maximum',None)
0265 self.graph[aname].Minimum = attrs.get('Minimum',None)
0266 self.graph[aname].Labels = attrs.get('Labels',None)
0267 self.tmpgraphname = aname
0268 if name == 'superimposeItem':
0269
0270 self.superimpose[self.tmpsupername].histos.append(attrs.get('name',None))
0271 self.superimpose[self.tmpsupername].color.append(attrs.get('color',None))
0272 self.superimpose[self.tmpsupername].marker.append(attrs.get('MarkerStyle',None))
0273 self.superimpose[self.tmpsupername].legend.append(attrs.get('legend',None))
0274
0275
0276 if name == 'graphItem':
0277
0278 self.graph[self.tmpgraphname].histos.append(attrs.get('name',None))
0279 self.graph[self.tmpgraphname].color.append(attrs.get('color',None))
0280 self.graph[self.tmpgraphname].marker.append(attrs.get('MarkerStyle',None))
0281 self.graph[self.tmpgraphname].legend.append(attrs.get('legend',None))
0282 self.graph[self.tmpgraphname].flavour.append(attrs.get('flavour',None))
0283
0284
0285
0286
0287 if __name__ == '__main__':
0288
0289
0290
0291
0292 thestyle = Style.Style()
0293 thestyle.SetStyle()
0294
0295 printCanvas = False
0296 printFormat = "png"
0297 printBanner = False
0298 Banner = "CMS Preliminary"
0299 verbose = False
0300
0301
0302 option,args = parse(__doc__)
0303 if not args and not option: exit()
0304
0305 if option.batch:
0306 ROOT.gROOT.SetBatch()
0307
0308 if option.verbose:
0309 verbose = True
0310
0311 if option.list:
0312 ins = Inspector.Inspector()
0313 ins.Verbose(True)
0314 ins.createXML(False)
0315 ins.SetFilename(option.list)
0316 ins.GetListObjects()
0317 sys.exit()
0318
0319 if option.create:
0320 createXML = Inspector.Inspector()
0321 createXML.Verbose(False)
0322 createXML.createXML(True)
0323 if option.tag:
0324 createXML.SetTag(option.tag)
0325 createXML.SetFilename(option.create)
0326 createXML.GetListObjects()
0327 sys.exit()
0328
0329 if not option.xml: exit()
0330 if option.prt:
0331 printCanvas = True
0332 printFormat = option.prt
0333
0334 if option.flag:
0335 printBanner = True
0336 Banner = option.flag
0337
0338
0339 try:
0340 xmlfile = open(option.xml)
0341 xmlfile.close()
0342 except:
0343 print(" ERROR: xml file \"" + option.xml + "\" does not exist")
0344 sys.exit()
0345
0346
0347 parser = make_parser()
0348
0349
0350 parser.setFeature(feature_namespaces, 0)
0351
0352
0353 dh = FindIssue()
0354
0355
0356 parser.setContentHandler(dh)
0357
0358
0359 parser.parse(option.xml)
0360
0361
0362 cv = {}
0363 afilelist = {}
0364 stacklist = {}
0365
0366
0367 outputroot = TFile("cuy.root","RECREATE")
0368
0369
0370 newTH1list = []
0371
0372
0373 thedata = dh.data
0374
0375 firstFilename = ''
0376
0377 for ikey in thedata:
0378 if verbose : print("= Processing set called: " + ikey)
0379 afilename = thedata[ikey].filename
0380 if firstFilename == '':
0381 firstFilename = afilename
0382 arelease = ""
0383 if thedata[ikey].release != None:
0384 arelease = thedata[ikey].release
0385 if verbose : print("== filename: " + afilename)
0386 if verbose : print("== release: " + arelease)
0387 if verbose : print("== weight: " + thedata[ikey].weight)
0388 thehistos = thedata[ikey].histos
0389 afilelist[afilename] = TFile(afilename)
0390 if verbose : print("== get histograms: ")
0391 histonamekeys = thehistos.keys()
0392 for ihname in histonamekeys:
0393 if verbose : print("=== Histogram name: \""+ ihname + "\" source: \""+thehistos[ihname]+"\"")
0394 thedata[ikey].TH1s[ihname] = ROOT.gDirectory.Get(thehistos[ihname])
0395
0396
0397 print(thedata[ikey].TH1s[ihname].GetName())
0398
0399
0400
0401
0402 afilelist[firstFilename].cd()
0403
0404
0405
0406 theaddition = dh.addition
0407 if verbose : print("= Create addition histograms:")
0408
0409 for ikey in theaddition:
0410 if verbose : print("== plot name: \""+theaddition[ikey].name+"\" title: \""+theaddition[ikey].title+"\"")
0411 listname = theaddition[ikey].histos
0412 listweight = theaddition[ikey].weight
0413
0414
0415 cv[theaddition[ikey].name] = TCanvas(theaddition[ikey].name,theaddition[ikey].name,700,700)
0416
0417 isFirst = True
0418 ihnameIt = 0
0419 for ihname in listname:
0420 aweight = 1
0421 if listweight[ihnameIt]:
0422
0423 aweight = float(listweight[ihnameIt])
0424
0425 for jkey in thedata:
0426 tmpkeys = thedata[jkey].histos.keys()
0427 for tmpname in tmpkeys:
0428 if tmpname == ihname:
0429 ath = thedata[jkey].TH1s[tmpname]
0430 if ath is None:
0431 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0432 exit(0)
0433 if verbose : print("=== add histogram: "+ath.GetName() + " from " + thedata[jkey].filename + " mean = " + "%.2f" % round(ath.GetMean(),2) + " weight= " + str(aweight))
0434
0435 if isFirst:
0436 newth = ath.Clone(theaddition[ikey].name)
0437 newth.Sumw2()
0438 if theaddition[ikey].Normalize == "true":
0439 newth.Scale(1/newth.Integral())
0440 newth.Scale(aweight)
0441 isFirst = False
0442 else:
0443 atmpth = ath.Clone()
0444 atmpth.Sumw2()
0445 if theaddition[ikey].Normalize == "true":
0446 atmpth.Scale(1/atmpth.Integral())
0447 atmpth.Scale(aweight)
0448 newth.Add( atmpth )
0449 ihnameIt = ihnameIt + 1
0450
0451 if theaddition[ikey].XTitle != None:
0452 newth.SetXTitle(theaddition[ikey].XTitle)
0453 if theaddition[ikey].YTitle != None:
0454 newth.SetYTitle(theaddition[ikey].YTitle)
0455
0456 if theaddition[ikey].Option:
0457 newth.Draw(theaddition[ikey].Option)
0458 else:
0459 newth.Draw()
0460
0461 if theaddition[ikey].SetGrid == "true":
0462 cv[theaddition[ikey].name].SetGrid()
0463
0464 cv[theaddition[ikey].name].Update()
0465
0466
0467 newth.SetName(theaddition[ikey].name)
0468 newTH1list.append(newth.GetName())
0469 thedata[newth.GetName()] = ValElement()
0470 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
0471 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
0472
0473
0474 outputroot.cd()
0475 newth.Write()
0476
0477
0478 if verbose : print("= Create ratio histograms:")
0479
0480 thedivition = dh.divide
0481 for ikey in thedivition:
0482 if verbose : print("== plot name: \""+thedivition[ikey].name+"\" title: \""+"\"")
0483 numerator = thedivition[ikey].numerator
0484 denominator = thedivition[ikey].denominator
0485
0486
0487 cv[thedivition[ikey].name] = TCanvas(thedivition[ikey].name,thedivition[ikey].name,700,700)
0488
0489 for jkey in thedata:
0490 tmpkeys = thedata[jkey].histos.keys()
0491 for tmpname in tmpkeys:
0492 if tmpname == numerator:
0493 numeratorth = thedata[jkey].TH1s[tmpname]
0494 if numeratorth is None:
0495 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0496 exit(0)
0497
0498
0499 if tmpname == denominator:
0500 denominatorth = thedata[jkey].TH1s[tmpname]
0501 if denominatorth is None:
0502 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0503 exit(0)
0504
0505
0506
0507
0508 numeratorth.Sumw2()
0509 denominatorth.Sumw2()
0510 newth = numeratorth.Clone()
0511 newth.Clear()
0512 if thedivition[ikey].DivideOption is None:
0513 newth.Divide(numeratorth,denominatorth)
0514 else:
0515 newth.Divide(numeratorth,denominatorth,1.,1.,thedivition[ikey].DivideOption)
0516
0517
0518
0519
0520
0521 if thedivition[ikey].Option:
0522 newth.Draw(thedivition[ikey].Option)
0523 else:
0524 newth.Draw()
0525
0526 cv[thedivition[ikey].name].Update()
0527
0528
0529
0530 if option.wait:
0531 raw_input( 'Press ENTER to continue\n ' )
0532
0533
0534 newth.SetName(thedivition[ikey].name)
0535 newTH1list.append(newth.GetName())
0536 thedata[newth.GetName()] = ValElement()
0537 thedata[newth.GetName()].TH1s[newth.GetName()] = newth
0538 thedata[newth.GetName()].histos[newth.GetName()] = newth.GetName()
0539
0540
0541 outputroot.cd()
0542 newth.Write()
0543
0544
0545 thesuper = dh.superimpose
0546 if verbose : print("= Create superimpose histograms:")
0547 for ikey in thesuper:
0548 if verbose : print("== plot name: \""+thesuper[ikey].name+"\" title: \""+thesuper[ikey].title+"\"")
0549 listname = thesuper[ikey].histos
0550 listcolor = thesuper[ikey].color
0551 listmarker = thesuper[ikey].marker
0552 listlegend = thesuper[ikey].legend
0553
0554 dolegend = False
0555 for il in listlegend:
0556 if il==None: dolegend = False
0557 if verbose : print("dolegend = " +str(dolegend))
0558 doNormalize = False
0559 doRebin=thesuper[ikey].Rebin
0560 if doRebin is not None :
0561 doRebin=int(doRebin)
0562 if verbose : print("Rebin is ", doRebin)
0563 if thesuper[ikey].Normalize == "true":
0564 doNormalize = True
0565 if verbose : print("normalize = " +str(doNormalize))
0566 projectAxis = "no"
0567 projectBin = -1
0568 if thesuper[ikey].projection == "x": projectAxis = "x"
0569 if thesuper[ikey].projection == "y": projectAxis = "y"
0570 if thesuper[ikey].bin != None: projectBin = thesuper[ikey].bin
0571 profileAxis = "no"
0572 if thesuper[ikey].profile == "x": profileAxis = "x"
0573 if thesuper[ikey].profile == "y": profileAxis = "y"
0574 doFill = False
0575 if thesuper[ikey].Fill == "true": doFill = True
0576 if verbose : print("fill option:"+ doFill)
0577
0578 cv[thesuper[ikey].name] = TCanvas(thesuper[ikey].name,thesuper[ikey].title,700,700)
0579
0580 aleg = TLegend(0.6,0.4,0.8,0.6)
0581 SetOwnership( aleg, 0 )
0582 aleg.SetMargin(0.12)
0583 aleg.SetTextSize(0.035)
0584 aleg.SetFillColor(10)
0585 aleg.SetBorderSize(0)
0586
0587 isFirst = 1
0588 ii = 0
0589
0590 stacklist[thesuper[ikey].name] = THStack("astack"+thesuper[ikey].name,thesuper[ikey].title)
0591 astack = stacklist[thesuper[ikey].name]
0592 for ihname in listname:
0593
0594 for jkey in thedata:
0595 tmpkeys = thedata[jkey].histos.keys()
0596
0597 for tmpname in tmpkeys:
0598
0599 if tmpname == ihname:
0600 ath = thedata[jkey].TH1s[tmpname]
0601 if ath is None:
0602 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0603 exit(0)
0604 if verbose : print("=== superimpose histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2))
0605
0606 if projectAxis == "x":
0607 if projectBin == -1:
0608 newthpx = ath.ProjectionX(ath.GetName()+"_px",0,-1,"e")
0609 else:
0610 newthpx = ath.ProjectionX(ath.GetName()+"_px",int(projectBin),int(projectBin),"e")
0611 newth = newthpx.Clone()
0612 if projectAxis == "y":
0613 if projectBin == -1:
0614 newthpy = ath.ProjectionY(ath.GetName()+"_py",0,-1,"e")
0615 else:
0616 newthpx = ath.ProjectionY(ath.GetName()+"_py",int(projectBin),int(projectBin),"e")
0617 newth = newthpy.Clone()
0618 if profileAxis == "x":
0619 newthpx = ath.ProfileX(ath.GetName()+"_px",0,-1,"e")
0620 newth = newthpx.Clone()
0621 if profileAxis == "y":
0622 newthpy = ath.ProfileY(ath.GetName()+"_py",0,-1,"e")
0623 newth = newthpy.Clone()
0624
0625
0626 aweight = 1
0627 if thedata[jkey].weight != None and thesuper[ikey].Weight=="true":
0628 aweight = float( thedata[jkey].weight )
0629 if verbose: print(" with weight = " + str(aweight))
0630
0631
0632
0633
0634 if projectAxis == "no" and profileAxis == "no" :newth = ath.Clone()
0635
0636 if doRebin is not None and doRebin>0 :
0637 newth.Rebin(doRebin)
0638
0639 newth.Sumw2()
0640 newth.Scale(aweight)
0641
0642
0643 if not listcolor[ii]:
0644 listcolor[ii] = 1
0645
0646 newth.SetLineColor(int(listcolor[ii]))
0647 newth.SetMarkerColor(int(listcolor[ii]))
0648
0649 if doFill: newth.SetFillColor(int(listcolor[ii]))
0650
0651 if listmarker[ii] != None:
0652 newth.SetMarkerStyle(int(listmarker[ii]))
0653
0654 if doNormalize:
0655 newth.Scale(1./newth.Integral())
0656
0657
0658 if thesuper[ikey].Labels != None:
0659 thelabels = thesuper[ikey].Labels.split(',')
0660 ib = 1
0661
0662
0663 for ilabel in thelabels:
0664 newth.GetXaxis().SetBinLabel(ib,ilabel)
0665
0666
0667
0668 ib += 1
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686 if doFill:
0687 if thesuper[ikey].XTitle != None:
0688 newth.SetXTitle("")
0689 astack.Add(newth,"HIST")
0690 elif thesuper[ikey].Option:
0691 astack.Add(newth,thesuper[ikey].Option)
0692 else:
0693
0694 astack.Add(newth)
0695
0696 astack.SetTitle(thesuper[ikey].title)
0697
0698 if isFirst==1:
0699 newth.GetPainter().PaintStat(ROOT.gStyle.GetOptStat(),0);
0700 isFirst=0
0701 tmpsumth = newth.Clone()
0702 else:
0703 tmpsumth.Add(newth)
0704
0705
0706
0707
0708
0709
0710
0711 if dolegend and doFill:
0712 aleg.AddEntry(newth,listlegend[ii],"F")
0713 elif dolegend:
0714 aleg.AddEntry(newth,listlegend[ii],"P")
0715
0716 newth.SetName(tmpname)
0717 outputroot.cd()
0718 newth.Write()
0719 ii = ii + 1
0720
0721
0722 if thesuper[ikey].Maximum != None:
0723 astack.SetMaximum( float(thesuper[ikey].Maximum) )
0724 if thesuper[ikey].Minimum != None:
0725 astack.SetMinimum( float(thesuper[ikey].Minimum) )
0726 if thesuper[ikey].Stack == "true":
0727 astack.Draw()
0728 if thesuper[ikey].Stack == "false" or thesuper[ikey].Stack == None:
0729 astack.Draw()
0730 astack.Draw("nostack")
0731 if thesuper[ikey].XTitle != None:
0732 astack.GetHistogram().SetXTitle(thesuper[ikey].XTitle)
0733 if thesuper[ikey].YTitle != None:
0734 astack.GetHistogram().SetYTitle(thesuper[ikey].YTitle)
0735 if doFill:
0736 astack.Draw("sameaxis")
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752 if dolegend:
0753 aleg.Draw()
0754 if thesuper[ikey].SetLogy == "true":
0755 cv[thesuper[ikey].name].SetLogy()
0756 if thesuper[ikey].SetGrid == "true":
0757 cv[thesuper[ikey].name].SetGrid()
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771 if printBanner:
0772 tex = TLatex(0.35,0.95,Banner)
0773 tex.SetNDC()
0774 tex.SetTextSize(0.05)
0775 tex.Draw()
0776
0777 cv[thesuper[ikey].name].Update()
0778
0779
0780
0781 if option.wait:
0782 raw_input( 'Press ENTER to continue\n ' )
0783
0784
0785
0786
0787
0788
0789 thegraph = dh.graph
0790 if verbose : print("= Create graph histograms:")
0791 for ikey in thegraph:
0792 if verbose : print("== plot name: \""+thegraph[ikey].name+"\" title: \""+thegraph[ikey].title+"\"")
0793 listname = thegraph[ikey].histos
0794 listcolor = thegraph[ikey].color
0795 listmarker = thegraph[ikey].marker
0796 listlegend = thegraph[ikey].legend
0797 listflavour = thegraph[ikey].flavour
0798
0799 dolegend = False
0800 for il in listlegend:
0801 if il==None: dolegend = False
0802 if verbose : print("dolegend = " +str(dolegend))
0803 doNormalize = False
0804 if thegraph[ikey].Normalize == "true":
0805 doNormalize = True
0806 if verbose : print("normalize = " +str(doNormalize))
0807 projectAxis = "no"
0808 projectBin = -1
0809 if thegraph[ikey].projection == "x": projectAxis = "x"
0810 if thegraph[ikey].projection == "y": projectAxis = "y"
0811 if thegraph[ikey].bin != None: projectBin = thegraph[ikey].bin
0812 profileAxis = "no"
0813 if thegraph[ikey].profile == "x": profileAxis = "x"
0814 if thegraph[ikey].profile == "y": profileAxis = "y"
0815 doFill = False
0816 if thegraph[ikey].Fill == "true": doFill = True
0817 if verbose : print("fill option:"+ doFill)
0818
0819 cv[thegraph[ikey].name] = TCanvas(thegraph[ikey].name,thegraph[ikey].title,700,700)
0820
0821 aleg = TLegend(0.6,0.4,0.8,0.6)
0822 SetOwnership( aleg, 0 )
0823 aleg.SetMargin(0.12)
0824 aleg.SetTextSize(0.035)
0825 aleg.SetFillColor(10)
0826 aleg.SetBorderSize(0)
0827
0828 isFirst = 1
0829 ii = 0
0830
0831 stacklist[thegraph[ikey].name] = THStack("astack"+thegraph[ikey].name,thegraph[ikey].title)
0832 astack = stacklist[thegraph[ikey].name]
0833 xVal_val = TVectorD()
0834 yVal_val = TVectorD()
0835 yBin_val = std.vector(int)()
0836 xErr_val = TVectorD()
0837 yErr_val = TVectorD()
0838 zVal_val = TVectorD()
0839 zErr_val = TVectorD()
0840 nVal_val = 0
0841
0842 xVal_ref = TVectorD()
0843 yVal_ref = TVectorD()
0844 yBin_ref = std.vector(int)()
0845 xErr_ref = TVectorD()
0846 yErr_ref = TVectorD()
0847 zVal_ref = TVectorD()
0848 zErr_ref = TVectorD()
0849 nVal_ref = 0
0850
0851 RangeMax = 0.005
0852 RangeMin = 0.9
0853
0854 for ihname in listname:
0855
0856 for jkey in thedata:
0857 tmpkeys = thedata[jkey].histos.keys()
0858
0859 for tmpname in tmpkeys:
0860
0861 if tmpname == ihname:
0862
0863 ath = thedata[jkey].TH1s[tmpname]
0864 if ath is None:
0865 print("ERROR: histogram name \""+tmpname+"\" does not exist in file "+thedata[jkey].filename)
0866 exit(0)
0867 if verbose : print("=== graph histogram: "+ath.GetName() + " mean = " + "%.2f" % round(ath.GetMean(),2))
0868
0869 if listflavour[ii] == "5":
0870
0871 nBinB = 200
0872 BinWidth = (0.01+ath.GetMaximum())/nBinB
0873 BMid = 0.005+BinWidth/2
0874 Err = BinWidth
0875 for iBinB in range(1,nBinB+1):
0876
0877 BMid = BMid+Err
0878
0879 nAthBin = ath.GetNbinsX()-2
0880
0881 maxInHisto = ath.GetMaximum()
0882 minInHisto = ath.GetMinimum()
0883
0884 yClosestInit = 0
0885 iBinClosestInit = 0
0886 if BMid <= maxInHisto : yClosestInit = maxInHisto + 1
0887 else : yClosestInit = minInHisto - 1.0
0888 iBinClosest = iBinClosestInit
0889 yClosest = yClosestInit
0890 for iAthBin in range(1,nAthBin+1):
0891 yBin = ath.GetBinContent(iAthBin)
0892 dif1 = BMid-yBin
0893 if dif1 < 0 : dif1 = yBin-BMid
0894 dif2 = yClosest-BMid
0895 if dif2 < 0 : dif2 = BMid-yClosest
0896 if dif1 < dif2:
0897 yClosest = yBin
0898 iBinClosest = iAthBin
0899 min = BMid-Err/2
0900 max = BMid+Err/2
0901
0902 if yClosest < min or yClosest > max:
0903 iBinClosest = 0
0904
0905 if iBinClosest > 0 and listmarker[ii] == "8":
0906
0907 nVal_ref = nVal_ref+1
0908 xVal_ref.ResizeTo(nVal_ref)
0909
0910 xErr_ref.ResizeTo(nVal_ref)
0911 xVal_ref[nVal_ref-1] = BMid
0912 yBin_ref.push_back(iBinClosest)
0913 xErr_ref[nVal_ref-1] = ath.GetBinError ( iBinClosest )
0914 Err = xErr_ref[nVal_ref-1]
0915 if Err < BinWidth : Err = BinWidth
0916 elif iBinClosest > 0:
0917 nVal_val = nVal_val+1
0918 xVal_val.ResizeTo(nVal_val)
0919
0920 xErr_val.ResizeTo(nVal_val)
0921 xVal_val[nVal_val-1] = BMid
0922 yBin_val.push_back(iBinClosest)
0923 xErr_val[nVal_val-1] = ath.GetBinError ( iBinClosest )
0924 Err = xErr_val[nVal_val-1]
0925 if Err < BinWidth : Err = BinWidth
0926 elif listflavour[ii] == "4" and listmarker[ii] == "8":
0927 yVal_ref.ResizeTo(nVal_ref)
0928 yErr_ref.ResizeTo(nVal_ref)
0929 for iVal in range(0,nVal_ref):
0930 yVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
0931 if yVal_ref[iVal] > RangeMax : RangeMax = yVal_ref[iVal]
0932 yErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
0933 elif listflavour[ii] == "4":
0934 yVal_val.ResizeTo(nVal_val)
0935 yErr_val.ResizeTo(nVal_val)
0936 for iVal in range(0,nVal_val):
0937 yVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
0938 yErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
0939 elif listmarker[ii] == "8":
0940 zVal_ref.ResizeTo(nVal_ref)
0941 zErr_ref.ResizeTo(nVal_ref)
0942 for iVal in range(0,nVal_ref):
0943 zVal_ref[iVal] = ath.GetBinContent (yBin_ref[iVal])
0944 zErr_ref[iVal] = ath.GetBinError (yBin_ref[iVal])
0945 if zVal_ref[iVal] < RangeMin : RangeMin = zVal_ref[iVal]
0946 else:
0947 zVal_val.ResizeTo(nVal_val)
0948 zErr_val.ResizeTo(nVal_val)
0949 for iVal in range(0,nVal_val):
0950 zVal_val[iVal] = ath.GetBinContent (yBin_val[iVal])
0951 zErr_val[iVal] = ath.GetBinError (yBin_val[iVal])
0952 ii = ii + 1
0953
0954
0955
0956
0957
0958
0959
0960 graphs = [TGraphErrors(xVal_ref,yVal_ref,xErr_ref,yErr_ref),
0961 TGraphErrors(xVal_ref,zVal_ref,xErr_ref,zErr_ref),
0962 TGraphErrors(xVal_val,yVal_val,xErr_val,yErr_val),
0963 TGraphErrors(xVal_val,zVal_val,xErr_val,zErr_val)]
0964 ii = 0
0965
0966
0967
0968 for ii in range(0,4):
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991 aweight = 1
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008 col = 2
1009 mark = 22
1010 if ii == 0 or ii == 2:
1011 col = 1
1012 if ii == 0 or ii == 1:
1013 mark = 8
1014
1015 graphs[ii].SetLineColor(col)
1016 graphs[ii].SetMarkerStyle(mark)
1017 graphs[ii].SetMarkerColor(col)
1018 graphs[ii].SetTitle(thegraph[ikey].title)
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 if isFirst==1:
1069
1070 isFirst=0
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 outputroot.cd()
1090 graphs[ii].Write()
1091 ii = ii + 1
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 if thegraph[ikey].XTitle != None:
1104 graphs[0].GetHistogram().SetXTitle(thegraph[ikey].XTitle)
1105 if thegraph[ikey].YTitle != None:
1106 graphs[0].GetHistogram().SetYTitle(thegraph[ikey].YTitle)
1107
1108
1109 if RangeMax > 0.5 : RangeMax = 1.5
1110 if RangeMax < 0.5 : RangeMax = RangeMax + 0.05
1111
1112 RangeMin = RangeMin - 0.5*RangeMin
1113
1114
1115 if RangeMin < 0.00001 : RangeMin = 0.00005
1116 graphs[0].GetYaxis().SetRangeUser(RangeMin,RangeMax)
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133 graphs[0].Draw("AP")
1134 graphs[1].Draw("sameP")
1135 graphs[2].Draw("sameP")
1136 graphs[3].Draw("sameP")
1137 if dolegend:
1138 aleg.Draw()
1139 if thegraph[ikey].SetLogy == "true":
1140 cv[thegraph[ikey].name].SetLogy()
1141 if thegraph[ikey].SetGrid == "true":
1142 cv[thegraph[ikey].name].SetGrid()
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 if printBanner:
1157 tex = TLatex(0.35,0.95,Banner)
1158 tex.SetNDC()
1159 tex.SetTextSize(0.05)
1160 tex.Draw()
1161
1162 cv[thegraph[ikey].name].Update()
1163 save = thegraph[ikey].name
1164 cv[thegraph[ikey].name].Print(save + ".gif")
1165
1166
1167 if option.wait:
1168 raw_input( 'Press ENTER to continue\n ' )
1169
1170
1171
1172
1173
1174 if printCanvas:
1175
1176 for ikey in theaddition:
1177 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
1178 for ikey in thesuper:
1179 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
1180 for ikey in thegraph:
1181 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "notgood." + printFormat)
1182
1183
1184
1185
1186
1187
1188 rep = ''
1189 while not rep in [ 'q', 'Q', '.q', 'qq' 'p']:
1190 rep = raw_input( '\nenter: ["q",".q" to quit] ["p" or "print" to print all canvas]: ' )
1191 if 0<len(rep):
1192 if rep=='quit': rep = 'q'
1193 if rep=='p' or rep=='print':
1194 for ikey in theaddition:
1195 cv[theaddition[ikey].name].Print(theaddition[ikey].name + "." + printFormat)
1196 for ikey in thesuper:
1197 cv[thesuper[ikey].name].Print(thesuper[ikey].name + "." + printFormat)
1198 for ikey in thegraph:
1199 cv[thegraph[ikey].name].Print(thegraph[ikey].name + "." + printFormat)
1200
1201