95def 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()):
98 return
99 xtitle = MuonType + ' Muon '
100 if 'pT' in Var:
101 xtitle += 'pT [GeV]'
102 else:
103 xtitle += Var
104
105 prof_ave, prof_std, binnedRes = GetProfilesFromTH2( hist, doAverage = doAverage )
106
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' )
111
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' )
116
117 if not infile.Get( HistDir ).WriteTObject( prof_ave, prof_ave.GetName(), "Overwrite" ):
118
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" ):
121
122 print(
'WARNING failed to write histogram to file: ' + HistDir +
'/' + prof_std.GetName() )
123
124
125 bindirname = Var.lower() + 'Bins'
126 bindirpath = HistDir + '/' + bindirname
127 bindir = infile.GetDirectory( bindirpath )
128 if not bindir:
129 bindir = infile.GetDirectory( HistDir ).mkdir( bindirname )
130
131 if doBinned:
132 canv = ROOT.TCanvas("canv","",800,800);
133 canv.Divide(3,3)
134 ipad = 0
135 icanv = 0
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)
140
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' )
145 t=ROOT.TCanvas()
146
147
148 ipad+=1
149 if ipad>9:
150 canv.cd()
151 canv.SaveAs(storePlots+'/'+HistDir.replace('/','_')+'_PtResFits_{0}_bins_{1}.pdf'.format(Var,icanv))
152 canv.Clear()
153 canv.Divide(3,3)
154 icanv+=1
155 ipad=1
156 canv.cd(ipad)
157 t = ROOT.TLatex()
158 t.SetNDC(); t.SetTextColor(1);
159 tit = name.replace(
'm',
'-').
replace(
'_',
'<'+Var+
'<')
160 mu = mu_err = sigma = sigma_err = 0
161 canv.cd(ipad)
162 plot.Draw()
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+'}')
167
168 canv.cd()
169 canv.SaveAs(storePlots+'/'+HistDir.replace('/','_')+'_PtResFits_{0}_bins_{1}.pdf'.format(Var,icanv))
170 canv.Close()
171
172 del prof_ave, prof_std, binnedRes
173
174
void print(char *figname, TCanvas *c1)
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
std::string replace(std::string s, const std::string &s2, const std::string &s3)