1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 04/2002 <mailto:tbretz@uni-sw.gwdg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2002
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MDataChain
|
---|
28 | //
|
---|
29 | /////////////////////////////////////////////////////////////////////////////
|
---|
30 |
|
---|
31 | #include "MDataChain.h"
|
---|
32 |
|
---|
33 | #include <ctype.h> // isalnum, ...
|
---|
34 | #include <stdlib.h> // strtod, ...
|
---|
35 |
|
---|
36 | #include "MLog.h"
|
---|
37 | #include "MLogManip.h"
|
---|
38 |
|
---|
39 | #include "MDataList.h"
|
---|
40 | #include "MDataValue.h"
|
---|
41 | #include "MDataMember.h"
|
---|
42 | #
|
---|
43 | ClassImp(MDataChain);
|
---|
44 |
|
---|
45 | MDataChain::MDataChain(const char *rule, const char *name, const char *title)
|
---|
46 | {
|
---|
47 | fName = name ? name : "MDataChain";
|
---|
48 | fTitle = title ? title : rule;
|
---|
49 |
|
---|
50 | *fLog << inf << "Trying to resolve rule..." << endl;
|
---|
51 | if (!(fMember=ParseString(rule, 1)))
|
---|
52 | {
|
---|
53 | *fLog << err << dbginf << "Parsing '" << rule << "' failed." << endl;
|
---|
54 | return;
|
---|
55 | }
|
---|
56 |
|
---|
57 | *fLog << inf << endl;
|
---|
58 | *fLog << "Using Filter rule " << fMember->GetName();
|
---|
59 | *fLog << " for " << fName << ":" << endl;
|
---|
60 | fMember->Print();
|
---|
61 | *fLog << endl << endl;
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | // --------------------------------------------------------------------------
|
---|
66 | //
|
---|
67 | // PreProcesses all members in the list
|
---|
68 | //
|
---|
69 | Bool_t MDataChain::PreProcess(const MParList *pList)
|
---|
70 | {
|
---|
71 | return fMember ? fMember->PreProcess(pList) : kFALSE;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // --------------------------------------------------------------------------
|
---|
75 | //
|
---|
76 | // Destructor. Delete filters.
|
---|
77 | //
|
---|
78 | MDataChain::~MDataChain()
|
---|
79 | {
|
---|
80 | if (fMember)
|
---|
81 | delete fMember;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // --------------------------------------------------------------------------
|
---|
85 | //
|
---|
86 | // Returns the number of alphanumeric characters (including '.')
|
---|
87 | // in the given string
|
---|
88 | //
|
---|
89 | Int_t MDataChain::IsAlNum(TString txt)
|
---|
90 | {
|
---|
91 | int l = txt.Length();
|
---|
92 | for (int i = 0; i<l; i++)
|
---|
93 | if (!isalnum(txt[i]) && txt[i]!='.')
|
---|
94 | return i;
|
---|
95 |
|
---|
96 | return l;
|
---|
97 | }
|
---|
98 |
|
---|
99 | MData *MDataChain::ParseString(TString txt, Int_t level)
|
---|
100 | {
|
---|
101 | MData *member0=NULL;
|
---|
102 |
|
---|
103 | char type=0;
|
---|
104 | int nlist = 0;
|
---|
105 |
|
---|
106 | while (!txt.IsNull())
|
---|
107 | {
|
---|
108 | MData *newmember = NULL;
|
---|
109 |
|
---|
110 | txt = txt.Strip(TString::kBoth);
|
---|
111 |
|
---|
112 | switch (txt[0])
|
---|
113 | {
|
---|
114 | case '(':
|
---|
115 | {
|
---|
116 | //
|
---|
117 | // Search for the corresponding bracket
|
---|
118 | //
|
---|
119 | Int_t first=1;
|
---|
120 | for (int cnt=0; first<txt.Length(); first++)
|
---|
121 | {
|
---|
122 | if (txt[first]=='(')
|
---|
123 | cnt++;
|
---|
124 | if (txt[first]==')')
|
---|
125 | cnt--;
|
---|
126 |
|
---|
127 | if (cnt==-1)
|
---|
128 | break;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (first==txt.Length())
|
---|
132 | {
|
---|
133 | *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
|
---|
134 | if (member0)
|
---|
135 | delete member0;
|
---|
136 | return NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | //
|
---|
140 | // Make a copy of the 'interieur' and delete the substringä
|
---|
141 | // including the brackets
|
---|
142 | //
|
---|
143 | TString sub = txt(1, first-1);
|
---|
144 | txt.Remove(0, first+1);
|
---|
145 |
|
---|
146 | //
|
---|
147 | // Parse the substring
|
---|
148 | //
|
---|
149 | newmember = ParseString(sub, level+1);
|
---|
150 | if (!newmember)
|
---|
151 | {
|
---|
152 | *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
|
---|
153 | if (member0)
|
---|
154 | delete member0;
|
---|
155 | return NULL;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | break;
|
---|
159 |
|
---|
160 | case ')':
|
---|
161 | *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
|
---|
162 | if (member0)
|
---|
163 | delete member0;
|
---|
164 | return NULL;
|
---|
165 |
|
---|
166 | case '+':
|
---|
167 | case '-':
|
---|
168 | case '*':
|
---|
169 | case '/':
|
---|
170 | if (member0)
|
---|
171 | {
|
---|
172 | //
|
---|
173 | // Check for the type of the conditional
|
---|
174 | //
|
---|
175 | char is = txt[0];
|
---|
176 | txt.Remove(0, 1);
|
---|
177 |
|
---|
178 | //
|
---|
179 | // If no filter is available or the available filter
|
---|
180 | // is of a different conditional we have to create a new
|
---|
181 | // filter list with the new conditional
|
---|
182 | //
|
---|
183 | if (!member0->InheritsFrom(MDataMember::Class()) || type!=is)
|
---|
184 | {
|
---|
185 | MDataList *list = new MDataList(is);
|
---|
186 | list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
|
---|
187 |
|
---|
188 | list->SetOwner();
|
---|
189 | list->AddToList(member0);
|
---|
190 |
|
---|
191 | member0 = list;
|
---|
192 |
|
---|
193 | type = is;
|
---|
194 | }
|
---|
195 | continue;
|
---|
196 | }
|
---|
197 |
|
---|
198 | *fLog << err << dbginf << "Syntax Error: First argument of condition missing." << endl;
|
---|
199 | if (member0)
|
---|
200 | delete member0;
|
---|
201 | return NULL;
|
---|
202 |
|
---|
203 | case '0':
|
---|
204 | case '1':
|
---|
205 | case '2':
|
---|
206 | case '3':
|
---|
207 | case '4':
|
---|
208 | case '5':
|
---|
209 | case '6':
|
---|
210 | case '7':
|
---|
211 | case '8':
|
---|
212 | case '9':
|
---|
213 | {
|
---|
214 | char *end;
|
---|
215 | Double_t num = strtod(txt.Data(), &end);
|
---|
216 | if (!end || txt.Data()==end)
|
---|
217 | {
|
---|
218 | *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
|
---|
219 | if (member0)
|
---|
220 | delete member0;
|
---|
221 | return NULL;
|
---|
222 | }
|
---|
223 |
|
---|
224 | txt.Remove(0, end-txt.Data());
|
---|
225 |
|
---|
226 | newmember = new MDataValue(num);
|
---|
227 | }
|
---|
228 | break;
|
---|
229 |
|
---|
230 | default:
|
---|
231 | int i = IsAlNum(txt);
|
---|
232 |
|
---|
233 | if (i==0)
|
---|
234 | {
|
---|
235 | *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
|
---|
236 | if (member0)
|
---|
237 | delete member0;
|
---|
238 | return NULL;
|
---|
239 | }
|
---|
240 |
|
---|
241 | TString text = txt(0, i);
|
---|
242 |
|
---|
243 | txt.Remove(0, i);
|
---|
244 |
|
---|
245 | // *fLog << all << "Creating: '" << text.Data() << "'" << endl;
|
---|
246 |
|
---|
247 | newmember = new MDataMember(text.Data());
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (!member0)
|
---|
251 | {
|
---|
252 | member0 = newmember;
|
---|
253 | continue;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (!member0->InheritsFrom(MDataList::Class()))
|
---|
257 | continue;
|
---|
258 |
|
---|
259 | ((MDataList*)member0)->AddToList(newmember);
|
---|
260 | }
|
---|
261 |
|
---|
262 | return member0;
|
---|
263 | }
|
---|
264 |
|
---|
265 | Double_t MDataChain::GetValue() const
|
---|
266 | {
|
---|
267 | return fMember ? fMember->GetValue() : 0;
|
---|
268 | }
|
---|