CMS 3D CMS Logo

MergeFilesAndCalculateEfficiencies_cfg.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 
4  MergeFilesAndCalculateEfficiencies.py
5 
6  Merges multiple root files containing the numerator and denominator histograms produced by the
7  TauTagValidation package. The efficiency (num/denominator) is then computed
8  as defined Validation/RecoTau/python/RecoTauValidation_cff.py and stored in OutputFile_Eff.root
9 
10  Usage: cmsRun MergeFilesAndCalculateEfficiencies.py OutputFile InputFiles
11 
12  Example: ./MergeFilesAndCalculateEfficiencies.py CMSSW_3_1_0_Signal.root CMSSW_3_1_0_ZTT_*.root
13 
14 """
15 
16 import os
17 import sys
18 import FWCore.ParameterSet.Config as cms
19 from Validation.RecoTau.ValidationOptions_cff import allowedOptions
20 
21 if len(sys.argv) < 5:
22  print "Error. Expected at least 3 arguments\n\nUsage: MergeFilesAndCalculateEfficiencies.py dataType OutputFile InputFileGlob"
23  sys.exit()
24 
25 dataType = sys.argv[2]
26 OutputFile = sys.argv[3]
27 Inputs = sys.argv[4:]
28 
29 if not dataType in allowedOptions['eventType']:
30  print "Error. The first argument must be the dataType. Types availables are:"
31  print allowedOptions['eventType']
32  sys.exit()
33 
34 for aFile in Inputs:
35  if not os.path.exists(aFile):
36  print "Input file %s does not exist!" % aFile
37  sys.exit()
38 
39 if os.path.exists(OutputFile):
40  GotGoodValue = False
41  userInput = ""
42  while not GotGoodValue:
43  userInput = raw_input("Output file %s exists; replace it? [yn] " % OutputFile).strip()
44  if userInput != 'y' and userInput != 'n':
45  print "Please enter y or n"
46  else:
47  GotGoodValue = True
48  if userInput == 'n':
49  sys.exit()
50 
51 # Merge files using hadd utility
52 commandString = "hadd -f %s " % OutputFile
53 for aFile in Inputs:
54  commandString += aFile
55  commandString += " "
56 
57 os.system(commandString)
58 
59 print "Running cmsRun command to generate efficiencies"
60 
61 process = cms.Process("TEST")
62 
63 process.source = cms.Source("EmptySource")
64 
65 process.maxEvents = cms.untracked.PSet(
66  input = cms.untracked.int32(1)
67 )
68 
69 process.DQMStore = cms.Service("DQMStore")
70 process.load("Validation.RecoTau.dataTypes.ValidateTausOn%s_cff"%dataType)
71 
72 process.loadFile = cms.EDAnalyzer("TauDQMFileLoader",
73  myFiles = cms.PSet(
74  inputFileNames = cms.vstring(OutputFile),
75  scaleFactor = cms.double(1.),
76  )
77 )
78 
79 process.saveTauEff = cms.EDAnalyzer("TauDQMSimpleFileSaver",
80 # outputFileName = cms.string(OutputFile.replace('.root', '_Eff.root'))
81  outputFileName = cms.string(OutputFile)
82 )
83 
84 process.p = cms.Path(
85  process.loadFile*
86  getattr(process,'TauEfficiencies%s'%dataType)*
87  process.saveTauEff
88  )
89