ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/bin/mergeHist.py
Revision: 1.1
Committed: Tue Jan 25 13:52:19 2011 UTC (14 years, 3 months ago) by paus
Content type: text/x-python
Branch: MAIN
CVS Tags: Mit_020a, Mit_020, Mit_020pre1, Mit_018
Log Message:
Setup.

File Contents

# Content
1 #!/usr/bin/env python
2 #===================================================================================================
3 import sys, getopt, os, fnmatch, commands
4 import string
5
6 # declare
7 setupCmsswCommand = \
8 'cd /home/$USER/cms/cmssw/017/CMSSW_3_9_5_patch1/src;eval `scram runtime -sh`;cd - >& /dev/null;'
9
10 #===================================================================================================
11 def usage():
12 print "possible options are: --help, --InputPath=<myInputPath>, --OutputPath=<myOutputPath>," \
13 + " --FilenameHeader=<myFileHeader>, --DatasetListFile=<datasetListFile>"
14
15 #===================================================================================================
16 def filesExist(path,filenameExpression):
17 exists = False
18 for fileName in os.listdir(path):
19 if fnmatch.fnmatch (fileName,filenameExpression):
20 exists = True
21 return exists
22
23 #===================================================================================================
24 # Merge Filesets
25 #===================================================================================================
26 def MergeFilesets(versionList,datasetNameList,skimNameList,inputPath,outputPath,filenameHeader):
27 n = 0
28 for dataset in datasetNameList:
29 #print "================================================================"
30 print " Merging files for dataset: " + dataset
31
32 # create the output directory in case it is not yet there
33 command = 'mkdir -p ' + outputPath
34 os.system(command)
35
36 outputMergedFilename = filenameHeader + '_' + dataset + '_' + skimNameList[n] + '.root'
37 inputFilenameRegExp = filenameHeader + '_' + dataset + '_' + skimNameList[n] + '_????.root'
38 command = setupCmsswCommand + 'hadd -f ' + outputPath + outputMergedFilename \
39 + ' ' + inputPath + '/' + versionList[n] + '/' + dataset + '/' \
40 + inputFilenameRegExp + ' >& ./merging.tmp'
41 # command = 'hadd -f ' + outputPath + \
42 # outputMergedFilename + ' ' + inputPath + inputFilenameRegExp + \
43 # ' >& ./merging.tmp'
44
45 if (filesExist(inputPath+'/'+versionList[n]+'/'+dataset,inputFilenameRegExp) == True):
46 if (os.path.exists(outputPath+outputMergedFilename)):
47 print " Warning: merged file already exists. It will be deleted.\n " + \
48 outputPath+outputMergedFilename
49 os.system('rm ' + outputPath+outputMergedFilename)
50 #print ' merging: ' + command
51 os.system(command)
52 #print ''
53 else:
54 print " Warning: No files for dataset " + dataset + "\n at the location: " + inputPath \
55 + '/' + versionList[n] + '/' + dataset + '/' + inputFilenameRegExp
56 #print ''
57 n += 1
58
59 #===================================================================================================
60 # Main Program
61 #===================================================================================================
62 datasetListFile = ''
63 inputPath = ''
64 outputPath = ''
65 filenameHeader = ''
66 versionList = list()
67 datasetNameList = list()
68 skimNameList = list()
69
70 if len(sys.argv[1:]) < 1:
71 print "Error: not enough parameters specified"
72 usage()
73 sys.exit()
74
75 try:
76 opts, args = getopt.getopt(sys.argv[1:], "hi:o:f:d:",
77 ["help","InputPath=","OutputPath=",
78 "FilenameHeader=","DatasetListFile="])
79 for o, a in opts:
80 if o in ("-h", "--help"):
81 usage()
82 sys.exit()
83 elif o in ("-i", "--InputPath"):
84 inputPath = a + "/"
85 elif o in ("-o", "--OutputPath"):
86 outputPath = a + "/"
87 elif o in ("-f", "--FilenameHeader"):
88 filenameHeader = a
89 elif o in ("-d", "--DatasetListFile"):
90 datasetListFile = a
91 else:
92 usage()
93 sys.exit()
94 except getopt.GetoptError:
95 usage()
96 sys.exit(2)
97
98 if (inputPath == ''):
99 print "Error: No InputPath specified."
100 sys.exit()
101
102 if (outputPath == ''):
103 print "Error: No OutputPath specified."
104 sys.exit()
105
106 if (filenameHeader == ''):
107 print "Error: No FilenameHeader specified."
108 sys.exit()
109
110 if (datasetListFile == ''):
111 print "Error: No dataset list file specified."
112 sys.exit()
113
114 try:
115 inputFile = open(datasetListFile,"r")
116 except IOError:
117 print "Error: The specified dataset list file " + datasetListFile + " could not be opened."
118 sys.exit()
119
120 #===================================================================================================
121 # Read in list of datasets and skim names
122 #===================================================================================================
123 lineNumber = 1
124 templine = inputFile.readline()
125 while len(templine.split()) > 0:
126
127 # ignore commented lines
128 if (templine[0] == '#'):
129 templine = inputFile.readline()
130 lineNumber += 1
131 continue;
132
133 # check what type of list was provided and assume 'noskim' as default
134 if (len(templine.split()) == 7) :
135 tempInputList = templine.split()
136 versionList .append(tempInputList[0])
137 datasetNameList.append(tempInputList[1])
138 skimNameList .append('noskim')
139 lineNumber += 1
140 else:
141 print " ERROR: incorrect format for cross section file. Check line %s" % lineNumber
142 sys.exit()
143
144 # read the next line
145 templine = inputFile.readline()
146
147 inputFile.close()
148
149 # Check the list of variables
150 count = 0
151 for l in datasetNameList:
152 count += 1
153
154 MergeFilesets(versionList,datasetNameList,skimNameList,inputPath,outputPath,filenameHeader)