Diana Software
LASCIIGlobalWriter.cc
Go to the documentation of this file.
1 #include "LASCIIGlobalWriter.hh"
2 #include "QMatrix.hh"
3 #include "QBaseType.hh"
4 #include "QString.hh"
5 #include "QBool.hh"
6 #include <TClass.h>
7 #include <sstream>
8 
10 
11 using namespace Diana;
12 using std::string;
13 using std::stringstream;
14 using std::vector;
15 
17 {
18 }
19 
21 {
22  Close();
23 }
24 
25 
26 void LASCIIGlobalWriter::SetInt(const std::string& name, int obj)
27 {
28  string begintag = "begin " + name;
29 
30  stringstream ss;
31  ss << obj;
32 
33  fFile->Comment(begintag);
34  fFile->WriteLine(ss.str());
35 }
36 
37 void LASCIIGlobalWriter::SetDouble(const std::string& name, double obj)
38 {
39  string begintag = "begin " + name;
40 
41  stringstream ss;
42 
43  ss.precision(16);
44 
45  ss << obj;
46 
47  fFile->Comment(begintag);
48  fFile->WriteLine(ss.str());
49 }
50 
51 void LASCIIGlobalWriter::SetBool(const std::string& name, bool obj)
52 {
53  string begintag = "begin " + name;
54  string endtag = "end " + name;
55 
56  stringstream ss;
57  ss << obj;
58 
59  fFile->Comment(begintag);
60  fFile->WriteLine(ss.str());
61 }
62 
63 
64 void LASCIIGlobalWriter::SetFloat(const std::string& name, float obj)
65 {
66  string begintag = "begin " + name;
67 
68  stringstream ss;
69  ss << obj;
70 
71  fFile->Comment(begintag);
72  fFile->WriteLine(ss.str());
73 }
74 
75 void LASCIIGlobalWriter::SetString(const std::string& name, const std::string& obj)
76 {
77  string begintag = "begin " + name;
78  string endtag = "end " + name;
79 
80  fFile->Comment(begintag);
81  fFile->WriteLine(obj);
82  fFile->Comment(endtag);
83 }
84 
85 QError LASCIIGlobalWriter::SetQObject(const std::string& name, const std::string& className, const Diana::QObject* obj, const std::string& descr)
86 {
88  if(obj==NULL) {
90  stringstream msg;
91  msg<<"Error while writing object \""<< name <<"\": "<<" null pointer";
92  err.SetDescription(__FILE__,__LINE__,msg.str());
93  return err;
94  }
95 
96  std::string type = className;
97  if(type == "Diana::QVector")
98  {
99  const QVector* qvec = (const QVector*)(obj);
100  SetQVector(name,qvec);
101  }
102  else if(type == "Diana::QVectorC")
103  {
104  const QVectorC* qvec = (const QVectorC*)(obj);
105  SetQVectorC(name,qvec);
106  }
107  else if(type == "Diana::QTime")
108  {
109  const QTime* qtime = (const QTime*)(obj);
110  SetQTime(name,qtime);
111  }
112  else if(type == "Diana::QMatrix")
113  {
114  const QMatrix* qmat = (const QMatrix*)(obj);
115  SetQMatrix(name,qmat,descr);
116  }
117  else if(type == "Diana::QBaseType<int>")
118  {
119  const QInt* qint = (const QInt*)(obj);
120  SetInt(name,*qint);
121  }
122  else if(type == "Diana::QBaseType<float>")
123  {
124  const QFloat* qint = (const QFloat*)(obj);
125  SetFloat(name,*qint);
126  }
127  else if(type == "Diana::QBaseType<double>")
128  {
129  const QDouble* qint = (const QDouble*)(obj);
130  SetDouble(name,*qint);
131  }
132 
133  else if(type == "Diana::QBool")
134  {
135  const QBool* qint = (const QBool*)(obj);
136  SetBool(name,*qint);
137  }
138  else if(type == "Diana::QString")
139  {
140  const QString* qint = (const QString*)(obj);
141  SetString(name,*qint);
142  }
143  else
144  {
145  string begintag = "begin " + name;
146  string endtag = "end " + name;
147 
148  fFile->Comment(begintag);
149  fFile->Comment(type);
150  fFile->Comment("Automatic dump via QObject::WriteOnStream()");
151  std::stringstream str;
152  err = obj->WriteOnStream(str);
153  fFile->WriteString(str.str(), true);
154  fFile->Comment(endtag);
155  }
156  if(err != QERR_SUCCESS) {
157  stringstream msg;
158  msg<<"Error while writing object \""<< name <<"\": "<<err.GetDescription();
159  err.SetDescription(__FILE__,__LINE__,msg.str());
160  }
161 
162  return err;
163 }
164 
165 void LASCIIGlobalWriter::SetQVector(const std::string&name, const QVector* obj)
166 {
167  string begintag = "begin " + name;
168  string endtag = "end " + name;
169 
170  fFile->Comment(begintag);
171  fFile->Comment("QVector");
172  fFile->WriteQVector(*obj, true);
173  fFile->Comment(endtag);
174 
175 }
176 
177 void LASCIIGlobalWriter::SetQVectorC(const std::string&name, const QVectorC* obj)
178 {
179  string begintag = "begin " + name;
180  string endtag = "end " + name;
181 
182  vector<QVector> qvecs;
183 
184  qvecs.push_back(obj->Re());
185  qvecs.push_back(obj->Im());
186 
187  fFile->Comment(begintag);
188  fFile->Comment("QVectorC");
189  fFile->WriteQVectors(qvecs, true);
190  fFile->Comment(endtag);
191 
192 }
193 
194 void LASCIIGlobalWriter::SetQTime(const std::string&name, const QTime* obj)
195 {
196  string begintag = "begin " + name;
197  string endtag = "end " + name;
198  QTime qt = *obj;
199 
200  fFile->Comment(begintag);
201  fFile->Comment("QTime");
202  fFile->WriteQTime(&qt, true);
203  fFile->Comment(endtag);
204 }
205 
206 QError LASCIIGlobalWriter::Open(const std::string& filename, const std::string& opt)
207 {
209  fFile = new QASCII(filename);
210  fFile->WriteString(""); // Clear file. Will overwrite existing data!!
211  if(!fFile->Exists()) {
213  err.SetDescription(__FILE__,__LINE__,filename);
214  delete fFile;
215  fFile = NULL;
216  }
217  return err;
218 }
219 
220 void LASCIIGlobalWriter::SetQMatrix(const std::string&name, const QMatrix* obj, const std::string& descr)
221 {
222  string begintag = "begin " + name;
223  string endtag = "end " + name;
224 
225  vector<QVector> qvecs;
226 
227  int ncol = obj->GetNCol();
228  int nrow = obj->GetNRow();
229 
230  stringstream ss;
231  ss << nrow << " " << ncol;
232 
233  for(int i=0; i<ncol; i++)
234  qvecs.push_back(obj->GetCol(i));
235 
236  fFile->Comment(begintag);
237  fFile->Comment("QMatrix");
238  fFile->Comment(ss.str());
239  if(!descr.empty()) fFile->Comment(descr);
240  fFile->WriteQVectors(qvecs, true);
241  fFile->Comment(endtag);
242 
243 }
244 
246 {
248  delete fFile;
249  fFile = NULL;
250  return err;
251 }
err
Definition: CheckOF.C:114
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
#define REGISTER_GLOBAL_WRITER(clazz, ext)
global writer for txt files
void SetQVector(const std::string &name, const Diana::QVector *obj)
QError Close()
Close file, called by QGlobalWriterDispatcher.
void SetFloat(const std::string &name, float obj)
void SetDouble(const std::string &name, double obj)
void SetInt(const std::string &name, int obj)
void SetQMatrix(const std::string &name, const Diana::QMatrix *obj, const std::string &descr)
void SetQTime(const std::string &name, const Diana::QTime *obj)
void SetString(const std::string &name, const std::string &obj)
QError SetQObject(const std::string &name, const std::string &className, const Diana::QObject *obj, const std::string &descr)
void SetBool(const std::string &name, bool obj)
void SetQVectorC(const std::string &name, const Diana::QVectorC *obj)
QError Open(const std::string &filename, const std::string &opt)
Open file, called by QGlobalWriterDispatcher.
Class for reading and writing ascii text files.
Definition: QASCII.hh:22
bool Exists()
Returns true if the file exists and can be opened.
Definition: QASCII.cc:38
bool WriteLine(std::string line, bool append=true)
Writes a string to a file, terminated with newline.
Definition: QASCII.cc:209
int WriteQVectors(std::vector< Diana::QVector > outputVector, bool append=false)
Writes a vector of QVectors, each QVector in a new column. Warning: QVectors of different lengths can...
Definition: QASCII.cc:159
bool WriteQTime(Diana::QTime *qt, bool append=true)
Writes an object of type QTime.
Definition: QASCII.cc:469
bool Comment(std::string comment)
Writes a comment to a file; begins with '#', terminates with newline.
Definition: QASCII.cc:221
bool WriteString(std::string outputString, bool append=false)
Writes a string to a file.
Definition: QASCII.cc:197
int WriteQVector(Diana::QVector outputVector, bool append=false)
Writes data from a QVector to a file.
Definition: QASCII.cc:138
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
bool wrapped into a QObject
Definition: QBool.hh:17
error class with error type and description
Definition: QError.hh:115
Abstract class for global writers.
Interface for matrices in Diana analysis.
Definition: QMatrix.hh:24
QVector GetCol(UInt_t ncol)
get column
Definition: QMatrix.cc:221
UInt_t GetNRow() const
get number rows
Definition: QMatrix.hh:63
UInt_t GetNCol() const
get number of columns
Definition: QMatrix.hh:68
string wrapped into a QObject
Definition: QString.hh:18
Diana time.
Definition: QTime.hh:17
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...