Jpp
 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 
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 
18 
19 /**
20  * \author mdejong
21  */
22 namespace JAANET {
23 
24  /**
25  * Applications.
26  */
27  const std::string JHead::GENHEN = "GENHEN";
28  const std::string JHead::GENIE = "GENIE";
29  const std::string JHead::GSEAGEN = "gSeaGen";
30  const std::string JHead::MUPAGE = "HEMAS-DPM";
31  const std::string JHead::JSIRENE = "JSirene";
32  const std::string JHead::KM3 = "KM3";
33  const std::string JHead::KM3SIM = "KM3Sim";
34 
35 
36  /**
37  * Read header from input.
38  *
39  * \param in input stream
40  * \return input stream
41  */
42  std::istream& JHead::read(std::istream& in)
43  {
44  using namespace std;
45  using namespace JPP;
46 
47  JStringStream is(in);
48 
49  if (getFileStatus(is.str().c_str())) {
50  is.load();
51  }
52 
54 
55  JRootReadableClass cls(*this);
56 
57  for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) {
58 
59  JRedirectString redirect(reader, equation.getValue());
60 
61  const JRootReadableClass abc = cls.find(equation.getKey().c_str());
62 
63  if (abc.is_valid()) {
64  reader.getObject(abc);
65  }
66 
67  (*this)[equation.getKey()] = equation.getValue();
68  }
69 
70  return in;
71  }
72 
73 
74  /**
75  * Write header to output.
76  *
77  * \param out output stream
78  * \return output stream
79  */
80  std::ostream& JHead::write(std::ostream& out) const
81  {
82  using namespace std;
83  using namespace JPP;
84 
85  JRootWriter writer(out, JHead::getEquationParameters(), JAAnetDictionary::getInstance());
86 
87  JRootWritableClass cls(*this);
88 
89  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
90 
91  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
92  if (!JRootClass::is_static(*p)) {
93  if (this->find(p->GetName()) != this->end() ||
94  cls.get(*p) == JRootClass(&JHead::start_run) ||
95  cls.get(*p) == JRootClass(&JHead::end_event)) {
96  writer.put(p->GetName(), cls.get(*p), true);
97  }
98  }
99  }
100 
101  return out << flush;
102  }
103 
104 
105  /**
106  * Print header to output.
107  *
108  * \param out output stream
109  * \return output stream
110  */
111  std::ostream& JHead::print(std::ostream& out) const
112  {
113  using namespace std;
114  using namespace JPP;
115 
117 
118  JRootWriter writer(out, parameters, JAAnetDictionary::getInstance());
119 
120  JRootWritableClass cls(*this);
121 
122  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
123 
125 
126  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
127  if (!JRootClass::is_static(*p)) {
128  if (cls.get(*p) != JRootClass(&JHead::end_event))
129  writer.put(p->GetName(), cls.get(*p), true);
130  else
131  end_event = make_pair(p->GetName(), cls.get(*p));
132  }
133  }
134 
135  for (JHead::const_iterator i = this->begin(); i != this->end(); ++i) {
136  if (!cls.find(i->first.c_str()).is_valid()) {
137  out << i->first << parameters.getDefaultSeparator() << parameters.getDefaultWhiteSpace() << i->second << parameters.getDefaultEndOfLine();
138  }
139  }
140 
141  writer.put(end_event.first, end_event.second, true);
142 
143  return out << flush;
144  }
145 
146 
147  /**
148  * Copy header from <tt>from</tt> to <tt>to</tt>.
149  *
150  * \param from header
151  * \param to header
152  */
153  void copy(const Head& from, JHead& to)
154  {
155  using namespace std;
156  using namespace JPP;
157 
159  JRootReadableClass cls(to);
160 
161  for (Head::const_iterator i = from.begin(); i != from.end(); ++i) {
162 
163  const JRootReadableClass& abc = cls.find(getTag(i->first).c_str());
164 
165  const string buffer = trim(i->second);
166 
167  if (abc.is_valid() && buffer != "") {
168 
169  JRedirectString redirect(reader, buffer);
170 
171  reader.getObject(abc);
172 
173  if (i->first == getTag(i->first)) {
174  to.insert(*i); // keep track of parsed tags
175  }
176 
177  } else {
178 
179  //to.insert(*i); // store data of unknown tags
180  }
181  }
182  }
183 
184 
185  /**
186  * Copy header from <tt>from</tt> to <tt>to</tt>.
187  *
188  * \param from header
189  * \param to header
190  */
191  void copy(const JHead& from, Head& to)
192  {
193  using namespace std;
194 
195  to = Head();
196 
197  stringstream io;
198 
199  from.write(io);
200 
201  read(to, io);
202 
203  // copy pending data
204 
205  for (JHead::const_iterator i = from.begin(); i != from.end(); ++i) {
206  if (to.find(i->first) == to.end()) {
207  to.insert(*i);
208  }
209  }
210  }
211 }
static const std::string GSEAGEN
Definition: JHead.hh:844
static const std::string KM3
Definition: JHead.hh:847
static const std::string KM3SIM
Definition: JHead.hh:848
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Simple data structure to support I/O of equations (see class JLANG::JEquation).
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:1107
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:1119
static const std::string MUPAGE
Definition: JHead.hh:845
std::istream & read(std::istream &in)
Read header from input.
Definition: JHead.cc:42
static const std::string GENIE
Definition: JHead.hh:843
JAANET::start_run start_run
Definition: JHead.hh:1083
std::ostream & print(std::ostream &out) const
Print header to output.
Definition: JHead.cc:111
static const std::string JSIRENE
Definition: JHead.hh:846
Monte Carlo run header.
Definition: JHead.hh:836
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:66
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:153
static const std::string GENHEN
Generators.
Definition: JHead.hh:842
End of event record.
Definition: JHead.hh:815
bool is_valid(T JHead::*pd) const
Check validity of given data member in Head.
Definition: JHead.hh:918
std::string getTag(const std::string &tag)
Get tag without aanet extension &quot;_&lt;counter&gt;&quot; for identical tags.
Definition: JHead.hh:61
esac $JPP_BIN JLogger sh $LOGGER until pgrep JGetMessage</dev/null > dev null
std::ostream & write(std::ostream &out) const
Write header to output.
Definition: JHead.cc:80
File status.