CMS 3D CMS Logo

makeHGCalValidationPlots.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 import os
5 import argparse
6 from time import time
7 
8 from Validation.HGCalValidation.PostProcessorHGCAL_cfi import tracksterLabels as trackstersIters
9 
10 from Validation.RecoTrack.plotting.validation import SeparateValidation, SimpleValidation, SimpleSample
11 from Validation.HGCalValidation.HGCalValidator_cfi import hgcalValidator
12 import Validation.HGCalValidation.hgcalPlots as hgcalPlots
13 import Validation.RecoTrack.plotting.plotting as plotting
14 
15 simClustersIters = [hgcalValidator.label_SimClustersLevel._InputTag__moduleLabel, "ticlSimTracksters"]
16 
17 hitCalLabel = 'hitCalibration'
18 hitValLabel = 'hitValidation'
19 layerClustersLabel = 'layerClusters'
20 trackstersLabel = 'tracksters'
21 trackstersWithEdgesLabel = 'trackstersWithEdges'
22 candidatesLabel = 'candidates'
23 simLabel = 'simulation'
24 allLabel = 'all'
25 
26 collection_choices = [allLabel]
27 collection_choices.extend([hitCalLabel]+[hitValLabel]+[layerClustersLabel]+[trackstersLabel]+[trackstersWithEdgesLabel]+[candidatesLabel]+[simLabel])
28 
29 def main(opts):
30 
31  drawArgs={}
32  extendedFlag = False
33  if opts.no_ratio:
34  drawArgs["ratio"] = False
35  if opts.separate:
36  drawArgs["separate"] = True
37  if opts.png:
38  drawArgs["saveFormat"] = ".png"
39  if opts.extended:
40  extendedFlag = True
41  if opts.verbose:
42  plotting.verbose = True
43 
44  filenames = [(f, f.replace(".root", "")) for f in opts.files]
45  sample = SimpleSample(opts.subdirprefix[0], opts.html_sample, filenames)
46 
47  val = SimpleValidation([sample], opts.outputDir[0], nProc=opts.jobs)
48  if opts.separate:
49  val = SeparateValidation([sample], opts.outputDir[0])
50  htmlReport = val.createHtmlReport(validationName=opts.html_validation_name[0])
51 
52  #layerClusters
53  def plot_LC():
54  hgclayclus = [hgcalPlots.hgcalLayerClustersPlotter]
55  hgcalPlots.append_hgcalLayerClustersPlots(hgcalValidator.label_layerClusterPlots._InputTag__moduleLabel, "Layer Clusters", extendedFlag)
56  val.doPlots(hgclayclus, plotterDrawArgs=drawArgs)
57 
58  #simClusters
59  def plot_SC():
60  hgcsimclus = [hgcalPlots.hgcalSimClustersPlotter]
61  for i_iter in simClustersIters:
63  val.doPlots(hgcsimclus, plotterDrawArgs=drawArgs)
64 
65  #tracksters
66  def plot_Tst():
67  hgctrackster = [hgcalPlots.hgcalTrackstersPlotter]
68  for tracksterCollection in trackstersIters :
69  hgcalPlots.append_hgcalTrackstersPlots(tracksterCollection, tracksterCollection)
70  val.doPlots(hgctrackster, plotterDrawArgs=drawArgs)
71 
72  #trackstersWithEdges
73  def plot_TstEdges():
74  plot_Tst()
75  for tracksterCollection in trackstersIters :
76  hgctracksters = [hgcalPlots.create_hgcalTrackstersPlotter(sample.files(), tracksterCollection, tracksterCollection)]
77  val.doPlots(hgctracksters, plotterDrawArgs=drawArgs)
78 
79  #caloParticles
80  def plot_CP():
81  particletypes = {"pion-":"-211", "pion+":"211", "pion0": "111",
82  "muon-": "-13", "muon+":"13",
83  "electron-": "-11", "electron+": "11", "photon": "22",
84  "kaon0L": "310", "kaon0S": "130",
85  "kaon-": "-321", "kaon+": "321"}
86  hgcaloPart = [hgcalPlots.hgcalCaloParticlesPlotter]
87  for i_part, i_partID in particletypes.items() :
88  hgcalPlots.append_hgcalCaloParticlesPlots(sample.files(), i_partID, i_part)
89  val.doPlots(hgcaloPart, plotterDrawArgs=drawArgs)
90 
91  #hitValidation
92  def plot_hitVal():
93  hgchit = [hgcalPlots.hgcalHitPlotter]
94  hgcalPlots.append_hgcalHitsPlots('HGCalSimHitsV', "Simulated Hits")
95  hgcalPlots.append_hgcalHitsPlots('HGCalRecHitsV', "Reconstruced Hits")
96  hgcalPlots.append_hgcalDigisPlots('HGCalDigisV', "Digis")
97  val.doPlots(hgchit, plotterDrawArgs=drawArgs)
98 
99  #hitCalibration
100  def plot_hitCal():
101  hgchitcalib = [hgcalPlots.hgcalHitCalibPlotter]
102  val.doPlots(hgchitcalib, plotterDrawArgs=drawArgs)
103 
104  def plotCand():
105  ticlcand = [hgcalPlots.hgcalTICLCandPlotter]
106  val.doPlots(ticlcand, plotterDrawArgs=drawArgs)
107 
108  plotDict = {hitCalLabel:[plot_hitCal], hitValLabel:[plot_hitVal], layerClustersLabel:[plot_LC], trackstersLabel:[plot_Tst], trackstersWithEdgesLabel:[plot_TstEdges], simLabel:[plot_SC, plot_CP], candidatesLabel:[plotCand]}
109 
110  if (opts.collection != allLabel):
111  for task in plotDict[opts.collection]:
112  task()
113  else:
114  for label in plotDict:
115  if (label == trackstersLabel): continue # already run in trackstersWithEdges
116  for task in plotDict[label]:
117  task()
118 
119  if opts.no_html:
120  print("Plots created into directory '%s'." % opts.outputDir)
121  else:
122  htmlReport.write()
123 
124  print("Plots and HTML report created into directory '%s'. You can just move it to some www area and access the pages via web browser" % (','.join(opts.outputDir)))
125 
126 if __name__ == "__main__":
127  parser = argparse.ArgumentParser(description="Create set of HGCal validation plots from one or more DQM files.")
128  parser.add_argument("files", metavar="file", type=str, nargs="+",
129  default = "DQM_V0001_R000000001__Global__CMSSW_X_Y_Z__RECO.root",
130  help="DQM file to plot the validation plots from")
131  parser.add_argument("-o", "--outputDir", type=str, default=["plots1","plots2"], nargs="+",
132  help="Plot output directories (default: 'plots1'")
133  parser.add_argument("--subdirprefix", type=str, default=["plots1","plots2"], nargs="+",
134  help="Prefix for subdirectories inside outputDir (default: 'plots1')")
135  parser.add_argument("--no-ratio", action="store_true", default = False,
136  help="Disable ratio pads")
137  parser.add_argument("--separate", action="store_true", default = False,
138  help="Save all plots separately instead of grouping them")
139  parser.add_argument("--png", action="store_true", default = True,
140  help="Save plots in PNG instead of PDF")
141  parser.add_argument("--no-html", action="store_true", default = False,
142  help="Disable HTML page generation")
143  parser.add_argument("--html-sample", default="Sample",
144  help="Sample name for HTML page generation (default 'Sample')")
145  parser.add_argument("--html-validation-name", type=str, default=["",""], nargs="+",
146  help="Validation name for HTML page generation (enters to <title> element) (default '')")
147  parser.add_argument("--collection", choices=collection_choices, default=layerClustersLabel,
148  help="Choose output plots collections among possible choices")
149  parser.add_argument("--extended", action="store_true", default = False,
150  help="Include extended set of plots (e.g. bunch of distributions; default off)")
151  parser.add_argument("--jobs", default=0, type=int,
152  help="Number of jobs to run in parallel for generating plots. Default is 0 i.e. run number of cpu cores jobs.")
153  parser.add_argument("--verbose", action="store_true", default = False,
154  help="Be verbose")
155 
156  opts = parser.parse_args()
157 
158  for f in opts.files:
159  if not os.path.exists(f):
160  parser.error("DQM file %s does not exist" % f)
161 
162  main(opts)
def append_hgcalHitsPlots(collection="HGCalSimHitsV", name_collection="Simulated Hits")
Definition: hgcalPlots.py:2779
def append_hgcalTrackstersPlots(collection='ticlTrackstersMerge', name_collection="TrackstersMerge")
Definition: hgcalPlots.py:2542
def create_hgcalTrackstersPlotter(files, collection='ticlTrackstersMerge', name_collection="TrackstersMerge")
Definition: hgcalPlots.py:2644
def append_hgcalDigisPlots(collection="HGCalDigisV", name_collection="Digis")
Definition: hgcalPlots.py:2848
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
Definition: main.py:1
def append_hgcalCaloParticlesPlots(files, collection='-211', name_collection="pion-")
Definition: hgcalPlots.py:2595
def append_hgcalSimClustersPlots(collection, name_collection)
Definition: hgcalPlots.py:2477
def append_hgcalLayerClustersPlots(collection=hgcalValidator.label_layerClusterPlots._InputTag__moduleLabel, name_collection=layerClustersLabel, extended=False)
Definition: hgcalPlots.py:2339