Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-13 03:24:17

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import os
0005 import argparse
0006 from time import time
0007 
0008 from Validation.HGCalValidation.PostProcessorHGCAL_cfi import tracksterLabels as trackstersIters
0009 
0010 from Validation.RecoTrack.plotting.validation import SeparateValidation, SimpleValidation, SimpleSample
0011 from Validation.HGCalValidation.HGCalValidator_cfi import hgcalValidator
0012 import Validation.HGCalValidation.hgcalPlots as hgcalPlots
0013 import Validation.RecoTrack.plotting.plotting as plotting
0014 
0015 simClustersIters = [hgcalValidator.label_SimClustersLevel._InputTag__moduleLabel, "ticlSimTracksters"]
0016 
0017 hitCalLabel = 'hitCalibration'
0018 hitValLabel = 'hitValidation'
0019 layerClustersLabel = 'layerClusters'
0020 trackstersLabel = 'tracksters'
0021 trackstersWithEdgesLabel = 'trackstersWithEdges'
0022 candidatesLabel = 'candidates'
0023 simLabel = 'simulation'
0024 allLabel = 'all'
0025 
0026 collection_choices = [allLabel]
0027 collection_choices.extend([hitCalLabel]+[hitValLabel]+[layerClustersLabel]+[trackstersLabel]+[trackstersWithEdgesLabel]+[candidatesLabel]+[simLabel])
0028 
0029 def main(opts):
0030 
0031     drawArgs={}
0032     extendedFlag = False
0033     if opts.no_ratio:
0034         drawArgs["ratio"] = False
0035     if opts.separate:
0036         drawArgs["separate"] = True
0037     if opts.png:
0038         drawArgs["saveFormat"] = ".png"
0039     if opts.extended:
0040         extendedFlag = True
0041     if opts.verbose:
0042         plotting.verbose = True
0043 
0044     filenames = [(f, f.replace(".root", "")) for f in opts.files]
0045     sample = SimpleSample(opts.subdirprefix[0], opts.html_sample, filenames)
0046 
0047     val = SimpleValidation([sample], opts.outputDir[0], nProc=opts.jobs)
0048     if opts.separate:
0049         val = SeparateValidation([sample], opts.outputDir[0])
0050     htmlReport = val.createHtmlReport(validationName=opts.html_validation_name[0])
0051 
0052     #layerClusters
0053     def plot_LC():
0054         hgclayclus = [hgcalPlots.hgcalLayerClustersPlotter]
0055         hgcalPlots.append_hgcalLayerClustersPlots(hgcalValidator.label_layerClusterPlots._InputTag__moduleLabel, "Layer Clusters", extendedFlag)
0056         val.doPlots(hgclayclus, plotterDrawArgs=drawArgs)
0057 
0058     #simClusters
0059     def plot_SC():
0060         hgcsimclus = [hgcalPlots.hgcalSimClustersPlotter]
0061         for i_iter in simClustersIters:
0062             hgcalPlots.append_hgcalSimClustersPlots(i_iter, i_iter)
0063         val.doPlots(hgcsimclus, plotterDrawArgs=drawArgs)
0064 
0065     #tracksters
0066     def plot_Tst():
0067         hgctrackster = [hgcalPlots.hgcalTrackstersPlotter]
0068         for tracksterCollection in trackstersIters :
0069             hgcalPlots.append_hgcalTrackstersPlots(tracksterCollection, tracksterCollection)
0070         val.doPlots(hgctrackster, plotterDrawArgs=drawArgs)
0071 
0072     #trackstersWithEdges
0073     def plot_TstEdges():
0074         plot_Tst()
0075         for tracksterCollection in trackstersIters :
0076             hgctracksters = [hgcalPlots.create_hgcalTrackstersPlotter(sample.files(), tracksterCollection, tracksterCollection)]
0077             val.doPlots(hgctracksters, plotterDrawArgs=drawArgs)
0078 
0079     #caloParticles
0080     def plot_CP():
0081         particletypes = {"pion-":"-211", "pion+":"211", "pion0": "111",
0082                          "muon-": "-13", "muon+":"13",
0083                          "electron-": "-11", "electron+": "11", "photon": "22",
0084                          "kaon0L": "310", "kaon0S": "130",
0085                          "kaon-": "-321", "kaon+": "321"}
0086         hgcaloPart = [hgcalPlots.hgcalCaloParticlesPlotter]
0087         for i_part, i_partID in particletypes.items() :
0088             hgcalPlots.append_hgcalCaloParticlesPlots(sample.files(), i_partID, i_part)
0089         val.doPlots(hgcaloPart, plotterDrawArgs=drawArgs)
0090 
0091     #hitValidation
0092     def plot_hitVal():
0093         hgchit = [hgcalPlots.hgcalHitPlotter]
0094         hgcalPlots.append_hgcalHitsPlots('HGCalSimHitsV', "Simulated Hits")
0095         hgcalPlots.append_hgcalHitsPlots('HGCalRecHitsV', "Reconstruced Hits")
0096         hgcalPlots.append_hgcalDigisPlots('HGCalDigisV', "Digis")
0097         val.doPlots(hgchit, plotterDrawArgs=drawArgs)
0098 
0099     #hitCalibration
0100     def plot_hitCal():
0101         hgchitcalib = [hgcalPlots.hgcalHitCalibPlotter]
0102         val.doPlots(hgchitcalib, plotterDrawArgs=drawArgs)
0103 
0104     def plotCand():
0105         ticlcand = [hgcalPlots.hgcalTICLCandPlotter]
0106         val.doPlots(ticlcand, plotterDrawArgs=drawArgs)
0107 
0108     plotDict = {hitCalLabel:[plot_hitCal], hitValLabel:[plot_hitVal], layerClustersLabel:[plot_LC], trackstersLabel:[plot_Tst], trackstersWithEdgesLabel:[plot_TstEdges], simLabel:[plot_SC, plot_CP], candidatesLabel:[plotCand]}
0109 
0110     if (opts.collection != allLabel):
0111         for task in plotDict[opts.collection]:
0112             task()
0113     else:
0114         for label in plotDict:
0115             if (label == trackstersLabel): continue # already run in trackstersWithEdges
0116             for task in plotDict[label]:
0117                 task()
0118 
0119     if opts.no_html:
0120         print("Plots created into directory '%s'." % opts.outputDir)
0121     else:
0122         htmlReport.write()
0123 
0124         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)))
0125 
0126 if __name__ == "__main__":
0127     parser = argparse.ArgumentParser(description="Create set of HGCal validation plots from one or more DQM files.")
0128     parser.add_argument("files", metavar="file", type=str, nargs="+",
0129                         default = "DQM_V0001_R000000001__Global__CMSSW_X_Y_Z__RECO.root",
0130                         help="DQM file to plot the validation plots from")
0131     parser.add_argument("-o", "--outputDir", type=str, default=["plots1","plots2"], nargs="+",
0132                         help="Plot output directories (default: 'plots1'")
0133     parser.add_argument("--subdirprefix", type=str, default=["plots1","plots2"], nargs="+",
0134                         help="Prefix for subdirectories inside outputDir (default: 'plots1')")
0135     parser.add_argument("--no-ratio", action="store_true", default = False,
0136                         help="Disable ratio pads")
0137     parser.add_argument("--separate", action="store_true", default = False,
0138                         help="Save all plots separately instead of grouping them")
0139     parser.add_argument("--png", action="store_true", default = True,
0140                         help="Save plots in PNG instead of PDF")
0141     parser.add_argument("--no-html", action="store_true", default = False,
0142                         help="Disable HTML page generation")
0143     parser.add_argument("--html-sample", default="Sample",
0144                         help="Sample name for HTML page generation (default 'Sample')")
0145     parser.add_argument("--html-validation-name", type=str, default=["",""], nargs="+",
0146                         help="Validation name for HTML page generation (enters to <title> element) (default '')")
0147     parser.add_argument("--collection", choices=collection_choices, default=layerClustersLabel,
0148                         help="Choose output plots collections among possible choices")
0149     parser.add_argument("--extended", action="store_true", default = False,
0150                         help="Include extended set of plots (e.g. bunch of distributions; default off)")
0151     parser.add_argument("--jobs", default=0, type=int,
0152                         help="Number of jobs to run in parallel for generating plots. Default is 0 i.e. run number of cpu cores jobs.")
0153     parser.add_argument("--verbose", action="store_true", default = False,
0154                         help="Be verbose")
0155 
0156     opts = parser.parse_args()
0157 
0158     for f in opts.files:
0159         if not os.path.exists(f):
0160             parser.error("DQM file %s does not exist" % f)
0161 
0162     main(opts)