CFEL - ASG Software Suite  2.5.0
CASS
txt_reader.cpp
Go to the documentation of this file.
1 // Copyright (C) 2011 Lutz Foucar
2 
3 /**
4  * @file txt_reader.cpp contains class to read txt ascii files
5  *
6  * @author Lutz Foucar
7  */
8 
9 #include <algorithm>
10 
11 #include <QtCore/QFileInfo>
12 
13 #include "txt_reader.h"
14 
15 #include "cass_event.h"
16 #include "cass_settings.h"
17 #include "machine_device.hpp"
18 #include "cass.h"
19 #include "log.h"
20 
21 using namespace cass;
22 using namespace MachineData;
23 using namespace std;
24 
26  : FileReader("txt")
27 {}
28 
30 {
31  CASSSettings s;
32  s.beginGroup("TxtReader");
34  _delim = s.value("Deliminator",'\t').toInt();
35  _eventIdhead = s.value("EventIdHeader","Event ID 24 bits").toString().toStdString();
36  _linesToSkip = s.value("LinesToSkip",3).toUInt();
37  s.endGroup();
38  s.endGroup();
39 }
40 
41 void TxtReader::readHeaderInfo(std::ifstream &file)
42 {
43  string headerline;
44  string tmp;
45  for (size_t i=0;i<_linesToSkip;++i)
46  getline(file, tmp);
47  _headers.clear();
48  getline(file, headerline);
49  _split(headerline,_headers,_delim);
50  string info("TextReader: '" + _filename +"' contains the following variables:");
51  vector<string>::const_iterator h(_headers.begin()), end(_headers.end());
52  while(h != end)
53  info += "'"+ *h++ + "',";
55 }
56 
57 bool TxtReader::operator ()(ifstream &file, CASSEvent& event)
58 {
59  string line;
60  vector<double> values;
61  while(true)
62  {
63  getline(file, line);
64  values.clear();
65  _split(line,values,_delim);
66  if (!values.empty())
67  break;
68  }
69  if(_headers.size() != values.size())
70  throw runtime_error("TextReader(): In file '" + _filename +
71  "' are not enough values for the amount of values '" + toString(values.size()) +
72  "' suggested by the header '" + toString(_headers.size())+
73  "'. This is the line: " + line);
74 
75  if (event.devices().find(CASSEvent::MachineData) == event.devices().end())
76  throw runtime_error("TextReader():The CASSEvent does not contain a Machine Data Device");
77 
78  Device &md
79  (dynamic_cast<Device&>(*(event.devices()[CASSEvent::MachineData])));
80 
81  vector<double>::const_iterator value(values.begin());
82  vector<string>::const_iterator head(_headers.begin());
83  for (;value != values.end(); ++value, ++head)
84  md.BeamlineData()[*head] = *value;
85 
86  event.id() = md.BeamlineData()[_eventIdhead];
87 
88  return (event.id());
89 }
void loadSettings()
load the settings of the reader
Definition: txt_reader.cpp:29
Event to store all LCLS Data.
Definition: cass_event.h:32
file contains declaration of the CASSEvent
Settings for CASS.
Definition: cass_settings.h:30
STL namespace.
base class for all file readers
Definition: file_reader.h:24
TxtReader()
constructor
Definition: txt_reader.cpp:25
additional info
Splitter _split
a splitter object to split up the lines
Definition: txt_reader.h:70
char _delim
the deliminator by which the values are separated in the ascii file
Definition: txt_reader.h:73
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
fromStdString(const std::string &str)
bool operator()(std::ifstream &file, CASSEvent &event)
read the frms6 file contents put them into cassevent
Definition: txt_reader.cpp:57
Container for all Machine related Data.
devices_t & devices()
setters
Definition: cass_event.h:66
file contains global definitions for project cass
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::vector< std::string > _headers
the value names
Definition: txt_reader.h:67
value(const QString &key, const QVariant &defaultValue=QVariant()
size_t _linesToSkip
how many lines should be skipped before reading the header line
Definition: txt_reader.h:79
std::string _filename
the name of the file that we read the values from
Definition: file_reader.h:92
void readHeaderInfo(std::ifstream &file)
read the file header
Definition: txt_reader.cpp:41
std::string _eventIdhead
the header name under which the event id is stored
Definition: txt_reader.h:76
definitions of a machine device
file contains specialized class that do the settings for cass
contains class to read txt ascii files
contains a logger for cass
check if there is some light in the chamber based upon the GMD value
beginGroup(const QString &prefix)