-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecoTargetUserOptions.cxx
213 lines (179 loc) · 6.4 KB
/
RecoTargetUserOptions.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "RecoTargetUserOptions.h"
#include "RecoTargetMetrics.h"
#include <algorithm>
#include <iostream>
#include <getopt.h>
using namespace RecoTarget;
using namespace std;
/*! <ul>
* <li> parse command line arguments
* <li> check if all required arguments was passed
* <li> set up RecoTargetUserOptions based on arguments
* <\ul>
*/
RecoTargetUserOptions :: RecoTargetUserOptions (int argc, char **argv) : showSummary (false)
{
if (argc == 1) usage (); // return usage() if no arguments were passed
// short options triggers
static const char *shortOpts = "p:t:l:k:m:x:y:sh";
// long options triggers
static const struct option longOpts[]
{
{"path", required_argument, NULL, 'p'},
{"ntesting", required_argument, NULL, 't'},
{"nlearning", required_argument, NULL, 'l'},
{"nneighbors", required_argument, NULL, 'k'},
{"metric", required_argument, NULL, 'm'},
{"ttargets", required_argument, NULL, 'x'},
{"ltargets", required_argument, NULL, 'y'},
{"summary", no_argument, NULL, 's'},
{"help", no_argument, NULL, 'h'}
};
// flags for required arguments
bool isPathDefined = false;
bool isTestingDefined = false;
bool isLearningDefined = false;
bool isNeighborsDefined = false;
bool isMetricDefined = false;
bool isTestingTargetsDefined = false;
bool isLearningTargetsDefined = false;
// set initital values of targets flag to off
fill_n (isTestingTarget, nTargets, false);
fill_n (isLearningTarget, nTargets, false);
int o, id; // for get_opt (option and option index)
while ((o = getopt_long (argc, argv, shortOpts, longOpts, &id)) != -1)
switch (o)
{
case 'p':
pathToFiles = optarg;
isPathDefined = true;
break;
case 't':
nTestingSamples = atoi (optarg);
isTestingDefined = true;
break;
case 'l':
nLearningSamples = atoi (optarg);
isLearningDefined = true;
break;
case 'k':
nNearestNeighbors = atoi (optarg);
isNeighborsDefined = true;
break;
case 'm':
idMetric = atoi (optarg);
isMetricDefined = true;
break;
case 'x':
codeToFlags (atoi (optarg), isTestingTarget);
isTestingTargetsDefined = true;
break;
case 'y':
codeToFlags (atoi (optarg), isLearningTarget);
isLearningTargetsDefined = true;
break;
case 's':
showSummary = true;
break;
case 'h':
usage();
break;
case '?':
if (optopt == 'c' or isprint (optopt)) usage();
default:
usage();
}
if (!isPathDefined)
usage ("The path was not defined.");
if (!isTestingDefined)
usage ("The size of a testing sample was not defined.");
if (!isLearningDefined)
usage ("The size of a learning samples was not defined.");
if (!isNeighborsDefined)
usage ("The number of nearest neighbors was not defined.");
if (!isMetricDefined)
usage ("The metric was not defined.");
if (idMetric >= nMetrics)
usage ("Undefined metric.");
if (!isTestingTargetsDefined)
usage ("The list of testing targets was not defined.");
if (!isLearningTargetsDefined)
usage ("The list of learning targets was not defined.");
if (showSummary) summary();
}
//! print all available options with explanations
void RecoTargetUserOptions :: usage (const char *error)
{
if (error[0] != '\0') cerr << "\nERROR: " << error << "\n";
cout << "\n########## USAGE ##########\n";
cout << "\nUsage: ./RecoTarget [options]:\n\n";
cout << "\t -p, --path "
<< "\t [path_to_samples] (see example below)\n";
cout << "\t -t, --ntesting "
<< "\t [size of a testing sample]\n";
cout << "\t -l, --nlearning "
<< "\t [size of a learning sample]\n";
cout << "\t -x, --ttargets "
<< "\t [learning target code] (see examples below)\n";
cout << "\t -y, --ltargets "
<< "\t [testing target code] (see examples below)\n";
cout << "\t -k, --nneighbors "
<< "\t [number of nearest neighbors]\n";
cout << "\t -m, --metric "
<< "\t [metric] (see the options below)\n";
cout << "\t -s, --summary "
<< "\t (use to see your options summary)\n";
cout << "\t -h, --help "
<< "\t (show what you are reading now)\n";
cout << "\n########## PATH ##########\n";
cout << "\nUse path without run number, e.g.: "
<< "/minerva/data/users/goran/mc_production/nogrid/"
<< "central_value/minerva/ana/v10r8p1/";
cout << "\nThe following convention is assumed: "
<< "path/00/00/00/0X -> files for target X\n";
cout << "\n########## TARGETS ##########\n";
cout << "\nTarget code examples:\n\n";
cout << "\t x -t 145 \t (proceed targets 1, 4, 5)\n";
cout << "\t y -t 2514 \t (learn from targets 1, 2, 4, 5)\n";
cout << "\n########## METRICS ##########\n";
cout << "\nAvailable metrics:\n\n";
for (unsigned int i = 0; i < nMetrics; i++)
cout << "\t" << i << " - " << listOfMetrics[i] << "\n";
cout << "\n";
exit (1);
}
//! print options chosen by user and ask if program should continue
void RecoTargetUserOptions :: summary ()
{
cout << "\nThis is your setup:\n\n";
cout << "The path to ana files: \033[1m"
<< pathToFiles << "\033[0m\n";
cout << "The size of your testing sample = \033[1m"
<< nTestingSamples << "\033[0m\n";
cout << "The size of your learning sample = \033[1m"
<< nLearningSamples << "\033[0m\n";
cout << "The number of nearest neighbors = \033[1m"
<< nNearestNeighbors << "\033[0m\n";
cout << "Targets to proceed: \033[1m";
for (unsigned int i = 0; i < 5; i++)
if (isTestingTarget[i]) cout << i+1 << " ";
cout << "\n\033[0mTargets to learn from: \033[1m";
for (unsigned int i = 0; i < 5; i++)
if (isLearningTarget[i]) cout << i+1 << " ";
cout << "\n\033[0mYour metric: \033[1m"
<< listOfMetrics[idMetric] << "\033[0m\n";
char answer;
cout << "\nDo you want to proceed [y/n]? "; cin >> answer;
if (answer != 'Y' and answer != 'y') exit (2);
}
//! target code = sum_i^#targets (TARGET * 10^i)
void RecoTargetUserOptions::codeToFlags (int code, bool *flags)
{
while (code > 0)
{
const unsigned int target = code % 10;
if (target < 1 or target > 5) usage ("Wrong target.");
flags [target-1] = true;
code /= 10;
};
}