Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-30 08:50:49

0001 import ROOT
0002 ROOT.gROOT.SetBatch(True)
0003 
0004 
0005 
0006 def generate(tfile,name,outname,bins,mini,maxi,zeroSup=False,constant=-1,absol=False,factor=1,rebin=False):
0007     xaxis=ROOT.TAxis(512*factor,0,512*factor)
0008     axis=ROOT.TAxis(bins,mini,maxi)
0009     h=tfile.Get(name)
0010     if rebin:
0011         h.Rebin()
0012     histo = h.ProfileX().ProjectionX()
0013     for i in range(1,h.GetNbinsX()+1):
0014         proj = h.ProjectionY("q",i,i)
0015         mean=proj.GetMean()
0016         rms = proj.GetRMS()
0017         if proj.Integral()>15:
0018             histo.SetBinContent(i,mean)
0019             histo.SetBinError(i,rms)
0020         else:    
0021             histo.SetBinContent(i,0.0)
0022             histo.SetBinError(i,0.0)
0023 
0024     lastx=10000000000;        
0025     if zeroSup:
0026         g=ROOT.TGraphErrors()
0027         N=0
0028         for i in range(1,histo.GetNbinsX()+1):
0029             content = histo.GetBinContent(i)
0030             x = histo.GetXaxis().GetBinLowEdge(i)
0031             if content==0.0:
0032                 continue;
0033             g.SetPoint(N,x,content)
0034             g.SetPointError(N,0.0,histo.GetBinError(i))
0035             lastx=x
0036             N=N+1
0037     else:
0038         g = ROOT.TGraphErrors(histo)
0039         x1=ROOT.Double(0.0)
0040         y1=ROOT.Double(0.0)
0041         g.GetPoint(g.GetN()-1,x1,y1)
0042         lastx=x1
0043     newH = ROOT.TH1D(outname,outname,512*factor,0,512*factor)    
0044     for i in range(1,xaxis.GetNbins()+1):
0045         x = xaxis.GetBinLowEdge(i)
0046         if x>lastx:
0047             x=lastx
0048         if constant>0 and x>constant:
0049             content=g.Eval(constant,0,"")
0050         else: 
0051             content=g.Eval(x,0,"")
0052         if absol:
0053             content=abs(content)
0054         intCont = axis.GetBinLowEdge(axis.FindBin(content))    
0055         newH.SetBinContent(i,round(content,5))
0056     c1 = ROOT.TCanvas("temp", "", 600, 600)
0057     newH.Draw("p")
0058     histo.Draw("same")
0059     newH.GetYaxis().SetRangeUser(min(newH.GetMinimum(), histo.GetMinimum())-.1, max(newH.GetMaximum(), histo.GetMaximum())+.1)
0060     c1.SaveAs(outname+".png")
0061     return newH,h.ProfileX().ProjectionX(outname+'_orig')
0062     
0063 
0064 def printLUT(h,f,name,N):
0065     typ = 'updateLUT'+name[-1]+'_t'
0066     arr=[]
0067     for i in range(1,h.GetNbinsX()+1):
0068         arr.append("{}".format(h.GetBinContent(i)))
0069     st = "const "+typ+" "+name+"["+str(N)+"]={"+','.join(arr)+"};\n"
0070     f.write(st)
0071 
0072 def printLUT2(h,f,name,N):
0073     typ = 'update2LUT'+name[-4]+"_t"
0074     arr=[]
0075     if name[-2]=="L":
0076         st = "const "+typ+" "+name+"={}".format(h.GetBinContent(1))+";\n"
0077         f.write(st)
0078     else:
0079         for i in range(1,h.GetNbinsX()+1):
0080             arr.append("{}".format(h.GetBinContent(i)))
0081         st = "const "+typ+" "+name+"["+str(N)+"]={"+','.join(arr)+"};\n"
0082         f.write(st)
0083 
0084 def printLUTV(h,f,name,N):
0085     typ = 'updateLUTV'+name[-1]+'_t'
0086     arr=[]
0087     for i in range(1,h.GetNbinsX()+1):
0088         arr.append("{}".format(h.GetBinContent(i)))
0089     st = "const "+typ+" "+name+"["+str(N)+"]={"+','.join(arr)+"};\n"
0090     f.write(st)
0091 
0092 
0093 
0094 fileio=open("gainLUTs.h","w")
0095 fileio.write('#include common.h\n')
0096 
0097 f=ROOT.TFile("gains.root")
0098 
0099 fout = ROOT.TFile("gainLUTs.root","RECREATE")
0100 fout.cd()
0101 
0102 
0103 def parse(fileio,f,fout,ele,bins,mini,maxi,zeroSup=False,constant=-1,absol=False,factor=1,rebin=False):
0104     fout.cd()
0105     stele=str(ele)
0106     h,hO=generate(f,"gain_8_3_"+stele,"G_8_3_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0107     printLUT(h,fileio,"gain_1000_3_"+stele,1024)
0108     h.Write()
0109     hO.Write()
0110 
0111     h,hO=generate(f,"gain_8_2_"+stele,"G_8_2_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0112     printLUT(h,fileio,"gain_1000_2_"+stele,1024)
0113     h.Write()
0114     hO.Write()
0115 
0116 
0117     h,hO=generate(f,"gain_12_2_"+stele,"G_12_2_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0118     printLUT(h,fileio,"gain_1100_2_"+stele,1024)
0119     h.Write()
0120     hO.Write()
0121 
0122     h,hO=generate(f,"gain_12_1_"+stele,"G_12_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0123     printLUT(h,fileio,"gain_1100_1_"+stele,1024)
0124     h.Write()
0125     hO.Write()
0126 
0127     h,hO=generate(f,"gain_4_2_"+stele,"G_4_2_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0128     printLUT(h,fileio,"gain_0100_2_"+stele,1024)
0129     h.Write()
0130     hO.Write()
0131 
0132     h,hO=generate(f,"gain_10_1_"+stele,"G_10_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0133     printLUT(h,fileio,"gain_1010_1_"+stele,1024)
0134     h.Write()
0135     hO.Write()
0136 
0137     h,hO=generate(f,"gain_6_1_"+stele,"G_6_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0138     printLUT(h,fileio,"gain_0110_1_"+stele,1024)
0139     h.Write()
0140     hO.Write()
0141 
0142     h,hO=generate(f,"gain_14_1_"+stele,"G_14_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0143     printLUT(h,fileio,"gain_1110_1_"+stele,1024)
0144     h.Write()
0145     hO.Write()
0146 
0147 
0148 
0149 def parse2(fileio,f,fout,ele,bins,mini,maxi,zeroSup=False,constant=-1,absol=False,factor=1):
0150     fout.cd()
0151     stele=str(ele)
0152     for q1 in ['H', 'L']:
0153         for q2 in ['H', 'L']:
0154             stele = str(ele)
0155             stele = stele+"_"+q1+q2
0156             h,hO=generate(f,"gain2_8_3_"+stele,"G2_8_3_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0157             printLUT2(h,fileio,"gain2_1000_3_"+stele,512)
0158             h.Write()
0159             hO.Write()
0160 
0161             h,hO=generate(f,"gain2_8_2_"+stele,"G2_8_2_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0162             printLUT2(h,fileio,"gain2_1000_2_"+stele,512)
0163             h.Write()
0164             hO.Write()
0165 
0166             h,hO=generate(f,"gain2_8_1_"+stele,"G2_8_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0167             printLUT2(h,fileio,"gain2_1000_1_"+stele,512)
0168             h.Write()
0169             hO.Write()
0170 
0171             h,hO=generate(f,"gain2_4_2_"+stele,"G2_4_2_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0172             printLUT2(h,fileio,"gain2_0100_2_"+stele,512)
0173             h.Write()
0174             hO.Write()
0175 
0176             h,hO=generate(f,"gain2_4_1_"+stele,"G2_4_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0177             printLUT2(h,fileio,"gain2_0100_1_"+stele,512)
0178             h.Write()
0179             hO.Write()
0180 
0181             h,hO=generate(f,"gain2_2_1_"+stele,"G2_2_1_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor)
0182             printLUT2(h,fileio,"gain2_0010_1_"+stele,512)
0183             h.Write()
0184             hO.Write()
0185 
0186 
0187 
0188 
0189 
0190 def parseV(fileio,f,fout,ele,bins,mini,maxi,zeroSup=False,constant=-1,absol=False,factor=2,rebin=False):
0191     fout.cd()
0192     stele=str(ele)
0193     h,hO=generate(f,"gain_15_0_"+stele,"G_15_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0194     printLUTV(h,fileio,"gain_1111_0_"+stele,1024)
0195     h.Write()
0196     hO.Write()
0197     h,hO=generate(f,"gain_14_0_"+stele,"G_14_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0198     printLUTV(h,fileio,"gain_1110_0_"+stele,1024)
0199     h.Write()
0200     hO.Write()
0201     h,hO=generate(f,"gain_13_0_"+stele,"G_13_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0202     printLUTV(h,fileio,"gain_1101_0_"+stele,1024)
0203     h.Write()
0204     hO.Write()
0205     h,hO=generate(f,"gain_12_0_"+stele,"G_12_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0206     printLUTV(h,fileio,"gain_1100_0_"+stele,1024)
0207     h.Write()
0208     hO.Write()
0209     h,hO=generate(f,"gain_11_0_"+stele,"G_11_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0210     printLUTV(h,fileio,"gain_1011_0_"+stele,1024)
0211     h.Write()
0212     hO.Write()
0213     h,hO=generate(f,"gain_10_0_"+stele,"G_10_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0214     printLUTV(h,fileio,"gain_1010_0_"+stele,1024)
0215     h.Write()
0216     hO.Write()
0217     h,hO=generate(f,"gain_9_0_"+stele,"G_9_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0218     printLUTV(h,fileio,"gain_1001_0_"+stele,1024)
0219     h.Write()
0220     hO.Write()
0221     h,hO=generate(f,"gain_7_0_"+stele,"G_7_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0222     printLUTV(h,fileio,"gain_0111_0_"+stele,1024)
0223     h.Write()
0224     hO.Write()
0225     h,hO=generate(f,"gain_6_0_"+stele,"G_6_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0226     printLUTV(h,fileio,"gain_0110_0_"+stele,1024)
0227     h.Write()
0228     hO.Write()
0229     h,hO=generate(f,"gain_5_0_"+stele,"G_5_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0230     printLUTV(h,fileio,"gain_0101_0_"+stele,1024)
0231     h.Write()
0232     hO.Write()
0233     h,hO=generate(f,"gain_3_0_"+stele,"G_3_0_"+stele,bins,mini,maxi,zeroSup,constant,absol,factor,rebin)
0234     printLUTV(h,fileio,"gain_0011_0_"+stele,1024)
0235     h.Write()
0236     hO.Write()
0237 
0238 
0239 
0240 parse(fileio,f,fout,0,512*2,0,64*2,True,-1,False,2,False)    
0241 parse(fileio,f,fout,4,512,0,16,True,-1,True,2,False)    
0242 
0243 parse2(fileio,f,fout,0,512,0,64,True,-1,False,1)    
0244 parse2(fileio,f,fout,1,512,-8,8,True,-1,True,1)    
0245 parse2(fileio,f,fout,4,512,0,16,True,-1,True,1)    
0246 parse2(fileio,f,fout,5,512,0,1,True,-1,False,1)    
0247 
0248 parseV(fileio,f,fout,0,512,0,4,True,-1,True,2,True)    
0249 parseV(fileio,f,fout,1,512,0,4,True,-1,True,2,True)    
0250 
0251 fout.Close()
0252 f.Close()
0253 fileio.close()