Jpp test-rotations-new
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
8#include "JSystem/JStat.hh"
11#include "JLang/JNullStream.hh"
12#include "JLang/JLangToolkit.hh"
13#include "JROOT/JRoot.hh"
14#include "JROOT/JRootClass.hh"
16#include "JAAnet/JHead.hh"
17#include "JAAnet/JAAnetDictionary.hh"
18#include "JAAnet/JHeadWriter.hh"
19
20
21/**
22 * \author mdejong
23 */
24namespace JAANET {
25
26 /**
27 * Push all data members to Head.
28 */
30 {
31 using namespace std;
32 using namespace JPP;
33
34 JRootReadableClass cls(*this);
35
36 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
37
38 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
39 if (!JRoot::is_static(*p)) {
40 if (this->count(p->GetName()) == 0) {
41 (*this)[p->GetName()] = "";
42 }
43 }
44 }
45 }
46
47
48 /**
49 * Read header from input.
50 *
51 * \param in input stream
52 * \return input stream
53 */
54 std::istream& JHead::read(std::istream& in)
55 {
56 using namespace std;
57 using namespace JPP;
58
59 JStringStream is(in);
60
61 if (getFileStatus(is.str().c_str())) {
62 is.load();
63 }
64
65 JRootReader reader(is, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
66
67 JRootReadableClass cls(*this);
68
69 for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) {
70
71 JRedirectString redirect(reader, equation.getValue());
72
73 const JRootReadableClass abc = cls.find(equation.getKey().c_str());
74
75 if (abc.is_valid()) {
76 reader.getObject(abc);
77 }
78
79 (*this)[equation.getKey()] = equation.getValue();
80 }
81
82 return in;
83 }
84
85
86 /**
87 * Write header to output.
88 *
89 * \param out output stream
90 * \return output stream
91 */
92 std::ostream& JHead::write(std::ostream& out) const
93 {
94 using namespace std;
95 using namespace JPP;
96
97 JRootWriter writer(out, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
98
99 JRootWritableClass cls(*this);
100
101 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
102
103 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
104 if (!JRoot::is_static(*p)) {
105 if (this->find(p->GetName()) != this->end() ||
106 cls.get(*p) == JRootClass(&JHead::start_run) ||
107 cls.get(*p) == JRootClass(&JHead::end_event)) {
108 writer.put(p->GetName(), cls.get(*p), true);
109 }
110 }
111 }
112
113 return out << flush;
114 }
115
116
117 /**
118 * Print header to output.
119 *
120 * \param out output stream
121 * \return output stream
122 */
123 std::ostream& JHead::print(std::ostream& out) const
124 {
125 using namespace std;
126 using namespace JPP;
127
128 JRootWriter writer(out, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
129
130 JRootWritableClass cls(*this);
131
132 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
133
135
136 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
137 if (!JRoot::is_static(*p)) {
138 if (cls.get(*p) != JRootClass(&JHead::end_event))
139 writer.put(p->GetName(), cls.get(*p), true);
140 else
141 end_event = make_pair(p->GetName(), cls.get(*p));
142 }
143 }
144
145 for (JHead::const_iterator i = this->begin(); i != this->end(); ++i) {
146 if (!cls.find(i->first.c_str()).is_valid()) {
147 writer.put(i->first, i->second);
148 }
149 }
150
151 writer.put(end_event.first, end_event.second, true);
152
153 return out << flush;
154 }
155
156
157 /**
158 * Copy header from <tt>from</tt> to <tt>to</tt>.
159 *
160 * \param from header
161 * \param to header
162 */
163 void copy(const Head& from, JHead& to)
164 {
165 using namespace std;
166 using namespace JPP;
167
168 JRootReader reader(null, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
169
170 JRootReadableClass cls(to);
171
172 for (Head::const_iterator i = from.begin(); i != from.end(); ++i) {
173
174 const JRootReadableClass& abc = cls.find(getTag(i->first).c_str());
175
176 const string buffer = trim(i->second);
177
178 if (abc.is_valid() && buffer != "") {
179
180 JRedirectString redirect(reader, buffer);
181
182 reader.getObject(abc);
183
184 if (i->first == getTag(i->first)) {
185 to.insert(*i); // keep track of parsed tags
186 }
187
188 } else {
189
190 to.insert(*i); // store data of unknown tags
191 }
192 }
193 }
194
195
196 /**
197 * Copy header from <tt>from</tt> to <tt>to</tt>.
198 *
199 * \param from header
200 * \param to header
201 */
202 void copy(const JHead& from, Head& to)
203 {
204 using namespace std;
205 using namespace JPP;
206
207 to = Head();
208
209 JHeadWriter writer(to, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
210
211 JRootWritableClass cls(from);
212
213 unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator());
214
215 for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
216 if (!JRoot::is_static(*p)) {
217 if (from.find(p->GetName()) != from.end() ||
218 cls.get(*p) == JRootClass(&JHead::start_run) ||
219 cls.get(*p) == JRootClass(&JHead::end_event)) {
220 writer.put(p->GetName(), cls.get(*p), true);
221 }
222 }
223 }
224
225 // copy pending data
226
227 for (JHead::const_iterator i = from.begin(); i != from.end(); ++i) {
228 if (to.find(i->first) == to.end()) {
229 to.insert(*i);
230 }
231 }
232 }
233}
JNET::JTag getTag(JLANG::JType< KM3NETDAQ::JDAQTimeslice >)
Definition JDAQTags.hh:94
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:54
JAANET::start_run start_run
Definition JHead.hh:1583
std::ostream & write(std::ostream &out) const
Write header to output.
Definition JHead.cc:92
void push()
Push all data members to Head.
Definition JHead.cc:29
std::ostream & print(std::ostream &out) const
Print header to output.
Definition JHead.cc:123
JAANET::end_event end_event
Definition JHead.hh:1613
static JLANG::JEquationParameters & getEquationParameters()
Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
Definition JHead.hh:1642
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.
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:163
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.