CMS 3D CMS Logo

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