CMS 3D CMS Logo

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