Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootConfReader.cc@ bd8c884

ImprovedOutputFile Timing dual_readout llp
Last change on this file since bd8c884 was bd8c884, checked in by Pavel Demin <pavel.demin@…>, 9 years ago

add source command to ExRootConfReader

  • Property mode set to 100644
File size: 9.2 KB
Line 
1
2/** \class ExRootConfReader
3 *
4 * Class handling output ROOT tree
5 *
6 * \author P. Demin - UCL, Louvain-la-Neuve
7 *
8 */
9
10#include "ExRootAnalysis/ExRootConfReader.h"
11
12#include "tcl/tcl.h"
13
14#include <libgen.h>
15
16#include <iostream>
17#include <iomanip>
18#include <fstream>
19#include <string>
20#include <stdexcept>
21#include <sstream>
22
23using namespace std;
24
25static Tcl_ObjCmdProc ModuleObjCmdProc;
26static Tcl_ObjCmdProc SourceObjCmdProc;
27
28//------------------------------------------------------------------------------
29
30ExRootConfReader::ExRootConfReader() :
31 fTopDir(0), fTclInterp(0)
32{
33 fTclInterp = Tcl_CreateInterp();
34
35 Tcl_CreateObjCommand(fTclInterp, "module", ModuleObjCmdProc, this, 0);
36 Tcl_CreateObjCommand(fTclInterp, "source", SourceObjCmdProc, this, 0);
37}
38
39//------------------------------------------------------------------------------
40
41ExRootConfReader::~ExRootConfReader()
42{
43 Tcl_DeleteInterp(fTclInterp);
44}
45
46//------------------------------------------------------------------------------
47
48void ExRootConfReader::ReadFile(const char *fileName, bool isTop)
49{
50 stringstream message;
51
52 ifstream inputFileStream(fileName, ios::in | ios::ate);
53 if(!inputFileStream.is_open())
54 {
55 message << "can't open configuration file " << fileName;
56 throw runtime_error(message.str());
57 }
58
59 if(isTop) fTopDir = dirname(const_cast<char *>(fileName));
60
61 int file_length = inputFileStream.tellg();
62 inputFileStream.seekg(0, ios::beg);
63 inputFileStream.clear();
64 char *cmdBuffer = new char[file_length];
65 inputFileStream.read(cmdBuffer, file_length);
66
67 Tcl_Obj *cmdObjPtr = Tcl_NewObj();
68 cmdObjPtr->bytes = cmdBuffer;
69 cmdObjPtr->length = file_length;
70
71 Tcl_IncrRefCount(cmdObjPtr);
72
73 if(Tcl_EvalObj(fTclInterp, cmdObjPtr) != TCL_OK)
74 {
75 message << "can't read configuration file " << fileName << endl;
76 message << Tcl_GetStringResult(fTclInterp);
77 throw runtime_error(message.str());
78 }
79
80 cmdObjPtr->bytes = 0;
81 cmdObjPtr->length = 0;
82
83 Tcl_DecrRefCount(cmdObjPtr);
84
85 delete[] cmdBuffer;
86}
87
88//------------------------------------------------------------------------------
89
90ExRootConfParam ExRootConfReader::GetParam(const char *name)
91{
92 Tcl_Obj *object;
93 Tcl_Obj *variableName = Tcl_NewStringObj(const_cast<char *>(name), -1);
94 object = Tcl_ObjGetVar2(fTclInterp, variableName, 0, TCL_GLOBAL_ONLY);
95 return ExRootConfParam(name, object, fTclInterp);
96}
97
98//------------------------------------------------------------------------------
99
100int ExRootConfReader::GetInt(const char *name, int defaultValue, int index)
101{
102 ExRootConfParam object = GetParam(name);
103 if(index >= 0)
104 {
105 object = object[index];
106 }
107
108 return object.GetInt(defaultValue);
109}
110
111//------------------------------------------------------------------------------
112
113long ExRootConfReader::GetLong(const char *name, long defaultValue, int index)
114{
115 ExRootConfParam object = GetParam(name);
116 if(index >= 0)
117 {
118 object = object[index];
119 }
120
121 return object.GetLong(defaultValue);
122}
123
124//------------------------------------------------------------------------------
125
126double ExRootConfReader::GetDouble(const char *name, double defaultValue, int index)
127{
128 ExRootConfParam object = GetParam(name);
129 if(index >= 0)
130 {
131 object = object[index];
132 }
133
134 return object.GetDouble(defaultValue);
135}
136
137//------------------------------------------------------------------------------
138
139bool ExRootConfReader::GetBool(const char *name, bool defaultValue, int index)
140{
141 ExRootConfParam object = GetParam(name);
142 if(index >= 0)
143 {
144 object = object[index];
145 }
146
147 return object.GetBool(defaultValue);
148}
149
150//------------------------------------------------------------------------------
151
152const char *ExRootConfReader::GetString(const char *name, const char *defaultValue, int index)
153{
154 ExRootConfParam object = GetParam(name);
155 if(index >= 0)
156 {
157 object = object[index];
158 }
159
160 return object.GetString(defaultValue);
161}
162
163//------------------------------------------------------------------------------
164
165void ExRootConfReader::AddModule(const char *className, const char *moduleName)
166{
167 ExRootTaskMap::iterator itMoudles = fModules.find(moduleName);
168
169 if(itMoudles != fModules.end())
170 {
171 cout << "** WARNING: module '" << moduleName << "' is already configured.";
172 cout << " Only first entry will be used." << endl;
173 }
174 else
175 {
176 fModules.insert(make_pair(moduleName, className));
177 cout << left;
178 cout << setw(30) << "** INFO: adding module";
179 cout << setw(25) << className;
180 cout << setw(25) << moduleName << endl;
181 }
182}
183
184//------------------------------------------------------------------------------
185
186int ModuleObjCmdProc(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
187{
188 ExRootConfReader *reader;
189
190 if(objc < 3)
191 {
192 Tcl_WrongNumArgs(interp, 1, objv, "className moduleName ?arg...?");
193 return TCL_ERROR;
194 }
195
196 reader = (ExRootConfReader*) clientData;
197
198 // add module to a list of modules to be created
199
200 reader->AddModule(Tcl_GetStringFromObj(objv[1], 0), Tcl_GetStringFromObj(objv[2], 0));
201
202 if(objc > 3)
203 {
204 Tcl_Obj *object = Tcl_NewListObj(0, 0);
205 Tcl_ListObjAppendElement(interp, object, Tcl_NewStringObj("namespace", -1));
206 Tcl_ListObjAppendElement(interp, object, Tcl_NewStringObj("eval", -1));
207 Tcl_ListObjAppendList(interp, object, Tcl_NewListObj(objc-2, objv+2));
208
209 return Tcl_GlobalEvalObj(interp, object);
210 }
211
212 return TCL_OK;
213}
214
215//------------------------------------------------------------------------------
216
217int SourceObjCmdProc(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
218{
219 const char *fileName;
220 char *fullName;
221 ExRootConfReader *reader;
222 size_t size;
223
224 if(objc != 2)
225 {
226 Tcl_WrongNumArgs(interp, 1, objv, "fileName");
227 return TCL_ERROR;
228 }
229
230 reader = (ExRootConfReader*) clientData;
231 fileName = Tcl_GetStringFromObj(objv[1], 0);
232 size = strlen(reader->GetTopDir()) + strlen(fileName) + 2;
233 fullName = static_cast<char *>(malloc(size));
234 strcpy(fullName, reader->GetTopDir());
235 strcat(fullName, "/");
236 strcat(fullName, fileName);
237 reader->ReadFile(fullName, false);
238
239 return TCL_OK;
240}
241
242//------------------------------------------------------------------------------
243
244ExRootConfParam::ExRootConfParam(const char *name, Tcl_Obj *object, Tcl_Interp *interp) :
245 fName(name), fObject(object), fTclInterp(interp)
246{
247}
248
249//------------------------------------------------------------------------------
250
251int ExRootConfParam::GetInt(int defaultValue)
252{
253 stringstream message;
254 int result = defaultValue;
255 if(fObject && TCL_OK != Tcl_GetIntFromObj(fTclInterp, fObject, &result))
256 {
257 message << "parameter '"<< fName << "' is not an integer." << endl;
258 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
259 throw runtime_error(message.str());
260 }
261 return result;
262}
263
264//------------------------------------------------------------------------------
265
266long ExRootConfParam::GetLong(long defaultValue)
267{
268 stringstream message;
269 long result = defaultValue;
270 if(fObject && TCL_OK != Tcl_GetLongFromObj(fTclInterp, fObject, &result))
271 {
272 message << "parameter '"<< fName << "' is not an long integer." << endl;
273 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
274 throw runtime_error(message.str());
275 }
276 return result;
277}
278
279//------------------------------------------------------------------------------
280
281double ExRootConfParam::GetDouble(double defaultValue)
282{
283 stringstream message;
284 double result = defaultValue;
285 if(fObject && TCL_OK != Tcl_GetDoubleFromObj(fTclInterp, fObject, &result))
286 {
287 message << "parameter '"<< fName << "' is not a number." << endl;
288 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
289 throw runtime_error(message.str());
290 }
291 return result;
292}
293
294//------------------------------------------------------------------------------
295
296bool ExRootConfParam::GetBool(bool defaultValue)
297{
298 stringstream message;
299 int result = defaultValue;
300 if(fObject && TCL_OK != Tcl_GetBooleanFromObj(fTclInterp, fObject, &result))
301 {
302 message << "parameter '"<< fName << "' is not a boolean." << endl;
303 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
304 throw runtime_error(message.str());
305 }
306 return result;
307}
308
309//------------------------------------------------------------------------------
310
311const char *ExRootConfParam::GetString(const char *defaultValue)
312{
313 const char *result = defaultValue;
314 if(fObject) result = Tcl_GetStringFromObj(fObject, 0);
315 return result;
316}
317
318//------------------------------------------------------------------------------
319
320int ExRootConfParam::GetSize()
321{
322 stringstream message;
323 int length = 0;
324 if(fObject && TCL_OK != Tcl_ListObjLength(fTclInterp, fObject, &length))
325 {
326 message << "parameter '"<< fName << "' is not a list." << endl;
327 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
328 throw runtime_error(message.str());
329 }
330 return length;
331}
332
333//------------------------------------------------------------------------------
334
335ExRootConfParam ExRootConfParam::operator[](int index)
336{
337 stringstream message;
338 Tcl_Obj *object = 0;
339 if(fObject && TCL_OK != Tcl_ListObjIndex(fTclInterp, fObject, index, &object))
340 {
341 message << "parameter '"<< fName << "' is not a list." << endl;
342 message << fName << " = " << Tcl_GetStringFromObj(fObject, 0);
343 throw runtime_error(message.str());
344 }
345 return ExRootConfParam(fName, object, fTclInterp);
346}
Note: See TracBrowser for help on using the repository browser.