CFEL - ASG Software Suite  2.5.0
CASS
xtciterator.hpp
Go to the documentation of this file.
1 //Copyright (C) 2010-2014 Lutz Foucar
2 
3 /**
4  * @file xtciterator.hpp file contains iterator to iterate through a xtc datagram
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef _XTCITERATOR_H_
10 #define _XTCITERATOR_H_
11 
12 #include <map>
13 #include <iostream>
14 #include <string>
15 
16 #include "format_converter.h"
17 #include "conversion_backend.h"
18 #include "log.h"
19 
20 #include "pdsdata/xtc/XtcIterator.hh"
21 #include "pdsdata/xtc/Xtc.hh"
22 #include "pdsdata/compress/CompressedXtc.hh"
23 #include "pdsdata/xtc/BldInfo.hh"
24 #include "pdsdata/xtc/DetInfo.hh"
25 
26 namespace cass
27 {
28 /** overwrite the destruction of xtc pointers */
29 static void Destroy(Xtc*) {}
30 
31 /** Iteration over XTC's.
32  *
33  * class that will iterate over an xtc using the xtciterator
34  * provided by the lcls libary
35  *
36  * @author Lutz Foucar
37  */
38 class XtcIterator : public Pds::XtcIterator
39 {
40 public:
41  /** enum for more convenient code*/
42  enum {Stop, Continue};
43 
44  /** constructor.
45  *
46  * @param xtc the xtc which contents we iterate over
47  * @param converters the map that contains the used converters
48  * @param cassevent our event to write the information from the xtc to
49  * @param depth The Depth of recursion when called recursivly
50  */
51  XtcIterator(Pds::Xtc* xtc,
53  CASSEvent *cassevent,
54  unsigned depth)
55  : Pds::XtcIterator(xtc),
56  _depth(depth),
57  _converters(converters),
58  _cassevent(cassevent)
59  {}
60 
61  /** @overload
62  * function that is called for each xtc found in the xtc
63  *
64  * will check whether its an id or another xtc. if its another xtc it will
65  * call this with increased recursion depth, otherwise it will call the
66  * format converter for the id.
67  */
68  int process(Pds::Xtc* xtc_orig)
69  {
70  /** output information about the xtc */
71  using std::string;
72  Log::add(Log::DEBUG4,string("XTC Type '") + TypeId::name(xtc_orig->contains.id()) + "'(" + toString(xtc_orig->contains.id()) + ")");
73  Log::add(Log::DEBUG4,string("XTC Version '") + toString(xtc_orig->contains.version()) + "'");
74  Log::add(Log::DEBUG4,string("XTC Compressed '") + (xtc_orig->contains.compressed() ? "true":"false") + "'");
75  Log::add(Log::DEBUG4,string("XTC CompressedVersion '") + toString(xtc_orig->contains.compressed_version()) + "'");
76  Log::add(Log::DEBUG4,string("XTC Damage value '") + toString(xtc_orig->damage.value()) + "'");
77  Log::add(Log::DEBUG4,string("XTC Level '") + Level::name(xtc_orig->src.level()) + "'(" + toString(xtc_orig->src.level()) + ")");
78  switch (xtc_orig->src.level())
79  {
80  case Level::Source :
81  Log::add(Log::DEBUG4,string("XTC DetInfo: ") + DetInfo::name(reinterpret_cast<const DetInfo&>(xtc_orig->src)));
82  break;
83  case Level::Reporter :
84  Log::add(Log::DEBUG4,string("XTC BldInfo: ") + BldInfo::name(reinterpret_cast<const BldInfo&>(xtc_orig->src)));
85  break;
86  default :
87  Log::add(Log::DEBUG4,string("XTC Proc '") + toString(xtc_orig->src.log()) + ":" + toString(xtc_orig->src.phy()) + "'");
88  break;
89  }
90 
91  /** if it is another xtc, then iterate through it */
92  if (xtc_orig->contains.id() == Pds::TypeId::Id_Xtc)
93  {
94  if (!iterate(xtc_orig))
95  return Stop;
96  }
97  /** otherwise use the responsible format converter for this xtc */
98  else
99  {
100  /** need to check wether the xtc is compressed */
101  std::tr1::shared_ptr<Xtc> xtc = xtc_orig->contains.compressed() ?
102  Pds::CompressedXtc::uncompress(*xtc_orig) :
103  std::tr1::shared_ptr<Xtc>(xtc_orig,Destroy);
104 
105  /** check if the datagram is a known xtc, if not skip this datagram and
106  * continue with the next one.
107  */
108  uint32_t damage = xtc->damage.value();
109  if (xtc->contains.id() >= Pds::TypeId::NumberOf)
110  {
111  Log::add(Log::WARNING, toString(xtc->contains.id()) +
112  " is an unkown xtc id.");
113  return Continue;
114  }
115  /** if it is known check the damage state and only continue when its a
116  * user defined damage value, as it will mark only a broken beamline data
117  */
118  else if (damage)
119  {
120  Log::add(Log::VERBOSEINFO,std::string(Pds::TypeId::name(xtc->contains.id())) +
121  " is damaged: " + toString(xtc->damage.value()) );
122  if (damage & ( 0x1 << Pds::Damage::DroppedContribution))
123  {
124  Log::add(Log::ERROR,"'" + std::string(Pds::TypeId::name(xtc->contains.id())) +
125  "' is damaged with '"+ toString(xtc->damage.value()) +
126  "'(dropped Contribution). Skipping Event");
127  return Stop;
128  }
129  else if(damage & (0x1 <<Pds::Damage::UserDefined))
130  {
131  (*_converters[xtc->contains.id()])(xtc.get(),_cassevent);
132  }
133  else
134  {
135  Log::add(Log::ERROR,"'" + std::string(Pds::TypeId::name(xtc->contains.id())) +
136  "' is damaged with '"+ toString(xtc->damage.value()) +
137  "' (unkown Damage). Skipping Event");
138  return Stop;
139  }
140  }
141  /** in all other cases just use the appropriate converter for the datagram */
142  else
143  {
144  (*_converters[xtc->contains.id()])(xtc.get(),_cassevent);
145  }
146  }
147  return Continue;
148  }
149 
150 private:
151  /** counts the recursivness of this */
152  unsigned _depth;
153 
154  /** reference to the converters */
156 
157  /** pointer to the cassevent to work on */
159 };
160 }//end namespace cass
161 
162 #endif // XTCITERATOR_H
Event to store all LCLS Data.
Definition: cass_event.h:32
static void Destroy(Xtc *)
overwrite the destruction of xtc pointers
Definition: xtciterator.hpp:29
file contains declaration of the container for all format converters
file contains base class for all format converters
int process(Pds::Xtc *xtc_orig)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: xtciterator.hpp:68
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
unsigned _depth
counts the recursivness of this
Iteration over XTC's.
Definition: xtciterator.hpp:38
CASSEvent * _cassevent
pointer to the cassevent to work on
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
bool iterate(const msgpack::object &obj, int depth, extractmap_t &emap, string acckey="")
Definition: zmq_input.cpp:132
XtcIterator(Pds::Xtc *xtc, FormatConverter::usedConverters_t &converters, CASSEvent *cassevent, unsigned depth)
constructor.
Definition: xtciterator.hpp:51
contains a logger for cass
std::map< Pds::TypeId::Type, ConversionBackend::shared_pointer > usedConverters_t
typdef describing the map of used converters for easier readable code
FormatConverter::usedConverters_t & _converters
reference to the converters