AcclaimCmdLineArgs.cxx
1 #include "AcclaimCmdLineArgs.h"
2 
3 #include <unistd.h>
4 #include <getopt.h>
5 #include <iostream>
6 
7 #include "AnitaVersion.h"
8 #include "AnalysisFlow.h"
9 #include "RootTools.h"
10 
11 // Values used if value not specified on command line
12 const int default_run = 352;
13 const int default_numdivisions = 1;
14 const int default_subdivision = 0;
15 const int default_anitaversion = 3;
16 const char* default_settings_filename = "";
17 
23 void Acclaim::CmdLineArgs::printHelp(const char* argv0){
24 
25  // should probably use printf to get formatting more readable in the source code... oh well
26  std::cerr << std::endl;
27  std::cerr << argv0 << " options:" << std::endl;
28  std::cerr << "-h or --help : print this help message" << std::endl;
29  std::cerr << "-a " << default_anitaversion << " or --anitaversion=" << default_anitaversion << " : sets the initial anita version" << std::endl;
30  std::cerr << "-r " << default_run << " or --run=" << default_run << " : which run to process (352 in this example)" << std::endl;
31  std::cerr << "-m " << default_subdivision << " or --subdivision=" << default_subdivision << " : This job processes only part of the run, subdivision m of n divisions (where 0 <= m < n)" << std::endl;
32  std::cerr << "-n " << default_numdivisions << " or --numdivisions=" << default_numdivisions << " : This job processes only part of the run, subdivision m of n divisions (where 0 <= m < n)" << std::endl;
33  std::cerr << "-s " << default_settings_filename << " or --settings=" << default_settings_filename << " : Path to an AcclaimSettings.conf file (or directory containing), see AnalysisSettings class for default locations searched if not specified" << std::endl;
34 
35  std::cerr << std::endl;
36  std::cerr << "Different (mutually incompatible) data subselections can be made (default is --decimated)" << std::endl;
37  std::cerr << "--all : does every event in the run." << std::endl;
38  std::cerr << "--wais : for just the WAIS pulser events" << std::endl;
39  std::cerr << "--decimated : only processes events in the decimated (10%) data set" << std::endl;
40 
41  std::cerr << std::endl;
42  std::cerr << "An additional \"mc\" can be added to the output filename with the --mc flag" << std::endl;
43  std::cerr << "--mc : Add mc tag to output file name, this does NOT affect the selection flags listed above." << std::endl;
44 
45 }
46 
47 
48 
55 void Acclaim::CmdLineArgs::getArgs(int argc, char* argv[]){
56 
57  int c = 0;
58 
59  run = default_run;
60  numdivisions = default_numdivisions;
61  division = default_subdivision;
62  anitaversion = default_anitaversion;
63  settings_filename = default_settings_filename;
64  event_selection = AnalysisFlow::kDecimated;
65  tag_output_as_mc = 0;
66 
67  while (true){
68 
69  const int numEventSelectionFlags = 3; // should all go at the front
70  static struct option long_options[] = {{"all", no_argument, &event_selection, AnalysisFlow::kAll},
71  {"decimated", no_argument, &event_selection, AnalysisFlow::kDecimated},
72  {"wais", no_argument, &event_selection, AnalysisFlow::kWaisPulser},
73  {"mc", no_argument, &tag_output_as_mc, 1},
74  {"help", no_argument, NULL, 'h'},
75  {"anitaversion", required_argument, NULL, 'a'},
76  {"run", required_argument, NULL, 'r'},
77  {"numdivisions", required_argument, NULL, 'n'},
78  {"subdivision", required_argument, NULL, 'm'},
79  {"settings", required_argument, NULL, 's'},
80  {"output", required_argument, NULL, 'o'},
81  {0, 0, 0, 0}};
82 
83  /* getopt_long stores the option index here. */
84  int option_index = 0;
85 
86  c = getopt_long (argc, argv, "ha:r:n:m:d:s:",
87  long_options, &option_index);
88 
89  /* Detect the end of the options. */
90  if (c == -1){
91  break;
92  }
93 
94  switch (c){
95  case 0:
96  /* If this option set a flag, do nothing else now. */
97  if (long_options[option_index].flag != 0){
98  if(option_index < numEventSelectionFlags){
99  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", got " << long_options[option_index].name << ", so set event_selection = " << event_selection << std::endl;
100  }
101  break;
102  }
103 
104  // std::cout << "option " << long_options[option_index].name << std::endl;
105  // if (optarg){
106  // std::cout << " with arg " << optarg << std::endl;
107  // }
108  // std::cout << std::endl;
109  // break;
110 
111  case 'h':
112  printHelp(argv[0]);
113  exit(0);
114  break;
115 
116  case 'a':
117  anitaversion = atoi(optarg);
118  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", set anitaversion = " << anitaversion << std::endl;
119  AnitaVersion::set(anitaversion);
120  break;
121 
122  case 'r':
123  run = atoi(optarg);
124  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", set run = " << run << std::endl;
125  break;
126 
127  case 'n':
128  numdivisions = atoi(optarg);
129  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", set numdivisions = " << numdivisions << std::endl;
130  break;
131 
132  case 'm':
133  division = atoi(optarg);
134  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", set division = " << division << std::endl;
135  break;
136 
137  case 's':
138  settings_filename = TString::Format("%s", optarg);
139  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", set settings_filename = " << settings_filename << std::endl;
140 
141  case '?':
142  // printHelp(argv[0]);
143  /* getopt_long already printed an error message. */
144  break;
145 
146  default:
147  abort();
148  }
149  }
150 
151  /* Print any remaining command line arguments (not options). */
152  if (optind < argc)
153  {
154  std::cerr << "non-option ARGV-elements: ";
155  while (optind < argc){
156  std::cerr << argv[optind++] << " ";
157  }
158  std::cerr << std::endl;
159  }
160 
161 
162  /* Now do the file name, after parsing the settings so the selection and mc tag can be used */
163  std::vector<TString> argv0Tokens;
164  RootTools::tokenize(argv0Tokens, argv[0], "/");
165  output_filename = argv0Tokens.back();
166 
167  if(tag_output_as_mc){
168  output_filename += "_mc";
169  }
170 
171  output_filename += TString::Format("_%s", AnalysisFlow::selectionAsString((AnalysisFlow::selection)event_selection));
172 
173 }
174 
175 
182 Acclaim::CmdLineArgs::CmdLineArgs(int argc, char* argv[]){
183 
184  getArgs(argc, argv);
185  checkArgs(argv[0]);
186 
187 }
188 
189 
190 void Acclaim::CmdLineArgs::checkArgs(const char* argv0){
191 
192  // if(anitaversion!=3){
193  // std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", currently only checking arguments for ANITA-3" << std::endl;
194  // }
195 
196  Bool_t mustDie = false; // will set to true if have nonsense, program will then die
197 
198  if(numdivisions <= 0){
199  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", require numdivisions to be a positive integer" << std::endl;
200  mustDie = true;
201  }
202 
203  if(division < 0 || division >= numdivisions){
204  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", require 0 <= division < numdivisions" << std::endl;
205  mustDie = true;
206  }
207 
208  if(mustDie){
209  printHelp(argv0);
210  exit(1);
211  }
212 
213 
214 
215 }
selection
A human readable method of selecting the set of events to process, this can be expanded as required...
Definition: AnalysisFlow.h:42
static const char * selectionAsString(selection sel)
Definition: AnalysisFlow.h:56
void printHelp(const char *argv0)
void getArgs(int argc, char *argv[])
CmdLineArgs(int argc, char *argv[])