Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHead.cc
Go to the documentation of this file.
1 #include <string>
2 #include <map>
3 #include <sstream>
4 #include <memory>
5 
7 
8 #include "JSystem/JStat.hh"
10 #include "JLang/JStringStream.hh"
11 #include "JLang/JNullStream.hh"
12 #include "JLang/JLangToolkit.hh"
13 #include "JROOT/JRootClass.hh"
14 #include "JROOT/JRootStreamer.hh"
15 #include "JAAnet/JHead.hh"
16 #include "JAAnet/JAAnetDictionary.hh"
17 #include "JAAnet/JHeadWriter.hh"
18 
19 
20 /**
21  * \author mdejong
22  */
23 namespace JAANET {
24 
25  /**
26  * Push all data members to Head.
27  */
28  void JHead::push()
29  {
30  using namespace std;
31  using namespace JPP;
32 
33  JRootReadableClass cls(*this);
34 
35  unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
36 
37  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
38  if (!JRootClass::is_static(*p)) {
39  if (this->count(p->GetName()) == 0) {
40  (*this)[p->GetName()] = "";
41  }
42  }
43  }
44  }
45 
46 
47  /**
48  * Read header from input.
49  *
50  * \param in input stream
51  * \return input stream
52  */
53  std::istream& JHead::read(std::istream& in)
54  {
55  using namespace std;
56  using namespace JPP;
57 
58  JStringStream is(in);
59 
60  if (getFileStatus(is.str().c_str())) {
61  is.load();
62  }
63 
65 
66  JRootReadableClass cls(*this);
67 
68  for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) {
69 
70  JRedirectString redirect(reader, equation.getValue());
71 
72  const JRootReadableClass abc = cls.find(equation.getKey().c_str());
73 
74  if (abc.is_valid()) {
75  reader.getObject(abc);
76  }
77 
78  (*this)[equation.getKey()] = equation.getValue();
79  }
80 
81  return in;
82  }
83 
84 
85  /**
86  * Write header to output.
87  *
88  * \param out output stream
89  * \return output stream
90  */
91  std::ostream& JHead::write(std::ostream& out) const
92  {
93  using namespace std;
94  using namespace JPP;
95 
97 
98  JRootWritableClass cls(*this);
99 
100  unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
101 
102  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
103  if (!JRootClass::is_static(*p)) {
104  if (this->find(p->GetName()) != this->end() ||
105  cls.get(*p) == JRootClass(&JHead::start_run) ||
106  cls.get(*p) == JRootClass(&JHead::end_event)) {
107  writer.put(p->GetName(), cls.get(*p), true);
108  }
109  }
110  }
111 
112  return out << flush;
113  }
114 
115 
116  /**
117  * Print header to output.
118  *
119  * \param out output stream
120  * \return output stream
121  */
122  std::ostream& JHead::print(std::ostream& out) const
123  {
124  using namespace std;
125  using namespace JPP;
126 
128 
129  JRootWritableClass cls(*this);
130 
131  unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
132 
134 
135  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
136  if (!JRootClass::is_static(*p)) {
137  if (cls.get(*p) != JRootClass(&JHead::end_event))
138  writer.put(p->GetName(), cls.get(*p), true);
139  else
140  end_event = make_pair(p->GetName(), cls.get(*p));
141  }
142  }
143 
144  for (JHead::const_iterator i = this->begin(); i != this->end(); ++i) {
145  if (!cls.find(i->first.c_str()).is_valid()) {
146  writer.put(i->first, i->second);
147  }
148  }
149 
150  writer.put(end_event.first, end_event.second, true);
151 
152  return out << flush;
153  }
154 
155 
156  /**
157  * Copy header from <tt>from</tt> to <tt>to</tt>.
158  *
159  * \param from header
160  * \param to header
161  */
162  void copy(const Head& from, JHead& to)
163  {
164  using namespace std;
165  using namespace JPP;
166 
168 
169  JRootReadableClass cls(to);
170 
171  for (Head::const_iterator i = from.begin(); i != from.end(); ++i) {
172 
173  const JRootReadableClass& abc = cls.find(getTag(i->first).c_str());
174 
175  const string buffer = trim(i->second);
176 
177  if (abc.is_valid() && buffer != "") {
178 
179  JRedirectString redirect(reader, buffer);
180 
181  reader.getObject(abc);
182 
183  if (i->first == getTag(i->first)) {
184  to.insert(*i); // keep track of parsed tags
185  }
186 
187  } else {
188 
189  to.insert(*i); // store data of unknown tags
190  }
191  }
192  }
193 
194 
195  /**
196  * Copy header from <tt>from</tt> to <tt>to</tt>.
197  *
198  * \param from header
199  * \param to header
200  */
201  void copy(const JHead& from, Head& to)
202  {
203  using namespace std;
204  using namespace JPP;
205 
206  to = Head();
207 
209 
210  JRootWritableClass cls(from);
211 
212  unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
213 
214  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
215  if (!JRootClass::is_static(*p)) {
216  if (from.find(p->GetName()) != from.end() ||
217  cls.get(*p) == JRootClass(&JHead::start_run) ||
218  cls.get(*p) == JRootClass(&JHead::end_event)) {
219  writer.put(p->GetName(), cls.get(*p), true);
220  }
221  }
222  }
223 
224  // copy pending data
225 
226  for (JHead::const_iterator i = from.begin(); i != from.end(); ++i) {
227  if (to.find(i->first) == to.end()) {
228  to.insert(*i);
229  }
230  }
231  }
232 }
void push()
Push all data members to Head.
Definition: JHead.cc:28
const std::string & getKey() const
Get key.
Definition: JEquation.hh:163
Implementation for ASCII output of objects with ROOT dictionary.
is
Definition: JDAQCHSM.chsm:167
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JAANET::end_event end_event
Definition: JHead.hh:1595
General purpose equation class.
Definition: JEquation.hh:47
ASCII I/O of objects with ROOT dictionary.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
static JLANG::JEquationParameters & getEquationParameters()
Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
Definition: JHead.hh:1624
std::istream & read(std::istream &in)
Read header from input.
Definition: JHead.cc:53
JAANET::start_run start_run
Definition: JHead.hh:1566
std::ostream & print(std::ostream &out) const
Print header to output.
Definition: JHead.cc:122
Monte Carlo run header.
Definition: JHead.hh:1221
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
virtual JRootWriter & put(const JEquation &equation) override
Write equation.
Definition: JHeadWriter.hh:54
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
End of event record.
Definition: JHead.hh:1200
JRootWriter & put(const T &object)
Write object according equation format.
Implementation for Head output of JHead objects with ROOT dictionary.
Definition: JHeadWriter.hh:28
static JNullStream null
Null I/O stream.
Definition: JNullStream.hh:51
bool is_valid(T JHead::*pd) const
Check validity of given data member in JHead.
Definition: JHead.hh:1305
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
std::string getTag(const std::string &tag)
Get tag without aanet extension &quot;_&lt;counter&gt;&quot; for identical tags.
Definition: JHead.hh:94
std::ostream & write(std::ostream &out) const
Write header to output.
Definition: JHead.cc:91
File status.