def plot_projection(t_file, hist_name): h = t_file.Get(hist_name) c = TCanvas('c_extrapol','c_extrapol',10,10,1400,900) c.Divide(1,3) c.cd(1) h.Draw('colz') h.FitSlicesY(0,5,25,10) hM = gDirectory.Get(h.GetName()+'_1') if hM == None: print 'no slice 1 histo found' else: c.cd(2) hM.Draw() hW = gDirectory.Get(h.GetName()+'_2') if hW == None: print 'no slice 2 histo found' else: c.cd(3) hW.Draw() f = TF1('fpol2','pol2',h.GetXaxis().GetBinCenter(6),h.GetXaxis().GetBinCenter(24)) hW.Fit(f,'R') min_val = f.GetParameter(1)/(-2.0*f.GetParameter(2)) plotutils.myText(0.5,0.5,'min_val=%.0f mm' % min_val,0.07,2) ans = raw_input('continue?') c.Update() c.SaveAs(hist_name.replace(' ','_') + '.png')
def fitMom(f,name): c = TCanvas('c','c',10,10,700,500) h_p = f.Get(name) setStyle(h_p) peakbin = findFEEPeakBin(h_p) l = h_p.GetBinCenter(peakbin)-0.11 h = h_p.GetBinCenter(peakbin)+0.2 f_p = TF1('f'+'_'+name,'gaus',l,h) h_p.Fit(f_p,'R') h_p.Draw() plotutils.myText(0.5,0.85,'<m>=%.2f #sigma=%.2f'%(f_p.GetParameter(1),f_p.GetParameter(2)), 0.05, 2) c.SaveAs(name + '-' + getFileName(f) + args.tag + '.png') ans = raw_input('continue?')
def plotResiduals(f,name,half): f.cd() c = TCanvas('c','c',10,10,700,500) c.Print('res-' +name+'-'+half + '-' + getFileName(f) + args.tag + '.ps[') histos = plotutils.getHistograms(gDirectory) print 'found ', len(histos), ' histograms' res_map = {} for h_p in histos: if re.match('^' + name + '.*' + half + '$', h_p.GetName() ) and not 'large' in h_p.GetName() and not 'truth' in h_p.GetName() : print 'found ' , h_p.GetName() sensorName = utils.getSensor(h_p.GetName()) print 'sensor ', sensorName setStyle(h_p) peakbin = findPeakBin(h_p) l = h_p.GetBinCenter(peakbin) - 5*h_p.GetRMS() h = h_p.GetBinCenter(peakbin) + 5*h_p.GetRMS() print 'peakbin ', peakbin, ' c ' , h_p.GetBinCenter(peakbin), ' l ', l, ' h', h f_p = TF1('f'+'_'+name+'-'+half+'_'+h_p.GetName(),'gaus',l,h) h_p.Fit(f_p,'R') c1 = TCanvas('c1-'+h_p.GetName(),'c1-'+h_p.GetName(),10,10,700,500) h_p.Draw() plotutils.myText(0.5,0.85,'<m>=%.2f #sigma=%.2f'%(f_p.GetParameter(1),f_p.GetParameter(2)), 0.05, 2) res_map[sensorName] = f_p c1.Print('res-' +name+'-'+half + '-' + getFileName(f) + args.tag + '.ps') #c.SaveAs(name + '-' + getFileName(f) + args.tag + '.png') #ans = raw_input('continue?') grMean = TGraph() grSigma = TGraph() i=0 binLabels = [] res_map_sort = sorted(res_map.keys()) print res_map print 'found ', len(res_map), ' res plots' for sensor in res_map_sort: fit = res_map[sensor] grMean.SetPoint(i,i,fit.GetParameter(1)) grSigma.SetPoint(i,i,fit.GetParameter(2)) binLabels.append(sensor) i=i+1 plotutils.setBinLabels(grMean,binLabels) plotutils.setBinLabels(grSigma,binLabels) grMean.SetTitle(name+'-'+half + '-' + getFileName(f) + args.tag + ';;'+name+'-'+half+' mean') grMean.SetMarkerStyle(20) c1 = TCanvas('c1-res','c1-res',10,10,700,500) c1.SetBottomMargin(0.4) grMean.Draw('ALP') c1.Print('res-' +name+'-'+half + '-' + getFileName(f) + args.tag + '.ps') grSigma.SetTitle(name+'-'+half + '-' + getFileName(f) + args.tag + ';;'+name+'-'+half+' #sigma') grSigma.SetMarkerStyle(20) c1 = TCanvas('c1-ress','c1-ress',10,10,700,500) c1.SetBottomMargin(0.4) grSigma.Draw('ALP') c1.Print('res-' +name+'-'+half + '-' + getFileName(f) + args.tag + '.ps') ans = raw_input('continue?') c.Print('res-' +name+'-'+half + '-' + getFileName(f) + args.tag + '.ps]')
def plotRotations(t_file): names = hps_utils.getSensorNames() print ' got ', len(names), ' sensor names' hname = 'h_res_gbl_vs_u_' #hname = 'h_res_gbl_vs_vpred_' c = TCanvas('c','c',10,10,700,500) grSlopes = TGraphErrors() grSlopes.SetName('slopes_' + hname) grSlopesBinLabels = [] for sensorname in names: name = hname + sensorname if args.regexp != None: m = re.match(args.regexp, name) if m == None: print 'skip this histogram \"', name, '\"' continue h = t_file.Get(name) if h == None: print 'no histogram \"', name, '\"' sys.exit(1) print 'process \"', name , '\"' c.Clear() h.Draw('colz') c.SaveAs(name + '-' + args.tag + '.png') #ans = raw_input('continue?') grMean = TGraphErrors() grMean.SetName( h.GetName() + '_prjYmean' ) for b in range(1, h.GetNbinsX()+1): h_p = h.ProjectionY(h.GetName() + '_prjy' + str(b), b, b, 'E') f = None if h_p.GetEntries() > 77.: peakbin = findPeakBin(h_p) x = h_p.GetBinCenter(peakbin) minv = x - 1.5*h_p.GetRMS() maxv = x + 1.5*h_p.GetRMS() print 'vpred ', h.GetXaxis().GetBinCenter(b) , ' peakbin ', peakbin, ' c ' , h_p.GetBinCenter(peakbin), ' minv ', minv, ' maxv ', maxv f = TF1('f'+'_'+name,'gaus',minv,maxv) h_p.Fit(f,'RQ') ipoint = grMean.GetN() grMean.SetPoint(ipoint, h.GetXaxis().GetBinCenter(b), f.GetParameter(1)) grMean.SetPointError(ipoint, 0., f.GetParError(1)) c.Clear() if f != None: plotutils.myText(0.5,0.85,'<m>=%.2f #sigma=%.2f'%(f.GetParameter(1),f.GetParameter(2)), 0.05, 2) h_p.Draw() c.SaveAs(h_p.GetName() + '-' + args.tag + '.png') #ans = raw_input('continue?') grMean.SetTitle(name + ';;mean') grMean.SetMarkerStyle(20) c.Clear() fpol1 = TF1('f' + grMean.GetName(),'pol1') grMean.Fit(fpol1) grMean.Draw('ALP') plotutils.myText(0.35,0.8,'slope=%.2e m=%.2e'%(fpol1.GetParameter(1),fpol1.GetParameter(0)), 0.05, 2) ipoint = grSlopes.GetN() if args.uflip and hps_utils.getAxialStereo(sensorname) == 'stereo': print 'flip ', sensorname, ' ', fpol1.GetParameter(1) , ' -> ', -1.*fpol1.GetParameter(1) grSlopes.SetPoint( ipoint, ipoint, -1.*fpol1.GetParameter(1) ) else: print 'NO flip ', sensorname, ' ', fpol1.GetParameter(1) grSlopes.SetPoint( ipoint, ipoint, fpol1.GetParameter(1) ) grSlopes.SetPointError( ipoint, 0., fpol1.GetParError(1) ) grSlopesBinLabels.append( hps_utils.getshortsensorname( sensorname ) ) c.SaveAs(name + '-mean-' + args.tag + '.png') #ans = raw_input('continue?') c.Clear() grSlopes.Draw('ALP') plotutils.setBinLabels( grSlopes, grSlopesBinLabels ) c.SetBottomMargin(0.2) if args.uflip: plotutils.myText(0.35,0.8,'u-flipped', 0.05, 2) else: plotutils.myText(0.35,0.8,'NOT u-flipped', 0.05, 2) if args.uflip: c.SaveAs(grSlopes.GetName() + '-uflipped-' + args.tag + '.png') else: c.SaveAs(grSlopes.GetName() + '-' + args.tag + '.png') ans = raw_input('continue?')
def noise(files): if files==None: print 'No ROOT files' return c = TCanvas('cBiasCheck','cBiasCheck',10,10,700,500) c.Print(c.GetName() + '.ps[') grOnRMS = TGraph() grOnMean = TGraph() grOffRMS = TGraph() grOffMean = TGraph() for filename in files: f = TFile(filename) if f==None: print "couldnt open file ", filename return m = re.match('hps_00(\d+)\.\S+\.(\d+)\.root',os.path.basename(filename)) if m==None: print ' this filename is weird' , filename sys.exit(1) run = m.group(1) runName = m.group(1) if m.group(2)!='': runName = run + '.' + m.group(2) #hOffName = 'module_L1t_halfmodule_stereo_slot_sensor0 raw adc - ped maxSample>4 OFF' #hOnName = 'module_L1t_halfmodule_stereo_slot_sensor0 raw adc - ped maxSample>4 ON' #hOffName = 'module_L3t_halfmodule_stereo_sensor0 raw adc - ped maxSample>4 OFF' #hOnName = 'module_L3t_halfmodule_stereo_sensor0 raw adc - ped maxSample>4 ON' hOffName = 'module_L5t_halfmodule_stereo_slot_sensor0 raw adc - ped maxSample>4 OFF' hOnName = 'module_L5t_halfmodule_stereo_slot_sensor0 raw adc - ped maxSample>4 ON' hOff = f.Get(hOffName) hOn = f.Get(hOnName) if hOff!=None and hOn!=None: if hOn.GetEntries()>0: cOn = TCanvas('cBiasCheckOn %s'%runName,'cBiasCheckOn %s'%runName,10,10,700,500) hOn.SetFillColor(2) hOn.SetFillStyle(3003) hOn.Draw("hist") myText(0.2,0.7,'HV ON %s'%runName, 0.05,2) cOn.Print(c.GetName() + '.ps') grOnRMS.SetPoint(grOnRMS.GetN(),int(run), hOn.GetRMS()) grOnMean.SetPoint(grOnMean.GetN(),int(run), hOn.GetMean()) if hOff.GetEntries()>0: cOff = TCanvas('cBiasCheckOff %s'%runName,'cBiasCheckOff %s'%runName,10,10,700,500) hOff.SetFillColor(4) hOff.SetFillStyle(3003) hOff.Draw() myText(0.2,0.7,'HV OFF %s'%runName, 0.05,1) cOff.Print(c.GetName() + '.ps') grOffRMS.SetPoint(grOffRMS.GetN(),int(run), hOff.GetRMS()) grOffMean.SetPoint(grOffMean.GetN(),int(run), hOff.GetMean()) if hOn.GetEntries()>0 and hOff.GetEntries()>0: cOnOff = TCanvas('cBiasCheckOnOff %s'%runName,'cBiasCheckOnOff %s'%runName,10,10,700,500) hOn.SetLineColor(2) hOn.SetFillColor(2) hOn.SetFillStyle(3003) hOn.DrawNormalized("hist") hOff.DrawNormalized("same") myText(0.2,0.7,'HV ON %s'%runName, 0.05,2) myText(0.2,0.78,'HV OFF %s'%runName, 0.05,1) cOnOff.Print(c.GetName() + '.ps') #ans = raw_input('continue?') else: print 'couldnt get ', hOnName setGraphStyle(grOnMean) setGraphStyle(grOnRMS) setGraphStyle(grOffMean) setGraphStyle(grOffRMS) cGrOnMean = TCanvas('cGrOnMean','cGrOnMean',10,10,700,500) if grOnMean.GetN()>0: grOnMean.Draw('ALP') myText(0.2,0.7,'Mean HV ON', 0.05,1) cGrOnRMS = TCanvas('cGrOnRMS','cGrOnRMS',10,10,700,500) if grOnRMS.GetN()>0: grOnRMS.Draw('ALP') myText(0.2,0.7,'RMS HV ON', 0.05,1) cGrOffMean = TCanvas('cGrOffMean','cGrOffMean',10,10,700,500) if grOffMean.GetN()>0: grOffMean.Draw('ALP') myText(0.2,0.7,'Mean HV OFF', 0.05,1) cGrOffRMS = TCanvas('cGrOffRMS','cGrOffRMS',10,10,700,500) if grOffRMS.GetN()>0: grOffRMS.Draw('ALP') myText(0.2,0.7,'RMS HV OFF', 0.05,1) #ans = raw_input('continue?') cGrOnMean.Print(c.GetName() + '.ps') cGrOffMean.Print(c.GetName() + '.ps') cGrOnRMS.Print(c.GetName() + '.ps') cGrOffRMS.Print(c.GetName() + '.ps') c.Print(c.GetName() + '.ps]') return
def compareHists(histos,legends=None,normalize=None,fitName=None, t='',pad=None,myTextSize=0.05): print 'compareHists:' if fitName != None: print 'fitName: ', fitName c = None cName = 'c_' if pad == None: for h in histos: print h.GetName(), ' ', h.GetEntries() cName += h.GetName() c = TCanvas(cName, cName ,10 ,10 ,700 ,500) else: pad.cd() fillColors = [33,45,20,4,4,4,4,4,4,4,4] lineColors = [1,2,3,4,5,6,7,8,9,10] #lineColors = [1,2,3,4,5,6,7,8,9,10] lineWidth = 2 n = 0 nm = 0 maxBC = -1. hFirst = None for ih in range(len(histos)): h = histos[ih] if ih == 0: hFirst = h h.SetLineColor(lineColors[ih]) h.SetLineWidth(lineWidth) h.Sumw2() if normalize != None: if h.Integral() == 0: print 'warning: histogram ', h.GetName(), ' cannot be normalized with no integral!' else: h.Scale(1.0/h.Integral()) if h.GetMaximum() > maxBC: maxBC = h.GetMaximum() f = None if fitName != None: if fitName['name'] == 'gaus': print 'Fitting ', h.GetName() fitFuncName = 'fg_' + h.GetName() fitOpt = '0R' fg = TF1(fitFuncName,'gaus') if h.GetRMS() < 1e-10 or h.GetRMS() < h.GetBinWidth(1): print 'WARNING: histogram ', h.GetName(), ' has small RMS', h.GetRMS(), ' dont fit but use that RMS and mean ', h.GetMean() fg.SetParameter(0,0) fg.SetParameter(1, h.GetMean() ) fg.SetParameter(2, h.GetRMS() ) listOfFunctions = h.GetListOfFunctions() print 'got ', len(listOfFunctions), ' function' for func in listOfFunctions: print func.GetName() listOfFunctions.append(fg) print 'now got ', len(listOfFunctions), ' function' for func in listOfFunctions: print func.GetName() else: if 'xmin' and 'xmax' in fitName: fg.SetRange(fitName['xmin'],fitName['xmax']) else: fg.SetRange(h.GetBinCenter(1),h.GetBinCenter(h.GetNbinsX()+1)) if 'peakbin' in fitName: bc = getHistMaxBinValue(h) fg.SetParameter(1,bc) if 'rms' in fitName: bc = getHistMaxBinValue(h) rms = h.GetRMS() fg.SetParameter(1,bc) fg.SetParameter(2,rms) fg.SetRange( bc - rms*fitName['rms'], bc + rms*fitName['rms'] ) h.Fit(fg,fitOpt) f = fg #h.GetFunction(fitFuncName) if f != None: f.SetLineColor(lineColors[ih]) f.SetLineWidth(lineWidth) if ih != -1: h.SetFillColor(fillColors[ih]) h.SetFillStyle(3002) if ih == 0: h.Draw('hist') else: h.Draw('hist,same') if f != None: f.Draw('same') if fitName['name'] == 'gaus': myText(0.13,0.2+ih*myTextSize,'<m>=%.2e #sigma=%.2e'%(f.GetParameter(1),f.GetParameter(2)),myTextSize,lineColors[ih]) n=n+1 #ans = raw_input('press anywhere to continue') hFirst.SetMaximum(maxBC) if legends: styles = ['FL' for x in range(len(histos))] l = getLegendList(0.13,0.75,0.25,0.85,histos,legends,styles) l.Draw() if c != None: name = cName if t != '': name += '_' + t c.SaveAs(name + '.png')