Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:20

0001 #!/usr/bin/env python3
0002 """
0003 
0004      MergeFilesAndCalculateEfficiencies.py
0005 
0006         Merges multiple root files containing the numerator and denominator histograms produced by the
0007         TauTagValidation package.  The efficiency (num/denominator) is then computed 
0008         as defined Validation/RecoTau/python/RecoTauValidation_cff.py and stored in OutputFile_Eff.root
0009 
0010      Usage: cmsRun MergeFilesAndCalculateEfficiencies.py OutputFile InputFiles
0011 
0012      Example: ./MergeFilesAndCalculateEfficiencies.py CMSSW_3_1_0_Signal.root CMSSW_3_1_0_ZTT_*.root
0013 
0014 """
0015 from __future__ import print_function
0016 
0017 import os
0018 import sys
0019 import FWCore.ParameterSet.Config as cms
0020 from Validation.RecoTau.ValidationOptions_cff import allowedOptions
0021 
0022 if len(sys.argv) < 4:
0023    print("Error. Expected at least 3 arguments\n\nUsage: MergeFilesAndCalculateEfficiencies.py dataType OutputFile InputFileGlob")
0024    sys.exit()
0025 
0026 dataType   = sys.argv[1]
0027 OutputFile = sys.argv[2]
0028 Inputs     = sys.argv[3:]
0029 
0030 if not dataType in allowedOptions['eventType']:
0031    print("Error. The first argument must be the dataType. Types availables are:")
0032    print(allowedOptions['eventType'])
0033    sys.exit()
0034 
0035 for aFile in Inputs:
0036    if not os.path.exists(aFile):
0037       print("Input file %s does not exist!" % aFile)
0038       sys.exit()
0039 
0040 if os.path.exists(OutputFile):
0041    GotGoodValue = False
0042    userInput = ""
0043    while not GotGoodValue:
0044       userInput = raw_input("Output file %s exists; replace it? [yn] " % OutputFile).strip()
0045       if userInput != 'y' and userInput != 'n':
0046          print("Please enter y or n")
0047       else:
0048          GotGoodValue = True
0049    if userInput == 'n':
0050       sys.exit()
0051 
0052 # Merge files using hadd utility
0053 commandString  = "hadd -f %s " % OutputFile
0054 for aFile in Inputs:
0055    commandString += aFile
0056    commandString += " "
0057 
0058 os.system(commandString)
0059 
0060 print("Running cmsRun command to generate efficiencies")
0061 
0062 process = cms.Process("TEST")
0063 
0064 process.source = cms.Source("EmptySource")
0065 
0066 process.maxEvents = cms.untracked.PSet(
0067     input = cms.untracked.int32(1)
0068 )
0069 
0070 process.DQMStore = cms.Service("DQMStore")
0071 process.load("Validation.RecoTau.dataTypes.ValidateTausOn%s_cff"%dataType)
0072 
0073 process.loadFile   = cms.EDAnalyzer("TauDQMFileLoader",
0074       myFiles = cms.PSet(
0075          inputFileNames = cms.vstring(OutputFile),
0076          scaleFactor = cms.double(1.),
0077          )
0078 )
0079 
0080 process.saveTauEff = cms.EDAnalyzer("TauDQMSimpleFileSaver",
0081 #  outputFileName = cms.string(OutputFile.replace('.root', '_Eff.root'))
0082   outputFileName = cms.string(OutputFile)
0083 )
0084 
0085 process.p = cms.Path(
0086       process.loadFile*
0087       getattr(process,'TauEfficiencies%s'%dataType)*
0088       process.saveTauEff
0089       )
0090