source: trunk/MagicSoft/Mars/mdata/MDataChain.cc@ 1304

Last change on this file since 1304 was 1304, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.1 KB
Line 
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#
43ClassImp(MDataChain);
44
45MDataChain::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... " << flush;
51 if (!(fMember=ParseString(rule, 1)))
52 {
53 *fLog << err << dbginf << "Parsing '" << rule << "' failed." << endl;
54 return;
55 }
56 *fLog << inf << "found: " << flush;
57 fMember->Print();
58 *fLog << endl;
59
60}
61
62// --------------------------------------------------------------------------
63//
64// PreProcesses all members in the list
65//
66Bool_t MDataChain::PreProcess(const MParList *pList)
67{
68 return fMember ? fMember->PreProcess(pList) : kFALSE;
69}
70
71// --------------------------------------------------------------------------
72//
73// Destructor. Delete filters.
74//
75MDataChain::~MDataChain()
76{
77 if (fMember)
78 delete fMember;
79}
80
81// --------------------------------------------------------------------------
82//
83// Returns the number of alphanumeric characters (including '.')
84// in the given string
85//
86Int_t MDataChain::IsAlNum(TString txt)
87{
88 int l = txt.Length();
89 for (int i = 0; i<l; i++)
90 if (!isalnum(txt[i]) && txt[i]!='.')
91 return i;
92
93 return l;
94}
95
96MData *MDataChain::ParseString(TString txt, Int_t level)
97{
98 MData *member0=NULL;
99
100 char type=0;
101 int nlist = 0;
102
103 while (!txt.IsNull())
104 {
105 MData *newmember = NULL;
106
107 txt = txt.Strip(TString::kBoth);
108
109 switch (txt[0])
110 {
111 case '(':
112 {
113 //
114 // Search for the corresponding bracket
115 //
116 Int_t first=1;
117 for (int cnt=0; first<txt.Length(); first++)
118 {
119 if (txt[first]=='(')
120 cnt++;
121 if (txt[first]==')')
122 cnt--;
123
124 if (cnt==-1)
125 break;
126 }
127
128 if (first==txt.Length())
129 {
130 *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
131 if (member0)
132 delete member0;
133 return NULL;
134 }
135
136 //
137 // Make a copy of the 'interieur' and delete the substringä
138 // including the brackets
139 //
140 TString sub = txt(1, first-1);
141 txt.Remove(0, first+1);
142
143 //
144 // Parse the substring
145 //
146 newmember = ParseString(sub, level+1);
147 if (!newmember)
148 {
149 *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
150 if (member0)
151 delete member0;
152 return NULL;
153 }
154 }
155 break;
156
157 case ')':
158 *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
159 if (member0)
160 delete member0;
161 return NULL;
162
163 case '+':
164 case '-':
165 case '*':
166 case '/':
167 if (member0)
168 {
169 //
170 // Check for the type of the conditional
171 //
172 char is = txt[0];
173 txt.Remove(0, 1);
174
175 //
176 // If no filter is available or the available filter
177 // is of a different conditional we have to create a new
178 // filter list with the new conditional
179 //
180 if (!member0->InheritsFrom(MDataMember::Class()) || type!=is)
181 {
182 MDataList *list = new MDataList(is);
183 list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
184
185 list->SetOwner();
186 list->AddToList(member0);
187
188 member0 = list;
189
190 type = is;
191 }
192 continue;
193 }
194
195 *fLog << err << dbginf << "Syntax Error: First argument of condition missing." << endl;
196 if (member0)
197 delete member0;
198 return NULL;
199
200 case '0':
201 case '1':
202 case '2':
203 case '3':
204 case '4':
205 case '5':
206 case '6':
207 case '7':
208 case '8':
209 case '9':
210 {
211 char *end;
212 Double_t num = strtod(txt.Data(), &end);
213 if (!end || txt.Data()==end)
214 {
215 *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
216 if (member0)
217 delete member0;
218 return NULL;
219 }
220
221 txt.Remove(0, end-txt.Data());
222
223 newmember = new MDataValue(num);
224 }
225 break;
226
227 default:
228 int i = IsAlNum(txt);
229
230 if (i==0)
231 {
232 *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
233 if (member0)
234 delete member0;
235 return NULL;
236 }
237
238 TString text = txt(0, i);
239
240 txt.Remove(0, i);
241
242 // *fLog << all << "Creating: '" << text.Data() << "'" << endl;
243
244 newmember = new MDataMember(text.Data());
245 }
246
247 if (!member0)
248 {
249 member0 = newmember;
250 continue;
251 }
252
253 if (!member0->InheritsFrom(MDataList::Class()))
254 continue;
255
256 ((MDataList*)member0)->AddToList(newmember);
257 }
258
259 return member0;
260}
261
262Double_t MDataChain::GetValue() const
263{
264 return fMember ? fMember->GetValue() : 0;
265}
266
267void MDataChain::Print(Option_t *opt = "") const
268{
269 fMember->Print();
270}
Note: See TracBrowser for help on using the repository browser.