def compareSensorHists(files,histName,tag='',half=None,singleCanvas=None,beamspot=False,legends=None): print 'compareSensorHists for ', len(files), ' for histName ', histName, ' tag ', tag, ' beamspot ', beamspot sensorHistNames = [] sensors = hps_utils.getSensorNames(beamspot) for s in sensors: if hps_utils.getLayer > 3 and hps_utils.getHoleSlot(s) == 'slot': continue #if hps_utils.getHalf(s) != 'b' or hps_utils.getLayer(s) !=2 or hps_utils.getAxialStereo(s) != 'axial': # continue #if hps_utils.getHalf(s) != 't' or hps_utils.getLayer(s) !=6 or hps_utils.getHoleSlot(s) != 'slot' and hps_utils.getAxialStereo(s) != 'slot': # continue #if beamspot and hps_utils.getLayer(s)==0: # if hps_utils.getHalf(s) != 'b': # print 'this beamspot sensor is weird! ', s # sys.exit(1) if hps_utils.getHalf(s) == 't' and half != 'top': continue if hps_utils.getHalf(s) == 'b' and half != 'bottom': continue addOn = '' if hps_utils.getHalf(s) == 't': addOn = '_top' else: addOn = '_bot' sensorHistNames.append(histName + s + addOn) print sensorHistNames print 'open TFiles' tFiles = [] for f in files: tFiles.append(TFile(f)) print 'opened ', len(tFiles) c = None print 'singleCanvas ', singleCanvas if singleCanvas != None: cName = 'c_' + histName + '_' + half + '_' + tag c = TCanvas(cName, cName, 10, 10, 690*2, 390*2) c.Divide(2,12) currentPad = None graphBinNames = {} graphMean = [] graphRMS = [] for tf in tFiles: print 'tf ', tf.GetName() grM = TGraphErrors() grM.SetName('grMean_' + tf.GetName()) grR = TGraphErrors() grR.SetName('grRMS_' + tf.GetName()) graphMean.append( grM ) graphRMS.append( grR ) for hName in sensorHistNames: histos = [] print 'get ', hName for tf in tFiles: print 'tf ', tf.GetName() h = tf.Get(hName) histos.append(h) print 'compare ', len(histos),' root histos' if c != None: i = hps_utils.getCanvasIdxTwoCols(hName,beamspot) currentPad = c.cd(i) print currentPad compareRootHists.compareHists(histos,legends=legends,normalize=True,fitName={'name':'gaus','rms':2},t=tag,pad=currentPad,myTextSize=0.1) print 'make graphs of mean and RMS' for ih in range(len(histos)): h = histos[ih] f = h.GetFunction('fg_' + h.GetName()) if f != None: mean = f.GetParameter(1) meanError = f.GetParError(1) rms = f.GetParameter(2) rmsError = f.GetParError(2) ipoint = graphMean[ih].GetN() graphMean[ih].SetPoint(ipoint, ipoint, mean) graphRMS[ih].SetPoint(ipoint, ipoint, rms) graphMean[ih].SetPointError(ipoint, 0., meanError) graphRMS[ih].SetPointError(ipoint, 0., rmsError) graphBinNames[ ipoint ] = hps_utils.getshortsensorname( hName ) print 'mean ', mean, ' RMS ', rms, ' fg ', f.GetName() else: print 'No fg in histo ', ih, ' name ', h.GetName() print 'done comparing ', len(histos),' root histos' if c != None: c.SaveAs(cName + '.png') cGrName = 'c_summary' + histName + '_' + half + '_' + tag cGr = TCanvas(cGrName, cGrName, 10, 10, 690*2, 390*2) cGr.Divide(1,2) grMaxValMean = -1000000. grMaxValRMS = -10000000. grMinValMean = 1000000. grMinValRMS = 10000000. for igr in range(len(graphMean)): grM = graphMean[ igr ] grR = graphRMS [ igr ] limVals = plotutils.getGraphMaxMinVal(grM) print limVals if limVals[1] > grMaxValMean: grMaxValMean = limVals[1] if limVals[0] < grMinValMean: grMinValMean = limVals[0] limVals = plotutils.getGraphMaxMinVal(grR) if limVals[1] > grMaxValRMS: grMaxValRMS = limVals[1] if limVals[0] < grMinValMean: grMinValRMS = limVals[0] print grMaxValMean, grMinValMean for igr in range(len(graphMean)): grM = graphMean[ igr ] grR = graphRMS [ igr ] #print 'igr ', igr, ' name ', grM.GetName() plotutils.setGraphStyle(grM,igr+1) plotutils.setGraphStyle(grR,igr+1) plotutils.setBinLabelsDict(grM,graphBinNames) plotutils.setBinLabelsDict(grR,graphBinNames) if igr == 0: cGr.cd(1) gPad.SetBottomMargin(0.3) grM.GetHistogram().SetMaximum(grMaxValMean*1.2) grM.GetHistogram().SetMinimum(grMinValMean*1.2) grM.Draw('AXPL') cGr.cd(2) gPad.SetBottomMargin(0.3) grR.SetMaximum(grMaxValRMS) grR.SetMinimum(grMinValRMS) grR.Draw('AXPL') else: cGr.cd(1) grM.Draw('PL,same') cGr.cd(2) grR.Draw('PL,same') if legends: styles = [ 'PL' for x in range( len( graphMean ) ) ] l = plotutils.getLegendList(0.13,0.75,0.25,0.85,graphMean,legends,styles) l.Draw() cGr.SaveAs(cGr.GetName() + '.png') #ans = raw_input('press anything') for tf in tFiles: print 'closing ', tf.GetName() tf.Close() return
def plot_sensor_hist(tFile,hist_name,half,maxminlist): names = hps_utils.get_module_names() print 'found ', len(names), ' sensor names' graphBinNames = [] graphMean = TGraphErrors() graphMean.SetName('grMean_' + half) graphRMS = TGraphErrors() graphRMS.SetName('grRMS_' + half) for h_name in [hist_name]: print 'Process histogram \"', h_name, '\"' for m_name in names: print 'Sensor \"', m_name, '\"' m_half = hps_utils.getHalf(m_name) if half != '' and m_half != half: continue name = m_name + h_name if args.regexp != None: if debug: print 'apply regexp \"', args.regexp, '\" to file \"', name, '\"' m = re.match(args.regexp, name) if m == None: if debug: print 'no match for regexp \"', args.regexp, '\" for \"', name, '\"' continue print 'Try to find histogram \"', name, '\"' h = t_file.Get(name) if h == None: print 'no histogram name \"', name, '\" found' else: print 'got hist \"', h.GetName(), '\"' c = TCanvas('c_' + name, 'c_' + name,10,10,1400,900) if h.GetEntries() > 10: fitFuncName = 'fg_' + h.GetName() fitOpt = 'R' fg = TF1(fitFuncName,'gaus') bc = plotutils.getHistMaxBinValue(h) rms = h.GetRMS() fg.SetParameter(1,bc) fg.SetParameter(2,rms) fg.SetRange( bc - rms*2, bc + rms*2 ) h.Fit(fg,fitOpt) #print 'make graphs of mean and RMS' mean = fg.GetParameter(1) meanError = fg.GetParError(1) rms = fg.GetParameter(2) rmsError = fg.GetParError(2) ipoint = graphMean.GetN() graphMean.SetPoint(ipoint, ipoint, mean) graphRMS.SetPoint(ipoint, ipoint, rms) graphMean.SetPointError(ipoint, 0., meanError) graphRMS.SetPointError(ipoint, 0., rmsError) graphBinNames.append(hps_utils.getshortsensorname( m_name ) ) print 'mean ', mean, '+-',meanError,' RMS ', rms, '+-', rmsError, ' fg ', fg.GetName() else: print 'Not enough entries for histogram \"', name, '\"' h.Draw() #ans = raw_input('continue?') saveName = name c.SaveAs(saveName + '.png') c = TCanvas('c_' + hist_name +'_mean_'+half, 'c_' + hist_name +'_mean_'+half,10,10,1400,900) c.Divide(1,2) c.cd(1) gPad.SetBottomMargin(0.3) gPad.SetGridy() plotutils.setBinLabels(graphMean,graphBinNames) plotutils.setGraphStyle(graphMean) graphMean.SetTitle(hist_name+';;Mean') if len(maxminlist) == 4: graphMean.GetHistogram().SetMaximum(maxminlist[0]) graphMean.GetHistogram().SetMinimum(maxminlist[1]) graphMean.Draw('APL') c.cd(2) gPad.SetBottomMargin(0.3) gPad.SetGridy() plotutils.setBinLabels(graphRMS,graphBinNames) plotutils.setGraphStyle(graphRMS) graphRMS.SetTitle(';;Width') if len(maxminlist) == 4: graphRMS.GetHistogram().SetMaximum(maxminlist[2]) graphRMS.GetHistogram().SetMinimum(maxminlist[3]) graphRMS.Draw('APL') c.SaveAs('summary_' + hist_name+'_mean_'+half+'.png') ans = raw_input('continue?')
def plot_residuals(tFile,hist_name,half,side,maxminlist): #module_L6b_halfmodule_axial_hole_sensor0_hitresglobal #module_L1t_halfmodule_axial_sensor0_hitresglobal names = hps_utils.get_module_names() print 'found ', len(names), ' histogram names' graphBinNames = [] graphMean = TGraphErrors() graphMean.SetName('grMean_' + half) graphRMS = TGraphErrors() graphRMS.SetName('grRMS_' + half) for h_name in [hist_name]: for m_name in names: m_half = hps_utils.getHalf(m_name) if half != '' and m_half != half: continue if hps_utils.getAxialStereo(m_name) == 'stereo': continue if hps_utils.getLayer(m_name) > 3: if side != '' and hps_utils.getHoleSlot(m_name) != side: continue name = m_name + '_' + h_name h = t_file.Get(name) if h == None: print 'no histogram name \"', name, '\" found' print 'got hist \"', h.GetName(), '\"' c = TCanvas('c_' + name, 'c_' + name,10,10,1400,900) if h.GetEntries() > 10: fitFuncName = 'fg_' + h.GetName() fitOpt = 'R' fg = TF1(fitFuncName,'gaus') bc = plotutils.getHistMaxBinValue(h) rms = h.GetRMS() fg.SetParameter(1,bc) fg.SetParameter(2,rms) fg.SetRange( bc - rms*2, bc + rms*2 ) h.Fit(fg,fitOpt) #print 'make graphs of mean and RMS' mean = fg.GetParameter(1) meanError = fg.GetParError(1) rms = fg.GetParameter(2) rmsError = fg.GetParError(2) ipoint = graphMean.GetN() graphMean.SetPoint(ipoint, ipoint, mean) graphRMS.SetPoint(ipoint, ipoint, rms) graphMean.SetPointError(ipoint, 0., meanError) graphRMS.SetPointError(ipoint, 0., rmsError) graphBinNames.append(hps_utils.getshortsensorname( m_name ) ) print 'mean ', mean, '+-',meanError,' RMS ', rms, '+-', rmsError, ' fg ', fg.GetName() else: print 'Not enough entries for histogram \"', name, '\"' h.Draw() #ans = raw_input('continue?') saveName = name c.SaveAs(saveName + '.png') c = TCanvas('c_' + hist_name +'_mean_'+half+'_'+side, 'c_' + hist_name +'_mean_'+half+'_'+side,10,10,1400,900) c.Divide(1,2) c.cd(1) gPad.SetBottomMargin(0.3) gPad.SetGridy() plotutils.setBinLabels(graphMean,graphBinNames) plotutils.setGraphStyle(graphMean) graphMean.SetTitle(hist_name+';;Track residual mean (mm)') if len(maxminlist) == 4: graphMean.GetHistogram().SetMaximum(maxminlist[0]) graphMean.GetHistogram().SetMinimum(maxminlist[1]) graphMean.Draw('APL') c.cd(2) gPad.SetBottomMargin(0.3) gPad.SetGridy() plotutils.setBinLabels(graphRMS,graphBinNames) plotutils.setGraphStyle(graphRMS) graphRMS.SetTitle(';;Axial track residual width (mm)') if len(maxminlist) == 4: graphRMS.GetHistogram().SetMaximum(maxminlist[2]) graphRMS.GetHistogram().SetMinimum(maxminlist[3]) graphRMS.Draw('APL') c.SaveAs('summary_' + hist_name+'_mean_'+half+'_'+side +'.png') ans = raw_input('continue?')
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?')