CMS 3D CMS Logo

lumiregperbunch.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys,commands,os,calendar
3 from ROOT import gDirectory,TFile
4 
5 from online_beamspot_reader import BeamspotMeasurement
6 from online_beamspot_reader import paragraphs, \
7  start_of_new_beamspot_measurement
8 
9 FILL=''
10 OUTDIR="./perbunchfiles/"
11 
12 runlstime={}
13 
14 
15 class bsmeas(object):
16  def __init__(self, x=None,y=None,z=None,ex=None,ey=None,ez=None,
17  wx=None,wy=None,wz=None,ewx=None,ewy=None,ewz=None,
18  dxdz=None,dydz=None,edxdz=None,edydz=None):
19  self.x = x
20  self.y = y
21  self.z = z
22  self.ex = ex
23  self.ey = ey
24  self.ez = ez
25  self.wx = ex
26  self.wy = ey
27  self.wz = ez
28  self.ewx = ex
29  self.ewy = ey
30  self.ewz = ez
31  self.dxdz = dxdz
32  self.dydz = dydz
33  self.edxdz = edxdz
34  self.edydz = edydz
35 
36 
37 
38 
39 def timeof(run,lumisection):
40  # first check if this run is already in the list, otherwise read it
41  if run not in runlstime.keys():
42  print "Reading lumi time from lumireg localcopy files"
43  filename="localcopy/BeamFitResults_Run"+run+".txt"
44  if not os.path.exists(filename):
45  print "WARNING: file ",filename," does not exist. Returning null."
46  return -1
47 
48  # reading file
49  lstime={}
50  in_file = open(filename)
51  pieces = paragraphs(in_file,start_of_new_beamspot_measurement,True)
52  for piece in pieces:
53  if len(piece) < 1:
54  continue
55  try:
56  tmp = BeamspotMeasurement(piece)
57  except Exception as err:
58  print >> sys.stderr, \
59  " ERROR Found corrupt " \
60  "beamspot measurement entry!"
61  print >> sys.stderr, \
62  " !!! %s !!!" % str(err)
63  continue
64  # Argh!
65  runfromfile=tmp.run_number
66  (lumimin,lumimax)=tmp.lumi_range
67  time_begin=tmp.time_begin
68  time_end=tmp.time_end
69  time_begin=calendar.timegm(time_begin.timetuple())
70  time_end=calendar.timegm(time_end.timetuple())-23 # assume end of lumisection
71  lstime[lumimin]=time_begin
72  lstime[lumimax]=time_end
73 
74  # order lumisections and make a list
75  lslist=lstime.keys()
76  lslist.sort()
77  lstimesorted=[]
78  for ls in lslist:
79  lstimesorted.append((ls,lstime[ls]))
80  runlstime[run]=lstimesorted
81 
82  # print runfromfile
83  # print lumirange
84  # print time_begin, calendar.timegm(time_begin.timetuple())
85  # print time_end, calendar.timegm(time_end.timetuple())
86 
87  in_file.close()
88  # now give a time
89  dcloselumi=999999
90  closelumi=-1
91  closetime=-1
92  lstimesorted=runlstime[run]
93 
94  for pair in lstimesorted:
95  (lumi,time)=pair
96  if abs(lumisection-lumi)<dcloselumi:
97  dcloselumi=abs(lumisection-lumi)
98  closelumi=lumi
99  closetime=time
100  if closelumi!=-1:
101  finaltime=closetime+(lumisection-closelumi)*23
102  else:
103  finaltime=-1
104 
105  return finaltime
106 
107 
108 def readroot():
109  rls=[]
110  bxlist=[]
111  allmeas={}
112 
113  DIRES=['X0','Y0','Z0','width_X0','Width_Y0','Sigma_Z0','dxdz','dydz']
114  # DIRES=['X0']
115  rootfile="BxAnalysis_Fill_"+FILL+".root"
116  filein=TFile(rootfile)
117  for dire in DIRES:
118  filein.cd(dire)
119  # get list of histograms
120  histolist=gDirectory.GetListOfKeys()
121  iter = histolist.MakeIterator()
122  key = iter.Next()
123  while key:
124  if key.GetClassName() == 'TH1F':
125  td = key.ReadObj()
126  histoname = td.GetName()
127  if "bx" in histoname:
128 # print histoname
129  bx=histoname.split('_')[-1]
130  if bx not in bxlist:
131 # this is to be removed
132 # if len(bxlist)>=2:
133 # key = iter.Next()
134 # continue
135 # end to be removed
136  bxlist.append(bx)
137  allmeas[bx]={}
138 # print bx,histoname
139  histo=gDirectory.Get(histoname)
140  nbin=histo.GetNbinsX()
141 
142  thisbx=allmeas[bx]
143 
144  for bin in range(1,nbin+1):
145  label=histo.GetXaxis().GetBinLabel(bin)
146  label=label.strip()
147  if ":" not in label:
148  # not a valid label of type run:lumi-lumi, skip it
149  continue
150 
151  cont=histo.GetBinContent(bin)
152  if cont!=cont:
153  # it's a nan
154  cont=-999.0
155  err=histo.GetBinError(bin)
156  if err!=err:
157  err=-999.0
158  # if len(bxlist)==1:
159  # rls.append(label)
160  # print label
161  # else:
162  if label not in rls:
163  print "New range:",label," found in ",histoname
164  rls.append(label)
165 
166  if label in thisbx.keys():
167  thismeas=thisbx[label]
168  else:
169  thisbx[label]=bsmeas()
170  thismeas=thisbx[label]
171  # now filling up
172  if dire=='X0':
173  thismeas.x=cont
174  thismeas.ex=err
175  if dire=='Y0':
176  thismeas.y=cont
177  thismeas.ey=cont
178  if dire=='Z0':
179  thismeas.z=cont
180  thismeas.ez=err
181  if dire=='width_X0':
182  thismeas.wx=cont
183  thismeas.ewx=err
184  if dire=='Width_Y0':
185  thismeas.wy=cont
186  thismeas.ewy=err
187  if dire=='Sigma_Z0':
188  thismeas.wz=cont
189  thismeas.ewz=err
190  if dire=='dxdz':
191  thismeas.dxdz=cont
192  thismeas.edxdz=err
193  if dire=='dydz':
194  thismeas.dydz=cont
195  thismeas.edydz=err
196 
197 
198  key = iter.Next()
199 
200 
201  # for name in pippo:
202  # print name
203 
204  filein.Close()
205 
206  # let's try to show it
207 # for bx in allmeas.keys():
208 # print "bx=",bx
209 # bxmeas=allmeas[bx]
210 # for meas in bxmeas.keys():
211 # print "meas time=",meas
212 # thismeas=bxmeas[meas]
213 # print thismeas.x,thismeas.ex,thismeas.y,thismeas.ey,thismeas.z,thismeas.ez
214 # print thismeas.wx,thismeas.ewx,thismeas.wy,thismeas.ewy,thismeas.wz,thismeas.ewz
215 # print thismeas.dxdz,thismeas.edxdz,thismeas.dydz,thismeas.edydz
216  return allmeas
217 
218 if __name__ == '__main__':
219  if len(sys.argv)!=2:
220  print "Usage: :",sys.argv[0]," <fillnr>"
221  sys.exit(1)
222  FILL=sys.argv[1]
223 
224  allmeas=readroot()
225  # now write it
226 
227  for bx in allmeas.keys():
228  print "writing bx=",bx
229  bxmeas=allmeas[bx]
230  lines={}
231  for meas in bxmeas.keys():
232  # first derive time in unix time
233  runno=meas.split(':')[0]
234  runno=runno.strip()
235  lumirange=meas.split(':')[1]
236  lumimin=lumirange.split('-')[0]
237  lumimin=lumimin.strip()
238  lumimax=lumirange.split('-')[1]
239  lumimax=lumimax.strip()
240  lumimid=int((int(lumimin)+int(lumimax))/2.)
241  meastime=timeof(runno,lumimid)
242  print runno,str(lumimid),meastime
243 
244  thismeas=bxmeas[meas]
245 # print thismeas.x,thismeas.ex,thismeas.y,thismeas.ey,thismeas.z,thismeas.ez
246 # print thismeas.wx,thismeas.ewx,thismeas.wy,thismeas.ewy,thismeas.wz,thismeas.ewz
247 # print thismeas.dxdz,thismeas.edxdz,thismeas.dydz,thismeas.edydz
248  line=str(meastime)+" "
249  line+="1 "
250  line+="%11.7f %11.7f %11.7f %11.7f %11.7f %11.7f " % (-thismeas.x*10,thismeas.ex*10,
251  thismeas.y*10,thismeas.ey*10,
252  -thismeas.z*10,thismeas.ez*10)
253  line+="%11.7f %11.7f %11.7f %11.7f %11.7f %11.7f " % (thismeas.wx*10,thismeas.ewx*10,
254  thismeas.wy*10,thismeas.ewy*10,
255  thismeas.wz*10,thismeas.ewz*10)
256  line+="%11.7f %11.7f %11.7f %11.7f" % (thismeas.dxdz,thismeas.edxdz,-thismeas.dydz,thismeas.edydz)
257  line+="\n"
258 
259  # validate it
260  if (thismeas.x != 0.0 and thismeas.y != 0.0 and thismeas.z != 0.0 and
261  thismeas.wx != 0.0 and thismeas.wy != 0.0 and thismeas.wz != 0.0 and
262  thismeas.dxdz != 0.0 and thismeas.dydz != 0.0 ):
263  lines[meastime]=line
264 
265 
266  # now write it
267  WORKDIR=OUTDIR+FILL
268  os.system("mkdir -p "+WORKDIR)
269  rfbucket=(int(bx)-1)*10+1
270  filename=WORKDIR+"/"+FILL+"_lumireg_"+str(rfbucket)+"_CMS.txt"
271  file=open(filename,'w')
272  sortedtimes=lines.keys()
273  sortedtimes.sort()
274  for meastime in sortedtimes:
275  file.write(lines[meastime])
276  file.close()
277 
278  # print timeof("168264",25)
def __init__(self, x=None, y=None, z=None, ex=None, ey=None, ez=None, wx=None, wy=None, wz=None, ewx=None, ewy=None, ewz=None, dxdz=None, dydz=None, edxdz=None, edydz=None)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def timeof(run, lumisection)