00001
00002
00003
00004
00005
00006
00007
00008
00009 class FigureGenerator:
00010 """Figure Generator Class"""
00011 def __init__(self, filename, fileRoot, diskRoot):
00012 "Constructor"
00013 self.filename = filename
00014 self.fileRoot = fileRoot
00015 self.diskRoot = diskRoot
00016 self.file = None
00017 self.format = "png"
00018 return
00019
00020 def process(self):
00021 """Generate directories and figures"""
00022 if not self.openFile():
00023 return
00024 self.processPath("")
00025 return
00026
00027 def openFile(self):
00028 "Open root file"
00029 import ROOT
00030 gROOT = ROOT.gROOT
00031 gROOT.SetBatch(True)
00032
00033 ROOT.gErrorIgnoreLevel = ROOT.kInfo
00034 gROOT.SetStyle("Plain")
00035 ROOT.gStyle.SetTitleFillColor(0)
00036 ROOT.gStyle.SetTitleBorderSize(0)
00037 ROOT.gStyle.SetPalette(1)
00038
00039
00040 ROOT.gSystem.Setenv("TZ","PRC")
00041 rootFile = ROOT.TFile(self.filename)
00042 if not rootFile:
00043 print "Failed to open root file:",filename
00044 return False
00045 self.file = rootFile
00046 self.canvas = ROOT.TCanvas()
00047 return True
00048
00049 def processPath(self, itemPath):
00050 """Process a path in the current root file"""
00051
00052 filePath = self.fileRoot
00053 diskPath = self.diskRoot
00054 if len(itemPath)>0:
00055 filePath += "/"+itemPath
00056 diskPath += "/"+itemPath
00057 content = self.file.Get(filePath)
00058 if not content and itemPath=="":
00059 print "FIXME: Sim data has no run number"
00060 self.fileRoot = self.fileRoot[:-7] + "0000000"
00061 filePath = self.fileRoot
00062 content = self.file.Get(filePath)
00063 if not content:
00064 print "Failed to get item at: ", filePath
00065 return
00066 if content.IsA().InheritsFrom("TH1"):
00067
00068 if content.IsA().InheritsFrom("TH2F"):
00069
00070 content.Draw("colz");
00071 elif content.IsA().InheritsFrom("TH1F"):
00072
00073 self.canvas.SetLogy(1)
00074 content.Draw();
00075 else:
00076 content.Draw();
00077 self.canvas.SaveAs(diskPath+"."+self.format)
00078 self.canvas.SetLogy(0)
00079 elif content.IsA().InheritsFrom("TGraph"):
00080
00081 content.Draw("APL");
00082 self.canvas.SaveAs(diskPath+"."+self.format)
00083 elif content.IsA().InheritsFrom("TDirectory"):
00084 self.makeDirectory(diskPath)
00085 keylist = content.GetListOfKeys()
00086 for key in keylist:
00087 dirItem = key.ReadObj()
00088 dirItemPath = None
00089 if len(itemPath)>0:
00090 dirItemPath = itemPath+"/"+dirItem.GetName()
00091 else:
00092 dirItemPath = dirItem.GetName()
00093 self.processPath(dirItemPath)
00094 else:
00095
00096 print "Not handing %s of type %s" % (filepath,content.ClassName())
00097 return
00098
00099 def makeDirectory(self, diskPath):
00100 """Make sure the output directory exists"""
00101 import os
00102 os.system("mkdir -p "+diskPath)
00103 return
00104
00105 if __name__ == "__main__":
00106
00107 fileRoot = "stats"
00108 diskRoot = "."
00109 filename = None
00110 import sys, getopt
00111 opts, args = getopt.getopt(sys.argv[1:],"o:r:")
00112 for opt, arg in opts:
00113 if opt == "-o":
00114 diskRoot = arg
00115 elif opt == "-r":
00116 fileRoot = arg
00117 print args
00118 filename = args[0]
00119 print "Filename: ",filename
00120 print "File Root: ",fileRoot
00121 print "Disk Root: ",diskRoot
00122 figGen = FigureGenerator(filename, fileRoot, diskRoot)
00123 figGen.process()
00124 print "Done with figure generation"
00125
00126