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 from __future__ import print_function
16 
17 import os
18 import sys
19 import FWCore.ParameterSet.Config as cms
20 from Validation.RecoTau.ValidationOptions_cff import allowedOptions
21 
22 if len(sys.argv) < 5:
23  print("Error. Expected at least 3 arguments\n\nUsage: MergeFilesAndCalculateEfficiencies.py dataType OutputFile InputFileGlob")
24  sys.exit()
25 
26 dataType = sys.argv[2]
27 OutputFile = sys.argv[3]
28 Inputs = sys.argv[4:]
29 
30 if not dataType in allowedOptions['eventType']:
31  print("Error. The first argument must be the dataType. Types availables are:")
32  print(allowedOptions['eventType'])
33  sys.exit()
34 
35 for aFile in Inputs:
36  if not os.path.exists(aFile):
37  print("Input file %s does not exist!" % aFile)
38  sys.exit()
39 
40 if os.path.exists(OutputFile):
41  GotGoodValue = False
42  userInput = ""
43  while not GotGoodValue:
44  userInput = raw_input("Output file %s exists; replace it? [yn] " % OutputFile).strip()
45  if userInput != 'y' and userInput != 'n':
46  print("Please enter y or n")
47  else:
48  GotGoodValue = True
49  if userInput == 'n':
50  sys.exit()
51 
52 # Merge files using hadd utility
53 commandString = "hadd -f %s " % OutputFile
54 for aFile in Inputs:
55  commandString += aFile
56  commandString += " "
57 
58 os.system(commandString)
59 
60 print("Running cmsRun command to generate efficiencies")
61 
62 process = cms.Process("TEST")
63 
64 process.source = cms.Source("EmptySource")
65 
66 process.maxEvents = cms.untracked.PSet(
67  input = cms.untracked.int32(1)
68 )
69 
70 process.DQMStore = cms.Service("DQMStore")
71 process.load("Validation.RecoTau.dataTypes.ValidateTausOn%s_cff"%dataType)
72 
73 process.loadFile = cms.EDAnalyzer("TauDQMFileLoader",
74  myFiles = cms.PSet(
75  inputFileNames = cms.vstring(OutputFile),
76  scaleFactor = cms.double(1.),
77  )
78 )
79 
80 process.saveTauEff = cms.EDAnalyzer("TauDQMSimpleFileSaver",
81 # outputFileName = cms.string(OutputFile.replace('.root', '_Eff.root'))
82  outputFileName = cms.string(OutputFile)
83 )
84 
85 process.p = cms.Path(
86  process.loadFile*
87  getattr(process,'TauEfficiencies%s'%dataType)*
88  process.saveTauEff
89  )
90 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66