ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
option.cxx
Go to the documentation of this file.
1 //
2 // option.cpp
3 // Class and functions for parsing and storing command line options.
4 //
5 // Author: Jens Wilberg, Lukas Aymans, Christoph Dalitz
6 // Date: 2018-08-30
7 // License: see ../LICENSE
8 //
9 
10 #include "option.h"
11 
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 #include <stdexcept>
17 
18 // initialize default values
20 {
21  this->infile_name = NULL;
22  this->outfile_prefix = NULL;
23  this->gnuplot = false;
24  this->delimiter = ' ';
25  this->skip = 0;
26  this->verbose = 0;
27 
28  // neighbourship smoothing
29  this->r = 2;
30  this->rdnn = true;
31 
32  // triplet building
33  this->k = 19;
34  this->n = 2;
35  this->a = 0.03;
36 
37  // triplet clustering
38  this->s = 0.3;
39  this->sdnn = true;
40  this->t = 4.0;
41  this->tauto = false;
42  this->dmax = 0.0;
43  this->isdmax = false;
44  this->dmax_dnn = false;
45  this->link = SINGLE;
46 
47  this->m = 15;
48 }
49 
50 //-------------------------------------------------------------------
51 // Parses basic parameters for track clustering
52 //-------------------------------------------------------------------
53 void Opt::set_parameters(double _s, double _k, double _n, double _m, double _r, double _a, double _t)
54 {
55  this->s = _s;
56  this->k = _k;
57  this->n = _n;
58  this->m = _m;
59  this->r = _r;
60  this->a = _a;
61  this->t = _t;
62 
63  // std::cout<<" Set parameters - s, "<<this->s<<" - k, "<<this->k<<" - n, "<<this->n<<" - m, "<<this->m<<" - r,
64  // "<<this->r<<" - a, "<<this->a<<" - t, "<<this->t<<"\n"; std::cout<<" Parameters set by default (not accessible) -
65  // rdnn, "<<this->rdnn<<" - sdnn, "<<this->sdnn<<" - tauto, "<<this->tauto<<" - dmax, "<<this->dmax<<" - isdmax,
66  // "<<this->isdmax<<" - dmax_dnn, "<<this->dmax_dnn<<" - Link, "<<this->link<<"\n";
67 }
68 
69 //-------------------------------------------------------------------
70 // parse commandline arguments.
71 // Parses the commandline arguments in *argv* and returns 0 if no error
72 // occurred. *argc* is the count off arguments.
73 //-------------------------------------------------------------------
74 int Opt::parse_args(int argc, char **argv)
75 {
76  // parse command line
77  std::pair<double, bool> tmp;
78  try {
79  for (int i = 1; i < argc; i++) {
80  if (0 == strcmp(argv[i], "-v")) {
81  if (this->verbose < 1)
82  this->verbose = 1;
83  } else if (0 == strcmp(argv[i], "-vv")) {
84  if (this->verbose < 2)
85  this->verbose = 2;
86  } else if (0 == strcmp(argv[i], "-s")) {
87  ++i;
88  if (i >= argc) {
89  return 1;
90  }
91  tmp = this->parse_argument(argv[i]);
92  this->s = tmp.first;
93  this->sdnn = tmp.second;
94  } else if (0 == strcmp(argv[i], "-r")) {
95  ++i;
96  if (i >= argc) {
97  return 1;
98  }
99  tmp = this->parse_argument(argv[i]);
100  this->r = tmp.first;
101  this->rdnn = tmp.second;
102  } else if (0 == strcmp(argv[i], "-k")) {
103  ++i;
104  if (i < argc) {
105  this->k = (int)stod(argv[i]);
106  } else {
107  return 1;
108  }
109  } else if (0 == strcmp(argv[i], "-n")) {
110  ++i;
111  if (i < argc) {
112  this->n = (int)stod(argv[i]);
113  } else {
114  return 1;
115  }
116  } else if (0 == strcmp(argv[i], "-a")) {
117  ++i;
118  if (i < argc) {
119  this->a = stod(argv[i]);
120  } else {
121  return 1;
122  }
123  } else if (0 == strcmp(argv[i], "-t")) {
124  ++i;
125  if (i < argc) {
126  if (strcmp(argv[i], "auto") == 0 || strcmp(argv[i], "automatic") == 0) {
127  this->tauto = true;
128  } else {
129  this->t = stod(argv[i]);
130  this->tauto = false;
131  }
132  } else {
133  return 1;
134  }
135  } else if (0 == strcmp(argv[i], "-m")) {
136  ++i;
137  if (i < argc) {
138  this->m = (int)stod(argv[i]);
139  } else {
140  return 1;
141  }
142  } else if (0 == strcmp(argv[i], "-delim")) {
143  ++i;
144  if (i < argc) {
145  if (strlen(argv[i]) > 1) {
146  std::cerr << "[Error] only a character as delimiter is allowed" << std::endl;
147  return 1;
148  }
149  this->delimiter = *argv[i];
150  } else {
151  return 1;
152  }
153  } else if (0 == strcmp(argv[i], "-dmax")) {
154  ++i;
155  if (i >= argc) {
156  return 1;
157  }
158  if (strcmp(argv[i], "none") == 0) {
159  this->isdmax = false;
160  } else {
161  tmp = this->parse_argument(argv[i]);
162  this->dmax = tmp.first;
163  this->dmax_dnn = tmp.second;
164  this->isdmax = true;
165  }
166  } else if (0 == strcmp(argv[i], "-link")) {
167  ++i;
168  if (i >= argc) {
169  return 1;
170  }
171  if (strcmp(argv[i], "single") == 0) {
172  this->link = SINGLE;
173  } else if (strcmp(argv[i], "complete") == 0) {
174  this->link = COMPLETE;
175  } else if (strcmp(argv[i], "average") == 0) {
176  this->link = AVERAGE;
177  } else {
178  std::cerr << "[Error] " << argv[i] << " is not a valide option!" << std::endl;
179  return 1;
180  }
181  } else if (0 == strcmp(argv[i], "-skip")) {
182  ++i;
183  if (i < argc) {
184  int tmp2 = atoi(argv[i]);
185  if (tmp2 < 0) {
186  std::cerr << "[Error] skip takes only positive integers. parameter "
187  "is ignored!"
188  << std::endl;
189  } else {
190  this->skip = (size_t)tmp2;
191  }
192  } else {
193  return 1;
194  }
195  } else if (0 == strcmp(argv[i], "-oprefix")) {
196  if (i + 1 == argc) {
197  std::cerr << "[Error] not enough parameters" << std::endl;
198  return 1;
199  } else if (argv[i + 1][0] == '-') {
200  std::cerr << "[Error] please enter outfile name" << std::endl;
201  return 1;
202  }
203  this->outfile_prefix = argv[++i];
204  } else if (0 == strcmp(argv[i], "-gnuplot")) {
205  this->gnuplot = true;
206  } else if (argv[i][0] == '-') {
207  return 1;
208  } else {
209  this->infile_name = argv[i];
210  }
211  }
212  } catch (std::invalid_argument e) {
213  std::cerr << e.what() << std::endl;
214  return 1;
215  }
216  return 0;
217 }
218 
219 //-------------------------------------------------------------------
220 // parses the argument string *str*.
221 // the result is returned in *result*. *result_dnn* stores if *result*
222 // depends on dnn. If *str* is not a number a invalid_argument exception
223 // is thrown.
224 //-------------------------------------------------------------------
225 std::pair<double, bool> Opt::parse_argument(const char *str)
226 {
227  double result = 0.0;
228  bool dnn = false;
229  char buff[4];
230  size_t count = sscanf(str, "%lf%3s", &result, buff);
231  if (count == 2) {
232  if (std::strcmp("dnn", buff) && std::strcmp("dNN", buff))
233  throw std::invalid_argument("not a number");
234  dnn = true;
235  } else if (count == 0) {
236  throw std::invalid_argument("not a number");
237  }
238  return std::pair<double, bool>(result, dnn);
239 }
240 
241 //-------------------------------------------------------------------
242 // compute attributes which depend on dnn.
243 // If r,s,dmax depend on dnn their new value will be computed.
244 //-------------------------------------------------------------------
245 void Opt::set_dnn(double dnn)
246 {
247  if (this->rdnn) {
248  this->r *= dnn;
249  if (this->verbose > 0) {
250  std::cout << "[Info] computed smoothed radius: " << this->r << std::endl;
251  }
252  }
253  if (this->sdnn) {
254  this->s *= dnn;
255  if (this->verbose > 0) {
256  std::cout << "[Info] computed distance scale: " << this->s << std::endl;
257  }
258  }
259  if (this->dmax_dnn) {
260  this->dmax *= dnn;
261  if (this->verbose > 0) {
262  std::cout << "[Info] computed max gap: " << this->dmax << std::endl;
263  }
264  }
265 }
266 
267 // read access functions
268 const char *Opt::get_ifname()
269 {
270  return this->infile_name;
271 }
272 const char *Opt::get_ofprefix()
273 {
274  return this->outfile_prefix;
275 }
277 {
278  return this->rdnn || this->sdnn || this->dmax_dnn;
279 }
281 {
282  return this->gnuplot;
283 }
285 {
286  return this->skip;
287 }
289 {
290  return this->delimiter;
291 }
293 {
294  return this->verbose;
295 }
296 double Opt::get_r()
297 {
298  return this->r;
299 }
300 size_t Opt::get_k()
301 {
302  return this->k;
303 }
304 size_t Opt::get_n()
305 {
306  return this->n;
307 }
308 double Opt::get_a()
309 {
310  return this->a;
311 }
312 double Opt::get_s()
313 {
314  return this->s;
315 }
317 {
318  return this->tauto;
319 }
320 double Opt::get_t()
321 {
322  return this->t;
323 }
325 {
326  return this->isdmax;
327 }
329 {
330  return this->dmax;
331 }
333 {
334  return this->link;
335 }
336 size_t Opt::get_m()
337 {
338  return this->m;
339 }
Opt::get_skip
size_t get_skip()
Definition: option.cxx:284
Linkage
Linkage
Definition: util.h:13
Opt::is_dmax
bool is_dmax()
Definition: option.cxx:324
Opt::get_t
double get_t()
Definition: option.cxx:320
Opt::needs_dnn
bool needs_dnn()
Definition: option.cxx:276
stod
double stod(const char *s)
Definition: util.cxx:20
Opt::set_parameters
void set_parameters(double _s, double _k, double _n, double _m, double _r, double _a, double _t)
Definition: option.cxx:53
Opt::set_dnn
void set_dnn(double dnn)
Definition: option.cxx:245
Opt::get_k
size_t get_k()
Definition: option.cxx:300
Opt::is_gnuplot
bool is_gnuplot()
Definition: option.cxx:280
AVERAGE
@ AVERAGE
Definition: util.h:13
Opt::get_ifname
const char * get_ifname()
Definition: option.cxx:268
Opt::get_ofprefix
const char * get_ofprefix()
Definition: option.cxx:272
Opt::get_m
size_t get_m()
Definition: option.cxx:336
Opt::Opt
Opt()
Definition: option.cxx:19
Opt::parse_args
int parse_args(int argc, char **argv)
Definition: option.cxx:74
Opt::get_n
size_t get_n()
Definition: option.cxx:304
Opt::is_tauto
bool is_tauto()
Definition: option.cxx:316
Opt::get_verbosity
int get_verbosity()
Definition: option.cxx:292
Opt::get_delimiter
char get_delimiter()
Definition: option.cxx:288
SINGLE
@ SINGLE
Definition: util.h:13
Opt::get_dmax
double get_dmax()
Definition: option.cxx:328
COMPLETE
@ COMPLETE
Definition: util.h:13
option.h
Opt::get_r
double get_r()
Definition: option.cxx:296
Opt::get_linkage
Linkage get_linkage()
Definition: option.cxx:332
Opt::get_a
double get_a()
Definition: option.cxx:308
Opt::get_s
double get_s()
Definition: option.cxx:312