travex
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProgramOptions.cxx
Go to the documentation of this file.
1 #include <exception>
2 #include <iostream>
3 #include <fstream>
4 #include <boost/regex.hpp>
5 
6 #include "travex/utils.h"
8 
9 
10 using namespace tvx;
11 
12 
13 
14 ProgramOptions::ProgramOptions(int argc, char **argv) :
15  fArgc(argc), fArgv(argv),
16  fOptions("Available program options", 120),
17  fOptionsValues(),
18  fInFilePath(),
19  fOutPrefix("./"),
20  fMaxEventsUser(0),
21  fSparsity(1),
22  fSaveGraphics(false)
23 {
24  fOptions.add_options()
25  ("help,h", "Print this help message")
26  ("input-file,f", po::value<std::string>(&fInFilePath), "Full path to a ROOT file containing a TTree " \
27  "OR a text file with a list of such ROOT files")
28  ("prefix,o", po::value<std::string>(&fOutPrefix)->default_value("./"), "Absolute or relative path to prefix output files")
29  ("max-events,n", po::value<unsigned int>(&fMaxEventsUser)->default_value(0), "Maximum number of events to process")
30  ("sparsity,s", po::value<float>(&fSparsity)->default_value(1), "Approximate fraction of events to read and process")
31  ("save-graph,g", po::value<bool>(&fSaveGraphics)->default_value(false)->implicit_value(true), "Use this option to save histograms and such as images")
32  ;
33 }
34 
35 
42 {
43  try {
44  po::store(po::parse_command_line(fArgc, fArgv, fOptions), fOptionsValues);
45  po::notify(fOptionsValues);
46  } catch(const std::exception& ex) {
47  TVX_ERROR(ex.what());
48  std::cout << fOptions << std::endl;
49  exit(EXIT_FAILURE);
50  }
51 
52  VerifyOptions();
53 }
54 
55 
58 {
59  std::cout << "Program options set to following values:\n";
60 
61  for (const std::pair< std::string, po::variable_value >& option : fOptionsValues)
62  {
63  std::cout << "\t" << option.first << ":\t" << option.second.value() << "\n";
64  }
65 }
66 
67 
73 {
74  if (fOptionsValues.count("help"))
75  {
76  std::cout << fOptions << std::endl;
77  exit(EXIT_SUCCESS);
78  }
79 
80 
81  if (fOptionsValues.count("input-file"))
82  {
83  std::string inputFileName = boost::any_cast<std::string>(fOptionsValues["input-file"].value());
84 
85  std::ifstream tmpFileCheck(inputFileName.c_str());
86  if (!tmpFileCheck.good()) {
87  TVX_FATAL("File \"%s\" does not exist", inputFileName.c_str());
88  }
89  } else {
90  TVX_ERROR("Input file not set");
91  std::cout << fOptions << std::endl;
92  exit(EXIT_FAILURE);
93  }
94 
95  if (fOptionsValues.count("sparsity"))
96  {
97  if (fSparsity > 1 || fSparsity <= 0) {
98  TVX_WARNING("VerifyOptions", "Sparsity specified value outside allowed limits. Set to 1");
99  fSparsity = 1;
100  }
101  }
102 }
103 
104 
114 std::string ProgramOptions::GetOutFileName(std::string suffix) const
115 {
116  boost::regex extension_regex("^(.*)\\.root$");
117 
118  if ( boost::regex_match(fInFilePath, extension_regex) ) {
119  return boost::regex_replace(fInFilePath, extension_regex, "\\1." + suffix + ".root");
120  } else {
121  return fInFilePath + "." + suffix + ".root";
122  }
123 }
124 
125 
126 std::ostream& tvx::operator<<(std::ostream& os, const boost::any& any_value)
127 {
128  // List all types you want to try
129  if(!out_to_stream<int>(os, any_value))
130  if(!out_to_stream<unsigned int>(os, any_value))
131  if(!out_to_stream<float>(os, any_value))
132  if(!out_to_stream<bool>(os, any_value))
133  if(!out_to_stream<std::string>(os, any_value))
134  os<<"{unknown}"; // all cast are failed, an unknown type of any
135 
136  return os;
137 }
void Print() const
Prints the options and their values.
float fSparsity
An approximate fraction of events to read and process.
std::string fInFilePath
Full path to either a root file with event tree or a text file with a list of such root files...
unsigned int fMaxEventsUser
The maximum number of input events to process.
#define TVX_ERROR(...)
Definition: utils.h:9
po::variables_map fOptionsValues
Definition: Event.h:12
void ProcessOptions()
Takes the standard command line arguments and parses them with the boost program_options utility...
bool fSaveGraphics
This flag controls whether to produce images for created histograms.
ProgramOptions(int argc, char **argv)
po::options_description fOptions
std::string fOutPrefix
A prefix to specify the location of the output files.
std::string GetOutFileName(std::string suffix="hist") const
Form the name of the output file from the input file name by appending a suffix to it...
#define TVX_WARNING(...)
Definition: utils.h:8
std::ostream & operator<<(std::ostream &os, const boost::any &any_value)
void VerifyOptions()
Verifies user submitted values.
#define TVX_FATAL(...)
Definition: utils.h:10