forked from predragm/HGCalAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SampleHelper.py
executable file
·189 lines (166 loc) · 8.5 KB
/
SampleHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Sample helper tools for HGCal ntuples on EOS."""
import commands
import ROOT
import logging
from copy import copy
class NullHandler(logging.Handler):
"""NullHandler for logging module."""
def emit(self, record):
"""emit."""
pass
logging.getLogger(__name__).addHandler(NullHandler())
class Sample(object):
"""Sample class to get ROOT Chain and individual files."""
def __init__(self, name, inDir, treeName="ana/hgc", fileList=[]):
"""basic sample settings."""
self.name = name
self.inDir = inDir
self.treeName = treeName
# fileList needs copy since it's a reference
self.fileList = copy(fileList)
def getFiles(self, numberOfFiles=-1):
"""get input files."""
fileList = []
for i, fileName in enumerate(self.fileList):
fileList.append(fileName)
if numberOfFiles > 0:
if (i == numberOfFiles):
break
return fileList
def addFile(self, fileName):
"""add an individual file to sample."""
logging.debug("addFile: {} - {}".format(self.name, fileName))
if fileName not in self.fileList:
logging.debug("addFile: Adding " + fileName)
self.fileList.append(fileName)
else:
logging.error("file " + fileName +
" has already been added before.")
def getChain(self):
"""create a ROOT Chain from sample."""
chain = ROOT.TChain(self.treeName)
for fileName in self.fileList:
logging.debug("getChain: Adding " + fileName)
chain.AddFile(fileName)
return chain
class SampleManager(object):
"""Sample class to get ROOT Chain and individual files."""
def __init__(self, initialise=True):
"""basic location settings and initialisation.
mind the slashes in the path names.
"""
self.eosExec = '/afs/cern.ch/project/eos/installation/0.3.84-aquamarine/bin/eos.select'
self.eosPrefix = "root://eoscms.cern.ch/"
self.baseDir = "/eos/cms/store/cmst3/group/hgcal/CMG_studies/Production/"
self.pathSuffix = "/NTUP/"
self.sampleDict = {}
if initialise:
self.sampleDict = self.addDefaultSamples()
tmpList = {}
for name, sample in self.sampleDict.items():
tmpList[name] = self.addSampleFiles(sample)
self.sampleDict = tmpList
self.printSamples()
def getFullPath(self, inDir, forRoot=False):
"""return full path."""
fullPath = "{baseDir}{inDir}{pathSuffix}".format(
baseDir=self.baseDir, inDir=inDir, pathSuffix=self.pathSuffix)
if forRoot:
fullPath = fullPath + self.eosPrefix
return fullPath
def addSample(self, name, inDir):
"""add an individual sample."""
self.sampleDict[name] = Sample(name, inDir)
def addSampleFiles(self, sample):
"""add files to a Sample."""
logging.debug(
"addSampleFiles - sample: {}, inDir: {}".format(sample.name, sample.inDir))
searchPath = self.getFullPath(sample.inDir)
eosCmd = "{eosExec} ls {searchPath}".format(
eosExec=self.eosExec, searchPath=searchPath)
eosOutput = processCmd(eosCmd)
for line in eosOutput.split("\n"):
sample.addFile(self.eosPrefix + searchPath + line)
return sample
def addDefaultSamples(self):
"""add default samples."""
sampleDict = {}
sampleDict["photons_nPart1_Pt5_pre12"] = Sample(
"photons_nPart1_Pt5_pre12", "partGun_clange_PDGid22_nPart1_Pt5_20160812")
sampleDict["chargedPions_nPart1_Pt5_pre12"] = Sample(
"chargedPions_nPart1_Pt5_pre12", "partGun_clange_PDGid211_nPart1_Pt5_20160901")
sampleDict["chargedPions_nPart1_Pt10_pre12"] = Sample(
"chargedPions_nPart1_Pt10_pre12", "partGun_clange_PDGid211_nPart1_Pt10_20160901")
sampleDict["chargedPions_nPart1_Pt20_pre12"] = Sample(
"chargedPions_nPart1_Pt20_pre12", "partGun_clange_PDGid211_nPart1_Pt20_20160901")
sampleDict["chargedPions_nPart1_Pt35_pre12"] = Sample(
"chargedPions_nPart1_Pt35_pre12", "partGun_clange_PDGid211_nPart1_Pt35_20160901")
# pre15 samples
# sampleDict["photons_nPart1_Pt5_pre15"] = Sample(
# "photons_nPart1_Pt5_pre15", "partGun_predragm_PDGid22_nPart1_Pt5_pre15_20161024")
sampleDict["chargedPions_nPart1_Pt5_pre15"] = Sample(
"chargedPions_nPart1_Pt5_pre15", "partGun_predragm_PDGid211_nPart1_Pt5_pre15_20161024")
sampleDict["chargedPions_nPart1_Pt10_pre15"] = Sample(
"chargedPions_nPart1_Pt10_pre15", "partGun_predragm_PDGid211_nPart1_Pt10_pre15_20161024")
sampleDict["chargedPions_nPart1_Pt20_pre15"] = Sample(
"chargedPions_nPart1_Pt20_pre15", "partGun_predragm_PDGid211_nPart1_Pt20_pre15_20161024")
sampleDict["chargedPions_nPart1_Pt35_pre15"] = Sample(
"chargedPions_nPart1_Pt35_pre15", "partGun_predragm_PDGid211_nPart1_Pt35_pre15_20161024")
# 5k events charged pion samples (Pt gun)
sampleDict["chargedPions_nPart1_Pt2_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt2_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt2_20161102")
sampleDict["chargedPions_nPart1_Pt5_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt5_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt5_20161102")
sampleDict["chargedPions_nPart1_Pt10_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt10_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt10_20161102")
sampleDict["chargedPions_nPart1_Pt20_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt20_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt20_20161102")
sampleDict["chargedPions_nPart1_Pt35_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt35_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt35_20161102")
sampleDict["chargedPions_nPart1_Pt200_pre15_5k"] = Sample(
"chargedPions_nPart1_Pt200_pre15_5k", "partGun_clange_PDGid211_nPart1_Pt200_20161102")
# 5k events charged pion samples (E gun)
sampleDict["chargedPions_nPart1_E2_pre15_5k"] = Sample(
"chargedPions_nPart1_E2_pre15_5k", "partGun_clange_PDGid211_nPart1_E2_20161125")
sampleDict["chargedPions_nPart1_E5_pre15_5k"] = Sample(
"chargedPions_nPart1_E5_pre15_5k", "partGun_clange_PDGid211_nPart1_E5_20161125")
sampleDict["chargedPions_nPart1_E10_pre15_5k"] = Sample(
"chargedPions_nPart1_E10_pre15_5k", "partGun_clange_PDGid211_nPart1_E10_20161125")
sampleDict["chargedPions_nPart1_E20_pre15_5k"] = Sample(
"chargedPions_nPart1_E20_pre15_5k", "partGun_clange_PDGid211_nPart1_E20_20161125")
sampleDict["chargedPions_nPart1_E40_pre15_5k"] = Sample(
"chargedPions_nPart1_E40_pre15_5k", "partGun_clange_PDGid211_nPart1_E40_20161125")
sampleDict["chargedPions_nPart1_E80_pre15_5k"] = Sample(
"chargedPions_nPart1_E80_pre15_5k", "partGun_clange_PDGid211_nPart1_E80_20161125")
sampleDict["chargedPions_nPart1_E160_pre15_5k"] = Sample(
"chargedPions_nPart1_E160_pre15_5k", "partGun_clange_PDGid211_nPart1_E160_20161125")
sampleDict["chargedPions_nPart1_E320_pre15_5k"] = Sample(
"chargedPions_nPart1_E320_pre15_5k", "partGun_clange_PDGid211_nPart1_E320_20161125")
return sampleDict
def getSample(self, sampleName):
"""return Sample by name."""
if sampleName not in self.sampleDict:
logging.error("getSample: Sample " + sampleName + "does not exist")
self.printSamples()
return 0
return self.sampleDict[sampleName]
def getSamples(self):
"""return Sample name list."""
sampleNameList = []
for sampleName in self.sampleDict:
sampleNameList.append(sampleName)
return sampleNameList
def printSamples(self):
"""print available samples."""
logging.info("Available samples:")
for sampleName, sample in self.sampleDict.items():
logging.info(" o " + sampleName +
" (" + str(len(sample.fileList)) + " files)")
def processCmd(cmd, quiet=False):
"""processing the external os commands."""
logging.debug("processCmd: " + cmd)
status, output = commands.getstatusoutput(cmd)
if (status != 0 and not quiet):
logging.error('Error in processing command:\n [' + cmd + ']')
logging.error('Output:\n [' + output + '] \n')
return output