31int usage(
const std::string& err_msg=
"",
int status=0 ) {
33 std::string name =
"dependence";
35 if ( err_msg!=
"" ) std::cerr << err_msg << std::endl;
37 std::ostream& s = std::cout;
39 s <<
"Usage: " << name <<
"\t [OPTIONS] \n\n";
40 s <<
"\t" <<
" plots comparison histograms";
41 s <<
" - compiled on " << __DATE__ <<
" at " << __TIME__ <<
"\n\n";
43 s <<
" -c, --config value \t configure which histograms to plot from config file,\n\n";
44 s <<
" -f, --fit \t fit a pol2 to the efficiencies\n\n";
45 s <<
" -lx, --logx \t drawwith log x axis\n";
46 s <<
" -as, --atlasstyle \t usethe atlas style\n\n";
48 s <<
" -h, --help \t this help\n";
64 int ibin =
h->FindBin(d);
65 if ( ibin<1 || ibin>
h->GetNbinsX() )
return;
69 for (
int i=1 ; i<=h0->GetNbinsX() ; i++ )
if ( h0->GetBinCenter(i)<
ptmax ) v += h0->GetBinContent(i);
70 h->SetBinContent( ibin, v );
74 for (
int i=1 ; i<=h0->GetNbinsX() ; i++ ) v += h0->GetBinContent(i);
75 h->SetBinContent( ibin, v );
86 std::string fitf =
"";
88 TF1* hfit =
new TF1(
"hyper",
"[0]-[3]*( (x-[1]) + sqrt( (x-[1])*(x-[1]) + 4*[2]*[2] ) )/(2*[2])", 0, 5 );
90 hfit->SetParameter(0, 0.95);
91 hfit->SetParameter(1, 1);
92 hfit->SetParameter(2, 1);
93 hfit->SetParameter(3, 1);
95 std::string fitname[4] = {
"pol0",
"pol1",
"hyper",
"pol2" };
97 for (
int ifit=0 ; ifit<4 ; ifit++ ) {
99 h->Fit( fitname[ifit].c_str() );
100 TF1* f1 = (TF1*)
h->GetListOfFunctions()->FindObject( fitname[ifit].c_str() );
103 f1->SetLineColor(kRed);
105 chi2 = f1->GetChisquare();
110 fitf = fitname[ifit];
114 std::sprintf( flabel,
"%s: #chi^{2}/dof = %6.2lf/%d", fitf.c_str(),
chi2, ndof );
116 std::string chi2label = flabel;
118 if ( chi2dof < 1.5 )
return chi2label;
128void efficiency( std::vector<double>&
bins, std::vector<double>& values,
const std::vector<std::string>&
files,
129 const std::string& histname,
const std::string& tplotname,
const std::string&
label=
"" ) {
132 std::string plotname = tplotname;
133 std::string labels =
";value;mean";
135 size_t pos = plotname.find(
';');
137 if ( pos!=std::string::npos ) {
138 labels = plotname.substr( pos, plotname.size() );
139 plotname.resize(pos);
140 std::cout <<
"plotname: " << plotname <<
"\tlabels: " << labels << std::endl;
144 if ( values.size() !=
files.size() ) {
145 std::cerr <<
"number of values (" << values.size() <<
") and files (" <<
files.size() <<
") do not match" << std::endl;
152 if (
bins.size()==3 ) {
153 hd =
new TH1F(
"denominator", labels.c_str(),
int(
bins[0]),
bins[1],
bins[2] );
154 hn =
new TH1F(
"numerator", labels.c_str(),
int(
bins[0]),
bins[1],
bins[2] );
156 else if (
bins.size()>3 ) {
157 hd =
new TH1F(
"denominator", labels.c_str(),
bins.size()-1, &
bins[0] );
158 hn =
new TH1F(
"numerator", labels.c_str(),
bins.size()-1, &
bins[0] );
161 std::exit(
usage(
"unusable binning", -1) );
164 std::cout <<
"looping over files ..." << std::endl;
166 for (
size_t i=0 ; i<
files.size() ; i++ ) {
168 const std::string& s =
files[i];
170 std::cout <<
"file: "<< s <<
"\tvalue: "<< values[i] << std::endl;
172 TFile* f =
new TFile( s.c_str() );
175 std::cerr <<
"could not open file" << s << std::endl;
182 TH1F* he_n = (TH1F*)f->Get( (histname+
"_n").c_str() );
183 TH1F* he_d = (TH1F*)f->Get( (histname+
"_d").c_str() );
186 std::cerr <<
"histogram " << histname+
"_n" <<
"could not be retrieved" << std::endl;
191 std::cerr <<
"histogram " << histname+
"_d" <<
"could not be retrieved" << std::endl;
204 double scale_eff = 1;
207 TGraphAsymmErrors* tgtest = e.Bayes(scale_eff);
209 TCanvas* c2 =
new TCanvas( plotname.c_str(),
"eff", 10, 10, 700, 500 );
214 h->SetMarkerStyle(20);
215 h->SetLineColor(
h->GetMarkerColor());
217 h->GetYaxis()->SetRangeUser(0.85,1.02);
218 h->GetXaxis()->SetRangeUser(0.4,1.7);
220 tgtest->SetMarkerStyle(20);
221 tgtest->SetMarkerColor(
h->GetMarkerColor());
222 tgtest->SetLineColor(
h->GetMarkerColor());
225 std::string chi2label =
"";
229 h->GetXaxis()->SetMoreLogLabels(
true);
233 tgtest->Draw(
"samep");
235 if (
doLogx ) gPad->SetLogx(
true);
238 if ( chi2label!=
"" )
DrawLabel( 0.2, 0.21, chi2label );
241 gPad->Print( plotname.c_str() );
254void mean( std::vector<double>&
bins, std::vector<double>& values,
const std::vector<std::string>&
files,
255 const std::string& histname,
const std::string& tplotname,
const std::string&
label=
"" ) {
257 if ( values.size() !=
files.size() ) {
258 std::cerr <<
"number of values (" << values.size() <<
") and files (" <<
files.size() <<
") do not match" << std::endl;
265 std::string plotname = tplotname;
266 std::string labels =
";value;mean";
268 size_t pos = plotname.find(
';');
270 if ( pos!=std::string::npos ) {
271 labels = plotname.substr( pos, plotname.size() );
272 plotname.resize(pos);
273 std::cout <<
"plotname: " << plotname <<
"\tlabels: " << labels << std::endl;
277 if (
bins.size()==3 ) {
278 h =
new TH1F(
"mean", labels.c_str(),
int(
bins[0]),
bins[1],
bins[2] );
280 else if (
bins.size()>3 ) {
281 h =
new TH1F(
"mean", labels.c_str(),
bins.size()-1, &
bins[0] );
284 std::exit(
usage(
"unusable binning", -1) );
287 std::cout <<
"looping over files ..." << std::endl;
289 for (
size_t i=0 ; i<
files.size() ; i++ ) {
291 const std::string& s =
files[i];
293 TFile* f =
new TFile( s.c_str() );
296 std::cerr <<
"could not open file" << s << std::endl;
302 TH1F* he = (TH1F*)f->Get( histname.c_str() );
305 std::cerr <<
"histogram " << histname <<
"could not be retrieved" << std::endl;
309 std::cout <<
"\t he: " << he << std::endl;
311 int ibin =
h->FindBin(values[i]);
312 if ( ibin<1 || ibin>
h->GetNbinsX() )
continue;
315 std::cout <<
"bin: " << ibin << std::endl;
316 std::cout <<
"mean: " << he->GetMean() <<
" +- " << he->GetMeanError() << std::endl;
319 h->SetBinContent( ibin, he->GetMean() );
320 h->SetBinError( ibin, he->GetMeanError() );
330 TCanvas* c =
new TCanvas( plotname.c_str(),
"eff", 10, 10, 700, 500 );
333 h->SetMarkerStyle(20);
334 h->SetLineColor(
h->GetMarkerColor());
336 std::string chi2label =
"";
340 h->GetXaxis()->SetMoreLogLabels(
true);
344 if (
doLogx ) gPad->SetLogx(
true);
347 if ( chi2label!=
"" )
DrawLabel( 0.2, 0.21, chi2label );
349 gPad->Print( plotname.c_str() );
360int main(
int argc,
char** argv ) {
364 std::string config_file =
"";
366 bool atlasstyle =
false;
370 for (
int i=1 ; i<argc ; i++ ) {
371 std::string arg = argv[i];
373 if ( arg==
"-h" || arg==
"--help" )
return usage();
374 else if ( arg==
"-f" || arg==
"--fit" )
doFit =
true;
375 else if ( arg==
"-as" || arg==
"--atlasstyle" ) atlasstyle =
true;
376 else if ( arg==
"-lx" || arg==
"--logx" )
doLogx =
true;
377 else if ( arg==
"-c" ) {
378 if ( ++i<argc ) config_file = argv[i];
379 else return usage(
"no config file provided", -1 );
383 if ( config_file ==
"" )
return usage(
"no config file provided", -1 );
387 gStyle->SetPadTopMargin(0.05);
388 gStyle->SetPadRightMargin(0.05);
390 gStyle->SetOptStat(0);
391 gStyle->SetErrorX(0);
397 bool do_mean =
false;
398 bool do_efficiency =
false;
402 if (
config.isTagDefined(
"mean") ) do_mean = (
config.GetValue(
"mean")>0 ?
true :
false );
403 if (
config.isTagDefined(
"efficiency") ) do_efficiency = (
config.GetValue(
"efficiency")>0 ?
true :
false );
408 std::vector<void (*)( std::vector<double>&
bins, std::vector<double>& values,
const std::vector<std::string>&
files,
409 const std::string& histname,
const std::string& plotname,
const std::string&
label ) > function;
411 std::vector<std::vector<std::string> > plots;
413 std::vector<std::vector<std::string> >
files;
415 if ( do_efficiency ) {
417 files.push_back(
config.GetStringVector(
"efiles") );
418 plots.push_back(
config.GetStringVector(
"eplots") );
422 function.push_back(
mean );
423 files.push_back(
config.GetStringVector(
"mfiles") );
424 plots.push_back(
config.GetStringVector(
"mplots") );
427 if ( function.empty() )
return 0;
434 std::vector<double>
bins =
config.GetVector(
"bins");
436 std::vector<double> values =
config.GetVector(
"values");
450 std::cout <<
"looping over plots ..." << std::endl;
452 for (
size_t j=function.size() ; j-- ; ) {
454 for (
size_t i=0 ; i<plots[j].size() ; i+=3 ) {
456 std::string hist = plots[j][i];
458 std::string plotname = plots[j][i+1];
460 std::string
label = plots[j][i+2];
462 std::cout <<
"plot: " << plotname <<
" :\t" << hist <<
"\tlabel: " <<
label << std::endl;
static const std::vector< std::string > bins
ATLAS Style, based on a style file from BaBar.
Header file for AthHistogramAlgorithm.
Get tag-value pairs from a file.
double chi2(TH1 *h0, TH1 *h1)
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
std::string runfit(TH1F *h)
void add_to_bin(double d, TH1 *h, TH1 *h0)
void efficiency(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
std::vector< std::string > files
file names and file pointers
std::string label(const std::string &format, int i)