CFEL - ASG Software Suite  2.5.0
CASS
format_converter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2009 Jochen Kuepper
2 // Copyright (C) 2009, 2010, 2011 Lutz Foucar
3 
4 /**
5  * @file format_converter.cpp file contains definition of the container for all
6  * format converters
7  *
8  * @author Lutz Foucar
9  */
10 
11 #include <iostream>
12 #include <iomanip>
13 #include <algorithm>
14 #include <sstream>
15 #include <tr1/functional>
16 
17 #include <QtCore/QMutexLocker>
18 #include <QtCore/QString>
19 #include <QtCore/QStringList>
20 
21 #include "format_converter.h"
22 
23 #include "cass_event.h"
24 #include "machine_device.hpp"
25 #include "xtciterator.hpp"
26 #include "pdsdata/xtc/Dgram.hh"
27 
28 using namespace std;
29 using namespace cass;
30 
31 // ===============define static members====================
32 FormatConverter::shared_pointer FormatConverter::_instance;
33 QMutex FormatConverter::_mutex;
34 
35 tr1::shared_ptr<FormatConverter> FormatConverter::instance()
36 {
37  QMutexLocker locker(&_mutex);
38  if(!_instance)
39  _instance = tr1::shared_ptr<FormatConverter>(new FormatConverter());
40  return _instance;
41 }
42 //==========================================================
43 
44 
45 
46 FormatConverter::FormatConverter()
47  :_configseen(false)
48 {}
49 
51 {
52  for (int i(Pds::TypeId::Any); i<Pds::TypeId::NumberOf; ++i)
53  _usedConverters[static_cast<Pds::TypeId::Type>(i)] =
55 
57  typedef ConversionBackend::pdstypelist_t pdslist_t;
58  CASSSettings s;
59  s.beginGroup("Converter");
60  QStringList usedConvertersList(s.value("Used").toStringList());
61 
62  QStringList::const_iterator convType(usedConvertersList.begin());
63  QStringList::const_iterator convEnd(usedConvertersList.end());
64  while (convType != convEnd)
65  {
66  const string convertertype((*convType++).toStdString());
67  const csp_t converter(ConversionBackend::instance(convertertype));
68  const pdslist_t &pdsTypeList(converter->pdsTypeList());
69  pdslist_t::const_iterator pdsType(pdsTypeList.begin());
70  pdslist_t::const_iterator pdsEnd(pdsTypeList.end());
71  while (pdsType != pdsEnd)
72  _usedConverters[(*pdsType++)] = converter;
73  }
74 }
75 
78 {
79  /** intialize the return value
80  * (the return value reflects the whether the datagram was a good L1Transition(an event))
81  */
82  bool retval(NoGoodData);
83  /** get the datagram from the cassevent */
84  Pds::Dgram *datagram = reinterpret_cast<Pds::Dgram*>(&cassevent->datagrambuffer().front());
85 
86  Log::add(Log::DEBUG4,"Dgram Transition '"+ string(Pds::TransitionId::name(datagram->seq.service()))+ "'");
87  Log::add(Log::DEBUG4,"DGram Payload size '" + toString(datagram->xtc.sizeofPayload()) + "'");
88  Log::add(Log::DEBUG4,"Dgram Damage value '" + toString(datagram->xtc.damage.value()) + "'");
89  Log::add(Log::DEBUG4,"DGram Clock seconds '" + toString(datagram->seq.clock().seconds()) + "'");
90  Log::add(Log::DEBUG4,"Dgram Fiducials '" + toString(datagram->seq.stamp().fiducials()) + "'");
91 
92  /** if datagram is configuration or an event (L1Accept) then we will iterate through it
93  * otherwise we ignore the datagram
94  */
95  if ((datagram->seq.service() == Pds::TransitionId::Configure) ||
96  (datagram->seq.service() == Pds::TransitionId::L1Accept) ||
97  (datagram->seq.service() == Pds::TransitionId::BeginCalibCycle))
98  {
99  /** when it is a configuration transition then set the flag accordingly */
100  if (datagram->seq.service() == Pds::TransitionId::Configure)
101  _configseen=true;
102  /** if the datagram is an event, create the id from time and fiducial */
103  if (_configseen && datagram->seq.service() == Pds::TransitionId::L1Accept)
104  {
105  /** extract the bunchId from the datagram */
106  uint64_t bunchId = datagram->seq.clock().seconds();
107  bunchId = (bunchId<<32) + static_cast<uint32_t>(datagram->seq.stamp().fiducials()<<8);
108  /** put the id into the cassevent */
109  cassevent->id() = bunchId;
110 
111  /** set the return value to true */
112  retval = GoodData;
113  }
114  /** prepare the cassevent
115  *
116  * @note maybe, when more converters need preparing the data, we will do
117  * it for all loaded converters, which will need another list that
118  * conatins the loaded converters
119  */
120  _usedConverters[Pds::TypeId::Id_Epics]->prepare(cassevent);
121  /** now iterate through the datagram and find the wanted information
122  * if the return value of the iterator is false, then the transition
123  * did not contain all information
124  */
125  XtcIterator iter(&(datagram->xtc),_usedConverters,cassevent,0);
126  retval = iter.iterate() && retval;
127  /** finalize the epics conversion, so get the epics converter and finalize
128  *
129  * @note maybe, when more converters need finalizing the data, we will do it
130  * for all loaded converters, which will need another list that
131  * conatins the loaded converters
132  */
133  _usedConverters[Pds::TypeId::Id_Epics]->finalize(cassevent);
134  }
135  return retval;
136 }
Event to store all LCLS Data.
Definition: cass_event.h:32
file contains iterator to iterate through a xtc datagram
static shared_pointer instance(const std::string &type)
return the requested converter type
file contains declaration of the CASSEvent
Settings for CASS.
Definition: cass_settings.h:30
file contains declaration of the container for all format converters
STL namespace.
Format converter container.
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
bool _configseen
status whether a configure has already been seen
Iteration over XTC's.
Definition: xtciterator.hpp:38
std::tr1::shared_ptr< FormatConverter > shared_pointer
a shared pointer of this type
id_t & id()
setters
Definition: cass_event.h:64
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
std::tr1::shared_ptr< ConversionBackend > shared_pointer
typedef
value(const QString &key, const QVariant &defaultValue=QVariant()
definitions of a machine device
buffer_t & datagrambuffer()
setters
Definition: cass_event.h:65
void loadSettings(size_t what)
function to load the settings for the format converter
usedConverters_t _usedConverters
map that contains all type id's of all known xtc in a transition.
std::list< Pds::TypeId::Type > pdstypelist_t
typedef
bool operator()(CASSEvent *evt)
function to process a datagram and turn it into a cassevent.
beginGroup(const QString &prefix)