CMS 3D CMS Logo

monitorPlot.py
Go to the documentation of this file.
1 
4 
5 import logging
6 import os
7 try:
8  import cPickle as pickle
9 except:
10  import pickle
11 
12 import ROOT
13 ROOT.PyConfig.IgnoreCommandLineOptions = True
14 ROOT.gROOT.SetBatch()
15 
16 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
17 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
18 
19 
20 def plot(config):
21  logger = logging.getLogger("mpsvalidate")
22 
23  # adjust the plot style
24  # show the skewness in the legend
25  ROOT.gStyle.SetOptStat("emrs")
26  ROOT.gStyle.SetPadLeftMargin(0.07)
27 
28  # retrieve the weights of the different datasets
29  with open(os.path.join(config.jobDataPath, ".weights.pkl"), "rb") as f:
30  weight_conf = pickle.load(f)
31 
32  # loop over all millepedemonitor_X.root files
33  for filename in os.listdir("{0}".format(config.jobDataPath)):
34  if (filename.endswith(".root") and filename.startswith("millepedemonitor_")):
35  # get X out of millepedemonitor_X.root files
36  inputname = os.path.splitext(filename.split("millepedemonitor_")[-1])[0]
37 
38  # open file
39  rootfile = ROOT.TFile(os.path.join(config.jobDataPath, filename))
40 
41  plotPaths = ["usedTrackHists/usedptTrack", "usedTrackHists/usedetaTrack",
42  "usedTrackHists/usedphiTrack", "usedTrackHists/usednHitTrack"]
43 
44  # loop over plots which should be plotted
45  for plotNumber, plotPath in enumerate(plotPaths):
46  # get plotname
47  plotName = plotPath.split("/")[1]
48  # get plot
49  plot = rootfile.Get(plotPath)
50 
51  if (plotNumber == 0):
52  # get number of used tracks
53  ntracks = int(plot.GetEntries())
54  weight = [item[1]
55  for item in weight_conf
56  if item[0] == inputname][0]
57  mpsv_classes.MonitorData(inputname.replace("_", " "), ntracks, weight)
58 
59  # create canvas
60  canvas = ROOT.TCanvas("canvas{0}_{1}".format(
61  inputname, plotName), "Monitor", 300, 0, 800, 600)
62  canvas.cd()
63 
64  # set statistics size
65  mpsv_style.setstatsize(canvas, plot, config)
66 
67  # draw
68  plot.Draw()
69 
70  # save as pdf
71  canvas.Print(
72  "{0}/plots/pdf/monitor_{1}_{2}.pdf".format(config.outputPath, inputname.replace(".","_"), plotName))
73 
74  # export as png
75  image = ROOT.TImage.Create()
76  image.FromPad(canvas)
77  image.WriteImage(
78  "{0}/plots/png/monitor_{1}_{2}.png".format(config.outputPath, inputname.replace(".","_"), plotName))
79 
80  # add to output list
81  output = mpsv_classes.OutputData(plottype="monitor", name=inputname.replace("_", " "), number=plotName, filename="monitor_{1}_{2}".format(
82  config.outputPath, inputname.replace(".","_"), plotName))
83  config.outputList.append(output)
84 
85  # reset the plot style
86  ROOT.gStyle.SetOptStat(0)
87  ROOT.gStyle.SetPadLeftMargin(0.17)
def plot(config)
Definition: monitorPlot.py:20