ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
plot_opt.cpp
Go to the documentation of this file.
1 #include "core/plot_opt.hpp"
2 
3 #include <cmath>
4 
5 #include <algorithm>
6 #include <fstream>
7 
8 #include "core/utilities.hpp"
9 
10 using namespace std;
11 using namespace PlotOptTypes;
12 
14  bottom_type_(BottomType::off),
15  y_axis_type_(YAxisType::linear),
16  title_type_(TitleType::info),
17  stack_type_(StackType::signal_overlay),
18  overflow_type_(OverflowType::both),
19  file_extensions_({"pdf"}),
20  title_size_(0.045),
21  label_size_(0.04),
22  x_title_offset_(1.),
23  y_title_offset_(2.2),
24  z_title_offset_(1.),
25  auto_y_axis_(true),
26  canvas_width_(600),
27  canvas_height_(600),
28  left_margin_(0.19),
29  right_margin_(0.055),
30  bottom_margin_(0.12),
31  top_margin_(0.07),
32  bottom_height_(0.33),
33  legend_columns_(2),
34  legend_entry_height_(0.038),
35  legend_max_height_(0.28),
37  legend_pad_(0.028),
38  legend_density_(1.),
39  log_minimum_(0.),
40  ratio_minimum_(0.1),
41  ratio_maximum_(1.9),
42  n_divisions_(606),
44  font_(42),
46  use_cmyk_(true),
47  print_vals_(false){
48 }
49 
50 PlotOpt::PlotOpt(const string &file_name,
51  const string &config_name):
52  PlotOpt(){
53  LoadOptions(file_name, config_name);
54 }
55 
57  return *this;
58 }
59 
60 PlotOpt & PlotOpt::LoadOptions(const string &file_name,
61  const string &config_name){
62  ifstream file(file_name);
63  string line;
64  string current_config = "";
65  int line_num = 0;
66  while(getline(file, line)){
67  ++line_num;
68  ReplaceAll(line, " ", "");
69  ReplaceAll(line, "\t", "");
70  auto start = line.find('[');
71  auto end = line.find(']');
72  if(start==string::npos && end!=string::npos){
73  ERROR("Could not find opening brace in line "+to_string(line_num));
74  }
75  if(start!=string::npos && end==string::npos){
76  ERROR("Could not find closing brace in line "+to_string(line_num));
77  }
78  if(start<end && start != string::npos && end != string::npos){
79  current_config = line.substr(start+1, end-start-1);
80  }else if(current_config == config_name
81  && line.size()
82  && line.at(0)!='#'){
83  auto pos = line.find("=");
84  if(pos == string::npos) continue;
85  string prop_name = line.substr(0,pos);
86  string value = line.substr(pos+1);
87  SetProperty(prop_name, value);
88  }
89  }
90  return *this;
91 }
92 
94  bottom_type_ = bottom_type;
95  return *this;
96 }
97 
99  return bottom_type_;
100 }
101 
103  y_axis_type_ = y_axis_type;
104  return *this;
105 }
106 
108  return y_axis_type_;
109 }
110 
112  title_type_ = title_type;
113  return *this;
114 }
115 
117  return title_type_;
118 }
119 
121  stack_type_ = stack_type;
122  return *this;
123 }
124 
126  return stack_type_;
127 }
128 
130  overflow_type_ = overflow_type;
131  return *this;
132 }
133 
135  return overflow_type_;
136 }
137 
138 PlotOpt & PlotOpt::FileExtensions(const set<string> &file_extensions){
139  file_extensions_ = file_extensions;
140  return *this;
141 }
142 
143 const set<string> & PlotOpt::FileExtensions() const{
144  return file_extensions_;
145 }
146 
147 PlotOpt & PlotOpt::TitleSize(double title_size){
148  title_size_ = title_size;
149  return *this;
150 }
151 
152 double PlotOpt::TitleSize() const{
153  return title_size_;
154 }
155 
156 PlotOpt & PlotOpt::LabelSize(double label_size){
157  label_size_ = label_size;
158  return *this;
159 }
160 
161 double PlotOpt::LabelSize() const{
162  return label_size_;
163 }
164 
165 PlotOpt & PlotOpt::XTitleOffset(double x_title_offset){
166  x_title_offset_ = x_title_offset;
167  return *this;
168 }
169 
170 double PlotOpt::XTitleOffset() const{
171  return x_title_offset_;
172 }
173 
174 PlotOpt & PlotOpt::YTitleOffset(double y_title_offset){
175  y_title_offset_ = y_title_offset;
176  return *this;
177 }
178 
179 double PlotOpt::YTitleOffset() const{
180  return y_title_offset_;
181 }
182 
183 PlotOpt & PlotOpt::ZTitleOffset(double z_title_offset){
184  z_title_offset_ = z_title_offset;
185  return *this;
186 }
187 
188 double PlotOpt::ZTitleOffset() const{
189  return z_title_offset_;
190 }
191 
192 PlotOpt & PlotOpt::AutoYAxis(bool auto_y_axis){
193  auto_y_axis_ = auto_y_axis;
194  return *this;
195 }
196 
197 bool PlotOpt::AutoYAxis() const{
198  return auto_y_axis_;
199 }
200 
201 PlotOpt & PlotOpt::CanvasSize(int width, int height){
202  canvas_width_ = width;
203  canvas_height_ = height;
204  return *this;
205 }
206 
208  canvas_width_ = width;
209  return *this;
210 }
211 
213  return canvas_width_;
214 }
215 
217  canvas_height_ = height;
218  return *this;
219 }
220 
222  return canvas_height_;
223 }
224 
225 PlotOpt & PlotOpt::Margin(double left, double right, double bottom, double top){
226  left_margin_ = left;
227  right_margin_ = right;
228  bottom_margin_ = bottom;
229  top_margin_ = top;
230  return *this;
231 }
232 
234  left_margin_ = left;
235  return *this;
236 }
237 
238 double PlotOpt::LeftMargin() const{
239  return left_margin_;
240 }
241 
243  right_margin_ = right;
244  return *this;
245 }
246 
247 double PlotOpt::RightMargin() const{
248  return right_margin_;
249 }
250 
252  bottom_margin_ = bottom;
253  return *this;
254 }
255 
256 double PlotOpt::BottomMargin() const{
257  return bottom_margin_;
258 }
259 
261  top_margin_ = top;
262  return *this;
263 }
264 
265 double PlotOpt::TopMargin() const{
266  return top_margin_;
267 }
268 
269 PlotOpt & PlotOpt::BottomHeight(double bottom_height){
270  bottom_height_ = bottom_height;
271  return *this;
272 }
273 
274 double PlotOpt::BottomHeight() const{
275  return bottom_height_;
276 }
277 
279  legend_columns_ = columns;
280  return *this;
281 }
282 
284  return legend_columns_;
285 }
286 
288  legend_entry_height_ = height;
289  return *this;
290 }
291 
293  return legend_entry_height_;
294 }
295 
297  legend_max_height_ = height;
298  return *this;
299 }
300 
302  return legend_max_height_;
303 }
304 
306  legend_marker_width_ = width;
307  return *this;
308 }
309 
311  return legend_marker_width_;
312 }
313 
315  legend_pad_ = pad;
316  return *this;
317 }
318 
319 double PlotOpt::LegendPad() const{
320  return legend_pad_;
321 }
322 
323 PlotOpt & PlotOpt::LegendDensity(double density){
324  legend_density_ = density;
325  return *this;
326 }
327 
328 double PlotOpt::LegendDensity() const{
329  return legend_density_;
330 }
331 
332 PlotOpt & PlotOpt::NDivisions(int n_divisions){
333  n_divisions_ = n_divisions;
334  return *this;
335 }
336 
338  return n_divisions_;
339 }
340 
342  n_divisions_bottom_ = n_divisions;
343  return *this;
344 }
345 
347  return n_divisions_bottom_;
348 }
349 
350 PlotOpt & PlotOpt::LogMinimum(double log_minimum){
351  log_minimum_ = log_minimum;
352  return *this;
353 }
354 
355 double PlotOpt::LogMinimum() const{
356  return log_minimum_;
357 }
358 
359 PlotOpt & PlotOpt::RatioMinimum(double ratio_minimum){
360  ratio_minimum_ = ratio_minimum;
361  return *this;
362 }
363 
364 double PlotOpt::RatioMinimum() const{
365  return ratio_minimum_;
366 }
367 
368 PlotOpt & PlotOpt::RatioMaximum(double ratio_maximum){
369  ratio_maximum_ = ratio_maximum;
370  return *this;
371 }
372 
373 double PlotOpt::RatioMaximum() const{
374  return ratio_maximum_;
375 }
376 
377 PlotOpt & PlotOpt::Font(int font){
378  font_ = font;
379  return *this;
380 }
381 
382 int PlotOpt::Font() const{
383  return font_;
384 }
385 
386 PlotOpt & PlotOpt::ShowBackgroundError(bool show_background_error){
387  show_background_error_ = show_background_error;
388  return *this;
389 }
390 
392  return show_background_error_;
393 }
394 
395 PlotOpt & PlotOpt::UseCMYK(bool use_cmyk){
396  use_cmyk_ = use_cmyk;
397  return *this;
398 }
399 
400 bool PlotOpt::UseCMYK() const{
401  return use_cmyk_;
402 }
403 
404 PlotOpt & PlotOpt::PrintVals(bool print_vals){
405  print_vals_ = print_vals;
406  return *this;
407 }
408 
409 bool PlotOpt::PrintVals() const{
410  return print_vals_;
411 }
412 
413 double PlotOpt::TopToGlobalYNDC(double top_y) const{
414  if(bottom_type_ == BottomType::off) return top_y;
415  else return 1.-(1.-top_y)*(1. - bottom_margin_ - bottom_height_);
416 }
417 
418 double PlotOpt::GlobalToTopYNDC(double global_y) const{
419  if(bottom_type_ == BottomType::off) return global_y;
420  else return 1.-(1.-global_y)/(1. - bottom_margin_ - bottom_height_);
421 }
422 
423 double PlotOpt::BottomToGlobalYNDC(double bottom_y) const{
424  if(bottom_type_ == BottomType::off) return bottom_y;
425  else return bottom_y*(bottom_margin_+bottom_height_);
426 }
427 
428 double PlotOpt::GlobalToBottomYNDC(double global_y) const{
429  if(bottom_type_ == BottomType::off) return global_y;
430  else return global_y*(bottom_margin_+bottom_height_);
431 }
432 
433 double PlotOpt::TrueLegendHeight(size_t num_entries) const{
434  return min(legend_max_height_, legend_entry_height_*ceil(static_cast<double>(num_entries)/legend_columns_));
435 }
436 
437 double PlotOpt::TrueLegendEntryHeight(size_t num_entries) const{
438  return TrueLegendHeight(num_entries)/ceil(static_cast<double>(num_entries)/legend_columns_);
439 }
440 
441 double PlotOpt::TrueLegendWidth(size_t num_entries) const{
442  double left = left_margin_ + legend_pad_;
443  double right = 1. - right_margin_ - legend_pad_;
444  return (right-left)/min(num_entries, static_cast<size_t>(legend_columns_));
445 }
446 
448  switch(stack_type_){
449  default:
450  DBG("Invalid stack type " << static_cast<int>(stack_type_));
451  case StackType::signal_overlay:
452  case StackType::signal_on_top:
453  case StackType::data_norm:
454  return true;
455  case StackType::lumi_shapes:
456  case StackType::shapes:
457  return false;
458  }
459 }
460 
462  return title_type_ == TitleType::info
463  && BackgroundsStacked();
464 }
465 
466 string PlotOpt::TypeString() const{
467  string out = "";
468 
469  switch(stack_type_){
470  default: DBG("Bad stack type: " << static_cast<int>(stack_type_));
471  case StackType::signal_overlay: out += "lumi_nonorm"; break;
472  case StackType::signal_on_top: out += "sigontop"; break;
473  case StackType::data_norm: out += "lumi"; break;
474  case StackType::lumi_shapes: out += "lumi_shapes"; break;
475  case StackType::shapes: out += "shapes"; break;
476  }
477 
478  out += "_";
479 
480  switch(y_axis_type_){
481  default: DBG("Bad y-axis type: " << static_cast<int>(y_axis_type_));
482  case YAxisType::linear: out += "lin"; break;
483  case YAxisType::log: out += "log"; break;
484  }
485 
486  return out;
487 }
488 
490  if(!BackgroundsStacked()){
491  if(ShowBackgroundError()){
492  DBG("Don't know how to show total MC uncertainty with unstacked MC. Turning off MC uncertainty band.");
493  show_background_error_ = false;
494  }
495  }
496 }
497 
498 void PlotOpt::SetProperty(const string &property,
499  const string &value){
500  if(property == "BottomType"){
501  Bottom(static_cast<BottomType>(stoi(value)));
502  }else if(property == "YAxisType"){
503  YAxis(static_cast<YAxisType>(stoi(value)));
504  }else if(property == "TitleType"){
505  Title(static_cast<TitleType>(stoi(value)));
506  }else if(property == "StackType"){
507  Stack(static_cast<StackType>(stoi(value)));
508  }else if(property == "OverflowType"){
509  Overflow(static_cast<OverflowType>(stoi(value)));
510  }else if(property == "FileExtensions"){
511  auto exts = FileExtensions();
512  exts.insert(value);
513  FileExtensions(exts);
514  }else if(property == "TitleSize"){
515  TitleSize(stod(value));
516  }else if(property == "LabelSize"){
517  LabelSize(stod(value));
518  }else if(property == "xTitleOffset" || property == "XTitleOffset"){
519  XTitleOffset(stod(value));
520  }else if(property == "yTitleOffset" || property == "YTitleOffset"){
521  YTitleOffset(stod(value));
522  }else if(property == "zTitleOffset" || property == "ZTitleOffset"){
523  ZTitleOffset(stod(value));
524  }else if(property == "AutoYAxis"){
525  AutoYAxis(static_cast<bool>(stoi(value)));
526  }else if(property == "CanvasWidth" || property == "CanvasW"){
527  CanvasWidth(stoi(value));
528  }else if(property == "CanvasHeight" || property == "CanvasH"){
529  CanvasHeight(stoi(value));
530  }else if(property == "PadLeftMargin"){
531  LeftMargin(stod(value));
532  }else if(property == "PadRightMargin"){
533  RightMargin(stod(value));
534  }else if(property == "PadBottomMargin"){
535  BottomMargin(stod(value));
536  }else if(property == "PadTopMargin"){
537  TopMargin(stod(value));
538  }else if(property == "LegendColumns"){
539  LegendColumns(stoi(value));
540  }else if(property == "LegendEntrySize" || property == "LegendSize"){
541  LegendEntryHeight(stod(value));
542  }else if(property == "LegendMaxSize"){
543  LegendMaxHeight(stod(value));
544  }else if(property == "LegendMarkerWidth"){
545  LegendMarkerWidth(stod(value));
546  }else if(property == "LegendPad"){
547  LegendPad(stod(value));
548  }else if(property == "LegendDensity"){
549  LegendDensity(stod(value));
550  }else if(property == "BottomPlotHeight"){
551  BottomHeight(stod(value));
552  }else if(property == "LogMinimum"){
553  LogMinimum(stod(value));
554  }else if(property == "RatioMinimum"){
555  RatioMinimum(stod(value));
556  }else if(property == "RatioMaximum"){
557  RatioMaximum(stod(value));
558  }else if(property == "nDivisions" || property == "NDivisions"){
559  NDivisions(stoi(value));
560  }else if(property == "nDivisionsBottom" || property == "NDivisionsBottom"){
561  NDivisionsBottom(stoi(value));
562  }else if(property == "Font"){
563  Font(stoi(value));
564  }else if(property == "ShowBackgroundError"){
565  ShowBackgroundError(stoi(value));
566  }else if(property == "UseCMYK"){
567  UseCMYK(stoi(value));
568  }else if(property == "PrintVals"){
569  PrintVals(stoi(value));
570  }else{
571  DBG("Did not understand property name "<<property);
572  }
573 }
PlotOptTypes::OverflowType Overflow() const
Definition: plot_opt.cpp:134
double legend_max_height_
Definition: plot_opt.hpp:156
#define DBG(x)
Definition: utilities.hpp:18
int legend_columns_
Definition: plot_opt.hpp:155
double RightMargin() const
Definition: plot_opt.cpp:247
double LogMinimum() const
Definition: plot_opt.cpp:355
int CanvasHeight() const
Definition: plot_opt.cpp:221
PlotOpt & LoadOptions(const std::string &file_name, const std::string &config_name)
Definition: plot_opt.cpp:60
PlotOptTypes::BottomType bottom_type_
Definition: plot_opt.hpp:143
PlotOptTypes::BottomType Bottom() const
Definition: plot_opt.cpp:98
bool DisplayLumiEntry() const
Definition: plot_opt.cpp:461
int NDivisionsBottom() const
Definition: plot_opt.cpp:346
double LeftMargin() const
Definition: plot_opt.cpp:238
double title_size_
Definition: plot_opt.hpp:149
double BottomToGlobalYNDC(double bottom_y) const
Definition: plot_opt.cpp:423
const std::set< std::string > & FileExtensions() const
Definition: plot_opt.cpp:143
double legend_entry_height_
Definition: plot_opt.hpp:156
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:52
STL namespace.
double TopMargin() const
Definition: plot_opt.cpp:265
PlotOptTypes::TitleType Title() const
Definition: plot_opt.cpp:116
double LegendMaxHeight() const
Definition: plot_opt.cpp:301
bool auto_y_axis_
Definition: plot_opt.hpp:151
double RatioMinimum() const
Definition: plot_opt.cpp:364
PlotOptTypes::StackType stack_type_
Definition: plot_opt.hpp:146
PlotOpt()
Definition: plot_opt.cpp:13
double ratio_maximum_
Definition: plot_opt.hpp:159
double legend_marker_width_
Definition: plot_opt.hpp:157
int canvas_width_
Definition: plot_opt.hpp:152
bool UseCMYK() const
Definition: plot_opt.cpp:400
PlotOptTypes::StackType Stack() const
Definition: plot_opt.cpp:125
double TopToGlobalYNDC(double top_y) const
Definition: plot_opt.cpp:413
bool PrintVals() const
Definition: plot_opt.cpp:409
bool print_vals_
Definition: plot_opt.hpp:164
double top_margin_
Definition: plot_opt.hpp:153
int n_divisions_
Definition: plot_opt.hpp:160
double TrueLegendWidth(std::size_t num_entries) const
Definition: plot_opt.cpp:441
double BottomMargin() const
Definition: plot_opt.cpp:256
PlotOpt & CanvasSize(int width, int height)
Definition: plot_opt.cpp:201
double label_size_
Definition: plot_opt.hpp:149
bool use_cmyk_
Definition: plot_opt.hpp:163
#define ERROR(x)
Definition: utilities.hpp:17
bool BackgroundsStacked() const
Definition: plot_opt.cpp:447
double z_title_offset_
Definition: plot_opt.hpp:150
std::string TypeString() const
Definition: plot_opt.cpp:466
PlotOpt operator()() const
Definition: plot_opt.cpp:56
double left_margin_
Definition: plot_opt.hpp:153
std::set< std::string > file_extensions_
Definition: plot_opt.hpp:148
double y_title_offset_
Definition: plot_opt.hpp:150
int NDivisions() const
Definition: plot_opt.cpp:337
double legend_pad_
Definition: plot_opt.hpp:157
bool AutoYAxis() const
Definition: plot_opt.cpp:197
int font_
Definition: plot_opt.hpp:161
double legend_density_
Definition: plot_opt.hpp:157
double TrueLegendEntryHeight(std::size_t num_entries) const
Definition: plot_opt.cpp:437
double TitleSize() const
Definition: plot_opt.cpp:152
double LegendPad() const
Definition: plot_opt.cpp:319
double ratio_minimum_
Definition: plot_opt.hpp:159
int CanvasWidth() const
Definition: plot_opt.cpp:212
int Font() const
Definition: plot_opt.cpp:382
double XTitleOffset() const
Definition: plot_opt.cpp:170
double GlobalToBottomYNDC(double global_y) const
Definition: plot_opt.cpp:428
double bottom_margin_
Definition: plot_opt.hpp:153
double YTitleOffset() const
Definition: plot_opt.cpp:179
double bottom_height_
Definition: plot_opt.hpp:154
double right_margin_
Definition: plot_opt.hpp:153
double RatioMaximum() const
Definition: plot_opt.cpp:373
double LabelSize() const
Definition: plot_opt.cpp:161
void MakeSane()
Definition: plot_opt.cpp:489
PlotOptTypes::YAxisType y_axis_type_
Definition: plot_opt.hpp:144
double x_title_offset_
Definition: plot_opt.hpp:150
double LegendMarkerWidth() const
Definition: plot_opt.cpp:310
double LegendDensity() const
Definition: plot_opt.cpp:328
PlotOpt & Margin(double left, double right, double bottom, double top)
Definition: plot_opt.cpp:225
double TrueLegendHeight(std::size_t num_entries) const
Definition: plot_opt.cpp:433
PlotOptTypes::YAxisType YAxis() const
Definition: plot_opt.cpp:107
double log_minimum_
Definition: plot_opt.hpp:158
int canvas_height_
Definition: plot_opt.hpp:152
double LegendEntryHeight() const
Definition: plot_opt.cpp:292
PlotOptTypes::OverflowType overflow_type_
Definition: plot_opt.hpp:147
void SetProperty(const std::string &property_name, const std::string &value_string)
Definition: plot_opt.cpp:498
int n_divisions_bottom_
Definition: plot_opt.hpp:160
int LegendColumns() const
Definition: plot_opt.cpp:283
double GlobalToTopYNDC(double global_y) const
Definition: plot_opt.cpp:418
bool show_background_error_
Definition: plot_opt.hpp:162
bool ShowBackgroundError() const
Definition: plot_opt.cpp:391
PlotOptTypes::TitleType title_type_
Definition: plot_opt.hpp:145
double ZTitleOffset() const
Definition: plot_opt.cpp:188
double BottomHeight() const
Definition: plot_opt.cpp:274