Newer
Older
TB_Chris / Kepler / GangaPlugin / Lib / .svn / text-base / TbMetaAnalysisMerger.py.svn-base
  1. from ROOT import *
  2. from RecursiveSearch import RecursiveSearch, makeDirectories
  3. import os
  4. from Ganga.GPIDev.Base import GangaObject
  5. from Ganga.GPIDev.Base.Proxy import GPIProxyObjectFactory
  6. from Ganga.GPIDev.Schema import *
  7. import operator
  8.  
  9. class TbBinning2D :
  10. name = ""
  11. title = ""
  12. minY = 0
  13. widthY = 0
  14. nBinsY = 0
  15. minX = 0
  16. widthX = 0
  17. nBinsX = 0
  18. maxX = 0
  19. maxY = 0
  20. path = ""
  21. def __init__(self,name,title,path,minX,widthX,nBinsX,minY,widthY,nBinsY) :
  22. self.name = name
  23. self.title = title
  24. self.path = path
  25. self.minX = minX
  26. self.widthX = widthX
  27. self.nBinsX = nBinsX
  28. self.minY = minY
  29. self.widthY = widthY
  30. self.nBinsY = nBinsY
  31. self.maxX = self.minX + self.nBinsX*self.widthX
  32. self.maxY = self.minY + self.nBinsY*self.widthY
  33. class TbProfile :
  34. path = ""
  35. title = ""
  36. name = ""
  37. def __init__(self,name,title,path):
  38. self.title = title
  39. self.path = path
  40. self.name = name
  41.  
  42. class TbMetaAnalysisMerger(GangaObject):
  43.  
  44. _category = 'postprocessor'
  45. _exportmethods = ['merge']
  46. _name = 'TbMetaAnalysisMerger'
  47. f2L = {}
  48. _schema = Schema(Version(1,0), {
  49. 'Mode' : SimpleItem(defvalue="Profile"),
  50. 'Output' : SimpleItem(defvalue="Kepler-Meta-Histos.root"),
  51. 'Top' : SimpleItem(defvalue=[]),
  52. 'Labels' : SimpleItem(defvalue={})
  53. })
  54.  
  55. def write2D( self, files, hists ):
  56. prev_path = ""
  57. for h in hists:
  58. if h.path != prev_path :
  59. print "Making histogram for: %s" %h.path
  60. prev_path = h.path
  61. out_file = TFile.Open(self.Output,'UPDATE')
  62. hist = TH2D( h.name, h.title, h.nBinsX, h.minX, h.maxX, h.nBinsY, h.minY, h.maxY )
  63. for f in files:
  64. in_file = TFile(f ,'READ')
  65. obj = in_file.Get(h.path +"/"+ h.name )
  66. if obj :
  67. if obj.GetEntries() != 0: obj.Scale(1/obj.GetEntries());
  68. for l in range(0, obj.GetNbinsX() ):
  69. hist.SetBinContent( self.f2L[f] - h.minX, l, obj.GetBinContent(l) )
  70. del obj
  71. else :
  72. print "ERROR: %s not found in %s" %(h.path + "/" + h.name,f)
  73. in_file.Close("R")
  74. del in_file
  75. out_file.cd(h.path)
  76. hist.Write()
  77. out_file.Close("R")
  78. del out_file
  79. def write1D( self, files, graphs) :
  80. means = []
  81. sigmas = []
  82. sorted_f2L = sorted(self.f2L.items(), key=operator.itemgetter(1))
  83. p = 0
  84. n = {}
  85. for f in sorted_f2L:
  86. print f
  87. n[f[0]] = p
  88. p = p + 1
  89. print n
  90. for g in graphs:
  91. mean = TGraphErrors()
  92. mean.SetNameTitle( g.name + "_m", g.title)
  93. sigma = TGraphErrors()
  94. sigma.SetNameTitle( g.name + "_s", g.title)
  95. means.append( mean)
  96. sigmas.append( sigma )
  97. for f in files:
  98. in_file = TFile(f,'READ')
  99. for x in range(0,len(graphs)):
  100. print "Making histogram for: %s" %(g.path + "/" + g.name)
  101. g = graphs[x]
  102. obj = in_file.Get( g.path + "/" + g.name )
  103. if obj :
  104. if( obj.GetEntries() != 0 ):
  105. means[x].SetPoint( n[f] , self.f2L[f] , obj.GetMean() )
  106. sigmas[x].SetPoint( n[f], self.f2L[f] , obj.GetRMS() )
  107. means[x].SetPointError( n[f] , 0 , obj.GetRMS()/sqrt(obj.GetEntries()) )
  108. sigmas[x].SetPointError( n[f], 0 , obj.GetRMS()/sqrt(obj.GetEntries()) )
  109. in_file.Close("R")
  110. del in_file
  111. out_file = TFile.Open(self.Output,'UPDATE')
  112.  
  113. for g in range(0,len(graphs)):
  114. out_file.cd(graphs[g].path)
  115. means[g].Write()
  116. sigmas[g].Write()
  117. out_file.Close()
  118. def merge(self, list ):
  119. file0 = TFile()
  120. found = False
  121. files = []
  122. for j in list:
  123. if j.status == 'completed':
  124. fname = j.outputdir + "/Kepler-histos.root"
  125. if os.path.isfile(fname) :
  126. if found == False :
  127. file0 = TFile(fname ,'READ')
  128. found = True
  129. files.append(fname)
  130. if j.inputdata.run in self.Labels :
  131. self.f2L[fname] = self.Labels[j.inputdata.run]
  132. else :
  133. self.f2L[fname] = j.inputdata.run
  134. all_histograms = []
  135. print "Searching for histograms..."
  136. RecursiveSearch( file0, all_histograms , 7 )
  137. histograms = []
  138. for x in all_histograms:
  139. for t in self.Top:
  140. if x.find(t) != -1 :
  141. if x not in histograms : histograms.append(x)
  142. hists = []
  143. graphs = []
  144. print "Building histogram objects..."
  145. for x in histograms:
  146. obj = file0.Get( x )
  147. k = x.rfind('/')
  148. if self.Mode == "Profile":
  149. profile = TbProfile(x[k+1:], obj.GetTitle(), x[:k] )
  150. graphs.append( profile )
  151. if self.Mode == "2D":
  152. Runs = job.inputdata.Runs
  153. hists.append( TbBinning2D( x[k+1:],obj.GetTitle(),x[:k],Runs[0],1,Runs[len(Runs)-1] - Runs[0] +1,
  154. obj.GetBinLowEdge(0), obj.GetBinWidth(0), obj.GetNbinsX() ) )
  155. file0.Close()
  156. del file0
  157. output = TFile.Open(self.Output,'RECREATE')
  158. output.Close("R")
  159. del output
  160. makeDirectories( self.Output, histograms )
  161. print "Filling %d histograms / graphs" %(len( hists ) + len(graphs ) )
  162. if self.Mode == "2D": self.write2D( files, hists )
  163. if self.Mode == "Profile": self.write1D( files, graphs )