MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Private Attributes | List of all members
MaCh3Plotting::PlottingManager Class Reference

The main class to be used in plotting scripts. More...

#include <Plotting/plottingUtils/plottingManager.h>

Collaboration diagram for MaCh3Plotting::PlottingManager:
[legend]

Public Member Functions

 PlottingManager ()
 Construct a new PlottingManager using default plottingConfig config.
 
 PlottingManager (const std::string &PlottingConfigName)
 Construct a new PlottingManager using specified plottingConfig config.
 
void initialise ()
 initalise this PlottingManager.
 
 ~PlottingManager ()
 
void parseInputs (int argc, char *const *argv)
 Parse command line arguments.
 
void parseInputsVec (std::vector< std::string > argv)
 Parse vector of command line arguments.
 
void addUserOption ()
 Describe an option you want to add to the PlottingManager which can be read in from the command line and retrieved later with getOption(). This should be done before calling parseInputs().
 
std::string getUserOption (std::string option)
 Retrieve a command line option you specified using addOption.
 
void usage ()
 Print a usage message for the current executable.
 
void parseFileLabels (std::string labelString, std::vector< std::string > &labelVec)
 Parse string of labels into a vector of strings.
 
void setOutFileName (std::string fileName)
 Parse and set the output file name, if extension specified, check its one root supports, if not, default to pdf.
 
void setExec (std::string execName)
 Internally set the name of the executable that manager is being used in.
 
template<typename T >
getOption (std::string option)
 Get a specific option from the config for this executable.
 
YAML::Node getOption (std::string option)
 
const StyleManagerstyle ()
 Get the StyleManager contained within this PlottingManager, for doing style related things.
 
const InputManagerinput ()
 Get the InputManager contained within this PlottingManager, for doing input related things.
 
General getters
const std::string getFileName (int i)
 
const std::string getFileLabel (int i)
 
const std::string getDrawOptions ()
 
const std::string getOutputName ()
 Get the straight up output file name with no bells or whistles, just the file extension.
 
const std::string getOutputName (const std::string &suffix)
 Get the output name but can specify a siffix to add to the name, before the file extension.
 
const std::vector< std::string > getFileNames ()
 
const std::vector< std::string > getFileLabels ()
 
size_t getNFiles ()
 
bool getSplitBySample ()
 
bool getPlotRatios ()
 
bool getDrawGrid ()
 

Public Attributes

const std::string DEFAULT_TRANSLATION_CONFIG
 
const std::string DEFAULT_STYLE_CONFIG
 
const std::string DEFAULT_PLOTTING_CONFIG
 

Private Attributes

std::string _configFileName = std::string(std::getenv("MACH3")) + "/plotting/PlottingConfig.yaml"
 
YAML::Node _plottingConfig
 
YAML::Node _execOptions
 
std::vector< std::string > _fileNames
 
std::vector< std::string > _fileLabels
 
std::vector< std::string > _defaultFileLabels
 
std::string _outputName = "Plot.pdf"
 
std::string _extraDrawOptions = ""
 
bool _splitBySample = false
 
bool _plotRatios = false
 
bool _drawGrid = false
 
std::unique_ptr< StyleManager_styleMan
 
std::unique_ptr< InputManager_inputMan
 

Detailed Description

The main class to be used in plotting scripts.

When it comes to plotting, this guys in charge, the main man, the head honcho, the big cheese. If it's a plot you need, this is the guy you call. You just call him in your scripts and let him and his goons StyleManager, and InputManager worry about the details, no questions asked. This class handles all the command line inputs and manages the other managers. Intention with this class is that you should be able to create one from your custom plotting script, then from that, be able to get all manner of inputs from the InputManager contained within, and set any style options you like from the StyleManager. Also a hopefully not too distant dream is to wrap this up in python so it is usable in .py scripts to take advantage of nice existing plotting libraries for e.g. MCMC plotting.

Author
Ewan Miller

Definition at line 33 of file plottingManager.h.

Constructor & Destructor Documentation

◆ PlottingManager() [1/2]

MaCh3Plotting::PlottingManager::PlottingManager ( )

Construct a new PlottingManager using default plottingConfig config.

Returns
Constructed PlottingManager instance.

Definition at line 5 of file plottingManager.cpp.

5 {
6 // set config file name
8}
const std::string DEFAULT_PLOTTING_CONFIG

◆ PlottingManager() [2/2]

MaCh3Plotting::PlottingManager::PlottingManager ( const std::string &  PlottingConfigName)

Construct a new PlottingManager using specified plottingConfig config.

Parameters
PlottingConfigNameThe config file file defining executables specific options, and other minor manager related options.
Returns
Constructed PlottingManager instance.

Definition at line 11 of file plottingManager.cpp.

11 {
12 // parse the config file
13 _configFileName = PlottingConfigName;
14}

◆ ~PlottingManager()

MaCh3Plotting::PlottingManager::~PlottingManager ( )
inline

Definition at line 64 of file plottingManager.h.

64 {
65 MACH3LOG_DEBUG("##### Deleting PlottingManager Instance #####");
66 }
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:22

Member Function Documentation

◆ addUserOption()

void MaCh3Plotting::PlottingManager::addUserOption ( )

Describe an option you want to add to the PlottingManager which can be read in from the command line and retrieved later with getOption(). This should be done before calling parseInputs().

Todo:

Would be good to add functionality to this to allow user to add their own options.

The way I can think to do this would be have fn addUserOption() to set the options, defining the cmd line option (e.g. -x), the name of the option, and maybe some description of the option to be used in the help message

can then store these options in some map or something to be retrieved later by getUserOption()

Todo:
Implement this.

Definition at line 176 of file plottingManager.cpp.

176 {
178}

◆ getDrawGrid()

bool MaCh3Plotting::PlottingManager::getDrawGrid ( )
inline

Definition at line 151 of file plottingManager.h.

◆ getDrawOptions()

const std::string MaCh3Plotting::PlottingManager::getDrawOptions ( )
inline

Definition at line 128 of file plottingManager.h.

128{ return _extraDrawOptions; }

◆ getFileLabel()

const std::string MaCh3Plotting::PlottingManager::getFileLabel ( int  i)
inline

Definition at line 126 of file plottingManager.h.

126{ return _fileLabels[i]; }
std::vector< std::string > _fileLabels

◆ getFileLabels()

const std::vector< std::string > MaCh3Plotting::PlottingManager::getFileLabels ( )
inline

Definition at line 143 of file plottingManager.h.

143{ return _fileLabels; }

◆ getFileName()

const std::string MaCh3Plotting::PlottingManager::getFileName ( int  i)
inline

Definition at line 124 of file plottingManager.h.

124{ return _fileNames[i]; }
std::vector< std::string > _fileNames

◆ getFileNames()

const std::vector< std::string > MaCh3Plotting::PlottingManager::getFileNames ( )
inline

Definition at line 141 of file plottingManager.h.

141{ return _fileNames; }

◆ getNFiles()

size_t MaCh3Plotting::PlottingManager::getNFiles ( )
inline

Definition at line 145 of file plottingManager.h.

145{ return _fileNames.size(); }

◆ getOption() [1/2]

template<typename T >
T MaCh3Plotting::PlottingManager::getOption ( std::string  option)
inline

Get a specific option from the config for this executable.

Template Parameters
Tthe type of parameter expected for this option, e.g. std::string.
Parameters
optionThe option that you want from the config.
Returns
The specified value for the option.

Definition at line 118 of file plottingManager.h.

118{ return _execOptions[option].as<T>(); }

◆ getOption() [2/2]

YAML::Node MaCh3Plotting::PlottingManager::getOption ( std::string  option)
inline

Definition at line 119 of file plottingManager.h.

119{ return _execOptions[option]; }

◆ getOutputName() [1/2]

const std::string MaCh3Plotting::PlottingManager::getOutputName ( )
inline

Get the straight up output file name with no bells or whistles, just the file extension.

Returns
The straight up output file name.

Definition at line 133 of file plottingManager.h.

133{ return _outputName; }

◆ getOutputName() [2/2]

const std::string MaCh3Plotting::PlottingManager::getOutputName ( const std::string &  suffix)

Get the output name but can specify a siffix to add to the name, before the file extension.

Parameters
suffixThe suffix to add to the file name.
Returns
Output file name with suffix added before the extension.

Output file name, including the file extension will be returned, but with specified suffix after the name but before the extension. This is useful for e.g. saving multiple LLH scan types to separate files: can specify suffix "_PriotLLH" will return OutputName_PriorLLH.ext

Todo:
Make this support .root files too

Definition at line 223 of file plottingManager.cpp.

223 {
224 std::string ext = _outputName;
225 std::string name = _outputName;
226
227 size_t dotPos = 0;
228 while (ext.find(".") != std::string::npos)
229 {
230 dotPos += ext.find(".");
231 ext.erase(0, ext.find(".") + 1);
232 }
233
234 name.erase(dotPos, ext.size() + 1);
235 return name + suffix + "." + ext;
236}

◆ getPlotRatios()

bool MaCh3Plotting::PlottingManager::getPlotRatios ( )
inline

Definition at line 149 of file plottingManager.h.

◆ getSplitBySample()

bool MaCh3Plotting::PlottingManager::getSplitBySample ( )
inline

Definition at line 147 of file plottingManager.h.

◆ getUserOption()

std::string MaCh3Plotting::PlottingManager::getUserOption ( std::string  option)

Retrieve a command line option you specified using addOption.

Todo:
Implement this.

Definition at line 180 of file plottingManager.cpp.

180 {
182 (void) option;
183 return "";
184}

◆ initialise()

void MaCh3Plotting::PlottingManager::initialise ( )

initalise this PlottingManager.

  • Read the plotting config file
  • Instantiate a StyleManager using the style config file specified in the plotting config file, or if none was provided, using DEFAULT_STYLE_CONFIG.
  • Instantiate an InputManager using the translation config file specified in the plotting config file, or if none was provided, using DEFAULT_TRANSLATION_CONFIG.
  • Add all files specified in this PlottingManagers _fileNames vector to the new InputManager that was just created
    Warning
    This should always be called After parseInputs() unless you are manually specifying all input file names and config file names in your drawing application.
Todo:
should add some kind of validataConfigs() method to got throught all of the specified config files and make sure that all provided options are valid and all necessary options are provided as it can be pretty annoying and difficult to identify what's going wrong when yaml just fails to find an option at runtime

Definition at line 25 of file plottingManager.cpp.

25 {
31
32 MACH3LOG_DEBUG("Initialising PlottingManager with plotting congif {}", _configFileName);
33 // read options from the config
34 YAML::Node managerOptions = _plottingConfig["ManagerOptions"];
35
36 std::string translationConfig = managerOptions["translationConfig"].as<std::string>();
37 if (translationConfig == "")
38 {
39 translationConfig = DEFAULT_TRANSLATION_CONFIG;
40 MACH3LOG_DEBUG("PlottingManager: Using default translation config file name");
41 }
42
43 MACH3LOG_DEBUG("PlottingManager: Using translation config file: {}", translationConfig);
44
45 std::string styleConfig = managerOptions["styleConfig"].as<std::string>();
46 if (styleConfig == "")
47 {
48 styleConfig = DEFAULT_STYLE_CONFIG;
49 MACH3LOG_DEBUG("PlottingManager: Using default style config file name");
50 }
51
52 MACH3LOG_DEBUG("PlottingManager: Using style config file: {}", styleConfig);
53
54 // create the StyleManager
55 _styleMan = std::make_unique<StyleManager>(styleConfig);
56
57 // create the InputManager and add all the files to it
58 _inputMan = std::make_unique<InputManager>(translationConfig);
59 for (std::string fileName : _fileNames)
60 {
61 _inputMan->addFile(fileName);
62 }
63}
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:560
const std::string DEFAULT_STYLE_CONFIG
const std::string DEFAULT_TRANSLATION_CONFIG
std::unique_ptr< InputManager > _inputMan
std::unique_ptr< StyleManager > _styleMan

◆ input()

const InputManager & MaCh3Plotting::PlottingManager::input ( )
inline

Get the InputManager contained within this PlottingManager, for doing input related things.

Definition at line 162 of file plottingManager.h.

162{ return *_inputMan; }

◆ parseFileLabels()

void MaCh3Plotting::PlottingManager::parseFileLabels ( std::string  labelString,
std::vector< std::string > &  labelVec 
)

Parse string of labels into a vector of strings.

Parameters
labelStringstring of labels of the form "label1;label2;...".
labelVecvector that the individual label strings will be placed into.

Definition at line 247 of file plottingManager.cpp.

247 {
248 // take in a string defining labels of the form "label1;label2;...;labelN" and parse it into a
249 // vector containing the labels
250
251 size_t end = labelString.find(";");
252 while (end != std::string::npos)
253 { // Loop until no delimiter is left in the string.
254 labelVec.push_back(labelString.substr(0, end));
255 labelString.erase(labelString.begin(), labelString.begin() + end + 1);
256 end = labelString.find(";");
257 }
258 labelVec.push_back(labelString.substr(0, end));
259}

◆ parseInputs()

void MaCh3Plotting::PlottingManager::parseInputs ( int  argc,
char *const *  argv 
)

Parse command line arguments.

Parameters
argcThe number of command line arguments.
argvThe arguments themselves.

Takes in c style command line arguments and parses particular general ones that are useful for a wide variety of plotting scripts.

Please also keep these options in mind and use them in your own plotting scripts.

General cmd line interface for a plotting script will look like:

./ScriptName [options] [FileName1 FileName2 FileName3 ...]
[options]
-o <_outputName> name of the file to output plots to
-l <FileLabelStr> string of labels for each file in the format Label1;Label2;... to be parsed
by parseFileLabels(), number of labels should match exactly the number of files -c
<PlottingConfig> specify a plotting config file to be used in place of the default one
when initialising this manager -d <DrawOptions> extra draw options to be passed when making
plots, see https://root.cern/doc/master/classTHistPainter.html#HP01a for examples
-s where possible, will try to split plots made by sample
-r where possible, make ratio plots comparing the different input files
-g where possible, draw a grid on the produced plots for improved readability
-h print the help message for the current executable and exit
unnamed options are treated as files to be processed
std::string config
Definition: ProcessMCMC.cpp:30
void parseFileLabels(std::string labelString, std::vector< std::string > &labelVec)
Parse string of labels into a vector of strings.
const InputManager & input()
Get the InputManager contained within this PlottingManager, for doing input related things.
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
Todo:
make this able to return any un-parsed arguments so that user can specify their own arguments for use in their plotting scripts

Definition at line 92 of file plottingManager.cpp.

92 {
93 // parse the inputs
94 int c;
95 while ((c = getopt(argc, argv, "o:l:d:c:srgh")) != -1)
96 {
97 if (c < 0)
98 break;
99 switch (c)
100 {
101 case 'o': {
102 setOutFileName(optarg);
103 break;
104 }
105 case 's': {
106 _splitBySample = true;
107 break;
108 }
109 case 'r': {
110 _plotRatios = true;
111 break;
112 }
113 case 'g': {
114 _drawGrid = true;
115 break;
116 }
117 case 'h': {
118 usage();
119 throw MaCh3Exception(__FILE__ , __LINE__ );
120 }
121 case 'l': {
123 MACH3LOG_INFO("Specified file labels: ");
124 for (std::string label : _fileLabels)
125 MACH3LOG_INFO(" {}", label);
126 break;
127 }
128 case 'c': {
129 _configFileName = optarg;
130 break;
131 }
132 case 'd': {
133 _extraDrawOptions = optarg;
134 break;
135 }
136 }
137 }
138
139 MACH3LOG_INFO("\nInput files provided");
140 // optind is for the extra arguments that are not parsed by the program
141 for (; optind < argc; optind++)
142 {
143 _fileNames.push_back(argv[optind]);
144 MACH3LOG_INFO(argv[optind]);
145 _defaultFileLabels.push_back(argv[optind]);
146 }
147
148 if (_splitBySample)
149 MACH3LOG_INFO("Splitting by sample");
150
151 if (_plotRatios && _fileNames.size() == 0)
152 {
153 MACH3LOG_ERROR("you specified -r <_plotRatios> = true but didnt specify any files to compare against, was this a mistake?");
154 }
155
156 if (_fileLabels.size() == 0)
157 {
158 MACH3LOG_INFO("No file labels specified, will just use the file names");
160 }
161
162 if ((_fileLabels.size() != 0) && (_fileLabels.size() != _fileNames.size()))
163 {
164 MACH3LOG_ERROR("hmmm, you gave me {} labels but {} files", _fileLabels.size(), _fileNames.size());
165 }
166
167 initialise();
168}
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:25
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:23
Custom exception class for MaCh3 errors.
void initialise()
initalise this PlottingManager.
std::vector< std::string > _defaultFileLabels
void usage()
Print a usage message for the current executable.
void setOutFileName(std::string fileName)
Parse and set the output file name, if extension specified, check its one root supports,...

◆ parseInputsVec()

void MaCh3Plotting::PlottingManager::parseInputsVec ( std::vector< std::string >  argv)
inline

Parse vector of command line arguments.

This mainly just exists for the sake of the python binding.

Parameters
argvThe arguments to parse.

Definition at line 77 of file plottingManager.h.

77 {
78 std::vector<char *> charVec;
79 MACH3LOG_DEBUG("Parsing Inputs :: was given vector:");
80 for( const std::string &arg : argv )
81 {
82 charVec.push_back( const_cast<char *>(arg.c_str()) );
83 MACH3LOG_DEBUG(" - {}", arg );
84 }
85 parseInputs(int(argv.size()), charVec.data());
86 }
void parseInputs(int argc, char *const *argv)
Parse command line arguments.

◆ setExec()

void MaCh3Plotting::PlottingManager::setExec ( std::string  execName)

Internally set the name of the executable that manager is being used in.

Parameters
execNameName of the current executable, will also need to be defined in the plotting config file.

This is used by e.g. getOption() so the PlottingManager knows where to look for the option in the plotting config file.

Definition at line 240 of file plottingManager.cpp.

240 {
241 MACH3LOG_DEBUG("Setting internal exec name to {}", execName);
242 _execOptions = _plottingConfig[execName];
243 if (_outputName == "Plot.pdf")
244 setOutFileName(getOption<std::string>("defaultOutputName"));
245}

◆ setOutFileName()

void MaCh3Plotting::PlottingManager::setOutFileName ( std::string  saveName)

Parse and set the output file name, if extension specified, check its one root supports, if not, default to pdf.

Parameters
fileNamethe name of the output file.

Will check the provided saveName for file extensions, if it is one of .pdf or .eps, then just use the provided string as the full output name. If no file extension is specified, append .pdf so plots will be saved as pdf. if some other file extension is specified, replace with .pdf as only .pdf and .eps support printing multiple plots to one file in root.

Definition at line 197 of file plottingManager.cpp.

197 {
198 if (saveName.find(".") == std::string::npos)
199 {
200 _outputName = saveName + ".pdf";
201 return;
202 }
203
204 std::string ext = saveName;
205 // if there are .'s then remove everything before them until we only have the file extension left
206 while (ext.find(".") != std::string::npos)
207 ext.erase(0, ext.find(".") + 1);
208 if (ext == "pdf" || ext == "ps" || ext == "eps")
209 {
210 _outputName = saveName;
211 return;
212 }
213
214 MACH3LOG_WARN("file extension '{}' that you provided doesnt support multiple plots in one file", ext);
215 MACH3LOG_WARN("should be one of .pdf, .eps .ps, will use pdf");
216 _outputName = saveName + ".pdf";
217}
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:24

◆ style()

const StyleManager & MaCh3Plotting::PlottingManager::style ( )
inline

Get the StyleManager contained within this PlottingManager, for doing style related things.

Definition at line 158 of file plottingManager.h.

158{ return *_styleMan; }

◆ usage()

void MaCh3Plotting::PlottingManager::usage ( )

Print a usage message for the current executable.

Todo:
Implement this.
Todo:
Todo:
could add some function to allow user to specify the help message for their particular script, then auto generate what the cmd line syntax looks like based on user specified options?

Definition at line 186 of file plottingManager.cpp.

186 {
191}

Member Data Documentation

◆ _configFileName

std::string MaCh3Plotting::PlottingManager::_configFileName = std::string(std::getenv("MACH3")) + "/plotting/PlottingConfig.yaml"
private

Definition at line 166 of file plottingManager.h.

◆ _defaultFileLabels

std::vector<std::string> MaCh3Plotting::PlottingManager::_defaultFileLabels
private

Definition at line 175 of file plottingManager.h.

◆ _drawGrid

bool MaCh3Plotting::PlottingManager::_drawGrid = false
private

Definition at line 184 of file plottingManager.h.

◆ _execOptions

YAML::Node MaCh3Plotting::PlottingManager::_execOptions
private

Definition at line 170 of file plottingManager.h.

◆ _extraDrawOptions

std::string MaCh3Plotting::PlottingManager::_extraDrawOptions = ""
private

Definition at line 179 of file plottingManager.h.

◆ _fileLabels

std::vector<std::string> MaCh3Plotting::PlottingManager::_fileLabels
private

Definition at line 174 of file plottingManager.h.

◆ _fileNames

std::vector<std::string> MaCh3Plotting::PlottingManager::_fileNames
private

Definition at line 173 of file plottingManager.h.

◆ _inputMan

std::unique_ptr<InputManager> MaCh3Plotting::PlottingManager::_inputMan
private

Definition at line 188 of file plottingManager.h.

◆ _outputName

std::string MaCh3Plotting::PlottingManager::_outputName = "Plot.pdf"
private

Definition at line 178 of file plottingManager.h.

◆ _plotRatios

bool MaCh3Plotting::PlottingManager::_plotRatios = false
private

Definition at line 183 of file plottingManager.h.

◆ _plottingConfig

YAML::Node MaCh3Plotting::PlottingManager::_plottingConfig
private

Definition at line 168 of file plottingManager.h.

◆ _splitBySample

bool MaCh3Plotting::PlottingManager::_splitBySample = false
private

Definition at line 182 of file plottingManager.h.

◆ _styleMan

std::unique_ptr<StyleManager> MaCh3Plotting::PlottingManager::_styleMan
private

Definition at line 187 of file plottingManager.h.

◆ DEFAULT_PLOTTING_CONFIG

const std::string MaCh3Plotting::PlottingManager::DEFAULT_PLOTTING_CONFIG
Initial value:
=
std::string(std::getenv("MACH3")) +
"/plotting/PlottingConfig.yaml"

The default plotting config config to use when instantiating new PlottingManager objects.

Definition at line 46 of file plottingManager.h.

◆ DEFAULT_STYLE_CONFIG

const std::string MaCh3Plotting::PlottingManager::DEFAULT_STYLE_CONFIG
Initial value:
=
std::string(std::getenv("MACH3")) +
"/plotting/StyleConfig.yaml"

The default style config config to be used by PlottingManager instances when instantiating the StyleManager.

Definition at line 41 of file plottingManager.h.

◆ DEFAULT_TRANSLATION_CONFIG

const std::string MaCh3Plotting::PlottingManager::DEFAULT_TRANSLATION_CONFIG
Initial value:
=
std::string(std::getenv("MACH3")) +
"/plotting/universalTranslator.yaml"

The default translation config to be used by PlottingManager instances when instantiating the InputManager.

Definition at line 36 of file plottingManager.h.


The documentation for this class was generated from the following files: