3 import math,os,glob,subprocess
5 from collections
import OrderedDict
13 def __init__(self,istest=True,verbose=False,alwaysyes=False,xsRefFile=None,delim=";"):
25 if self.
verbose:
print "Initialising..."
40 return True,
False,origexplanation
44 while answer
not in [
'y',
'n',
'exp',
'a']: answer = raw_input(
'Continue with upload? (y/n/exp/a): ')
47 print self.
colors.OKGREEN+
"Commiting upload!"+self.
colors.ENDC
48 return True,
False,origexplanation
51 print self.
colors.OKGREEN+
"Commiting upload!"+self.
colors.ENDC
52 return True,
False,
"Automatic CentralPage upload"
57 explanation = raw_input(
'Please enter explanation: ')
59 while explanswer
not in [
'y',
'n',
'd',
'a']: explanswer = raw_input(
'Continue upload with new explanation "%s"? (y/n/d[go back to default]/a[automatic message]): '%explanation)
60 if explanswer
in [
'y',
'd',
'a']:
63 print self.
colors.OKGREEN+
"Commiting upload!"+self.
colors.ENDC
65 return True,
True,explanation
67 return True,
False,
"Automatic CentralPage upload"
69 return True,
False,origexplanation
73 print self.
colors.OKBLUE+
"Not comitting upload."+self.
colors.ENDC
74 return False,
False,origexplanation
81 oldexplanation=explanation
82 doUpload,newExplanation,explanation=self.
checkUpload(oldexplanation)
86 newcmd=cmd.split(
' --')
87 for n,x
in enumerate(newcmd):
88 if 'explanation=' in x:
89 newcmd[n]=
'explanation="%s"'%explanation
92 cmd=
' --'.
join(newcmd)
111 refexplanation=
"Unknown"
112 highXsecInfo=highXsecLine.split(
',')
113 if len(highXsecInfo)<3:
114 print self.
colors.FAIL+
"ERROR: Higher-order cross section line only has %i fields (expected 3)"%len(highXsecInfo)+self.
colors.ENDC
117 highXsecRef=highXsecInfo[0].strip()
118 highXsecVal=highXsecInfo[1].strip()
120 highXsecSrc=explanation
122 highXsecSrc=highXsecInfo[2].strip()
124 if str(highXsecRef) ==
str(HORef):
125 if highXsecVal==
"Undefined" or highXsecVal==
"":
126 print self.
colors.FAIL+
"ERROR: Higher order cross section value undefined - skipping"+self.
colors.ENDC
127 highXsecVal=
"Unknown"
129 elif highXsecRef==
"Undefined" or highXsecRef==
"":
130 print self.
colors.FAIL+
"ERROR: Higher order cross section reference undefined - skipping"+self.
colors.ENDC
131 highXsecRef=
"Unknown"
133 elif highXsecSrc==
"Undefined" or highXsecSrc==
"":
134 print self.
colors.WARNING+
"WARNING: Higher order cross section source undefined - skipping"+self.
colors.ENDC
136 refexplanation=highXsecVal+
","+highXsecSrc
144 print self.
colors.FAIL+
"New k-factor for existing HORef - do I need to recalculate HOXS and and propage to all samples with HORef?!"+self.
colors.ENDC
147 print self.
colors.FAIL+
"ERROR: Reference cross section %s not matched to CrossSectionReference file."%(HORef)+self.
colors.ENDC
148 explanation=
"Unknown"
157 HORef = raw_input(
'Please enter new HORef for %s (or type "skip"): '%(HORef))
164 while newanswer
not in [
'y',
'n',
'skip']: newanswer = raw_input(
'Continue upload with new HORef = %s? (y/n/skip): '%HOXS)
165 if newanswer
in [
'y',
'skip']:
171 while not tmpans
and newanswer!=
'skip':
172 HOXS = raw_input(
'Please enter new HOXS for %s (or type "skip"): '%(HORef))
179 while newanswer
not in [
'y',
'n',
'skip']: newanswer = raw_input(
'Continue upload with new HOXS = %s? (y/n/skip): '%HOXS)
180 if newanswer
in [
'y',
'skip']:
185 while not tmpans
and newanswer!=
'skip':
186 HOSrc = raw_input(
'Please enter new HOSrc for %s (or type "skip"): '%(HORef))
194 while newanswer
not in [
'y',
'n',
'skip']: newanswer = raw_input(
'Continue upload with new HOSrc = %s? (y/n/skip): '%HOSrc)
195 if newanswer
in [
'y',
'skip']:
198 if newanswer==
'skip':
199 print self.
colors.OKBLUE+
"INFO: Skipping new HOXS/HOSrc value for %s"%(HORef)+self.
colors.ENDC
200 highXsecVal=
"UNKNOWN"
202 print self.
colors.OKGREEN+
"INFO: Adding new HOXS,HOSrc values %s,%s for %s"%(HOXS,HOSrc,HORef)+self.
colors.ENDC
206 return HOXS,HORef,HOSrc
212 return ds.split(
'.')[0].
split(
'_')[0]
216 def updateValue(self,ds,param,value,explanation,highXsecRef=None):
218 if param ==
"crossSection":
220 elif param ==
"genFiltEff":
222 elif param ==
"kFactor":
225 elif param ==
"processGroup":
227 elif param ==
"physicsStatus":
229 elif param ==
"crossSectionRef":
232 print self.
colors.FAIL+
"ERROR: %s upload not currently supported"%(param)+self.
colors.ENDC
237 cmd=
'RemovePhysicsParameterVals --paramName="%s" --logicalDatasetName="%s" --scope="%s" --physicsGroup="PMG" --explanation="%s"'%(param,ds,scope,explanation)
243 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"processGroup",processGroup,ds,scope,explanation)
249 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"crossSection",xsVal,ds,scope,explanation)
255 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"genFiltEff",effVal,ds,scope,explanation)
260 highXsecVal,highXsecRef,highXsecSrc=self.
getXSRef(XSRefVal)
261 refexplanation=highXsecVal+
","+highXsecSrc
262 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"crossSectionRef",XSRefVal,ds,scope,refexplanation)
265 print "INFO: New XSRef - need to (re)calculate kFactor"
268 def updateKFactor(self,ds,scope,kFactorVal,explanation,highXsecRef,updateRef=True,refexplanation=""):
272 highXsecVal,highXsecRef,highXsecSrc=self.
getXSRef(highXsecRef,refexplanation)
274 if highXsecRef==
"Unknown":
276 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"kFactor",kFactorVal,ds,scope,explanation)
280 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"kFactor",kFactorVal,ds,scope,highXsecRef)
281 doExecKFact=self.
cmdExec(cmd,highXsecRef)
283 if updateRef
and highXsecRef!=self.
currentVals[ds][
"crossSectionRef"]:
284 newexplanation=highXsecVal+
","+highXsecSrc
285 if newexplanation!=refexplanation:
286 cmd_Ref=
'AddPhysicsParameterVals --paramName="crossSectionRef" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(highXsecRef,ds,scope,newexplanation)
287 self.
cmdExec(cmd_Ref,refexplanation)
289 print "INFO: crossSectionRef explanation the same as exiting - not uploading"
291 print "INFO: Not uploading crossSectionRef it is identical to current value (%s)"%(highXsecRef)
295 cmd=
'AddPhysicsParameterVals --paramName="%s" --paramValue="%s" --logicalDatasetName="%s" --physicsGroup="PMG" --scope="%s" --explanation="%s"'%(
"physicsStatus",val,ds,scope,explanation)
301 print "INFO: - XsecRef = %s, HOxs = %s"%(highXsecRef,highXsecVal)
303 print "kFactor = %f - HOxs= %f / ( xs=%f / BR=%f)"%(kFactor,
float(highXsecVal)/1000.,
float(Xsec),
float(BR))
308 print "Need implementation"
312 print "Need implementation"
318 myfile=
open(filename)
319 inlist=myfile.readlines()
320 for line
in inlist[1:]:
321 if not len(line.strip())
or line.strip()[0]==
'#':
323 elements=line.split(self.
delim)
324 if elements[0]
not in outlist:
325 outlist.append(elements[0])
334 myfile=
open(filename)
335 inlist=myfile.readlines()
338 for line
in inlist[1:]:
339 if '#getMetadata.py' in line:
341 fields=[arg
for arg
in line.split(
'--')
if 'fields=' in arg][0].
replace(
'fields=',
'')
343 fielddict={f.strip():n
for n,f
in enumerate(fields.replace(
'"',
'').
split(
','))}
346 if 'crossSection' in fielddict:
347 d.update({
'crossSection':fielddict[
'crossSection']})
348 del fielddict[
'crossSection']
351 for line
in inlist[0]:
354 fielddict={f.split(
'/')[0].strip():n
for n,f
in enumerate(line.replace(
'"',
'').
split(
':'))}
356 if 'crossSection' in fielddict:
357 d.update({
'crossSection':fielddict[
'crossSection']})
358 del fielddict[
'crossSection']
363 self.
colors.FAIL+
"ERROR: Failed to find getMetadata.py command in file"+self.
colors.ENDC
372 myfile=
open(filename)
373 inlist=myfile.readlines()
375 for line
in inlist[1:]:
376 if not len(line.strip())
or line.strip()[0]==
'#' or 'ldn' in line:
379 if len(elements)<len(fielddict):
380 print "ERROR: Number of elements found in line (%i) less than those in query fields (%i)"%(len(elements),len(fielddict))
381 print "ERROR: -> Elements:",elements
383 filedict[elements[fielddict[
"ldn"]]]={f:elements[fielddict[f]].strip()
for f
in fielddict
if f !=
"ldn"}
390 for f
in glob.glob(
"/tmp/"+os.environ[
"USER"]+
"/my.datasets*.txt"): os.remove(f)
395 if not os.access(
"/tmp/"+os.environ[
"USER"]+
"/my.datasets"+
str(counter)+
".txt",os.R_OK):
396 dsfile=
open(
"/tmp/"+os.environ[
"USER"]+
"/my.datasets"+
str(counter)+
".txt",
'w')
398 dsfile=
open(
"/tmp/"+os.environ[
"USER"]+
"/my.datasets"+
str(counter)+
".txt",
'a')
399 dsfile.write(ds+
"\n")
407 print "INFO: Found %i datasets, split into %i getMetadata queries"%(nDS,counter)
409 for f
in glob.glob(
"/tmp/"+os.environ[
"USER"]+
"/metadata*.txt"): os.remove(f)
410 metaFile=
open(
"/tmp/"+os.environ[
"USER"]+
"/metadata.txt",
'w')
413 for i
in range(1,counter+1):
414 print "getMetadata.py --delim="+self.
delim+
" --inDsTxt=/tmp/"+os.environ[
"USER"]+
"/my.datasets"+
str(i)+
".txt --outFile=/tmp/"+os.environ[
"USER"]+
"/metadata"+
str(i)+
".txt --fields",
" ".
join([f
for f,n
in fields.iteritems()])
415 getMeta = subprocess.Popen([
"getMetadata.py",
"--delim="+self.
delim+
"",
"--inDsTxt=/tmp/"+os.environ[
"USER"]+
"/my.datasets"+
str(i)+
".txt",
"--outFile=/tmp/"+os.environ[
"USER"]+
"/metadata"+
str(i)+
".txt",
"--fields"]+[f
for f,n
in fields.iteritems()])
418 tmpMetaFile=
open(
"/tmp/"+os.environ[
"USER"]+
"/metadata"+
str(i)+
".txt",
'r')
419 metaFile.write(tmpMetaFile.read())
425 metaFile=
open(
"/tmp/"+os.environ[
"USER"]+
"/metadata.txt",
'r')
426 metaFileList=metaFile.readlines()
427 nDSFound=len([ ln
for ln
in metaFileList[1:]
if ln[0]!=
'#' and len(ln.strip())])
429 print "INFO: %i/%i datasets found in AMI"%(nDSFound,nDS)
431 print "WARNING: %i/%i datasets found in AMI"%(nDSFound,nDS)
433 return "/tmp/"+os.environ[
"USER"]+
"/metadata.txt"
441 reffile.write(
"\n%s,%s,%s"%(HORef,HOXS,HOSrc))
447 print "findAllSamplesForXSRef not implemented"