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@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MDataMember
|
---|
28 | //
|
---|
29 | // This objects corresponds to the data member of another object.
|
---|
30 | // You can either specify the object as a string, eg "MHillas.fWidth"
|
---|
31 | // where MHillas is the name of the container in the parameterlist
|
---|
32 | // and fWidth is it's data member, or you can specify it by giving
|
---|
33 | // the pointer corresponding to the instance of your object and
|
---|
34 | // a TMethodCall pointer corresponding to the Member function returning
|
---|
35 | // the requested value.
|
---|
36 | //
|
---|
37 | /////////////////////////////////////////////////////////////////////////////
|
---|
38 | #include "MDataMember.h"
|
---|
39 |
|
---|
40 | #include <fstream>
|
---|
41 |
|
---|
42 | #include <TMethodCall.h>
|
---|
43 |
|
---|
44 | #include "MLog.h"
|
---|
45 | #include "MLogManip.h"
|
---|
46 |
|
---|
47 | #include "MParList.h"
|
---|
48 |
|
---|
49 | ClassImp(MDataMember);
|
---|
50 |
|
---|
51 | using namespace std;
|
---|
52 |
|
---|
53 | // --------------------------------------------------------------------------
|
---|
54 | //
|
---|
55 | // obj is a pointer to the instance of your class from which the data
|
---|
56 | // should be requested. TMethodCall (s. root dox) is a pointer
|
---|
57 | // to a TMethodCall object which should be the getter function for
|
---|
58 | // the data you want to get.
|
---|
59 | //
|
---|
60 | MDataMember::MDataMember(MParContainer *obj, TMethodCall *call)
|
---|
61 | {
|
---|
62 | fObject = obj;
|
---|
63 | fCall = call;
|
---|
64 |
|
---|
65 | if (fCall->ReturnType()==TMethodCall::kLong)
|
---|
66 | SetBit(kIsInt);
|
---|
67 | else
|
---|
68 | ResetBit(kIsInt);
|
---|
69 |
|
---|
70 | fDataMember = (TString)obj->GetName() + "." + call->GetName();
|
---|
71 | }
|
---|
72 |
|
---|
73 | // --------------------------------------------------------------------------
|
---|
74 | //
|
---|
75 | // obj is a pointer to the instance of your class from which the data
|
---|
76 | // should be requested. TMethodCall (s. root dox) is a pointer
|
---|
77 | // to a TMethodCall object which should be the getter function for
|
---|
78 | // the data you want to get.
|
---|
79 | //
|
---|
80 | MDataMember::MDataMember(MParContainer *obj, const TString call)
|
---|
81 | {
|
---|
82 | fObject = obj;
|
---|
83 | fCall = obj->GetterMethod(call);
|
---|
84 |
|
---|
85 | if (fCall->ReturnType()==TMethodCall::kLong)
|
---|
86 | SetBit(kIsInt);
|
---|
87 | else
|
---|
88 | ResetBit(kIsInt);
|
---|
89 |
|
---|
90 | fDataMember = (TString)obj->GetName() + "." + call;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // --------------------------------------------------------------------------
|
---|
94 | //
|
---|
95 | // Check if accessing fCall is safe
|
---|
96 | //
|
---|
97 | Bool_t MDataMember::CheckGet() const
|
---|
98 | {
|
---|
99 | if (!fObject)
|
---|
100 | {
|
---|
101 | *fLog << err << "ERROR - MDataMember::Get: fObject not initialized ";
|
---|
102 | *fLog << "(not PreProcessed)... returning 0." << endl;
|
---|
103 | return kFALSE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (!fCall)
|
---|
107 | {
|
---|
108 | *fLog << err << "ERROR - MDataMemberGet: No TMethodCall for " << fDataMember << " of ";
|
---|
109 | *fLog << fObject->GetName() << " available... returning 0." << endl;
|
---|
110 | return kFALSE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return kTRUE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // --------------------------------------------------------------------------
|
---|
117 | //
|
---|
118 | // Returns the value you requested
|
---|
119 | //
|
---|
120 | Double_t MDataMember::GetValue() const
|
---|
121 | {
|
---|
122 | if (!CheckGet())
|
---|
123 | return 0;
|
---|
124 |
|
---|
125 | switch (fCall->ReturnType())
|
---|
126 | {
|
---|
127 | case TMethodCall::kLong:
|
---|
128 | Long_t l;
|
---|
129 | fCall->Execute(fObject, l);
|
---|
130 | return (Double_t)l;
|
---|
131 |
|
---|
132 | case TMethodCall::kDouble:
|
---|
133 | Double_t v;
|
---|
134 | fCall->Execute(fObject, v);
|
---|
135 | return v;
|
---|
136 |
|
---|
137 | default:
|
---|
138 | *fLog << err << "DataMember " << fDataMember << " of ";
|
---|
139 | *fLog << fObject->GetName() << " neither int nor float... returning 0." << endl;
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | // Returns the string you requested
|
---|
147 | //
|
---|
148 | const char *MDataMember::GetString() const
|
---|
149 | {
|
---|
150 | if (!CheckGet())
|
---|
151 | return NULL;
|
---|
152 |
|
---|
153 | switch (fCall->ReturnType())
|
---|
154 | {
|
---|
155 | case TMethodCall::kString:
|
---|
156 | {
|
---|
157 | char *c=NULL;
|
---|
158 | fCall->Execute(fObject, &c);
|
---|
159 | return c;
|
---|
160 | }
|
---|
161 |
|
---|
162 | default:
|
---|
163 | *fLog << err << "DataMember " << fDataMember << " of ";
|
---|
164 | *fLog << fObject->GetName() << " not a char*... returning NULL." << endl;
|
---|
165 | return NULL;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | // --------------------------------------------------------------------------
|
---|
170 | //
|
---|
171 | // If a string was given PreProcess try to resolv the object name and
|
---|
172 | // tries to get it from the parlist. And also tries to resolve
|
---|
173 | // the data member (variable) name you requested and tries to get a
|
---|
174 | // corresponding TMethodCall from the root Dictionary.
|
---|
175 | // Remark: If your Data Member is called fDataMember the corresponding
|
---|
176 | // getter Method in your class must be calles fDataMember
|
---|
177 | //
|
---|
178 | Bool_t MDataMember::PreProcess(const MParList *plist)
|
---|
179 | {
|
---|
180 | // The following change has to be checked :
|
---|
181 | // In order that fCall is initialized properly
|
---|
182 | // the original statement "if (fCall)" is replaced by the statement
|
---|
183 | if (fCall && !fObject)
|
---|
184 | return kTRUE;
|
---|
185 |
|
---|
186 | TString cname(fDataMember);
|
---|
187 | TString mname(fDataMember);
|
---|
188 |
|
---|
189 | const char *dot = strrchr(cname, '.');
|
---|
190 |
|
---|
191 | if (dot)
|
---|
192 | {
|
---|
193 | const int pos = dot-cname;
|
---|
194 |
|
---|
195 | cname.Remove(pos);
|
---|
196 | mname.Remove(0, pos+1);
|
---|
197 | }
|
---|
198 |
|
---|
199 | fObject = (MParContainer*)plist->FindObject(cname);
|
---|
200 | if (!fObject)
|
---|
201 | {
|
---|
202 | *fLog << err << "Object '" << cname << "' not in parameter list... aborting." << endl;
|
---|
203 | return kFALSE;
|
---|
204 | }
|
---|
205 |
|
---|
206 | fCall = fObject->GetterMethod(mname);
|
---|
207 |
|
---|
208 | return fCall ? kTRUE : kFALSE;
|
---|
209 | }
|
---|
210 |
|
---|
211 | // --------------------------------------------------------------------------
|
---|
212 | //
|
---|
213 | // Returns the ready-to-save flag of the data member container
|
---|
214 | //
|
---|
215 | Bool_t MDataMember::IsReadyToSave() const
|
---|
216 | {
|
---|
217 | return IsValid() ? fObject->IsReadyToSave() : kFALSE;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // --------------------------------------------------------------------------
|
---|
221 | //
|
---|
222 | // Print the name of the data member without an CR.
|
---|
223 | //
|
---|
224 | /*
|
---|
225 | void MDataMember::Print(Option_t *opt) const
|
---|
226 | {
|
---|
227 | *fLog << fName << flush;
|
---|
228 | }
|
---|
229 | */
|
---|
230 |
|
---|
231 | // --------------------------------------------------------------------------
|
---|
232 | //
|
---|
233 | // Builds a rule which can be used in a MDataPhrase to describe this object
|
---|
234 | //
|
---|
235 | TString MDataMember::GetRule() const
|
---|
236 | {
|
---|
237 | return fDataMember;
|
---|
238 | }
|
---|
239 |
|
---|
240 | // --------------------------------------------------------------------------
|
---|
241 | //
|
---|
242 | // Returns the data member.
|
---|
243 | // This is mainly used in MTask::AddToBranchList
|
---|
244 | //
|
---|
245 | TString MDataMember::GetDataMember() const
|
---|
246 | {
|
---|
247 | return fDataMember;
|
---|
248 | }
|
---|