CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/RecoVertex/BeamSpotProducer/scripts/root/BxAnalysis/lumiregperbunch.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import sys,commands,os,calendar
00003 from ROOT import gDirectory,TFile
00004 
00005 from online_beamspot_reader import BeamspotMeasurement
00006 from online_beamspot_reader import paragraphs, \
00007      start_of_new_beamspot_measurement
00008 
00009 FILL=''
00010 OUTDIR="./perbunchfiles/"
00011 
00012 runlstime={}
00013 
00014 
00015 class bsmeas(object):
00016     def __init__(self, x=None,y=None,z=None,ex=None,ey=None,ez=None,
00017                  wx=None,wy=None,wz=None,ewx=None,ewy=None,ewz=None,
00018                  dxdz=None,dydz=None,edxdz=None,edydz=None):
00019         self.x = x
00020         self.y = y
00021         self.z = z
00022         self.ex = ex
00023         self.ey = ey
00024         self.ez = ez
00025         self.wx = ex
00026         self.wy = ey
00027         self.wz = ez
00028         self.ewx = ex
00029         self.ewy = ey
00030         self.ewz = ez
00031         self.dxdz = dxdz
00032         self.dydz = dydz
00033         self.edxdz = edxdz
00034         self.edydz = edydz
00035         
00036 
00037 
00038 
00039 def timeof(run,lumisection):
00040     # first check if this run is already in the list, otherwise read it
00041     if run not in runlstime.keys():
00042         print "Reading lumi time from lumireg localcopy files"
00043         filename="localcopy/BeamFitResults_Run"+run+".txt"
00044         if not os.path.exists(filename):
00045             print "WARNING: file ",filename," does not exist. Returning null."
00046             return -1
00047 
00048         # reading file
00049         lstime={}
00050         in_file = open(filename)
00051         pieces = paragraphs(in_file,start_of_new_beamspot_measurement,True)
00052         for piece in pieces:
00053             if len(piece) < 1:
00054                 continue
00055             try:
00056                 tmp = BeamspotMeasurement(piece)
00057             except Exception, err:
00058                 print >> sys.stderr, \
00059                       "    ERROR Found corrupt " \
00060                       "beamspot measurement entry!"
00061                 print >> sys.stderr, \
00062                       "    !!! %s !!!" % str(err)
00063                 continue
00064             # Argh!
00065             runfromfile=tmp.run_number
00066             (lumimin,lumimax)=tmp.lumi_range
00067             time_begin=tmp.time_begin
00068             time_end=tmp.time_end
00069             time_begin=calendar.timegm(time_begin.timetuple())
00070             time_end=calendar.timegm(time_end.timetuple())-23 # assume end of lumisection
00071             lstime[lumimin]=time_begin
00072             lstime[lumimax]=time_end
00073             
00074         # order lumisections and make a list
00075         lslist=lstime.keys()
00076         lslist.sort()
00077         lstimesorted=[]
00078         for ls in lslist:
00079             lstimesorted.append((ls,lstime[ls]))
00080         runlstime[run]=lstimesorted
00081 
00082         #            print runfromfile
00083         #            print lumirange
00084         #            print time_begin, calendar.timegm(time_begin.timetuple())
00085         #            print time_end, calendar.timegm(time_end.timetuple())
00086         
00087         in_file.close()
00088     # now give a time
00089     dcloselumi=999999
00090     closelumi=-1
00091     closetime=-1
00092     lstimesorted=runlstime[run]
00093     
00094     for pair in lstimesorted:
00095         (lumi,time)=pair
00096         if abs(lumisection-lumi)<dcloselumi:
00097             dcloselumi=abs(lumisection-lumi)
00098             closelumi=lumi
00099             closetime=time
00100     if closelumi!=-1:
00101         finaltime=closetime+(lumisection-closelumi)*23
00102     else:
00103         finaltime=-1
00104         
00105     return finaltime
00106 
00107 
00108 def readroot():
00109     rls=[]
00110     bxlist=[]
00111     allmeas={}
00112     
00113     DIRES=['X0','Y0','Z0','width_X0','Width_Y0','Sigma_Z0','dxdz','dydz']
00114     # DIRES=['X0']
00115     rootfile="BxAnalysis_Fill_"+FILL+".root"
00116     filein=TFile(rootfile)
00117     for dire in DIRES:
00118         filein.cd(dire)
00119         # get list of histograms
00120         histolist=gDirectory.GetListOfKeys()
00121         iter = histolist.MakeIterator()
00122         key = iter.Next()
00123         while key:
00124             if key.GetClassName() == 'TH1F':
00125                 td = key.ReadObj()
00126                 histoname = td.GetName()
00127                 if "bx" in histoname:
00128 #                    print histoname
00129                     bx=histoname.split('_')[-1]
00130                     if bx not in bxlist:
00131 # this is to be removed                        
00132 #                        if len(bxlist)>=2:
00133 #                            key = iter.Next()
00134 #                            continue
00135 # end to be removed                        
00136                         bxlist.append(bx)
00137                         allmeas[bx]={}
00138 #                    print bx,histoname
00139                     histo=gDirectory.Get(histoname)
00140                     nbin=histo.GetNbinsX()
00141 
00142                     thisbx=allmeas[bx]
00143                     
00144                     for bin in range(1,nbin+1):
00145                         label=histo.GetXaxis().GetBinLabel(bin)
00146                         label=label.strip()
00147                         if ":" not in label:
00148                             # not a valid label of type run:lumi-lumi, skip it
00149                             continue
00150                         
00151                         cont=histo.GetBinContent(bin)
00152                         if cont!=cont:
00153                             # it's a nan
00154                             cont=-999.0
00155                         err=histo.GetBinError(bin)
00156                         if err!=err:
00157                             err=-999.0
00158                         #                        if len(bxlist)==1:
00159                         #                            rls.append(label)
00160                         #                            print label
00161                         #                        else:
00162                         if label not in rls:
00163                             print "New range:",label," found in ",histoname
00164                             rls.append(label)
00165                                 
00166                         if label in thisbx.keys():
00167                             thismeas=thisbx[label]
00168                         else:
00169                             thisbx[label]=bsmeas()
00170                             thismeas=thisbx[label]
00171                         #  now filling up
00172                         if dire=='X0':
00173                             thismeas.x=cont
00174                             thismeas.ex=err
00175                         if dire=='Y0':
00176                             thismeas.y=cont
00177                             thismeas.ey=cont
00178                         if dire=='Z0':
00179                             thismeas.z=cont
00180                             thismeas.ez=err
00181                         if dire=='width_X0':
00182                             thismeas.wx=cont
00183                             thismeas.ewx=err
00184                         if dire=='Width_Y0':
00185                             thismeas.wy=cont
00186                             thismeas.ewy=err
00187                         if dire=='Sigma_Z0':
00188                             thismeas.wz=cont
00189                             thismeas.ewz=err
00190                         if dire=='dxdz':
00191                             thismeas.dxdz=cont
00192                             thismeas.edxdz=err
00193                         if dire=='dydz':
00194                             thismeas.dydz=cont
00195                             thismeas.edydz=err
00196 
00197     
00198             key = iter.Next()
00199         
00200         
00201     #    for name in pippo:
00202     #        print name
00203     
00204     filein.Close()
00205 
00206     # let's try to show it
00207 #    for bx in allmeas.keys():
00208 #        print "bx=",bx
00209 #        bxmeas=allmeas[bx]
00210 #        for meas in bxmeas.keys():
00211 #            print "meas time=",meas
00212 #            thismeas=bxmeas[meas]
00213 #            print thismeas.x,thismeas.ex,thismeas.y,thismeas.ey,thismeas.z,thismeas.ez
00214 #            print thismeas.wx,thismeas.ewx,thismeas.wy,thismeas.ewy,thismeas.wz,thismeas.ewz
00215 #            print thismeas.dxdz,thismeas.edxdz,thismeas.dydz,thismeas.edydz
00216     return allmeas
00217 
00218 if __name__ == '__main__':
00219     if len(sys.argv)!=2:
00220         print "Usage: :",sys.argv[0]," <fillnr>"
00221         sys.exit(1)
00222     FILL=sys.argv[1]
00223 
00224     allmeas=readroot()
00225     # now write it
00226     
00227     for bx in allmeas.keys():
00228         print "writing bx=",bx
00229         bxmeas=allmeas[bx]
00230         lines={}
00231         for meas in bxmeas.keys():
00232             # first derive time in unix time
00233             runno=meas.split(':')[0]
00234             runno=runno.strip()
00235             lumirange=meas.split(':')[1]
00236             lumimin=lumirange.split('-')[0]
00237             lumimin=lumimin.strip()
00238             lumimax=lumirange.split('-')[1]
00239             lumimax=lumimax.strip()
00240             lumimid=int((int(lumimin)+int(lumimax))/2.)
00241             meastime=timeof(runno,lumimid)
00242             print runno,str(lumimid),meastime
00243             
00244             thismeas=bxmeas[meas]
00245 #            print thismeas.x,thismeas.ex,thismeas.y,thismeas.ey,thismeas.z,thismeas.ez
00246 #            print thismeas.wx,thismeas.ewx,thismeas.wy,thismeas.ewy,thismeas.wz,thismeas.ewz
00247 #            print thismeas.dxdz,thismeas.edxdz,thismeas.dydz,thismeas.edydz
00248             line=str(meastime)+" "
00249             line+="1 "
00250             line+="%11.7f %11.7f %11.7f %11.7f %11.7f %11.7f " % (-thismeas.x*10,thismeas.ex*10,
00251                                                             thismeas.y*10,thismeas.ey*10,
00252                                                             -thismeas.z*10,thismeas.ez*10)
00253             line+="%11.7f %11.7f %11.7f %11.7f %11.7f %11.7f " % (thismeas.wx*10,thismeas.ewx*10,
00254                                                             thismeas.wy*10,thismeas.ewy*10,
00255                                                             thismeas.wz*10,thismeas.ewz*10)
00256             line+="%11.7f %11.7f %11.7f %11.7f" % (thismeas.dxdz,thismeas.edxdz,-thismeas.dydz,thismeas.edydz)
00257             line+="\n"
00258             
00259             # validate it
00260             if (thismeas.x != 0.0 and thismeas.y != 0.0 and thismeas.z != 0.0 and
00261                 thismeas.wx != 0.0 and thismeas.wy != 0.0 and thismeas.wz != 0.0 and
00262                 thismeas.dxdz != 0.0 and thismeas.dydz != 0.0 ):
00263                 lines[meastime]=line
00264 
00265             
00266         # now write it
00267         WORKDIR=OUTDIR+FILL
00268         os.system("mkdir -p "+WORKDIR)
00269         rfbucket=(int(bx)-1)*10+1
00270         filename=WORKDIR+"/"+FILL+"_lumireg_"+str(rfbucket)+"_CMS.txt"
00271         file=open(filename,'w')
00272         sortedtimes=lines.keys()
00273         sortedtimes.sort()
00274         for meastime in sortedtimes:
00275             file.write(lines[meastime])
00276         file.close()
00277 
00278     #    print timeof("168264",25)