40 HistPlot::HistPlot(
const std::string& sname,
const std::string& stitle)
41 : RFPlot(sname, stitle)
43 SetPlotLabel(
"#bf{#it{RestFrames}} Event Generation");
45 SetScaleLabel(
"a. u.");
49 m_Log.SetSource(
"HistPlot "+
GetName());
53 HistPlot::~HistPlot(){
54 int Nv = m_Vars.GetN();
55 for(
int i = 0; i < Nv; i++)
57 int Nc = m_Cats.GetN();
58 for(
int i = 0; i < Nc; i++)
64 int N = m_1DHists.size();
65 for(
int i = 0; i < N; i++)
69 for(
int i = 0; i < N; i++)
75 m_CatToHist1D.clear();
76 m_CatToHist2D.clear();
78 m_Plot1D_Cats.clear();
79 m_Plot1D_Color.clear();
80 m_Plot2D_Vars.clear();
82 m_Plot2D_Color.clear();
86 HistPlotVar const& HistPlot::GetNewVar(
const std::string& name,
87 const std::string& title,
88 double minval,
double maxval,
89 const std::string& unit){
95 HistPlotCategory
const& HistPlot::GetNewCategory(
const std::string& name,
96 const std::string& title){
97 HistPlotCategory* cat =
new HistPlotCategory(name, title);
102 void HistPlot::AddPlot(
const HistPlotVar& var,
103 RFList<const HistPlotCategory> cats,
105 if(!m_Vars.Contains(var))
108 int Ncat = cats.GetN();
109 for(
int i = 0; i < Ncat; i++){
110 if(m_Cats.Contains(cats[i]))
117 const HistPlotCategory* empty = &HistPlotCategory::Empty();
118 if(m_CatToHist1D.count(empty) <= 0)
119 m_CatToHist1D[empty] = std::vector<TH1D*>();
121 int Nhist = m_CatToHist1D[empty].size();
123 for(
int i = 0; i < Nhist; i++){
124 if(*m_HistToVar[m_CatToHist1D[empty][i]] == var){
130 std::string name = GetUniqueName(var.GetName()+
"_"+
GetName());
131 TH1D* hist =
new TH1D(name.c_str(),name.c_str(),
132 256,var.GetMin(),var.GetMax());
133 m_HistToVar[hist] = &var;
134 m_CatToHist1D[empty].push_back(hist);
135 m_1DHists.push_back(hist);
138 for(
int c = 0; c < Ncat; c++){
139 if(m_CatToHist1D.count(&cats[c]) <= 0)
140 m_CatToHist1D[&cats[c]] = std::vector<TH1D*>();
142 int Nhist = m_CatToHist1D[&cats[c]].size();
144 for(
int i = 0; i < Nhist; i++){
145 if(*m_HistToVar[m_CatToHist1D[&cats[c]][i]] == var){
151 std::string name = GetUniqueName(var.GetName()+
"_"+cats[c].GetName()+
"_"+
GetName());
152 TH1D* hist =
new TH1D(name.c_str(),name.c_str(),
153 256,var.GetMin(),var.GetMax());
154 m_HistToVar[hist] = &var;
155 m_CatToHist1D[&cats[c]].push_back(hist);
156 m_1DHists.push_back(hist);
160 m_Plot1D_Var.push_back(&var);
161 m_Plot1D_Cats.push_back(cats);
162 m_Plot1D_Color.push_back(invert_colors);
165 void HistPlot::AddPlot(
const HistPlotVar& varX,
const HistPlotVar& varY,
166 RFList<const HistPlotCategory> cats,
168 if(!m_Vars.Contains(varX) ||
169 !m_Vars.Contains(varY))
172 int Ncat = cats.GetN();
173 for(
int i = 0; i < Ncat; i++){
174 if(m_Cats.Contains(cats[i]))
181 const HistPlotCategory* empty = &HistPlotCategory::Empty();
182 if(m_CatToHist2D.count(empty) <= 0)
183 m_CatToHist2D[empty] = std::vector<TH2D*>();
185 int Nhist = m_CatToHist2D[empty].size();
187 for(
int i = 0; i < Nhist; i++){
188 if(*m_HistToVars[m_CatToHist2D[empty][i]].first == varX &&
189 *m_HistToVars[m_CatToHist2D[empty][i]].second == varY){
195 std::string name = GetUniqueName(varX.GetName()+
"_v_"+varY.GetName()+
"_"+
GetName());
196 TH2D* hist =
new TH2D(name.c_str(),name.c_str(),
197 128,varX.GetMin(),varX.GetMax(),
198 128,varY.GetMin(),varY.GetMax());
201 std::pair<const HistPlotVar*,const HistPlotVar*>(&varX,&varY);
202 m_CatToHist2D[empty].push_back(hist);
203 m_2DHists.push_back(hist);
204 m_Plot2D_Vars.push_back(std::pair<
const HistPlotVar*,
205 const HistPlotVar*>(&varX,&varY));
206 m_Plot2D_Cat.push_back(empty);
207 m_Plot2D_Color.push_back(invert_colors);
210 for(
int c = 0; c < Ncat; c++){
211 if(m_CatToHist2D.count(&cats[c]) <= 0)
212 m_CatToHist2D[&cats[c]] = std::vector<TH2D*>();
214 int Nhist = m_CatToHist2D[&cats[c]].size();
216 for(
int i = 0; i < Nhist; i++){
217 if(*m_HistToVars[m_CatToHist2D[&cats[c]][i]].first == varX &&
218 *m_HistToVars[m_CatToHist2D[&cats[c]][i]].second == varY){
224 std::string name = GetUniqueName(varX.GetName()+
"_v_"+varY.GetName()+
"_"+
225 cats[c].GetName()+
"_"+
GetName());
226 TH2D* hist =
new TH2D(name.c_str(),name.c_str(),
227 128,varX.GetMin(),varX.GetMax(),
228 128,varY.GetMin(),varY.GetMax());
230 std::pair<const HistPlotVar*,const HistPlotVar*>(&varX,&varY);
231 m_CatToHist2D[&cats[c]].push_back(hist);
232 m_2DHists.push_back(hist);
233 m_Plot2D_Vars.push_back(std::pair<
const HistPlotVar*,
234 const HistPlotVar*>(&varX,&varY));
235 m_Plot2D_Cat.push_back(&cats[c]);
236 m_Plot2D_Color.push_back(invert_colors);
242 void HistPlot::Fill(
double weight){
243 const HistPlotCategory* empty = &HistPlotCategory::Empty();
244 int N = m_CatToHist1D[empty].size();
245 for(
int i = 0; i < N; i++)
246 m_CatToHist1D[empty][i]->Fill(m_HistToVar[m_CatToHist1D[empty][i]]->GetVal(),
248 N = m_CatToHist2D[empty].size();
249 for(
int i = 0; i < N; i++)
250 m_CatToHist2D[empty][i]->Fill(m_HistToVars[m_CatToHist2D[empty][i]].first->GetVal(),
251 m_HistToVars[m_CatToHist2D[empty][i]].second->GetVal(),
255 void HistPlot::Fill(
const HistPlotCategory& cat,
double weight){
261 int N = m_CatToHist1D[&cat].size();
262 for(
int i = 0; i < N; i++)
263 m_CatToHist1D[&cat][i]->Fill(m_HistToVar[m_CatToHist1D[&cat][i]]->GetVal(),
265 N = m_CatToHist2D[&cat].size();
266 for(
int i = 0; i < N; i++)
267 m_CatToHist2D[&cat][i]->Fill(m_HistToVars[m_CatToHist2D[&cat][i]].first->GetVal(),
268 m_HistToVars[m_CatToHist2D[&cat][i]].second->GetVal(),
272 void HistPlot::Draw(
bool invert_colors){
273 int N1D = m_Plot1D_Var.size();
274 int N2D = m_Plot2D_Vars.size();
276 for(
int i = 0; i < N2D; i++)
277 DrawPlot(m_Plot2D_Vars[i], *m_Plot2D_Cat[i],
278 (m_Plot2D_Color[i] || invert_colors));
279 for(
int i = 0; i < N1D; i++)
280 DrawPlot(*m_Plot1D_Var[i], m_Plot1D_Cats[i],
281 (m_Plot1D_Color[i] || invert_colors));
284 void HistPlot::DrawPlot(
const HistPlotVar& var,
285 const HistPlotCatList& cats,
287 std::vector<TH1D*> hists;
288 int Ncat = cats.GetN();
289 std::string catname =
"";
292 const HistPlotCategory* empty = &HistPlotCategory::Empty();
293 int Nhist = m_CatToHist1D[empty].size();
294 for(
int i = 0; i < Nhist; i++){
295 if(*m_HistToVar[m_CatToHist1D[empty][i]] == var){
296 hists.push_back(m_CatToHist1D[empty][i]);
301 for(
int c = 0; c < Ncat; c++){
302 catname += cats[c].GetName() +
"_";
303 int Nhist = m_CatToHist1D[&cats[c]].size();
304 for(
int i = 0; i < Nhist; i++){
305 if(*m_HistToVar[m_CatToHist1D[&cats[c]][i]] == var){
306 hists.push_back(m_CatToHist1D[&cats[c]][i]);
313 std::string name = GetUniqueName(
"c_"+var.GetName()+
"_"+catname+
GetName());
314 TCanvas* can =
new TCanvas(name.c_str(),name.c_str(),600,500);
315 can->SetLeftMargin(0.2);
316 can->SetRightMargin(0.05);
319 can->SetFillColor(kBlack);
325 std::string XLabel = var.GetTitle();
327 std::string ScaleLabel;
329 ScaleLabel =
"#frac{1}{N} #frac{dN}{";
330 ScaleLabel +=
"d( "+XLabel+
" )}";
332 ScaleLabel = m_ScaleLabel;
335 if(var.GetUnit() !=
"")
336 XLabel +=
" "+var.GetUnit();
338 int N = hists.size();
346 for(
int i = 0; i < N; i++){
347 hists[i]->Rebin(m_Rebin);
349 if(hists[i]->Integral() > 0.)
350 hists[i]->Scale(1./hists[i]->Integral());
352 hists[i]->Scale(m_Scale);
354 if(hists[i]->GetMaximum() > hmax || imax < 0){
355 hmax = hists[i]->GetMaximum();
358 if(hists[i]->GetMinimum(0.) < hmin || imin < 0){
359 hmin = hists[i]->GetMinimum(0.);
362 if(hists[i]->GetMinimum() < hmin0 || imin0 < 0){
363 hmin0 = hists[i]->GetMinimum();
369 hists[imax]->GetXaxis()->SetTitle(XLabel.c_str());
370 hists[imax]->GetXaxis()->SetTitleOffset(1.27);
371 hists[imax]->GetXaxis()->CenterTitle();
372 hists[imax]->GetYaxis()->SetTitle(ScaleLabel.c_str());
373 hists[imax]->GetYaxis()->SetTitleOffset(1.42);
374 hists[imax]->GetYaxis()->CenterTitle();
376 hists[imax]->GetYaxis()->SetRangeUser(0., 1.1*hmax);
378 hists[imax]->GetYaxis()->SetRangeUser(0.9*hmin, 1.1*hmax);
381 hists[imax]->GetXaxis()->SetTitleColor(kWhite);
382 hists[imax]->GetXaxis()->SetLabelColor(kWhite);
383 hists[imax]->GetXaxis()->SetAxisColor(kWhite);
384 hists[imax]->GetYaxis()->SetTitleColor(kWhite);
385 hists[imax]->GetYaxis()->SetLabelColor(kWhite);
386 hists[imax]->GetYaxis()->SetAxisColor(kWhite);
389 for(
int i = N-1; i >= 0; i--){
390 int icolor0 = 7003 + (i%8)*10;
391 int icolor1 = 7000 + (i%8)*10;
393 icolor0 = 7000 + (i%8)*10;
394 icolor1 = 7003 + (i%8)*10;
396 hists[i]->SetFillColor(icolor1);
397 hists[i]->SetFillStyle(3002);
398 hists[i]->SetLineColor(icolor0);
399 hists[i]->SetLineWidth(3);
400 hists[i]->SetMarkerColor(icolor0);
401 hists[i]->SetMarkerSize(0);
402 hists[i]->Draw(
"same");
405 TLatex l(0.6,0.943,m_PlotTitle.c_str());
408 l.SetTextColor(kWhite);
409 l.SetTextSize(0.045);
411 l.DrawLatex(0.48+std::max(0.,0.47-l.GetXsize()),0.947,m_PlotTitle.c_str());
414 l.DrawLatex(0.02,0.95,m_PlotLabel.c_str());
417 TLegend* leg =
new TLegend(0.225,std::max(0.5,0.884-
double(N)*0.073),0.488,0.884);
419 leg->SetShadowColor(kWhite);
420 leg->SetLineColor(kWhite);
421 leg->SetFillColor(kWhite);
423 leg->SetShadowColor(kBlack);
424 leg->SetLineColor(kBlack);
425 leg->SetFillColor(kBlack);
426 leg->SetTextColor(kWhite);
428 leg->SetTextFont(132);
429 leg->SetTextSize(0.045);
430 for(
int i = 0; i < N; i++)
431 leg->AddEntry(hists[i],cats[i].GetTitle().c_str());
435 if(N == 1 && !cats[0].
IsEmpty()){
438 l.DrawLatex(0.23,0.84,cats[0].
GetTitle().c_str());
444 void HistPlot::DrawPlot(
const std::pair<
const HistPlotVar*,
445 const HistPlotVar*>& vars,
446 const HistPlotCategory& cat,
448 const HistPlotVar& varX = *vars.first;
449 const HistPlotVar& varY = *vars.second;
450 TH2D* hist =
nullptr;
451 std::string catname =
"";
453 const HistPlotCategory* empty = &HistPlotCategory::Empty();
454 int Nhist = m_CatToHist2D[empty].size();
455 for(
int i = 0; i < Nhist; i++){
456 if(*m_HistToVars[m_CatToHist2D[empty][i]].first == varX &&
457 *m_HistToVars[m_CatToHist2D[empty][i]].second == varY){
458 hist = m_CatToHist2D[empty][i];
463 catname += cat.GetName() +
"_";
464 int Nhist = m_CatToHist2D[&cat].size();
465 for(
int i = 0; i < Nhist; i++){
466 if(*m_HistToVars[m_CatToHist2D[&cat][i]].first == varX &&
467 *m_HistToVars[m_CatToHist2D[&cat][i]].second == varY){
468 hist = m_CatToHist2D[&cat][i];
474 std::string name = GetUniqueName(
"c_"+varX.GetName()+
"_v_"+varY.GetName()+
"_"+catname+
GetName());
475 TCanvas* can =
new TCanvas(name.c_str(),name.c_str(),600,500);
478 can->SetFillColor(kBlack);
485 std::string XLabel = varX.GetTitle();
486 std::string YLabel = varY.GetTitle();
488 std::string ScaleLabel;
490 if(hist->Integral() > 0.)
491 hist->Scale(1./hist->Integral());
492 ScaleLabel =
"#frac{1}{N} #frac{dN}{";
493 ScaleLabel +=
"d( "+XLabel+
" ) ";
494 ScaleLabel +=
"d( "+YLabel+
" )";
497 hist->Scale(m_Scale);
498 ScaleLabel = m_ScaleLabel;
501 hist->RebinX(m_Rebin);
502 hist->RebinY(m_Rebin);
504 if(varX.GetUnit() !=
"")
505 XLabel +=
" "+varX.GetUnit();
506 if(varY.GetUnit() !=
"")
507 YLabel +=
" "+varY.GetUnit();
509 SetZPalette(invert_colors);
512 hist->GetXaxis()->SetTitle(XLabel.c_str());
513 hist->GetXaxis()->SetTitleOffset(1.24);
514 hist->GetXaxis()->CenterTitle();
515 hist->GetYaxis()->SetTitle(YLabel.c_str());
516 hist->GetYaxis()->SetTitleOffset(1.11);
517 hist->GetYaxis()->CenterTitle();
518 hist->GetZaxis()->SetTitle(ScaleLabel.c_str());
519 hist->GetZaxis()->SetTitleOffset(1.5);
520 hist->GetZaxis()->CenterTitle();
521 hist->GetZaxis()->SetRangeUser(0.9*hist->GetMinimum(0.0),1.1*hist->GetMaximum());
523 hist->GetXaxis()->SetTitleColor(kWhite);
524 hist->GetXaxis()->SetLabelColor(kWhite);
525 hist->GetXaxis()->SetAxisColor(kWhite);
526 hist->GetYaxis()->SetTitleColor(kWhite);
527 hist->GetYaxis()->SetLabelColor(kWhite);
528 hist->GetYaxis()->SetAxisColor(kWhite);
529 hist->GetZaxis()->SetTitleColor(kWhite);
530 hist->GetZaxis()->SetLabelColor(kWhite);
531 hist->GetZaxis()->SetAxisColor(kWhite);
535 std::string title = m_PlotTitle;
537 title = cat.GetTitle();
539 TLatex l(0.6,0.943,title.c_str());
542 l.SetTextColor(kWhite);
543 l.SetTextSize(0.045);
545 l.DrawLatex(0.48+std::max(0.,0.32-l.GetXsize()),0.947,title.c_str());
548 l.DrawLatex(0.02,0.95,m_PlotLabel.c_str());
553 void HistPlot::SetScale(
double scale){
557 m_ScaleLabel =
"a. u.";
564 void HistPlot::SetScaleLabel(
const std::string& label){
565 m_ScaleLabel = label;
568 void HistPlot::SetPlotLabel(
const std::string& label){
572 void HistPlot::SetPlotTitle(
const std::string& title){
576 void HistPlot::SetRebin(
int rebin){
581 void HistPlot::WriteHist(
const std::string& name){
582 TFile* file =
new TFile(name.c_str(),
"UPDATE");
583 file->mkdir((
GetName()+
"/hist").c_str());
584 file->cd((
GetName()+
"/hist").c_str());
585 int N = m_1DHists.size();
586 for(
int i = 0; i < N; i++)
587 m_1DHists[i]->Write(
"",TObject::kOverwrite);
588 N = m_2DHists.size();
589 for(
int i = 0; i < N; i++)
590 m_2DHists[i]->Write(
"",TObject::kOverwrite);
std::string GetName() const
Returns object name.
std::string GetTitle() const
Returns object title.
virtual void Clear()
Clears RFBase of all connections to other objects.
bool IsEmpty() const
Checks whether this is default (empty) instance of class.
virtual void Clear()
Clears RFBase of all connections to other objects.