Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:03

0001 #!/usr/bin/env python3
0002 
0003 # Original author: Joerg Behr
0004 # Translation from Perl to Python: Gregor Mittag
0005 #
0006 # This script reads the histogram file produced by Pede and it extracts the plot
0007 # showing the average chi2/ndf per Mille binary number.  After reading the MPS
0008 # database, for which the file name has to be provided, an output file called
0009 # chi2pedehis.txt is produced where the first column corresponds to the
0010 # associated name, the second column corresponds to the Mille binary number, and
0011 # the last column is equal to <chi2/ndf>. As further argument this scripts
0012 # expects the file name of the Pede histogram file -- usually millepede.his. The
0013 # last required argument represents the location of the Python config which was
0014 # used by CMSSW.
0015 #
0016 # Use createChi2ndfplot.C to plot the output of this script.
0017 
0018 import os
0019 import sys
0020 import re
0021 import argparse
0022 
0023 import Alignment.MillePedeAlignmentAlgorithm.mpslib.tools as mps_tools
0024 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
0025 
0026 
0027 ################################################################################
0028 def main(argv = None):
0029     """Main routine of the script.
0030 
0031     Arguments:
0032     - `argv`: arguments passed to the main routine
0033     """
0034 
0035     if argv == None:
0036         argv = sys.argv[1:]
0037 
0038     parser = argparse.ArgumentParser(description="Analysis pede histogram file")
0039     parser.add_argument("-d", "--mps-db", dest="mps_db", required=True,
0040                         metavar="PATH", help="MPS database file ('mps.db')")
0041     parser.add_argument("--his", dest="his_file", required=True,
0042                         metavar="PATH", help="pede histogram file")
0043     parser.add_argument("-c", "--cfg", dest="cfg", metavar="PATH", required=True,
0044                         help="python configuration file of pede job")
0045     parser.add_argument("-b", "--no-binary-check", dest="no_binary_check",
0046                         default=False, action="store_true",
0047                         help=("skip check for existing binaries "
0048                               "(possibly needed if used interactively)"))
0049     args = parser.parse_args(argv)
0050 
0051 
0052     for input_file in (args.mps_db, args.his_file, args.cfg):
0053         if not os.path.exists(input_file):
0054             print("Could not find input file:", input_file)
0055             sys.exit(1)
0056 
0057     ids, names = get_all_ids_names(args.mps_db)
0058     used_binaries = get_used_binaries(args.cfg, args.no_binary_check)
0059     his_data = get_his_data(args.his_file)
0060 
0061     if len(his_data) != len(used_binaries):
0062         print("The number of used binaries is", len(used_binaries), end=' ')
0063         print("whereas in contrast, however, the <chi2/ndf> histogram in Pede has", end=' ')
0064         print(len(his_data), "bins (Pede version >= rev92 might help if #bins < #binaries).", end=' ')
0065         print("Exiting.")
0066         sys.exit(1)
0067 
0068     with open("chi2pedehis.txt", "w") as f:
0069         for i, b in enumerate(used_binaries):
0070             index = ids.index(b)
0071             name = names[index]
0072             f.write(" ".join([name, "{:03d}".format(b), his_data[i]])+"\n")
0073 
0074 
0075 ################################################################################
0076 def get_all_ids_names(mps_db):
0077     """Returns two lists containing the mille job IDs and the associated names.
0078     
0079     Arguments:
0080     - `mps_db`: path to the MPS database file
0081     """
0082 
0083     lib = mpslib.jobdatabase()
0084     lib.read_db(mps_db)
0085 
0086     ids = lib.JOBNUMBER[:lib.nJobs]
0087     names = lib.JOBSP3[:lib.nJobs]
0088 
0089     return ids, names
0090 
0091 
0092 def get_used_binaries(cfg, no_binary_check):
0093     """Returns list of used binary IDs.
0094     
0095     Arguments:
0096     - `cfg`: python config used to run the pede job
0097     - `no_binary_check`: if 'True' a check for file existence is skipped
0098     """
0099 
0100     cms_process = mps_tools.get_process_object(cfg)
0101 
0102     binaries = cms_process.AlignmentProducer.algoConfig.mergeBinaryFiles
0103     if no_binary_check:
0104         used_binaries = binaries
0105     else:
0106         # following check works only if 'args.cfg' was run from the same directory:
0107         used_binaries = [b for b in binaries
0108                          if os.path.exists(os.path.join(os.path.dirname(cfg), b))]
0109 
0110     used_binaries = [int(re.sub(r"milleBinary(\d+)\.dat", r"\1", b))
0111                      for b in used_binaries]
0112 
0113     return used_binaries
0114 
0115 
0116 def get_his_data(his_file):
0117     """Parse the pede histogram file.
0118     
0119     Arguments:
0120     - `his_file`: pede histogram file
0121     """
0122     
0123     his_data = []
0124     with open(his_file, "r") as his:
0125         found_chi2_start = False;
0126         
0127         for line in his:
0128             if r"final <Chi^2/Ndf> from accepted local fits vs file number" in line:
0129                 found_chi2_start = True
0130             if not found_chi2_start:
0131                 continue
0132             else:
0133                 if r"end of xy-data" in line: break
0134                 if not re.search("\d", line): continue
0135                 if re.search(r"[a-z]", line): continue
0136                 splitted = line.split()
0137                 his_data.append(splitted[-1])
0138 
0139     return his_data
0140 
0141     
0142 ################################################################################
0143 if __name__ == "__main__":
0144     main()