Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:38

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