Newer
Older
TB_Chris / Kepler / GangaPlugin / Lib / .svn / text-base / TbStack.py.svn-base
  1. from Ganga.GPIDev.Base import GangaObject
  2. from Ganga.GPIDev.Adapters.IPostProcessor import PostProcessException, IPostProcessor
  3. from Ganga.GPIDev.Base.Proxy import GPIProxyObject
  4. from Ganga.GPIDev.Schema import ComponentItem, FileItem, Schema, SimpleItem, Version
  5. from Ganga.Utility.Config import makeConfig, ConfigError, getConfig
  6. from Ganga.Utility.Plugin import allPlugins
  7. from Ganga.Utility.logging import getLogger, log_user_exception
  8. import commands
  9. import copy
  10. import os
  11. import string
  12. import re
  13. from ROOT import *
  14.  
  15. def displace(th1, dist=0,name=""):
  16. graph = TGraphErrors()
  17. counter=0
  18. for x in range( 0 , th1.GetNbinsX() ) :
  19. # print "Setting point %d %f %f" %( x, dist + x*th1.GetBinWidth(0), th1.GetBinContent(x) )
  20. if th1.GetBinContent(x) == 0 : continue
  21. graph.SetPoint( counter , dist + x*th1.GetBinWidth(0) , th1.GetBinContent(x) )
  22. graph.SetPointError( counter, 0, th1.GetBinError(x) )
  23. counter = counter + 1
  24. return graph
  25. class TbStack(GangaObject):
  26.  
  27. _category = 'postprocessor'
  28. _exportmethods = ['merge']
  29. _name = 'TbStack'
  30. _schema = Schema(Version(1,0), {
  31. 'Output' : SimpleItem(defvalue="Kepler-Meta-Histos.root"),
  32. 'Labels' : SimpleItem(defvalue={}),
  33. 'DrawOptions' : SimpleItem(defvalue=""),
  34. 'StackOptions' : SimpleItem(defvalue=""),
  35. 'Hist' : SimpleItem(defvalue={}),
  36. 'Displace' : SimpleItem(defvalue={}),
  37. 'Title' : SimpleItem(defvalue="")
  38. })
  39. def merge(self, job):
  40. out_file = TFile(self.Output,"UPDATE")
  41. for key in self.Hist:
  42. print "Making histogram: " + key
  43. histogram_name = self.Hist[key]
  44. stack = TMultiGraph(key, self.Title )
  45. legend = TLegend(0.8,0.3,0.995,0.4)
  46. counter=0
  47. color_counter=1
  48. xtitle=""
  49. ytitle=""
  50. for run in job.splitter.Files.keys():
  51. if run not in self.Labels.keys():
  52. counter = counter + 1
  53. continue
  54. j=job.subjobs(counter)
  55. print "Reading " + j.name + " output files"
  56. in_file = TFile(j.outputdir+"Kepler-histos.root" ,'READ')
  57. if in_file.IsOpen() == False: continue
  58. obj = in_file.Get(histogram_name)
  59. if obj == 0: continue
  60. gROOT.cd()
  61. dd = 0
  62. if run in self.Displace.keys(): dd = self.Displace[run]
  63. if self.Title == "": title = obj.GetTitle()
  64. if xtitle == "": xtitle = obj.GetXaxis().GetTitle()
  65. if ytitle == "": ytitle = obj.GetYaxis().GetTitle()
  66. thing = displace( obj, dd , j.name )
  67. thing.SetLineColor( color_counter )
  68. thing.SetLineWidth( 1 )
  69. stack.Add( thing , self.DrawOptions )
  70. legend.AddEntry( thing, self.Labels[ run ],"L")
  71. in_file.Close()
  72. del in_file
  73. counter = counter + 1
  74. color_counter = color_counter + 1
  75. stack.SetDrawOption("nostack")
  76. stack.ls()
  77. out_file.cd()
  78. stack.Write()
  79. legend.Write()
  80. c1 = TCanvas(key+"_canvas","",800,600)
  81. # c1.SetLogy()
  82. stack.Draw(self.StackOptions)
  83. print "Titles = %s %s %s" %( title, xtitle, ytitle)
  84. stack.GetXaxis().SetTitle(xtitle)
  85. stack.GetYaxis().SetTitle(ytitle)
  86. stack.SetTitle(self.Title)
  87. stack.Draw(self.StackOptions)
  88. legend.Draw()
  89. c1.Write()
  90.