11 from array 
import array
 
   21     ymax = hist.GetBinContent( hist.GetMaximumBin() ) + hist.GetBinError( hist.GetMaximumBin() )
 
   22     ymin = hist.GetBinContent( hist.GetMinimumBin() ) - hist.GetBinError( hist.GetMinimumBin() )
 
   28     hist.GetYaxis().SetRangeUser( ymin, ymax )
 
   32     binedges = [ hist.GetXaxis().GetBinLowEdge(1) ]
 
   33     binedges += [ hist.GetXaxis().GetBinUpEdge(i) 
for i 
in range( 1, hist.GetNbinsX()+1 ) ]
 
   34     if hist.GetXaxis().GetBinUpEdge(hist.GetNbinsX()) == 1000 
and hist.GetBinContent(hist.GetNbinsX()) < 3:    
 
   35         binedges = binedges[:-1]
 
   36     nBins = len(binedges) - 1
 
   38     prof_ave = ROOT.TH1D( 
'ave', 
'ave', nBins, 
array( 
'f', binedges ) )
 
   39     prof_std = ROOT.TH1D( 
'std', 
'std', nBins, 
array( 
'f', binedges ) )
 
   41     for i 
in range( 1, nBins + 1 ):
 
   42         h = hist.ProjectionY( hist.GetName()+
'_p_y', i, i )
 
   47         xmin = h.GetMean()-3*h.GetRMS()
 
   48         xmax = h.GetMean()+3*h.GetRMS()
 
   49         h.GetXaxis().SetRangeUser(xmin,xmax)
 
   50         print '>>> ' , h.GetName()
 
   51         mean, meanErr, sigma, sigmaErr, frame = 
fit(h, -0.5, 0.5) 
 
   52         prof_ave.SetBinContent( i, mean )
 
   53         prof_ave.SetBinError( i, meanErr )
 
   54         prof_std.SetBinContent( i, sigma )
 
   55         prof_std.SetBinError( i, sigmaErr )
 
   56         name = 
'{0}_{1}'.
format( binedges[i-1], binedges[i] )
 
   57         binnedRes[name] = frame.Clone(name)
 
   61     return prof_ave,prof_std,binnedRes
 
   69 def fit(h, emin,emax):
 
   70     x = RooRealVar(
'x',
'x',emin,emax);
 
   71     if (h.GetEntries()<20):
 
   72         return 0,0,0,0,x.frame()
 
   73     mean = RooRealVar(
'mean',
'mean',-0.001,-0.01,0.01);
 
   74     sigma = RooRealVar(
'sigma',
'#sigma',0.02, 0.01, 0.3);
 
   75     Gauss = RooGaussian(
'Gauss',
'Gauss',x,mean,sigma);
 
   76     f1 = RooRealVar(
'f1',
'f1',0.8,0.,1.);
 
   78     GaussModel = RooGaussModel(
'GaussModel',
'Gauss',x,mean,sigma)
 
   79     tau = RooRealVar(
'tau',
'tau',0.02,0.,1.)
 
   80     tailmodel = RooDecay(
'tailmodel',
'ExpxGaus',x,tau,GaussModel,RooDecay.DoubleSided);
 
   81     model = RooAddPdf(
'model',
'G1+E2',Gauss,tailmodel,f1)
 
   83     hdata = RooDataHist(
'hdata',
'hist', RooArgList(x), h);
 
   84     model.fitTo(hdata,RooFit.PrintLevel(-1),RooFit.Verbose(kFALSE));
 
   88     model.plotOn(frame,RooFit.LineColor(ROOT.kRed))
 
   89     model.plotOn(frame,RooFit.Components(
'tailmodel'),RooFit.DrawOption(
'F'),RooFit.FillColor(ROOT.kOrange),RooFit.MoveToBack())
 
   90     return mean.getVal(), mean.getError(), sigma.getVal(), sigma.getError(), frame
 
   95 def CreateProfile( infile, HistDir, HistName, Var, MuonType, doAverage = False, doBinned = False ):
 
   96     hist = infile.Get(HistDir).
Get(HistName)
 
   97     if hist.IsA().InheritsFrom(ROOT.TProfile.Class()):
 
   99     xtitle = MuonType + 
' Muon ' 
  107     prof_ave.SetName( HistName.replace( 
'pT_vs', 
'PtScale_vs' ) )
 
  108     prof_ave.SetTitle( 
'pT Scale vs ' + Var )
 
  109     prof_ave.SetXTitle( xtitle )
 
  110     prof_ave.SetYTitle( 
'pT scale' )
 
  112     prof_std.SetName( HistName.replace( 
'pT_vs', 
'PtResol_vs' ) )
 
  113     prof_std.SetTitle( 
'pT Resolution vs ' + Var )
 
  114     prof_std.SetXTitle( xtitle )
 
  115     prof_std.SetYTitle( 
'pT resolution' )
 
  117     if not infile.Get( HistDir ).WriteTObject( prof_ave, prof_ave.GetName(), 
"Overwrite" ):
 
  119         print( 
'WARNING failed to write histogram to file: ' + HistDir + 
'/' + prof_ave.GetName() )
 
  120     if not infile.Get( HistDir ).WriteTObject( prof_std, prof_std.GetName(), 
"Overwrite" ):
 
  122         print( 
'WARNING failed to write histogram to file: ' + HistDir + 
'/' + prof_std.GetName() )
 
  125     bindirname = Var.lower() + 
'Bins' 
  126     bindirpath = HistDir + 
'/' + bindirname
 
  127     bindir = infile.GetDirectory( bindirpath )
 
  129         bindir = infile.GetDirectory( HistDir ).
mkdir( bindirname )
 
  132         canv = ROOT.TCanvas(
"canv",
"",800,800);
 
  136         storePlots = 
'resPlots/'+os.path.split(infile.GetName())[1].
replace(
'.root',
'')
 
  137         if not os.path.exists(storePlots):
 
  138             os.makedirs(storePlots)
 
  139             print(
'Creating directory: '+storePlots)
 
  141         for x1,name,plot 
in sorted([ (
float(name.split(
'_')[0]), name, plot) 
for name, plot 
in binnedRes.iteritems() ]):
 
  142             plot.SetName( 
'bin_' + name.replace(
'-',
'm') )
 
  143             plot.SetTitle( 
'pT Resolution {0} < {1} < {2}'.
format( name.split(
'_')[0], Var, name.split(
'_')[1] ) )
 
  144             plot.SetYTitle( 
'Entries' )
 
  151                 canv.SaveAs(storePlots+
'/'+HistDir.replace(
'/',
'_')+
'_PtResFits_{0}_bins_{1}.pdf'.
format(Var,icanv))
 
  158             t.SetNDC(); t.SetTextColor(1);
 
  159             tit = name.replace(
'm',
'-').
replace(
'_',
'<'+Var+
'<')
 
  160             mu = mu_err = sigma = sigma_err = 0
 
  163             t.DrawLatex(0.2,0.96,plot.GetTitle())
 
  164             resultMeanLab =
'#mu = {0:0.2g} #pm {1:0.2g}'.
format( mu, mu_err )
 
  165             resultSigmaLab =
'#sigma = {0:0.2g} #pm {1:0.2g}'.
format( sigma, sigma_err )
 
  166             t.DrawLatex(0.2,0.85,
'#splitline{'+resultMeanLab+
'}{'+resultSigmaLab+
'}')
 
  169         canv.SaveAs(storePlots+
'/'+HistDir.replace(
'/',
'_')+
'_PtResFits_{0}_bins_{1}.pdf'.
format(Var,icanv))
 
  172     del prof_ave, prof_std, binnedRes
 
  179         print( 
'Usage: python {0} filename [doAverage doBinned]'.
format( args[0] ) )
 
  182     if not os.path.exists( filename ):
 
  183         print ( 
'File not found: ' + filename )
 
  189         doAverage = 
bool( 
'doAverage' in args[2:] )
 
  190         doBinned  = 
bool( 
'doBinned'  in args[2:] )
 
  192     print( 
'Opening file: ' + filename )
 
  193     infile = ROOT.TFile.Open( filename, 
'update' )
 
  195     MuonTypes = [ 
'Prompt' ]
 
  196     BinVars = [ 
'pT',
'lowpT',
'highpT',
'eta', 
'phi' ]
 
  198     for MuonType 
in MuonTypes:
 
  199         if not infile.Get( 
'Muons/' + MuonType ):
 
  200             print( 
'INFO TDirectory not found: Muons/' + MuonType )
 
  202         AuthDir = infile.Get( 
'Muons/{0}/matched'.
format( MuonType ) )
 
  203         Authors = [ i.GetName() 
for i 
in AuthDir.GetListOfKeys() 
if AuthDir.Get( i.GetName() ).InheritsFrom( 
'TDirectory' ) ]
 
  204         for Author 
in Authors:
 
  205             if Author==
'MSTrackParticles' or Author==
'CaloTag' or Author==
'AllMuons' or Author==
'Tight' or Author==
'Loose' or Author==
'VeryLoose':
 
  207             HistDir = 
'Muons/{0}/matched/{1}/resolution'.
format( MuonType, Author )
 
  208             FileDir = infile.Get( HistDir )
 
  210                 print( 
'INFO TDirectory not found: ' + HistDir )
 
  213                 resTypes= [ 
'',
'ID',
'MS' ]
 
  214                 for resType 
in resTypes:
 
  215                     HistName = 
'_'.
join( HistDir.split(
'/') ) + 
'_Res{0}_pT_vs_{1}'.
format( resType, Var )
 
  216                     if not FileDir.Get( HistName ):
 
  219                     CreateProfile( infile, HistDir, HistName, Var, MuonType, doAverage = doAverage, doBinned = doBinned )
 
  225 if __name__ == 
"__main__":
 
  226     ROOT.gROOT.SetBatch()