1 from __future__
import print_function
2 import math, re, optparse, commands, os, sys, time, datetime
3 from BeamSpotObj
import BeamSpot
13 timeFormat =
"%a,%Y/%m/%d,%H:%M:%S" 14 currentTime = time.gmtime()
15 timeoutLine = type +
' ' + time.strftime(timeFormat, currentTime) +
'\n' 19 fileExist = os.path.isfile(fileName)
29 fields = line.strip(
'\n').
split(
' ')
33 text = text.replace(line,
'')
36 fileTime = time.strptime(fields[1],timeFormat)
37 myTime = time.mktime(fileTime)
38 referenceTime = time.mktime(time.gmtime())
40 if currentTime.tm_isdst == 0:
42 elapsedTime = referenceTime-myTime-daylight
43 if elapsedTime > timeout:
46 print(
"Timeout! " +
str(elapsedTime) +
" seconds passed since the " + type +
" timeout was set and you can't tolerate more than " +
str(timeout) +
" seconds!")
49 print(
"Timeout of type " + type +
" already exist and was generated " +
str(elapsedTime) +
" seconds ago at " + fields[1])
53 if not fileExist
or not alreadyThere
and not reset:
57 if not fileExist
or not alreadyThere
or isTimeout
or (reset
and alreadyThere):
59 commands.getstatusoutput(
"rm -rf " + fileName)
60 file = open(fileName,
'w')
75 if os.path.isfile(lockFile):
83 commands.getstatusoutput(
"touch " + lockFile)
89 commands.getstatusoutput(
"rm " + lockFile)
94 raise SystemExit(msg
or optionstring.replace(
"%prog",sys.argv[0]))
104 USAGE = re.compile(
r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
110 optionstring = docstring
111 match = USAGE.search(optionstring)
112 if not match:
raise ParsingError(
"Cannot find the option string")
113 optlines = match.group(1).splitlines()
115 p = optparse.OptionParser(optlines[0])
116 for line
in optlines[1:]:
117 opt, help=line.split(
':')[:2]
118 short,long=opt.split(
',')[:2]
121 long=long.split(
'=')[0]
124 p.add_option(short.strip(),long.strip(),
125 action = action, help = help.strip())
126 except (IndexError,ValueError):
127 raise ParsingError(
"Cannot parse the option string correctly")
128 return p.parse_args(arglist)
132 "True if options were given" 133 for v
in six.itervalues(self.__dict__):
134 if v
is not None:
return True 138 optparse.Values.__nonzero__ = nonzero
151 print(
"Sending email to " + mailList +
" with body: " + error)
152 list = mailList.split(
',')
154 p = os.popen(
"mail -s \"Automatic workflow error\" " + email ,
"w")
160 if dir.find(
"castor") != -1:
161 lsCommand =
"nsls " + dir
162 output = commands.getstatusoutput( lsCommand )
165 return os.path.exists(dir)
168 def ls(dir,filter=""):
171 if dir.find(
'castor') != -1:
173 elif not os.path.exists(dir):
174 print(
"ERROR: File or directory " + dir +
" doesn't exist")
177 aCommand = lsCommand +
'ls '+ dir
180 aCommand +=
" | grep " + filter
182 tmpStatus = commands.getstatusoutput( aCommand )
183 listOfFiles = tmpStatus[1].
split(
'\n')
184 if len(listOfFiles) == 1:
185 if listOfFiles[0].
find(
'No such file or directory') != -1:
186 exit(
"ERROR: File or directory " + dir +
" doesn't exist")
191 def cp(fromDir,toDir,listOfFiles,overwrite=False,smallList=False):
194 if fromDir.find(
'castor') != -1
or toDir.find(
'castor') != -1 :
196 elif fromDir.find(
'resilient') != -1:
198 if fromDir[len(fromDir)-1] !=
'/':
201 if toDir[len(toDir)-1] !=
'/':
204 for file
in listOfFiles:
205 if os.path.isfile(toDir+file):
207 print(
"File " + file +
" already exists in destination directory. We will overwrite it.")
209 print(
"File " + file +
" already exists in destination directory. We will Keep original file.")
211 copiedFiles.append(file)
214 aCommand = cpCommand +
'cp '+ fromDir + file +
" " + toDir
215 print(
" >> " + aCommand)
216 tmpStatus = commands.getstatusoutput( aCommand )
217 if tmpStatus[0] == 0:
218 copiedFiles.append(file)
220 print(
"[cp()]\tERROR: Can't copy file " + file)
230 """pack high,low 32bit unsigned int to one unsigned 64bit long long 231 Note:the print value of result number may appear signed, if the sign bit is used. 238 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low) 246 """unpack 64bit lumiid to dictionary {'run','lumisection'} 249 return {
'run':j[0],
'lumisection':j[1]}
256 if int(a.IOVfirst) <
int(b.IOVfirst):
return -1
257 if int(a.IOVfirst) ==
int(b.IOVfirst):
return 0
258 if int(a.IOVfirst) >
int(b.IOVfirst):
return 1
262 if int(a.Run) <
int(b.Run):
return -1
263 if int(a.Run) ==
int(b.Run):
264 if int(a.IOVfirst) <
int(b.IOVfirst):
return -1
265 if int(a.IOVfirst) ==
int(b.IOVfirst):
return 0
266 if int(a.IOVfirst) >
int(b.IOVfirst):
return 1
267 if int(a.Run) >
int(b.Run) :
return 1
280 x1 = x2/(x2err * x2err)
281 tmperr = 1/(x2err*x2err)
283 x1 = x1/(x1err*x1err) + x2/(x2err * x2err)
284 tmperr = 1/(x1err*x1err) + 1/(x2err*x2err)
287 x1err = math.sqrt(x1err)
288 return (
str(x1),
str(x1err))
293 file.write(
"Runnumber "+beam.Run+end)
294 file.write(
"BeginTimeOfFit "+
str(beam.IOVBeginTime)+end)
295 file.write(
"EndTimeOfFit "+
str(beam.IOVEndTime)+end)
296 file.write(
"LumiRange "+
str(beam.IOVfirst)+
" - "+
str(beam.IOVlast)+end)
302 file.write(
"Type "+
str(beam.Type)+end)
303 file.write(
"X0 "+
str(beam.X)+end)
304 file.write(
"Y0 "+
str(beam.Y)+end)
305 file.write(
"Z0 "+
str(beam.Z)+end)
306 file.write(
"sigmaZ0 "+
str(beam.sigmaZ)+end)
307 file.write(
"dxdz "+
str(beam.dxdz)+end)
308 file.write(
"dydz "+
str(beam.dydz)+end)
309 file.write(
"BeamWidthX "+beam.beamWidthX+end)
310 file.write(
"BeamWidthY "+beam.beamWidthY+end)
311 file.write(
"Cov(0,j) "+
str(math.pow(
float(beam.Xerr),2))+
" 0 0 0 0 0 0" +end)
312 file.write(
"Cov(1,j) 0 "+
str(math.pow(
float(beam.Yerr),2))+
" 0 0 0 0 0" +end)
313 file.write(
"Cov(2,j) 0 0 "+
str(math.pow(
float(beam.Zerr),2))+
" 0 0 0 0" +end)
314 file.write(
"Cov(3,j) 0 0 0 "+
str(math.pow(
float(beam.sigmaZerr),2))+
" 0 0 0" +end)
315 file.write(
"Cov(4,j) 0 0 0 0 "+
str(math.pow(
float(beam.dxdzerr),2))+
" 0 0" +end)
316 file.write(
"Cov(5,j) 0 0 0 0 0 "+
str(math.pow(
float(beam.dydzerr),2))+
" 0" +end)
317 file.write(
"Cov(6,j) 0 0 0 0 0 0 "+
str(math.pow(
float(beam.beamWidthXerr),2)) +end)
318 file.write(
"EmittanceX 0"+end)
319 file.write(
"EmittanceY 0"+end)
320 file.write(
"BetaStar 0"+end)
325 return (
float(x) -
float(nextx), math.sqrt(math.pow(
float(xerr),2) + math.pow(
float(nextxerr),2)) )
328 return math.fabs(x[0])/x[1]
331 def readBeamSpotFile(fileName,listbeam=[],IOVbase="runbase", firstRun='1',lastRun='4999999999'):
337 if IOVbase ==
"lumibase" and firstRun==
'1' and lastRun==
'4999999999' :
339 lastRun =
"4999999999:4999999999" 349 tmpfile = open(fileName)
350 atmpline = tmpfile.readline()
351 if atmpline.find(
'Runnumber') != -1:
353 if len(atmpline.split()) > 2:
355 print(
" Input data has been calculated as function of BUNCH CROSSINGS.")
359 if inputfiletype ==1:
364 if line.find(
'Type') != -1:
365 tmpbeam.Type =
int(line.split()[1])
367 if line.find(
'X0') != -1:
368 tmpbeam.X = line.split()[1]
372 if line.find(
'Y0') != -1:
373 tmpbeam.Y = line.split()[1]
377 if line.find(
'Z0') != -1
and line.find(
'sigmaZ0') == -1:
378 tmpbeam.Z = line.split()[1]
381 if line.find(
'sigmaZ0') !=-1:
382 tmpbeam.sigmaZ = line.split()[1]
385 if line.find(
'dxdz') != -1:
386 tmpbeam.dxdz = line.split()[1]
389 if line.find(
'dydz') != -1:
390 tmpbeam.dydz = line.split()[1]
393 if line.find(
'BeamWidthX') != -1:
394 tmpbeam.beamWidthX = line.split()[1]
397 if line.find(
'BeamWidthY') != -1:
398 tmpbeam.beamWidthY = line.split()[1]
401 if line.find(
'Cov(0,j)') != -1:
402 tmpbeam.Xerr =
str(math.sqrt(
float( line.split()[1] ) ) )
404 if line.find(
'Cov(1,j)') != -1:
405 tmpbeam.Yerr =
str(math.sqrt(
float( line.split()[2] ) ) )
407 if line.find(
'Cov(2,j)') != -1:
408 tmpbeam.Zerr =
str(math.sqrt(
float( line.split()[3] ) ) )
410 if line.find(
'Cov(3,j)') != -1:
411 tmpbeam.sigmaZerr =
str(math.sqrt(
float( line.split()[4] ) ) )
413 if line.find(
'Cov(4,j)') != -1:
414 tmpbeam.dxdzerr =
str(math.sqrt(
float( line.split()[5] ) ) )
416 if line.find(
'Cov(5,j)') != -1:
417 tmpbeam.dydzerr =
str(math.sqrt(
float( line.split()[6] ) ) )
419 if line.find(
'Cov(6,j)') != -1:
420 tmpbeam.beamWidthXerr =
str(math.sqrt(
float( line.split()[7] ) ) )
421 tmpbeam.beamWidthYerr = tmpbeam.beamWidthXerr
423 if line.find(
'LumiRange') != -1:
424 if IOVbase==
"lumibase":
425 tmpbeam.IOVfirst = line.split()[1]
426 tmpbeam.IOVlast = line.split()[3]
428 if line.find(
'Runnumber') != -1:
429 tmpbeam.Run = line.split()[1]
430 if IOVbase ==
"runbase":
431 tmpbeam.IOVfirst = line.split()[1]
432 tmpbeam.IOVlast = line.split()[1]
434 tmpBX = line.split()[3]
436 if line.find(
'BeginTimeOfFit') != -1:
437 tmpbeam.IOVBeginTime = line.split()[1] +
" "+line.split()[2] +
" "+line.split()[3]
438 if IOVbase ==
"timebase":
439 tmpbeam.IOVfirst = time.mktime( time.strptime(line.split()[1] +
" " + line.split()[2] +
" " + line.split()[3],
"%Y.%m.%d %H:%M:%S %Z") )
441 if line.find(
'EndTimeOfFit') != -1:
442 tmpbeam.IOVEndTime = line.split()[1] +
" "+line.split()[2] +
" "+line.split()[3]
443 if IOVbase ==
"timebase":
444 tmpbeam.IOVlast = time.mktime( time.strptime(line.split()[1] +
" " + line.split()[2] +
" " + line.split()[3],
"%Y.%m.%d %H:%M:%S %Z") )
446 if tmpbeamsize == 20:
447 if IOVbase==
"lumibase":
448 tmprunfirst =
int(firstRun.split(
":")[0])
449 tmprunlast =
int(lastRun.split(
":")[0])
450 tmplumifirst =
int(firstRun.split(
":")[1])
451 tmplumilast =
int(lastRun.split(
":")[1])
452 acceptiov1 = acceptiov2 =
False 454 if tmprunfirst == tmprunlast
and int(tmpbeam.Run)==tmprunfirst:
455 if int(tmpbeam.IOVfirst) >= tmplumifirst
and int(tmpbeam.IOVlast)<=tmplumilast:
456 acceptiov1 = acceptiov2 =
True 458 elif int(tmpbeam.Run) == tmprunfirst:
459 if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 =
True 460 elif int(tmpbeam.Run) == tmprunlast:
461 if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 =
True 462 elif tmprunfirst <=
int(tmpbeam.Run)
and tmprunlast >=
int(tmpbeam.Run):
463 acceptiov1 = acceptiov2 =
True 465 if acceptiov1
and acceptiov2:
466 if tmpbeam.Type != 2:
467 print(
"invalid fit, skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
468 elif isnan(tmpbeam.Z)
or isnan(tmpbeam.Zerr)
or isnan(tmpbeam.sigmaZerr)
or isnan(tmpbeam.beamWidthXerr)
or isnan(tmpbeam.beamWidthYerr):
469 print(
"invalid fit, NaN values!! skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
471 if (tmpBX
in maplist) ==
False:
472 maplist[tmpBX] = [tmpbeam]
474 maplist[tmpBX].
append(tmpbeam)
476 listbeam.append(tmpbeam)
478 elif int(tmpbeam.IOVfirst) >=
int(firstRun)
and int(tmpbeam.IOVlast) <=
int(lastRun):
479 if tmpbeam.Type != 2:
480 print(
"invalid fit, skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
481 elif isnan(tmpbeam.Z)
or isnan(tmpbeam.Zerr)
or isnan(tmpbeam.sigmaZerr)
or isnan(tmpbeam.beamWidthXerr)
or isnan(tmpbeam.beamWidthYerr):
482 print(
"invalid fit, NaN values!! skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
484 listbeam.append(tmpbeam)
493 if line.find(
'X0') != -1:
494 tmpbeam.X = line.split()[2]
495 tmpbeam.Xerr = line.split()[4]
498 if line.find(
'Y0') != -1:
499 tmpbeam.Y = line.split()[2]
500 tmpbeam.Yerr = line.split()[4]
503 if line.find(
'Z0') != -1
and line.find(
'Sigma Z0') == -1:
504 tmpbeam.Z = line.split()[2]
505 tmpbeam.Zerr = line.split()[4]
508 if line.find(
'Sigma Z0') !=-1:
509 tmpbeam.sigmaZ = line.split()[3]
510 tmpbeam.sigmaZerr = line.split()[5]
512 if line.find(
'dxdz') != -1:
513 tmpbeam.dxdz = line.split()[2]
514 tmpbeam.dxdzerr = line.split()[4]
516 if line.find(
'dydz') != -1:
517 tmpbeam.dydz = line.split()[2]
518 tmpbeam.dydzerr = line.split()[4]
520 if line.find(
'Beam Width X') != -1:
521 tmpbeam.beamWidthX = line.split()[4]
522 tmpbeam.beamWidthXerr = line.split()[6]
524 if line.find(
'Beam Width Y') != -1:
525 tmpbeam.beamWidthY = line.split()[4]
526 tmpbeam.beamWidthYerr = line.split()[6]
529 if line.find(
'for runs') != -1:
531 tmpbeam.Run = line.split()[2]
532 if IOVbase ==
"runbase":
533 tmpbeam.IOVfirst = line.split()[2]
534 tmpbeam.IOVlast = line.split()[4]
536 if line.find(
'LumiSection') != -1:
537 if IOVbase==
"lumibase":
538 tmpbeam.IOVfirst = line.split()[10]
539 tmpbeam.IOVlast = line.split()[10]
541 if tmpbeamsize == 10:
543 if IOVbase==
"lumibase":
544 tmprunfirst =
int(firstRun.split(
":")[0])
545 tmprunlast =
int(lastRun.split(
":")[0])
546 tmplumifirst =
int(firstRun.split(
":")[1])
547 tmplumilast =
int(lastRun.split(
":")[1])
548 acceptiov1 = acceptiov2 =
False 550 if tmprunfirst == tmprunlast
and int(tmpbeam.Run)==tmprunfirst:
551 if int(tmpbeam.IOVfirst) >= tmplumifirst
and int(tmpbeam.IOVlast)<=tmplumilast:
552 acceptiov1 = acceptiov2 =
True 554 elif int(tmpbeam.Run) == tmprunfirst:
555 if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 =
True 556 elif int(tmpbeam.Run) == tmprunlast:
557 if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 =
True 558 elif tmprunfirst <=
int(tmpbeam.Run)
and tmprunlast >=
int(tmpbeam.Run):
559 acceptiov1 = acceptiov2 =
True 561 if acceptiov1
and acceptiov2:
562 if isnan(tmpbeam.Z)
or isnan(tmpbeam.Zerr)
or isnan(tmpbeam.sigmaZerr)
or isnan(tmpbeam.beamWidthXerr)
or isnan(tmpbeam.beamWidthYerr):
563 print(
"invalid fit, NaN values!! skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
565 if (tmpBX
in maplist) ==
False:
566 maplist[tmpBX] = [tmpbeam]
568 maplist[tmpBX].
append(tmpbeam)
570 listbeam.append(tmpbeam)
572 elif int(tmpbeam.IOVfirst) >=
int(firstRun)
and int(tmpbeam.IOVlast) <=
int(lastRun):
573 if isnan(tmpbeam.Z)
or isnan(tmpbeam.Zerr)
or isnan(tmpbeam.sigmaZerr)
or isnan(tmpbeam.beamWidthXerr)
or isnan(tmpbeam.beamWidthYerr):
574 print(
"invalid fit, NaN values!! skip Run "+
str(tmpbeam.Run)+
" IOV: "+
str(tmpbeam.IOVfirst) +
" to "+
str(tmpbeam.IOVlast))
576 listbeam.append(tmpbeam)
583 print(
" got total number of IOVs = " +
str(len(listbeam)) +
" from file " + fileName)
594 if IOVbase ==
"lumibase":
595 listbeam.sort( cmp = cmp_list_lumi )
597 listbeam.sort( cmp = cmp_list_run )
601 for ii
in range(0,len(listbeam)):
603 datax = ibeam.IOVfirst
605 if datax ==
'0' and IOVbase ==
"runbase":
606 print(
" iov = 0? skip this IOV = "+
str(ibeam.IOVfirst) +
" to " +
str(ibeam.IOVlast))
607 tmpremovelist.append(ibeam)
609 if ii < len(listbeam) -1:
611 if IOVbase ==
"lumibase":
612 if ibeam.Run == listbeam[ii+1].Run
and ibeam.IOVfirst == listbeam[ii+1].IOVfirst:
613 print(
" duplicate IOV = "+datax+
", keep only last duplicate entry")
614 tmpremovelist.append(ibeam)
615 elif datax == listbeam[ii+1].IOVfirst:
616 print(
" duplicate IOV = "+datax+
", keep only last duplicate entry")
617 tmpremovelist.append(ibeam)
619 for itmp
in tmpremovelist:
620 listbeam.remove(itmp)
632 maxNlumis = 999999999
633 for ii
in range(0,len(listbeam)):
638 tmpbeam.IOVfirst = ibeam.IOVfirst
639 tmpbeam.IOVBeginTime = ibeam.IOVBeginTime
640 tmpbeam.Run = ibeam.Run
647 if ii < len(listbeam) - 1:
648 inextbeam = listbeam[ii+1]
650 if ii < len(listbeam) -2:
651 iNNbeam = listbeam[ii+2]
653 print(
"close payload because end of data has been reached. Run "+ibeam.Run)
656 if ibeam.Run != inextbeam.Run:
657 print(
"close payload because end of run "+ibeam.Run)
660 if countlumi == maxNlumis -1:
661 print(
"close payload because maximum lumi sections accumulated within run "+ibeam.Run)
665 (tmpbeam.X, tmpbeam.Xerr) =
weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
666 (tmpbeam.Y, tmpbeam.Yerr) =
weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
667 (tmpbeam.Z, tmpbeam.Zerr) =
weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
668 (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) =
weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
669 (tmpbeam.dxdz, tmpbeam.dxdzerr) =
weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
670 (tmpbeam.dydz, tmpbeam.dydzerr) =
weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
672 (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) =
weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
673 (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) =
weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
685 limit =
float(ibeam.beamWidthX)/2.
686 if limit < min_limit: limit = min_limit
689 adelta1 =
delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr)
691 adelta1dxdz =
delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
692 adelta2dxdz = (0.,1.e9)
693 adelta1dydz =
delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
694 adelta2dydz = (0.,1.e9)
695 adelta1widthx =
delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
696 adelta2widthx = (0.,1.e9)
697 adelta1widthy =
delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr)
698 adelta2widthy = (0.,1.e9)
699 adelta1z0 =
delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr)
700 adelta1sigmaZ =
delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr)
702 if iNNbeam.Type != -1:
703 adelta2 =
delta(inextbeam.X, inextbeam.Xerr, iNNbeam.X, iNNbeam.Xerr)
704 adelta2dxdz =
delta(inextbeam.dxdz, inextbeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr)
705 adelta2dydz =
delta(inextbeam.dydz, inextbeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr)
706 adelta2widthx =
delta(inextbeam.beamWidthX, inextbeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr)
707 adelta2widthy =
delta(inextbeam.beamWidthY, inextbeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr)
709 deltaX =
deltaSig(adelta1) > 3.5
and adelta1[0] >= limit
710 if ii < len(listbeam) -2:
711 if deltaX==
False and adelta1[0]*adelta2[0] > 0.
and math.fabs(adelta1[0]+adelta2[0]) >= limit:
714 elif deltaX==
True and adelta1[0]*adelta2[0]<=0
and adelta2[0] != 0
and math.fabs(adelta1[0]/adelta2[0]) > 0.33
and math.fabs(adelta1[0]/adelta2[0]) < 3:
721 adelta1 =
delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr)
723 if iNNbeam.Type != -1:
724 adelta2 =
delta(inextbeam.Y, inextbeam.Yerr, iNNbeam.Y, iNNbeam.Yerr)
726 deltaY =
deltaSig(adelta1) > 3.5
and adelta1[0] >= limit
727 if ii < len(listbeam) -2:
728 if deltaY==
False and adelta1[0]*adelta2[0] > 0.
and math.fabs(adelta1[0]+adelta2[0]) >= limit:
730 elif deltaY==
True and adelta1[0]*adelta2[0]<=0
and adelta2[0] != 0
and math.fabs(adelta1[0]/adelta2[0]) > 0.33
and math.fabs(adelta1[0]/adelta2[0]) < 3:
734 limit =
float(ibeam.sigmaZ)/2.
735 deltaZ =
deltaSig(adelta1z0) > 3.5
and math.fabs(adelta1z0[0]) >= limit
737 deltasigmaZ =
deltaSig(adelta1sigmaZ) > 5.0
740 adelta =
delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
742 if deltadxdz
and adelta1dxdz[0]*adelta2dxdz[0]<=0
and adelta2dxdz[0] != 0
and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) > 0.33
and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) < 3:
745 adelta =
delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
747 if deltadydz
and adelta1dydz[0]*adelta2dydz[0]<=0
and adelta2dydz[0] != 0
and math.fabs(adelta1dydz[0]/adelta2dydz[0]) > 0.33
and math.fabs(adelta1dydz[0]/adelta2dydz[0]) < 3:
750 adelta =
delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
752 if deltawidthX
and adelta1widthx[0]*adelta2widthx[0]<=0
and adelta2widthx[0] != 0
and math.fabs(adelta1widthx[0]/adelta2widthx[0]) > 0.33
and math.fabs(adelta1widthx[0]/adelta2widthx[0]) < 3:
755 adelta =
delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr)
757 if deltawidthY
and adelta1widthy[0]*adelta2widthy[0]<=0
and adelta2widthy[0] != 0
and math.fabs(adelta1widthy[0]/adelta2widthy[0]) > 0.33
and math.fabs(adelta1widthy[0]/adelta2widthy[0]) < 3:
771 if deltaX
or deltaY
or deltaZ
or deltasigmaZ
or deltadxdz
or deltadydz
or deltawidthX
or deltawidthY:
779 print(
"close payload because of movement in X= "+
str(deltaX)+
", Y= "+
str(deltaY) +
", Z= "+
str(deltaZ)+
", sigmaZ= "+
str(deltasigmaZ)+
", dxdz= "+
str(deltadxdz)+
", dydz= "+
str(deltadydz)+
", widthX= "+
str(deltawidthX)+
", widthY= "+
str(deltawidthY))
782 tmpbeam.IOVlast = ibeam.IOVlast
783 tmpbeam.IOVEndTime = ibeam.IOVEndTime
784 print(
" Run: "+tmpbeam.Run +
" Lumi1: "+
str(tmpbeam.IOVfirst) +
" Lumi2: "+
str(tmpbeam.IOVlast))
785 newlistbeam.append(tmpbeam)
791 payloadfile = open(fileName,
"w")
792 for iload
in newlistbeam:
793 dump( iload, payloadfile )
801 lastPayload = listbeam[0]
805 for ii
in range(0,len(listbeam)):
808 deltaX =
delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr) > 1.5
809 deltaY =
delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr) > 1.5
810 deltaZ =
delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr) > 2.5
812 deltasigmaZ =
delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr) > 2.5
813 deltadxdz =
delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr) > 2.5
814 deltadydz =
delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr) > 2.5
816 deltawidthX =
delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr) > 3
817 deltawidthY =
delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr) > 3
831 if deltaX
or deltaY
or deltaZ
or deltasigmaZ
or deltadxdz
or deltadydz
or deltawidthX
or deltawidthY:
841 print(
"close payload because of movement in X= "+
str(deltaX)+
", Y= "+
str(deltaY) +
", Z= "+
str(deltaZ)+
", sigmaZ= "+
str(deltasigmaZ)+
", dxdz= "+
str(deltadxdz)+
", dydz= "+
str(deltadydz)+
", widthX= "+
str(deltawidthX)+
", widthY= "+
str(deltawidthY))
844 if ii >= len(listbeam) - 1
or listbeam[ii].Run != listbeam[ii+1].Run :
845 print(
"close payload because end of run has been reached. Run " + listbeam[ii].Run)
857 for ibeam
in listbeam[firstToUse:lastToUse]:
858 (tmpbeam.X, tmpbeam.Xerr) =
weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
859 (tmpbeam.Y, tmpbeam.Yerr) =
weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
860 (tmpbeam.Z, tmpbeam.Zerr) =
weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
861 (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) =
weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
862 (tmpbeam.dxdz, tmpbeam.dxdzerr) =
weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
863 (tmpbeam.dydz, tmpbeam.dydzerr) =
weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
865 (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) =
weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
866 (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) =
weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
867 tmpbeam.IOVfirst = listbeam[firstToUse].IOVfirst
868 tmpbeam.IOVBeginTime = listbeam[firstToUse].IOVBeginTime
869 tmpbeam.Run = listbeam[firstToUse].Run
871 tmpbeam.IOVlast = listbeam[lastToUse].IOVlast
872 tmpbeam.IOVEndTime = listbeam[lastToUse].IOVEndTime
873 newlistbeam.append(tmpbeam)
874 firstToUse = lastToUse+1
875 print(
"Run: " + tmpbeam.Run +
" Lumi1: " +
str(tmpbeam.IOVfirst) +
" Lumi2: " +
str(tmpbeam.IOVlast))
877 payloadfile = open(fileName,
"w")
878 for iload
in newlistbeam:
879 dump( iload, payloadfile )
883 def writeSqliteFile(sqliteFileName,tagName,timeType,beamSpotFile,sqliteTemplateFile,tmpDir="/tmp/"):
884 writeDBOut = tmpDir +
"write2DB_" + tagName +
".py" 885 wFile = open(sqliteTemplateFile)
886 wNewFile = open(writeDBOut,
'w')
888 writeDBTags = [(
'SQLITEFILE',
'sqlite_file:' + sqliteFileName),
890 (
'TIMETYPE',timeType),
891 (
'BEAMSPOTFILE',beamSpotFile)]
894 for itag
in writeDBTags:
895 line = line.replace(itag[0],itag[1])
899 print(
"writing sqlite file ...")
900 status_wDB = commands.getstatusoutput(
'cmsRun '+ writeDBOut)
903 os.system(
"rm -f " + writeDBOut)
904 return not status_wDB[0]
908 readDBOut = tmpDir +
"readDB_" + tagName +
".py" 910 rFile = open(sqliteTemplateFile)
911 rNewFile = open(readDBOut,
'w')
913 readDBTags = [(
'SQLITEFILE',
'sqlite_file:' + sqliteFileName),
917 for itag
in readDBTags:
918 line = line.replace(itag[0],itag[1])
922 status_rDB = commands.getstatusoutput(
'cmsRun '+ readDBOut)
924 outtext = status_rDB[1]
926 os.system(
"rm -f " + readDBOut)
927 return not status_rDB[0]
930 def appendSqliteFile(combinedSqliteFileName, sqliteFileName, tagName, IOVSince, IOVTill ,tmpDir="/tmp/"):
931 aCommand =
"conddb_import -c sqlite_file:" + tmpDir + combinedSqliteFileName +
" -f sqlite_file:" + sqliteFileName +
" -i " + tagName +
" -t " + tagName +
" -b " + IOVSince +
" -e " + IOVTill
933 std = commands.getstatusoutput(aCommand)
940 acmd =
"chmod a+w " + sqliteFileDirName + sqliteFileName +
".txt" 941 outcmd = commands.getstatusoutput(acmd)
945 print(
"Can't change permission to file: " + sqliteFileDirName + sqliteFileName +
".txt")
948 acmd =
"cp " + sqliteFileDirName + sqliteFileName +
".db " + sqliteFileDirName + sqliteFileName +
".txt ." 950 outcmd = commands.getstatusoutput(acmd)
953 print(
"Couldn't cd to " + sqliteFileDirName)
956 acmd =
"tar -cvjf " + sqliteFileName +
".tar.bz2 " + sqliteFileName +
".db " + sqliteFileName +
".txt" 958 outcmd = commands.getstatusoutput(acmd)
961 print(
"Couldn't zip the files!")
964 acmd =
"chmod a+w " + sqliteFileName +
".tar.bz2" 965 outcmd = commands.getstatusoutput(acmd)
969 print(
"Can't change permission to file: " + sqliteFileDirName + sqliteFileName +
".tar.bz2")
972 acmd =
"scp -p " + sqliteFileName +
".tar.bz2" +
" webcondvm.cern.ch:" + dropbox
974 outcmd = commands.getstatusoutput(acmd)
977 print(
"Couldn't scp the files to DropBox!")
981 acmd =
"mv " + sqliteFileName +
".tar.bz2 " + sqliteFileDirName
983 outcmd = commands.getstatusoutput(acmd)
986 print(
"Couldn't mv the file to " + sqliteFileDirName)
989 acmd =
"rm " + sqliteFileName +
".db " + sqliteFileName +
".txt" 991 outcmd = commands.getstatusoutput(acmd)
994 print(
"Couldn't rm the db and txt files")
def readBeamSpotFile(fileName, listbeam=[], IOVbase="runbase", firstRun='1', lastRun='4999999999')
def dumpValues(beam, file)
def appendSqliteFile(combinedSqliteFileName, sqliteFileName, tagName, IOVSince, IOVTill, tmpDir="/tmp/")
def cmp_list_run(a, b)
end lumi tools
S & print(S &os, JobReport::InputFile const &f)
def createWeightedPayloads(fileName, listbeam=[], weighted=True)
CREATE FILE FOR PAYLOADS.
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
def uploadSqliteFile(sqliteFileDirName, sqliteFileName, dropbox="/DropBox")
def weight(x1, x1err, x2, x2err)
def parse(docstring, arglist=None)
def sortAndCleanBeamList(listbeam=[], IOVbase="lumibase")
Sort and clean list of data for consecutive duplicates and bad fits.
def readSqliteFile(sqliteFileName, tagName, sqliteTemplateFile, tmpDir="/tmp/")
def delta(x, xerr, nextx, nextxerr)
def timeoutManager(type, timeout=-1, fileName=".timeout")
def writeSqliteFile(sqliteFileName, tagName, timeType, beamSpotFile, sqliteTemplateFile, tmpDir="/tmp/")
def sendEmail(mailList, error)
General utilities.
def pack(high, low)
lumi tools CondCore/Utilities/python/timeUnitHelper.py
def createWeightedPayloadsNew(fileName, listbeam=[], weighted=True)
def cp(fromDir, toDir, listOfFiles, overwrite=False, smallList=False)