6 #include "TProfile2D.h" 11 #include "TGraphErrors.h" 45 TFile *
f = TFile::Open(file.c_str());
46 f->GetObject(name.c_str(),
h);
49 <<
" in " << f->GetName()
55 if(h->GetEntries()==0){
57 <<
" from " << f->GetName()
81 void DrawPlotStack( TCanvas *&
canv, TCanvas *&canvleg,
82 const std::vector< Plot > &plot_vec,
83 const Plot &plot_denom,
85 void DrawPlotStack( TCanvas *&canv, TCanvas *&canvleg,
86 const std::vector< Plot > &plot_vec );
89 const std::vector<Plot> &plot_vec,
90 const Plot &plot_denom,
92 void SetRatioErrors(TGraphErrors* &gRat, TH1* hNum, TH1* hDenom);
94 void Draw2DPlot( TCanvas *&canv, TH2*
h2 );
98 void GetBestYRange(
float &
ymin,
float &
ymax,
99 std::vector< TProfile * > profiles,
102 float outlierCut = 0.);
103 void GetBestYRange(
float & ymin,
float & ymax,
104 const std::vector< Plot > &plots,
105 float maxpad,
int rebin = 0,
float outlierCut = 0.);
112 int Cnum(){
return fCnum; fCnum++;}
113 int Pnum(){
return fPnum; fPnum++;}
138 const std::vector< Plot > &plot_vec )
146 const std::vector< Plot > &plot_vec,
154 gStyle->SetOptStat(0);
157 if(denom.
type!=
"TProfile")
return;
161 if(plot_vec.size()==0){
165 plotname = plot_vec[0].name;
166 isProf = plot_vec[0].type==
"TProfile";
168 plot_vec[0].type != denom.
type ){
169 std::cout <<
"Denom type: "<< plot_vec[0].type
170 <<
" not the same as plot vector: " 179 bool drawRatio = (ratio_title!=
"");
183 TPad *
p1 =
new TPad(
"p1",
"p1", 0, 0, 1, 1);
184 TPad *
p2 =
new TPad(
"p2",
"p2", 0, 0, 1, 1);
185 p1->SetBottomMargin(0.45);
186 p2->SetTopMargin(0.55);
187 p1->SetFillStyle(0); p2->SetFillStyle(0);
188 if(drawRatio) p1->cd();
195 float outlierCut(0.);
196 if( plotname.find(
"_time_") != std::string::npos ){
197 rebin = 0; outlierCut = 10.; }
199 std::vector< Plot > all_plot_vec = plot_vec;
200 all_plot_vec.push_back(denom);
203 0.1, rebin, outlierCut );
210 if( plotname.find(
"_time_") != std::string::npos ){
220 if(drawRatio) denom.
h->GetXaxis()->SetLabelSize(0.);
221 denom.
h->SetLineWidth(1);
222 denom.
h->SetLineColor(kGray+2);
223 denom.
h->SetMarkerColor(kGray+2);
225 denom.
h->SetMinimum(
ymin);
226 denom.
h->SetMaximum(ymax);
228 denom.
h->Draw( Form(
"%s", isProf ?
"" :
"hist" ));
234 for(
auto&
plot: plot_vec ){
240 if(
plot.h==denom.
h )
continue;
243 plot.h->SetLineWidth(2);
247 plot.h->SetMaximum(ymax);
250 plot.h->Scale(1/
plot.h->Integral());
251 plot.h->GetYaxis()->SetTitle(
"Arb. Units");
258 if(drawRatio)
plot.h->GetXaxis()->SetLabelSize(0.);
259 plot.h->Draw(Form(
"%s plc pmc", isProf ?
"" :
"hist"));
262 plot.h->Draw(Form(
"%s same plc pmc", isProf ?
"" :
"hist"));
269 MakeRatio( p2, plot_vec, denom, ratio_title );
284 if(!h2->InheritsFrom(TH2::Class())){
289 gStyle->SetPalette(kBird);
299 const std::vector<Plot> &plot_vec,
300 const Plot &plot_denom,
304 std::vector<TProfile*> ratio_vec;
305 if(plot_denom.
h->InheritsFrom(TProfile::Class()))
306 for(
auto&
pl : plot_vec)
307 ratio_vec.push_back((TProfile*)
pl.h->Clone(Form(
"%s_%i",
pl.h->GetName(),
Pnum())));
311 if( plot_denom.
type == plot_vec[0].type ){
312 for(
auto& rat : ratio_vec)
313 rat->Divide(plot_denom.
h);
320 TH1 *hAxes = (TH1*)plot_vec[0].
h->Clone();
322 hAxes->GetYaxis()->SetTitle(ratio_title.c_str());
324 for(
unsigned int i=0;
i<ratio_vec.size();
i++ ){
326 TGraphErrors *gRat =
new TGraphErrors(ratio_vec[
i]);
327 gRat->SetLineColor (plot_vec[i].
h->GetLineColor());
328 gRat->SetMarkerColor(plot_vec[i].
h->GetLineColor());
332 gRat->Draw(
"P PLC PMC");
333 double x[2] = {gRat->GetXaxis()->GetXmin(), gRat->GetXaxis()->GetXmax()};
334 double y[2] = {1.0, 1.0};
335 TGraph* l_Xaxis =
new TGraph(2, x, y);
336 l_Xaxis->SetLineColor(kGray);
337 l_Xaxis->SetLineWidth(3);
338 l_Xaxis->SetLineStyle(2);
339 l_Xaxis->Draw(
"L SAME");
342 gRat->Draw(
"P PLC PMC");
348 hAxes->GetYaxis()->CenterTitle();
349 hAxes->GetYaxis()->ChangeLabel();
350 hAxes->GetYaxis()->SetRangeUser(ymin, ymax);
359 for(
int bin = 1;
bin <= hNum->GetNbinsX();
bin++)
361 double num, denom, errnum, errdenom;
362 num = hNum->GetBinContent(
bin); errnum = hNum->GetBinError(
bin);
363 denom = hDenom->GetBinContent(
bin); errdenom = hDenom->GetBinError(
bin);
365 if(num!=0. && denom!=0.){
366 gRat->SetPointError(
bin-1, 0., (num/denom)*
std::sqrt((errnum/num)*(errnum/num)+(errdenom/denom)*(errdenom/denom)));
369 gRat->SetPointError(
bin-1, 0., 0.);
383 TLegend *
leg =
new TLegend(0, 0, 1, 1);
384 if(plots.size()+1>2 && plots.size()+1<7) leg->SetNColumns(3);
385 if(plots.size()+1>=7) leg->SetNColumns(5);
387 leg->AddEntry(denom.
h, denom.
legLabel.c_str(),
"L");
389 leg->AddEntry(
p.h,
p.legLabel.c_str(),
"L");
399 const std::vector< Plot > &
plots,
400 float maxpad,
int rebin,
float outlierCut )
402 if(plots[0].
type!=
"TProfile"){
403 std::cout <<
"GetBestYRange only works for profiles, skip" 409 std::vector< TProfile* > profiles;
410 for(
auto&
pl : plots )
411 profiles.push_back((TProfile*)
pl.h);
413 GetBestYRange( ymin, ymax, profiles, maxpad, rebin, outlierCut );
420 std::vector< TProfile* > profiles,
428 for(
auto&
p: profiles ){
429 h = (TProfile *)
p->Clone(Form(
"%s_%i",
p->GetName(),
Pnum()));
432 for(
int b = 1;
b < h->GetXaxis()->GetNbins()-1;
b++ ){
436 if( h->GetBinError(
b) == 0 )
continue;
438 if( h->GetBinContent(
b) <
ymin ) ymin = h->GetBinContent(
b);
439 if( ymax < h->GetBinContent(
b) ) ymax = h->GetBinContent(
b);
447 TH1F * binContDist =
new TH1F(Form(
"%s_bin_contents_%i",
450 for(
auto&
p: profiles ){
451 h = (TProfile *)
p->Clone(Form(
"%s_%i",
p->GetName(),
Pnum()));
454 for(
int b = 1;
b < h->GetXaxis()->GetNbins()-1;
b++ ){
455 if( h->GetBinError(
b) == 0 )
continue;
456 binContDist->Fill(h->GetBinContent(
b));
459 float combinedMean = binContDist->GetMean();
460 float combinedRMS = binContDist->GetRMS();
467 for(
auto&
p: profiles ){
468 h = (TProfile *)
p->Clone(Form(
"%s_%i",
p->GetName(),
Pnum()));
470 for(
int b = 1;
b < h->GetXaxis()->GetNbins()-1;
b++ ){
471 if( h->GetBinError(
b) == 0 )
continue;
473 if( outlierCut*combinedRMS <
std::abs(h->GetBinContent(
b) - combinedMean) )
continue;
475 if( h->GetBinContent(
b) <
ymin ) ymin = h->GetBinContent(
b);
476 if( ymax < h->GetBinContent(
b) ) ymax = h->GetBinContent(
b);
484 std::cout <<
"Min/Max Y Failed in " << profiles[0]->GetName()
485 <<
": min = " << ymin <<
", max = " << ymax
486 <<
"\n The plot may be empty." 491 float ymid = (ymax+
ymin)/2;
492 float fracRange = (ymax-
ymin)/ymid;
494 float pad = ( (maxpad - 0.1) / (0.01 - 0.2) ) * fracRange;
495 if( pad > maxpad ) pad = maxpad;
496 if( pad < 0.1 ) pad = 0.1;
498 ymax = ymax + pad*ymid;
499 ymin = ymin - pad*ymid;
505 std::cout <<
"Min/Max Y Failed in " << profiles[0]->GetName()
506 <<
": min = " << ymin <<
", max = " << ymax
Plot(std::string file_, std::string name_, std::string legLabel_)
Plot(TH1 *h_, std::string name_, std::string legLabel_)
void MakeRatio(TPad *&pad, const std::vector< Plot > &plot_vec, const Plot &plot_denom, std::string ratio_name)
TH1D * MakeRatio(TH1D *num, TH1D *denom, int Col, std::string FType)
std::string GetName(int i)
====================================================================== ///
void GetBestYRange(float &ymin, float &ymax, std::vector< TProfile * > profiles, float maxpad, int rebin=0, float outlierCut=0.)
void DrawPlotStack(TCanvas *&canv, TCanvas *&canvleg, const std::vector< Plot > &plot_vec, const Plot &plot_denom, std::string ratio_title="")
const std::vector< Plot > plots
void Draw2DPlot(TCanvas *&canv, TH2 *h2)
static float min(const float a, const float b, const float c)
void SetRatioErrors(TGraphErrors *&gRat, TH1 *hNum, TH1 *hDenom)
void MakeLegend(TCanvas *&cleg, const std::vector< Plot > &plots, const Plot &denom)
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
TLegend * MakeLegend(TString beam, TString defname, TH2 *hpred, TH2 *hcosm, TPolyLine *line, TGraph *gr)