Jpp  19.1.0
the software that should make you happy
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 }
ASCII I/O of objects with ROOT dictionary.
File status.
Monte Carlo run header.
Definition: JHead.hh:1236
std::istream & read(std::istream &in)
Read header from input.
Definition: JHead.cc:53
JAANET::start_run start_run
Definition: JHead.hh:1583
std::ostream & write(std::ostream &out) const
Write header to output.
Definition: JHead.cc:91
static JLANG::JEquationParameters & getEquationParameters()
Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
Definition: JHead.hh:1642
void push()
Push all data members to Head.
Definition: JHead.cc:28
std::ostream & print(std::ostream &out) const
Print header to output.
Definition: JHead.cc:122
JAANET::end_event end_event
Definition: JHead.hh:1613
General purpose equation class.
Definition: JEquation.hh:47
This class can be used to temporarily redirect an input stream to an input string.
Wrapper class around STL stringstream class to facilitate optional loading of data from file.
void load()
Load data from file with name corresponding to current contents.
Implementation for ASCII input of objects with ROOT dictionary.
JRootReader & getObject(T &object)
Read object.
Implementation for ASCII output of objects with ROOT dictionary.
JRootWriter & put(const T &object)
Write object according equation format.
Extensions to Evt data format.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
std::string getTag(const std::string &tag)
Get tag without aanet extension "_<counter>" for identical tags.
Definition: JHead.hh:94
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
Definition: JSTDTypes.hh:14
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
Implementation for Head output of JHead objects with ROOT dictionary.
Definition: JHeadWriter.hh:30
virtual JRootWriter & put(const JEquation &equation) override
Write equation.
Definition: JHeadWriter.hh:54
End of event record.
Definition: JHead.hh:1213
bool is_valid() const
Check validity of this addressable class.
Definition: JRootClass.hh:471
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:495
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
Definition: JRootClass.hh:483
Auxiliary class to manage access to base classes and data members of ROOT class.
Definition: JRootClass.hh:43
TClass * getClass() const
Get class.
Definition: JRootClass.hh:194
ROOT class for reading object.
Definition: JRootClass.hh:544
ROOT class for writing object.
Definition: JRootClass.hh:604