Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JHead.cc
Go to the documentation of this file.
1#include <string>
2#include <map>
3#include <sstream>
4#include <memory>
5
7
10#include "JLang/JNullStream.hh"
11#include "JLang/JLangToolkit.hh"
12#include "JROOT/JRoot.hh"
13#include "JROOT/JRootClass.hh"
15#include "JAAnet/JHead.hh"
17#include "JAAnet/JHeadWriter.hh"
18
19
20/**
21 * \author mdejong
22 */
23namespace JAANET {
24
25 /**
26 * Push all data members to Head.
27 */
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 (!JRoot::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 JPP;
56
57 JStringStream is(in);
58
60
61 JRootReadableClass cls(*this);
62
63 for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) {
64
65 JRedirectString redirect(reader, equation.getValue());
66
67 const JRootReadableClass abc = cls.find(equation.getKey().c_str());
68
69 if (abc.is_valid()) {
70 reader.getObject(abc);
71 }
72
73 (*this)[equation.getKey()] = equation.getValue();
74 }
75
76 return in;
77 }
78
79
80 /**
81 * Write header to output.
82 *
83 * \param out output stream
84 * \return output stream
85 */
86 std::ostream& JHead::write(std::ostream& out) const
87 {
88 using namespace std;
89 using namespace JPP;
90
92
93 JRootWritableClass cls(*this);
94
95 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
96
97 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
98 if (!JRoot::is_static(*p)) {
99 if (this->find(p->GetName()) != this->end() ||
100 cls.get(*p) == JRootClass(&JHead::start_run) ||
101 cls.get(*p) == JRootClass(&JHead::end_event)) {
102 writer.put(p->GetName(), cls.get(*p), true);
103 }
104 }
105 }
106
107 return out << flush;
108 }
109
110
111 /**
112 * Print header to output.
113 *
114 * \param out output stream
115 * \return output stream
116 */
117 std::ostream& JHead::print(std::ostream& out) const
118 {
119 using namespace std;
120 using namespace JPP;
121
123
124 JRootWritableClass cls(*this);
125
126 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
127
129
130 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
131 if (!JRoot::is_static(*p)) {
132 if (cls.get(*p) != JRootClass(&JHead::end_event))
133 writer.put(p->GetName(), cls.get(*p), true);
134 else
135 end_event = make_pair(p->GetName(), cls.get(*p));
136 }
137 }
138
139 for (JHead::const_iterator i = this->begin(); i != this->end(); ++i) {
140 if (!cls.find(i->first.c_str()).is_valid()) {
141 writer.put(i->first, i->second);
142 }
143 }
144
145 writer.put(end_event.first, end_event.second, true);
146
147 return out << flush;
148 }
149
150
151 /**
152 * Copy header from <tt>from</tt> to <tt>to</tt>.
153 *
154 * \param from header
155 * \param to header
156 */
157 void copy(const Head& from, JHead& to)
158 {
159 using namespace std;
160 using namespace JPP;
161
163
164 JRootReadableClass cls(to);
165
166 for (Head::const_iterator i = from.begin(); i != from.end(); ++i) {
167
168 const JRootReadableClass& abc = cls.find(getTag(i->first).c_str());
169
170 const string buffer = trim(i->second);
171
172 if (abc.is_valid() && buffer != "") {
173
174 JRedirectString redirect(reader, buffer);
175
176 reader.getObject(abc);
177
178 if (i->first == getTag(i->first)) {
179 to.insert(*i); // keep track of parsed tags
180 }
181
182 } else {
183
184 to.insert(*i); // store data of unknown tags
185 }
186 }
187 }
188
189
190 /**
191 * Copy header from <tt>from</tt> to <tt>to</tt>.
192 *
193 * \param from header
194 * \param to header
195 */
196 void copy(const JHead& from, Head& to)
197 {
198 using namespace std;
199 using namespace JPP;
200
201 to = Head();
202
204
205 JRootWritableClass cls(from);
206
207 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
208
209 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
210 if (!JRoot::is_static(*p)) {
211 if (from.find(p->GetName()) != from.end() ||
212 cls.get(*p) == JRootClass(&JHead::start_run) ||
213 cls.get(*p) == JRootClass(&JHead::end_event)) {
214 writer.put(p->GetName(), cls.get(*p), true);
215 }
216 }
217 }
218
219 // copy pending data
220
221 for (JHead::const_iterator i = from.begin(); i != from.end(); ++i) {
222 if (to.find(i->first) == to.end()) {
223 to.insert(*i);
224 }
225 }
226 }
227}
JNET::JTag getTag(JLANG::JType< KM3NETDAQ::JDAQTimeslice >)
Definition JDAQTags.hh:94
ASCII I/O of objects with ROOT dictionary.
static JRootDictionary & getInstance()
Get reference to unique instance of this class object.
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:1601
std::ostream & write(std::ostream &out) const
Write header to output.
Definition JHead.cc:86
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:117
JAANET::end_event end_event
Definition JHead.hh:1631
static JLANG::JEquationParameters & getEquationParameters()
Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
Definition JHead.hh:1660
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.
const char * c_str() const
C-string.
Definition JTag.hh:201
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:157
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
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.
virtual JRootWriter & put(const JEquation &equation) override
Write equation.
End of event record.
Definition JHead.hh:1213
bool is_valid() const
Check validity of this addressable class.
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
JRootAddressableClass get(const TDataMember &object) const
Get addressable class of given data member.
Auxiliary class to manage access to base classes and data members of ROOT class.
Definition JRootClass.hh:44
TClass * getClass() const
Get class.
ROOT class for reading into object.
ROOT class for writing from object.