Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-25 04:54:50

0001 ##########################################################################
0002 # Draw the plots saved in the millePedeMonitor_merge.root file
0003 #
0004 
0005 import logging
0006 import os
0007 try:
0008     import cPickle as pickle
0009 except:
0010     import pickle
0011 
0012 import ROOT
0013 ROOT.PyConfig.IgnoreCommandLineOptions = True
0014 ROOT.gROOT.SetBatch()
0015 
0016 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.style as mpsv_style
0017 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
0018 
0019 
0020 def plot(config):
0021     logger = logging.getLogger("mpsvalidate")
0022     
0023     # adjust the plot style
0024     # show the skewness in the legend
0025     ROOT.gStyle.SetOptStat("emrs")
0026     ROOT.gStyle.SetPadLeftMargin(0.07)
0027 
0028     # retrieve the weights of the different datasets
0029     with open(os.path.join(config.jobDataPath, ".weights.pkl"), "rb") as f:
0030         weight_conf = pickle.load(f)
0031 
0032     # loop over all millepedemonitor_X.root files
0033     for filename in os.listdir("{0}".format(config.jobDataPath)):
0034         if (filename.endswith(".root") and filename.startswith("millepedemonitor_")):
0035             # get X out of millepedemonitor_X.root files
0036             inputname = os.path.splitext(filename.split("millepedemonitor_")[-1])[0]
0037 
0038             # open file
0039             rootfile = ROOT.TFile(os.path.join(config.jobDataPath, filename))
0040 
0041             plotPaths = ["usedTrackHists/usedptTrack", "usedTrackHists/usedetaTrack",
0042                          "usedTrackHists/usedphiTrack", "usedTrackHists/usednHitTrack"]
0043 
0044             # loop over plots which should be plotted
0045             for plotNumber, plotPath in enumerate(plotPaths):
0046                 # get plotname
0047                 plotName = plotPath.split("/")[1]
0048                 # get plot
0049                 plot = rootfile.Get(plotPath)
0050 
0051                 if (plotNumber == 0):
0052                     # get number of used tracks
0053                     ntracks = int(plot.GetEntries())
0054                     weight = [item[1]
0055                               for item in weight_conf
0056                               if item[0] == inputname][0]
0057                     mpsv_classes.MonitorData(inputname.replace("_", " "), ntracks, weight)
0058 
0059                 # create canvas
0060                 canvas = ROOT.TCanvas("canvas{0}_{1}".format(
0061                     inputname, plotName), "Monitor", 300, 0, 800, 600)
0062                 canvas.cd()
0063 
0064                 # set statistics size
0065                 mpsv_style.setstatsize(canvas, plot, config)
0066 
0067                 # draw
0068                 plot.Draw()
0069 
0070                 # save as pdf
0071                 canvas.Print(
0072                     "{0}/plots/pdf/monitor_{1}_{2}.pdf".format(config.outputPath, inputname.replace(".","_"), plotName))
0073 
0074                 # export as png
0075                 image = ROOT.TImage.Create()
0076                 image.FromPad(canvas)
0077                 image.WriteImage(
0078                     "{0}/plots/png/monitor_{1}_{2}.png".format(config.outputPath, inputname.replace(".","_"), plotName))
0079 
0080                 # add to output list
0081                 output = mpsv_classes.OutputData(plottype="monitor", name=inputname.replace("_", " "), number=plotName, filename="monitor_{1}_{2}".format(
0082                     config.outputPath, inputname.replace(".","_"), plotName))
0083                 config.outputList.append(output)
0084 
0085     # reset the plot style
0086     ROOT.gStyle.SetOptStat(0)
0087     ROOT.gStyle.SetPadLeftMargin(0.17)