Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootConfReader.cc@ 16747bf

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 16747bf was d7d2da3, checked in by pavel <pavel@…>, 12 years ago

move branches/ModularDelphes to trunk

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