Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:39:30

0001 ##########################################################################
0002 # Creates pdf out of the histograms, parsed data and a given template.
0003 ##
0004 
0005 from builtins import range
0006 import logging
0007 import os
0008 import string
0009 import Alignment.MillePedeAlignmentAlgorithm.mpsvalidate.classes as mpsv_classes
0010 
0011 
0012 # create class to have delimiter %% which is not used in latex
0013 
0014 
0015 class TexTemplate(string.Template):
0016     delimiter = "%%"
0017 
0018 
0019 def create(alignables, pedeDump, additionalData, outputFile, config):
0020     logger = logging.getLogger("mpsvalidate")
0021 
0022     # load template
0023     with open(os.path.join(config.mpspath, "templates",
0024                            "mpsvalidate_tex_template.tex")) as template:
0025         data = template.read()
0026         template.close()
0027 
0028     # create object where data could be substituted
0029     data = TexTemplate(data)
0030 
0031     # output string
0032     out = ""
0033     
0034     # title page
0035     if (config.message):
0036         out += """\\begin{{titlepage}}
0037                     \centering
0038                     \\vspace*{{4cm}}
0039                     \Huge\\bfseries Alignment Validation\par
0040                     \\vspace{{2cm}} 
0041                     \scshape\huge Alignment Campaign\\\\ {{{0}}}\par
0042                     \\vfill
0043                     \large \\today\par
0044                     \\end{{titlepage}}
0045                     \\tableofcontents
0046                     \\newpage""".format(config.message)
0047     else:
0048         out += """\\begin{titlepage}
0049                     \centering
0050                     \\vspace*{4cm}
0051                     \Huge\\bfseries Alignment Validation\par
0052                     \\vfill
0053                     \large \\today\par
0054                     \\end{titlepage}
0055                     \\tableofcontents
0056                     \\newpage"""
0057 
0058     # general information
0059 
0060     out += "\section{{General information}}\n"
0061 
0062     if (config.message):
0063         out += "Project: {{{0}}}\\\\\n".format(config.message)
0064     out += "Input-Path:\n"
0065     out += "\\begin{verbatim}\n"
0066     out += config.jobDataPath+"\n"
0067     out += "\\end{verbatim}\n"
0068 
0069     # alignment_merge.py
0070     try:
0071         out += "\subsection{Alignment Configuration}\n"
0072         out += "\\textbf{{PedeSteerer method:}} {{{0}}}\\\\\n".format(
0073             additionalData.pede_steerer_method)
0074         out += "\\textbf{{PedeSteerer options:}}\\\\\n"
0075         for line in additionalData.pede_steerer_options:
0076             out += "{{{0}}}\\\\\n".format(line)
0077         out += "\\textbf{{PedeSteerer command:}} {0}\\\\\n".format(
0078             additionalData.pede_steerer_command)
0079 
0080         for i in sorted(additionalData.selectors):
0081             out += "\\textbf{{{0}:}}\n".format(additionalData.selectors[i]["name"])
0082             out += "\\begin{verbatim}\n"
0083             for line in additionalData.selectors[i]["selector"].dumpPython().split("\n"):
0084                 out += line + "\n"
0085             out += "\\end{verbatim}\n"
0086 
0087         if len(additionalData.iov_definition) > 0:
0088             out += "\\textbf{{IOV defintion:}}\n"
0089             out += "\\begin{verbatim}\n"
0090             for line in additionalData.iov_definition.dumpPython().split("\n"):
0091                 out += line + "\n"
0092             out += "\\end{verbatim}\n\n"
0093         out += "\n"
0094 
0095     except Exception as e:
0096         logger.error("data not found - {0} {1}".format(type(e), e))
0097 
0098     # table of input files with number of tracks
0099     if config.showmonitor:
0100         out += "\subsection{Datasets with tracks}\n"
0101         out += """\\begin{table}[h]
0102             \centering
0103             \caption{Datasets with tracks}
0104             \\begin{tabular}{ccc}
0105             \hline
0106             Dataset & Number of used tracks & Weight \\\\
0107             \hline \n"""
0108         for monitor in mpsv_classes.MonitorData.monitors:
0109             out += "{0} & {1} & {2}\\\\\n".format(monitor.name, monitor.ntracks,
0110                                                   monitor.weight if monitor.weight != None else "--")
0111         try:
0112             if (pedeDump.nrec):
0113                 out += "\hline\nNumber of records & {0}\\\\\n".format(pedeDump.nrec)
0114         except Exception as e:
0115             logger.error("data not found - {0} {1}".format(type(e), e))
0116         out += """\hline
0117                   \end{tabular}\n
0118                   \end{table}\n"""
0119         out += "The information in this table is based on the monitor root files. Note that the number of tracks which where used in the pede step can differ from this table.\n"
0120     try:
0121         # pede.dump.gz
0122         if config.showdump:
0123             out += "\subsection{{Pede monitoring information}}\n"
0124             if (pedeDump.sumValue != 0):
0125                 out += r"\begin{{align*}}Sum(Chi^2)/Sum(Ndf) &= {0}\\ &= {1}\end{{align*}}".format(
0126                     pedeDump.sumSteps, pedeDump.sumValue)
0127             else:
0128                 out += r"\begin{{align*}}Sum(W*Chi^2)/Sum(Ndf)/<W> &= {0}\\ &= {1}\end{{align*}}".format(
0129                     pedeDump.sumSteps, pedeDump.sumWValue)
0130             out += r"with correction for down-weighting: {0}\\".format(
0131                 pedeDump.correction)
0132             out += r"Peak dynamic memory allocation: {0} GB\\".format(
0133                 pedeDump.memory)
0134             out += r"Total time: {0} h {1} m {2} s\\".format(
0135                 pedeDump.time[0], pedeDump.time[1], pedeDump.time[2])
0136             out += r"Number of records: {0}\\".format(pedeDump.nrec)
0137             out += r"Total number of parameters: {0}\\".format(pedeDump.ntgb)
0138             out += r"Number of variable parameters: {0}\\".format(pedeDump.nvgb)
0139             out += r"Warning:\\"
0140             for line in pedeDump.warning:
0141 
0142                 # check if line empty
0143                 if line.replace(r" ", r""):
0144                     out += "\\begin{verbatim}\n"
0145                     out += line + "\n"
0146                     out += "\\end{verbatim}\n"
0147 
0148             out += "\section{{Parameter plots}}\n"
0149     except Exception as e:
0150         logger.error("data not found - {0} {1}".format(type(e), e))
0151 
0152     # high level structures
0153     if config.showhighlevel:
0154         big = [x for x in config.outputList if (x.plottype == "big")]
0155 
0156         if big:
0157             out += "\subsection{{High-level parameters}}\n"
0158             for i in big:
0159                 out += "\includegraphics[width=\linewidth]{{{0}/plots/pdf/{1}.pdf}}\n".format(
0160                     config.outputPath, i.filename)
0161 
0162     # time (IOV) dependent plots
0163     if config.showtime:
0164         time = [x for x in config.outputList if (x.plottype == "time")]
0165 
0166         if time:
0167             out += "\subsection{{High-level parameters versus time (IOV)}}\n"
0168             # get list with names of the structures
0169             for structure in [x.name for x in time if x.parameter == "xyz"]:
0170                 out += "\subsubsection{{{0}}}\n".format(structure)
0171                 for mode in ["xyz", "rot"]:
0172                     if any([x.filename for x in time if (x.parameter == mode and x.name == structure)]):
0173                         filename = [x.filename for x in time if (x.parameter == mode and x.name == structure)][0]
0174                         out += "\includegraphics[width=\linewidth]{{{0}/plots/pdf/{1}.pdf}}\n".format(
0175                             config.outputPath, filename)
0176 
0177     # hole modules
0178     if config.showmodule:
0179         # check if there are module plots
0180         if any(x for x in config.outputList if (x.plottype == "mod" and x.number == "")):
0181             out += "\subsection{{Module-level parameters}}\n"
0182 
0183             # loop over all structures
0184             for moduleName in [x.name for x in alignables.structures]:
0185 
0186                 # check if there is a plot for this module
0187                 if any(x for x in config.outputList if (x.plottype == "mod" and x.number == "" and x.name == moduleName)):
0188                     out += "\subsubsection{{{0}}}\n".format(moduleName)
0189                     # loop over modes
0190                     for mode in ["xyz", "rot", "dist"]:
0191 
0192                         # get module plot
0193                         module = [x for x in config.outputList if (
0194                             x.plottype == "mod" and x.number == "" and x.name == moduleName and x.parameter == mode)]
0195                         # get list of sub module plots
0196                         moduleSub = [x for x in config.outputList if (
0197                             x.plottype == "subMod" and x.number != "" and x.name == moduleName and x.parameter == mode)]
0198 
0199                         # check if plot there is a plot in this mode
0200                         if module:
0201                             out += "\includegraphics[width=\linewidth]{{{0}/plots/pdf/{1}.pdf}}\n".format(
0202                                 config.outputPath, module[0].filename)
0203                             if config.showsubmodule:
0204                                 # loop over submodules
0205                                 for plot in moduleSub:
0206                                     out += "\includegraphics[width=\linewidth]{{{0}/plots/pdf/{1}.pdf}}\n".format(
0207                                         config.outputPath, plot.filename)
0208 
0209     # plot taken from the millePedeMonitor_merge.root file
0210     if config.showmonitor:
0211         if any(x for x in config.outputList if x.plottype == "monitor"):
0212             out += "\section{{Monitor plots}}\n"
0213 
0214             lastdataset = ""
0215             for plot in [x for x in config.outputList if x.plottype == "monitor"]:
0216                 # all plots of a dataset together in one section
0217                 if (lastdataset != plot.name):
0218                     out += "\subsection{{{0}}}\n".format(plot.name)
0219                 lastdataset = plot.name
0220                 out += "\includegraphics[width=\linewidth]{{{0}/plots/pdf/{1}.pdf}}\n".format(
0221                     config.outputPath, plot.filename)
0222 
0223     data = data.substitute(out=out)
0224 
0225     with open(os.path.join(config.outputPath, outputFile), "w") as output:
0226         output.write(data)
0227         output.close()
0228 
0229     # TODO run pdflatex
0230     for i in range(2):
0231         os.system("pdflatex -output-directory={0}  {1}/{2}".format(
0232             config.outputPath, config.outputPath, outputFile))