Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditMechanics.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <map>
4 #include <set>
5 
7 
8 #include "JSupport/JMeta.hh"
9 
10 #include "Jeep/JContainer.hh"
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 
16 namespace {
17 
18  using namespace JPP;
19 
20  static const std::string set_t = "set"; //!< Set mechanics
21 
22  /**
23  * Auxiliary class to apply mechanics modifications.
24  */
25  struct JModifier {
26  /**
27  * Apply modification to given mechanics.
28  *
29  * \param mechanics mechanics
30  * \return true if valid action; else false
31  */
32  bool apply(JMechanics& mechanics) const
33  {
34  if (action == set_t)
35  mechanics = this->mechanics;
36  else
37  return false;
38 
39  return true;
40  }
41 
42 
43  /**
44  * Read modifier from input.
45  *
46  * \param in input stream
47  * \param modifier modifier
48  * \return input stream
49  */
50  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
51  {
52  return in >> modifier.id >> modifier.action >> modifier.mechanics;
53  }
54 
55 
56  /**
57  * Write modifier to output.
58  *
59  * \param out output stream
60  * \param modifier modifier
61  * \return output stream
62  */
63  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
64  {
65  out << modifier.id;
66  out << ' ';
67  out << modifier.action;
68  out << ' ';
69  out << modifier.mechanics;
70 
71  return out;
72  }
73 
74 
75  int id;
76  std::string action;
77  JMechanics mechanics;
78  };
79 }
80 
81 /**
82  * \file
83  *
84  * Auxiliary program to add or modify mechanical model data of detector string.
85  *
86  * Syntax:
87  * <pre>
88  * -M "<string number> (set) a b"
89  * </pre>
90  *
91  * The options
92  * <pre>
93  * -A "<string number> a b"
94  * -r "<string number>"
95  * </pre>
96  * can be used to add or remove a specific string, respectively.
97  *
98  * \author mdejong
99  */
100 int main(int argc, char **argv)
101 {
102  using namespace std;
103  using namespace JPP;
104 
105  typedef JContainer< map<int, JMechanics> > container_type;
106 
107  string inputFile;
108  string outputFile;
109  vector<JModifier> mod;
111  set<int> rm;
112  bool squash;
113  int debug;
114 
115  try {
116 
117  JParser<> zap("Auxiliary program to add or modify mechanical model data of detector string.");
118 
119  zap['f'] = make_field(inputFile, "mechanics input file");
120  zap['o'] = make_field(outputFile, "mechanics output file");
121  zap['M'] = make_field(mod, "mechanics modifier") = JPARSER::initialised();
122  zap['A'] = make_field(add, "add string mechanics") = JPARSER::initialised();
123  zap['r'] = make_field(rm, "remove string[s]") = JPARSER::initialised();
124  zap['q'] = make_field(squash, "squash meta data");
125  zap['d'] = make_field(debug, "debug level") = 2;
126 
127  zap(argc, argv);
128  }
129  catch(const exception &error) {
130  FATAL(error.what() << endl);
131  }
132 
133  container_type data;
134 
135  try {
136  data.load(inputFile.c_str());
137  }
138  catch(const exception&) {}
139 
140  if (squash) {
141  data.comment.clear();
142  }
143 
144  data.comment.add(JMeta(argc,argv));
145 
146  for (map<int, JMechanics>::const_iterator i = add.begin(); i != add.end(); ++i) {
147  if (data.count(i->first) == 0u)
148  data[i->first] = i->second;
149  else
150  ERROR("String " << i->first << " already exists." << endl);
151  }
152 
153  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
154 
155  DEBUG("Modifier" << ' '
156  << "(" << FILL(2,'0') << i->id << FILL() << ")" << ' '
157  << "action" << ' ' << i->mechanics << endl);
158 
159  JMechanics mechanics;
160 
161  container_type::iterator p = data.find(i->id);
162 
163  if (p != data.end()) {
164  mechanics = p->second;
165  }
166 
167  if (!i->apply(mechanics))
168  ERROR("No valid action: " << *i << endl);
169  else
170  data[i->id] = mechanics;
171  }
172 
173  for (set<int>::const_iterator i = rm.begin(); i != rm.end(); ++i) {
174 
175  container_type::iterator p = data.find(*i);
176 
177  if (p != data.end()) {
178  data.erase(p);
179  }
180  }
181 
182  data.store(outputFile.c_str());
183 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Mechanical modelling of string.
Utility class to parse command line options.
Definition: JParser.hh:1517
int main(int argc, char *argv[])
Definition: Main.cc:15
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
string outputFile
I/O formatting auxiliaries.
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:39
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1756
Utility class to parse command line options.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
double u[N+1]
Definition: JPolint.hh:776
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Container I/O.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:39