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 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2005
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | //////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MParContainer
|
---|
28 | //
|
---|
29 | // The MParContainer class is the base class for all MARS parameter
|
---|
30 | // containers. At the moment it is almost the same than ROOT's TNamed.
|
---|
31 | // A TNamed contains the essential elements (name, title)
|
---|
32 | // to identify a derived object in lists like our MParList or MTaskList.
|
---|
33 | // The main difference is that the name and title isn't stored and read
|
---|
34 | // to and from root files ("//!")
|
---|
35 | //
|
---|
36 | // MParContainer has several enhancements compared to TNamed:
|
---|
37 | // - GetDescriptor(): returns name and class type
|
---|
38 | // - GetUniqueName(): returns a unique name (used in StreamPrimitive)
|
---|
39 | // - SetLogStream(MLog *lg): Set a logging stream to which loggingis stored
|
---|
40 | // - Reset(): Reset content of class in an eventloop
|
---|
41 | // - IsReadyToSave(): The contents are ready to be saved to a file
|
---|
42 | // - IsSavedAsPrimitive(): A unique name for this instance is already
|
---|
43 | // existing
|
---|
44 | // - SetVariables(): Can be overloaded if the containers stores
|
---|
45 | // coefficients (to be used in fits)
|
---|
46 | // - SetDisplay(): Set a display for redirecting graphical output
|
---|
47 | // - GetNames(): Get Name/Title from instance and store it in
|
---|
48 | // a TObjArray (used to store the names of the
|
---|
49 | // conteiners in a file
|
---|
50 | // - SetNames(): vice versa
|
---|
51 | // - ReadEnv(), WriteEnv(): Function which is used for automatical setup
|
---|
52 | // IsEnvDefined() from a TEnv file
|
---|
53 | //
|
---|
54 | //////////////////////////////////////////////////////////////////////////////
|
---|
55 | #include "MParContainer.h"
|
---|
56 |
|
---|
57 | #include <ctype.h> // isdigit
|
---|
58 | #include <fstream> // ofstream
|
---|
59 |
|
---|
60 | #include <TEnv.h> // Env::Lookup
|
---|
61 | #include <TClass.h> // IsA
|
---|
62 | #include <TObjArray.h> // TObjArray
|
---|
63 | #include <TBaseClass.h> // GetClassPointer
|
---|
64 | #include <TMethodCall.h> // TMethodCall, AsciiWrite
|
---|
65 | #include <TDataMember.h> // TDataMember, AsciiWrite
|
---|
66 | #include <TVirtualPad.h> // gPad
|
---|
67 |
|
---|
68 | #include "MString.h"
|
---|
69 |
|
---|
70 | #include "MLog.h"
|
---|
71 | #include "MLogManip.h"
|
---|
72 |
|
---|
73 | TList *gListOfPrimitives; // forard declaration in MParContainer.h
|
---|
74 |
|
---|
75 | #undef DEBUG
|
---|
76 | //#define DEBUG
|
---|
77 |
|
---|
78 | ClassImp(MParContainer);
|
---|
79 |
|
---|
80 | using namespace std;
|
---|
81 |
|
---|
82 | TObjArray MParContainer::fgListMethodCall;
|
---|
83 |
|
---|
84 | MParContainer::MParContainer(const char *name, const char *title) :
|
---|
85 | fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
|
---|
86 | {
|
---|
87 | fgListMethodCall.SetOwner();
|
---|
88 | }
|
---|
89 |
|
---|
90 | MParContainer::MParContainer(const TString &name, const TString &title) :
|
---|
91 | fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
|
---|
92 | {
|
---|
93 | fgListMethodCall.SetOwner();
|
---|
94 | }
|
---|
95 |
|
---|
96 | // --------------------------------------------------------------------------
|
---|
97 | //
|
---|
98 | // MParContainer copy ctor
|
---|
99 | //
|
---|
100 | MParContainer::MParContainer(const MParContainer &named)
|
---|
101 | {
|
---|
102 | fName = named.fName;
|
---|
103 | fTitle = named.fTitle;
|
---|
104 |
|
---|
105 | fLog = named.fLog;
|
---|
106 |
|
---|
107 | fReadyToSave = named.fReadyToSave;
|
---|
108 |
|
---|
109 | fDisplay = named.fDisplay;
|
---|
110 | }
|
---|
111 |
|
---|
112 | MParContainer::~MParContainer()
|
---|
113 | {
|
---|
114 | #ifdef DEBUG
|
---|
115 | if (fName.IsNull() || fName==(TString)"MTime")
|
---|
116 | return;
|
---|
117 |
|
---|
118 | *fLog << all << "Deleting " << GetDescriptor() << endl;
|
---|
119 | if (TestBit(kMustCleanup) && gROOT && gROOT->MustClean())
|
---|
120 | {
|
---|
121 | *fLog << "Recursive Remove..." << flush;
|
---|
122 | if (TestBit(kCanDelete))
|
---|
123 | *fLog << "kCanDelete..." << flush;
|
---|
124 | TIter Next(gROOT->GetListOfCleanups());
|
---|
125 | TObject *o=0;
|
---|
126 | while ((o=Next()))
|
---|
127 | *fLog << dbg << o->GetName() << " [" << o->ClassName() << "]" << endl;
|
---|
128 | *fLog << dbg << "Removing..." << flush;
|
---|
129 | gROOT->GetListOfCleanups()->RecursiveRemove(this);
|
---|
130 | *fLog << "Removed." << endl;
|
---|
131 | }
|
---|
132 | #endif
|
---|
133 | }
|
---|
134 |
|
---|
135 | // --------------------------------------------------------------------------
|
---|
136 | //
|
---|
137 | // MParContainer assignment operator.
|
---|
138 | //
|
---|
139 | MParContainer& MParContainer::operator=(const MParContainer& rhs)
|
---|
140 | {
|
---|
141 | if (this == &rhs)
|
---|
142 | return *this;
|
---|
143 |
|
---|
144 | TObject::operator=(rhs);
|
---|
145 |
|
---|
146 | fName = rhs.fName;
|
---|
147 | fTitle = rhs.fTitle;
|
---|
148 |
|
---|
149 | fLog = rhs.fLog;
|
---|
150 | fReadyToSave = rhs.fReadyToSave;
|
---|
151 |
|
---|
152 | return *this;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // --------------------------------------------------------------------------
|
---|
156 | //
|
---|
157 | // Make a clone of an object using the Streamer facility.
|
---|
158 | // If newname is specified, this will be the name of the new object
|
---|
159 | //
|
---|
160 | TObject *MParContainer::Clone(const char *newname) const
|
---|
161 | {
|
---|
162 |
|
---|
163 | MParContainer *named = (MParContainer*)TObject::Clone();
|
---|
164 | if (newname && strlen(newname)) named->SetName(newname);
|
---|
165 | return named;
|
---|
166 | }
|
---|
167 |
|
---|
168 | // --------------------------------------------------------------------------
|
---|
169 | //
|
---|
170 | // Compare two MParContainer objects. Returns 0 when equal, -1 when this is
|
---|
171 | // smaller and +1 when bigger (like strcmp).
|
---|
172 | //
|
---|
173 | Int_t MParContainer::Compare(const TObject *obj) const
|
---|
174 | {
|
---|
175 | if (this == obj) return 0;
|
---|
176 | return fName.CompareTo(obj->GetName());
|
---|
177 | }
|
---|
178 |
|
---|
179 | // --------------------------------------------------------------------------
|
---|
180 | //
|
---|
181 | // Copy this to obj.
|
---|
182 | //
|
---|
183 | void MParContainer::Copy(TObject &obj)
|
---|
184 | #if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
|
---|
185 | const
|
---|
186 | #endif
|
---|
187 | {
|
---|
188 | MParContainer &cont = (MParContainer&)obj;
|
---|
189 |
|
---|
190 | TObject::Copy(obj);
|
---|
191 |
|
---|
192 | cont.fName = fName;
|
---|
193 | cont.fTitle = fTitle;
|
---|
194 |
|
---|
195 | cont.fLog = fLog;
|
---|
196 | cont.fReadyToSave = fReadyToSave;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // --------------------------------------------------------------------------
|
---|
200 | //
|
---|
201 | // Encode MParContainer into output buffer.
|
---|
202 | //
|
---|
203 | void MParContainer::FillBuffer(char *&buffer)
|
---|
204 | {
|
---|
205 | fName.FillBuffer(buffer);
|
---|
206 | fTitle.FillBuffer(buffer);
|
---|
207 | }
|
---|
208 |
|
---|
209 | // --------------------------------------------------------------------------
|
---|
210 | //
|
---|
211 | // Returns the name of the object. If the name of the object is not the
|
---|
212 | // class name it returns the object name and in []-brackets the class name.
|
---|
213 | //
|
---|
214 | const TString MParContainer::GetDescriptor() const
|
---|
215 | {
|
---|
216 | return GetDescriptor(*this);
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // Returns the name of the object. If the name of the object is not the
|
---|
222 | // class name it returns the object name and in []-brackets the class name.
|
---|
223 | //
|
---|
224 | const TString MParContainer::GetDescriptor(const TObject &o)
|
---|
225 | {
|
---|
226 | //
|
---|
227 | // Because it returns a (const char*) we cannot return a casted
|
---|
228 | // local TString. The pointer would - immediatly after return -
|
---|
229 | // point to a random memory segment, because the TString has gone.
|
---|
230 | //
|
---|
231 | MString desc;
|
---|
232 | desc.Print("%s [%s]", o.GetName(), o.ClassName());
|
---|
233 | return (TString)o.GetName()==o.ClassName() ? o.ClassName() : desc;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // --------------------------------------------------------------------------
|
---|
237 | //
|
---|
238 | // Return a unique name for this container. It is created from
|
---|
239 | // the container name and the unique Id. (This is mostly used
|
---|
240 | // in the StreamPrimitive member functions)
|
---|
241 | //
|
---|
242 | const TString MParContainer::GetUniqueName() const
|
---|
243 | {
|
---|
244 | TString ret = ToLower(fName);
|
---|
245 |
|
---|
246 | if (isdigit(ret[ret.Length()-1]))
|
---|
247 | ret+="_";
|
---|
248 |
|
---|
249 | ret+=GetUniqueID();
|
---|
250 |
|
---|
251 | return ret;
|
---|
252 | }
|
---|
253 |
|
---|
254 | // --------------------------------------------------------------------------
|
---|
255 | //
|
---|
256 | // List MParContainer name and title.
|
---|
257 | //
|
---|
258 | void MParContainer::ls(Option_t *) const
|
---|
259 | {
|
---|
260 | TROOT::IndentLevel();
|
---|
261 | *fLog << all << GetDescriptor() << " " << GetTitle() << ": kCanDelete=";
|
---|
262 | *fLog << Int_t(TestBit(kCanDelete)) << endl;
|
---|
263 | }
|
---|
264 |
|
---|
265 | // --------------------------------------------------------------------------
|
---|
266 | //
|
---|
267 | // Print MParContainer name and title.
|
---|
268 | //
|
---|
269 | void MParContainer::Print(Option_t *) const
|
---|
270 | {
|
---|
271 | *fLog << all << GetDescriptor() << " " << GetTitle() << endl;
|
---|
272 | }
|
---|
273 |
|
---|
274 | // --------------------------------------------------------------------------
|
---|
275 | //
|
---|
276 | // Change (i.e. set) the name of the MParContainer.
|
---|
277 | // WARNING !!
|
---|
278 | // If the object is a member of a THashTable, THashList container
|
---|
279 | // The HashTable must be Rehashed after SetName
|
---|
280 | // For example the list of objects in the current directory is a THashList
|
---|
281 | //
|
---|
282 | void MParContainer::SetName(const char *name)
|
---|
283 | {
|
---|
284 | fName = name;
|
---|
285 | ResetBit(kIsSavedAsPrimitive);
|
---|
286 | if (gPad && TestBit(kMustCleanup)) gPad->Modified();
|
---|
287 | }
|
---|
288 |
|
---|
289 | // --------------------------------------------------------------------------
|
---|
290 | //
|
---|
291 | // Change (i.e. set) all the MParContainer parameters (name and title).
|
---|
292 | // See also WARNING in SetName
|
---|
293 | //
|
---|
294 | void MParContainer::SetObject(const char *name, const char *title)
|
---|
295 | {
|
---|
296 | fName = name;
|
---|
297 | fTitle = title;
|
---|
298 | ResetBit(kIsSavedAsPrimitive);
|
---|
299 | if (gPad && TestBit(kMustCleanup)) gPad->Modified();
|
---|
300 | }
|
---|
301 |
|
---|
302 | // --------------------------------------------------------------------------
|
---|
303 | //
|
---|
304 | // Change (i.e. set) the title of the MParContainer.
|
---|
305 | //
|
---|
306 | void MParContainer::SetTitle(const char *title)
|
---|
307 | {
|
---|
308 | fTitle = title;
|
---|
309 | ResetBit(kIsSavedAsPrimitive);
|
---|
310 | if (gPad && TestBit(kMustCleanup)) gPad->Modified();
|
---|
311 | }
|
---|
312 |
|
---|
313 | // --------------------------------------------------------------------------
|
---|
314 | //
|
---|
315 | // Return size of the MParContainer part of the TObject.
|
---|
316 | //
|
---|
317 | Int_t MParContainer::Sizeof() const
|
---|
318 | {
|
---|
319 | Int_t nbytes = fName.Sizeof() + fTitle.Sizeof();
|
---|
320 | return nbytes;
|
---|
321 | }
|
---|
322 |
|
---|
323 | // --------------------------------------------------------------------------
|
---|
324 | //
|
---|
325 | // If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
|
---|
326 | // container, overload this function.
|
---|
327 | //
|
---|
328 | void MParContainer::AsciiRead(istream &fin)
|
---|
329 | {
|
---|
330 | *fLog << warn << "To use the the ascii input of " << GetName();
|
---|
331 | *fLog << " you have to overload " << ClassName() << "::AsciiRead." << endl;
|
---|
332 | }
|
---|
333 |
|
---|
334 | // --------------------------------------------------------------------------
|
---|
335 | //
|
---|
336 | // Write out a data member given as a TDataMember object to an output stream.
|
---|
337 | //
|
---|
338 | Bool_t MParContainer::WriteDataMember(ostream &out, const TDataMember *member, Double_t scale) const
|
---|
339 | {
|
---|
340 | if (!member)
|
---|
341 | return kFALSE;
|
---|
342 |
|
---|
343 | if (!member->IsPersistent() || member->Property()&kIsStatic)
|
---|
344 | return kFALSE;
|
---|
345 |
|
---|
346 | /*const*/ TMethodCall *call = ((TDataMember*)member)->GetterMethod(); //FIXME: Root
|
---|
347 | if (!call)
|
---|
348 | {
|
---|
349 | *fLog << warn << "Sorry, no getter method found for " << member->GetName() << endl;
|
---|
350 | return kFALSE;
|
---|
351 | }
|
---|
352 |
|
---|
353 | // For debugging: out << member->GetName() << ":";
|
---|
354 |
|
---|
355 | switch (call->ReturnType())
|
---|
356 | {
|
---|
357 | case TMethodCall::kLong:
|
---|
358 | Long_t l;
|
---|
359 | call->Execute((void*)this, l); // FIXME: const, root
|
---|
360 | out << l << " ";
|
---|
361 | return kTRUE;
|
---|
362 |
|
---|
363 | case TMethodCall::kDouble:
|
---|
364 | Double_t d;
|
---|
365 | call->Execute((void*)this, d); // FIXME: const, root
|
---|
366 | out << (scale*d) << " ";
|
---|
367 | return kTRUE;
|
---|
368 |
|
---|
369 | default:
|
---|
370 | //case TMethodCall::kString:
|
---|
371 | //case TMethodCall::kOther:
|
---|
372 | /* someone may want to enhance this? */
|
---|
373 | return kFALSE;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | // --------------------------------------------------------------------------
|
---|
378 | //
|
---|
379 | // Write out a data member given by name to an output stream.
|
---|
380 | //
|
---|
381 | Bool_t MParContainer::WriteDataMember(ostream &out, const char *member, Double_t scale) const
|
---|
382 | {
|
---|
383 | /*const*/ TClass *cls = IsA()->GetBaseDataMember(member);
|
---|
384 | if (!cls)
|
---|
385 | return kFALSE;
|
---|
386 |
|
---|
387 | return WriteDataMember(out, cls->GetDataMember(member), scale);
|
---|
388 | }
|
---|
389 |
|
---|
390 | // --------------------------------------------------------------------------
|
---|
391 | //
|
---|
392 | // Write out a data member from a given TList of TDataMembers.
|
---|
393 | // returns kTRUE when at least one member was successfully written
|
---|
394 | //
|
---|
395 | Bool_t MParContainer::WriteDataMember(ostream &out, const TList *list) const
|
---|
396 | {
|
---|
397 | Bool_t rc = kFALSE;
|
---|
398 |
|
---|
399 | TDataMember *data = NULL;
|
---|
400 |
|
---|
401 | TIter Next(list);
|
---|
402 | while ((data=(TDataMember*)Next()))
|
---|
403 | rc |= WriteDataMember(out, data);
|
---|
404 |
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 | // --------------------------------------------------------------------------
|
---|
409 | //
|
---|
410 | // If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
|
---|
411 | // container, you may overload this function. If you don't overload it
|
---|
412 | // the data member of a class are written to the file in the order of
|
---|
413 | // appearance in the class header (be more specfic: root dictionary)
|
---|
414 | // Only data members which are of integer (Bool_t, Int_t, ...) or
|
---|
415 | // floating point (Float_t, Double_t, ...) type are written.
|
---|
416 | // returns kTRUE when at least one member was successfully written
|
---|
417 | //
|
---|
418 | Bool_t MParContainer::AsciiWrite(ostream &out) const
|
---|
419 | {
|
---|
420 | // *fLog << warn << "To use the the ascii output of " << GetName();
|
---|
421 | // *fLog << " you have to overload " << ClassName() << "::AsciiWrite." << endl;
|
---|
422 |
|
---|
423 | Bool_t rc = WriteDataMember(out, IsA()->GetListOfDataMembers());
|
---|
424 |
|
---|
425 | TIter NextBaseClass(IsA()->GetListOfBases());
|
---|
426 | TBaseClass *base;
|
---|
427 | while ((base = (TBaseClass*) NextBaseClass()))
|
---|
428 | {
|
---|
429 | /*const*/ TClass *cls = base->GetClassPointer();
|
---|
430 |
|
---|
431 | if (!cls)
|
---|
432 | continue;
|
---|
433 |
|
---|
434 | if (cls->GetClassVersion())
|
---|
435 | rc |= WriteDataMember(out, cls->GetListOfDataMembers());
|
---|
436 | }
|
---|
437 |
|
---|
438 | return rc;
|
---|
439 | }
|
---|
440 |
|
---|
441 | // --------------------------------------------------------------------------
|
---|
442 | //
|
---|
443 | // This virtual function is called for all parameter containers which are
|
---|
444 | // found in the parameter list automatically each time the tasklist is
|
---|
445 | // executed.
|
---|
446 | //
|
---|
447 | // By overwriting this function you can invalidate the contents of a
|
---|
448 | // container before each execution of the tasklist:
|
---|
449 | //
|
---|
450 | // For example:
|
---|
451 | // void MHillas::Reset()
|
---|
452 | // {
|
---|
453 | // fWidth = -1;
|
---|
454 | // }
|
---|
455 | //
|
---|
456 | // (While -1 is obviously a impossible value for fWidth you can immediatly
|
---|
457 | // see - if you Print() the contents of this container - that MHillasCalc
|
---|
458 | // has not caluclated the width in this runthrough of the tasklist)
|
---|
459 | //
|
---|
460 | // Overwriting MParConatiner::Reset() in your container makes it
|
---|
461 | // unnecessary to call any Clear() or Reset() manually in your task and
|
---|
462 | // you make sure, that you don't keep results of previous runs of your
|
---|
463 | // tasklist by chance.
|
---|
464 | //
|
---|
465 | // MParContainer::Reset() itself does nothing.
|
---|
466 | //
|
---|
467 | void MParContainer::Reset()
|
---|
468 | {
|
---|
469 | }
|
---|
470 |
|
---|
471 | // --------------------------------------------------------------------------
|
---|
472 | //
|
---|
473 | // Return the pointer to the TClass (from the root dictionary) which
|
---|
474 | // corresponds to the class with name name.
|
---|
475 | //
|
---|
476 | // Make sure, that a new object of this type can be created.
|
---|
477 | //
|
---|
478 | // In case of failure return NULL
|
---|
479 | //
|
---|
480 | TClass *MParContainer::GetConstructor(const char *name) const
|
---|
481 | {
|
---|
482 | //
|
---|
483 | // try to get class from root environment
|
---|
484 | //
|
---|
485 | TClass *cls = gROOT->GetClass(name);
|
---|
486 | Int_t rc = 0;
|
---|
487 | if (!cls)
|
---|
488 | rc =1;
|
---|
489 | else
|
---|
490 | {
|
---|
491 | if (!cls->Property())
|
---|
492 | rc = 5;
|
---|
493 | if (!cls->Size())
|
---|
494 | rc = 4;
|
---|
495 | if (!cls->IsLoaded())
|
---|
496 | rc = 3;
|
---|
497 | if (!cls->HasDefaultConstructor())
|
---|
498 | rc = 2;
|
---|
499 | }
|
---|
500 |
|
---|
501 | if (!rc)
|
---|
502 | return cls;
|
---|
503 |
|
---|
504 | *fLog << err << dbginf << GetDescriptor() << " - Cannot create new instance of class '" << name << "': ";
|
---|
505 | switch (rc)
|
---|
506 | {
|
---|
507 | case 1:
|
---|
508 | *fLog << "gROOT->GetClass() returned NULL." << endl;
|
---|
509 | return NULL;
|
---|
510 | case 2:
|
---|
511 | *fLog << "no default constructor." << endl;
|
---|
512 | return NULL;
|
---|
513 | case 3:
|
---|
514 | *fLog << "not loaded." << endl;
|
---|
515 | return NULL;
|
---|
516 | case 4:
|
---|
517 | *fLog << "zero size." << endl;
|
---|
518 | return NULL;
|
---|
519 | case 5:
|
---|
520 | *fLog << "no property." << endl;
|
---|
521 | return NULL;
|
---|
522 | }
|
---|
523 |
|
---|
524 | *fLog << "rtlprmft." << endl;
|
---|
525 |
|
---|
526 | return NULL;
|
---|
527 | }
|
---|
528 |
|
---|
529 | // --------------------------------------------------------------------------
|
---|
530 | //
|
---|
531 | // Return a new object of class 'name'. Make sure that the object
|
---|
532 | // derives from the class base.
|
---|
533 | //
|
---|
534 | // In case of failure return NULL
|
---|
535 | //
|
---|
536 | // The caller is responsible of deleting the object!
|
---|
537 | //
|
---|
538 | MParContainer *MParContainer::GetNewObject(const char *name, TClass *base) const
|
---|
539 | {
|
---|
540 | return base ? GetNewObject(name, base->GetName()) : 0;
|
---|
541 | }
|
---|
542 |
|
---|
543 | // --------------------------------------------------------------------------
|
---|
544 | //
|
---|
545 | // Return a new object of class 'name'. Make sure that the object
|
---|
546 | // derives from the class base.
|
---|
547 | //
|
---|
548 | // In case of failure return NULL
|
---|
549 | //
|
---|
550 | // The caller is responsible of deleting the object!
|
---|
551 | //
|
---|
552 | MParContainer *MParContainer::GetNewObject(const char *name, const char *base) const
|
---|
553 | {
|
---|
554 | TClass *cls = GetConstructor(name);
|
---|
555 | if (!cls || !base)
|
---|
556 | return NULL;
|
---|
557 |
|
---|
558 | if (!cls->InheritsFrom(base))
|
---|
559 | {
|
---|
560 | *fLog << err;
|
---|
561 | *fLog << dbginf << GetDescriptor() << " - Cannot create new instance of class '" << name << "': ";
|
---|
562 | *fLog << " - Class " << cls->GetName() << " doesn't inherit from " << base << endl;
|
---|
563 | return NULL;
|
---|
564 | }
|
---|
565 |
|
---|
566 | //
|
---|
567 | // create the parameter container of the the given class type
|
---|
568 | //
|
---|
569 | TObject *obj = (TObject*)cls->New();
|
---|
570 | if (!obj)
|
---|
571 | {
|
---|
572 | *fLog << err;
|
---|
573 | *fLog << dbginf << GetDescriptor() << " - Cannot create new instance of class '" << name << "': ";
|
---|
574 | *fLog << " - Class " << cls->GetName() << " has no default constructor." << endl;
|
---|
575 | *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
|
---|
576 | return NULL;
|
---|
577 | }
|
---|
578 |
|
---|
579 | return (MParContainer*)obj;
|
---|
580 | }
|
---|
581 |
|
---|
582 | TMethodCall *MParContainer::GetterMethod(const char *name) const
|
---|
583 | {
|
---|
584 | const TString n(name);
|
---|
585 | const Int_t pos1 = n.First('.');
|
---|
586 |
|
---|
587 | const TString part1 = pos1<0 ? n : n(0, pos1);
|
---|
588 | const TString part2 = pos1<0 ? TString("") : n(pos1+1, n.Length());
|
---|
589 |
|
---|
590 | TClass *cls = IsA()->GetBaseDataMember(part1);
|
---|
591 | if (cls)
|
---|
592 | {
|
---|
593 | TDataMember *member = cls->GetDataMember(part1);
|
---|
594 | if (!member)
|
---|
595 | {
|
---|
596 | *fLog << err << "Datamember '" << part1 << "' not in " << GetDescriptor() << endl;
|
---|
597 | return NULL;
|
---|
598 | }
|
---|
599 |
|
---|
600 | // This handles returning references of contained objects, eg
|
---|
601 | // class X { TObject fO; TObject &GetO() { return fO; } };
|
---|
602 | if (!member->IsBasic() && !part2.IsNull())
|
---|
603 | {
|
---|
604 | cls = gROOT->GetClass(member->GetTypeName());
|
---|
605 | if (!cls)
|
---|
606 | {
|
---|
607 | *fLog << err << "Datamember " << part1 << " [";
|
---|
608 | *fLog << member->GetTypeName() << "] not in dictionary." << endl;
|
---|
609 | return NULL;
|
---|
610 | }
|
---|
611 | if (!cls->InheritsFrom(MParContainer::Class()))
|
---|
612 | {
|
---|
613 | *fLog << err << "Datamember " << part1 << " [";
|
---|
614 | *fLog << member->GetTypeName() << "] does not inherit from ";
|
---|
615 | *fLog << "MParContainer." << endl;
|
---|
616 | return NULL;
|
---|
617 | }
|
---|
618 |
|
---|
619 | const MParContainer *sub = (MParContainer*)((ULong_t)this+member->GetOffset());
|
---|
620 | return sub->GetterMethod(part2);
|
---|
621 | }
|
---|
622 |
|
---|
623 | if (member->IsaPointer())
|
---|
624 | {
|
---|
625 | *fLog << warn << "Data-member " << part1 << " is a pointer..." << endl;
|
---|
626 | *fLog << dbginf << "Not yet implemented!" << endl;
|
---|
627 | //TClass *test = gROOT->GetClass(member->GetTypeName());
|
---|
628 | return 0;
|
---|
629 | }
|
---|
630 |
|
---|
631 | TMethodCall *call = member->GetterMethod();
|
---|
632 | if (call)
|
---|
633 | return call;
|
---|
634 | }
|
---|
635 |
|
---|
636 | *fLog << inf << "No standard access for '" << part1 << "' in ";
|
---|
637 | *fLog << GetDescriptor() << " or one of its base classes." << endl;
|
---|
638 |
|
---|
639 | TMethodCall *call = NULL;
|
---|
640 |
|
---|
641 | *fLog << "Trying to find MethodCall '" << ClassName();
|
---|
642 | *fLog << "::Get" << part1 << "' instead..." << flush;
|
---|
643 | call = new TMethodCall(IsA(), (TString)"Get"+part1, "");
|
---|
644 | if (call->GetMethod())
|
---|
645 | {
|
---|
646 | fgListMethodCall.Add(call);
|
---|
647 | *fLog << "found." << endl;
|
---|
648 | return call;
|
---|
649 | }
|
---|
650 | *fLog << endl;
|
---|
651 |
|
---|
652 | delete call;
|
---|
653 |
|
---|
654 | *fLog << "Trying to find MethodCall '" << ClassName();
|
---|
655 | *fLog << "::" << part1 << "' instead..." << flush;
|
---|
656 | call = new TMethodCall(IsA(), part1, "");
|
---|
657 | if (call->GetMethod())
|
---|
658 | {
|
---|
659 | fgListMethodCall.Add(call);
|
---|
660 | *fLog << "found." << endl;
|
---|
661 | return call;
|
---|
662 | }
|
---|
663 | *fLog << endl;
|
---|
664 |
|
---|
665 | delete call;
|
---|
666 |
|
---|
667 | *fLog << err << "Sorry, no getter method found for " << part1 << endl;
|
---|
668 | return NULL;
|
---|
669 | }
|
---|
670 |
|
---|
671 | // --------------------------------------------------------------------------
|
---|
672 | //
|
---|
673 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
674 | // to a macro. In the original root implementation it is used to write
|
---|
675 | // gui elements to a macro-file.
|
---|
676 | //
|
---|
677 | void MParContainer::SavePrimitive(ofstream &out, Option_t *o)
|
---|
678 | {
|
---|
679 | static UInt_t uid = 0;
|
---|
680 |
|
---|
681 | if (IsSavedAsPrimitive())
|
---|
682 | return;
|
---|
683 |
|
---|
684 | SetUniqueID(uid++);
|
---|
685 | SetBit(kIsSavedAsPrimitive);
|
---|
686 |
|
---|
687 | if (gListOfPrimitives && !gListOfPrimitives->FindObject(this))
|
---|
688 | gListOfPrimitives->Add(this);
|
---|
689 |
|
---|
690 | StreamPrimitive(out);
|
---|
691 | }
|
---|
692 |
|
---|
693 | // --------------------------------------------------------------------------
|
---|
694 | //
|
---|
695 | // Creates the string written by SavePrimitive and returns it.
|
---|
696 | //
|
---|
697 | void MParContainer::StreamPrimitive(ofstream &out) const
|
---|
698 | {
|
---|
699 | out << " // Using MParContainer::StreamPrimitive" << endl;
|
---|
700 | out << " " << ClassName() << " " << GetUniqueName() << "(\"";
|
---|
701 | out << fName << "\", \"" << fTitle << "\");" << endl;
|
---|
702 | }
|
---|
703 |
|
---|
704 | void MParContainer::GetNames(TObjArray &arr) const
|
---|
705 | {
|
---|
706 | arr.AddLast(new TNamed(fName, fTitle));
|
---|
707 | }
|
---|
708 |
|
---|
709 | void MParContainer::SetNames(TObjArray &arr)
|
---|
710 | {
|
---|
711 | TNamed *name = (TNamed*)arr.First();
|
---|
712 |
|
---|
713 | fName = name->GetName();
|
---|
714 | fTitle = name->GetTitle();
|
---|
715 |
|
---|
716 | delete arr.Remove(name);
|
---|
717 | arr.Compress();
|
---|
718 | }
|
---|
719 |
|
---|
720 | // --------------------------------------------------------------------------
|
---|
721 | //
|
---|
722 | // Creates a new instance of this class. The idea is to create a clone of
|
---|
723 | // this class in its initial state.
|
---|
724 | //
|
---|
725 | MParContainer *MParContainer::New() const
|
---|
726 | {
|
---|
727 | return (MParContainer*)IsA()->New();
|
---|
728 | }
|
---|
729 |
|
---|
730 | // --------------------------------------------------------------------------
|
---|
731 | //
|
---|
732 | // Read the contents/setup of a parameter container/task from a TEnv
|
---|
733 | // instance (steering card/setup file).
|
---|
734 | // The key to search for in the file should be of the syntax:
|
---|
735 | // prefix.vname
|
---|
736 | // While vname is a name which is specific for a single setup date
|
---|
737 | // (variable) of this container and prefix is something like:
|
---|
738 | // evtloopname.name
|
---|
739 | // While name is the name of the containers/tasks in the parlist/tasklist
|
---|
740 | //
|
---|
741 | // eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
|
---|
742 | // Job4.MImgCleanStd.CleaningLevel2: 2.5
|
---|
743 | //
|
---|
744 | // If this cannot be found the next step is to search for
|
---|
745 | // MImgCleanStd.CleaningLevel1: 3.0
|
---|
746 | // And if this doesn't exist, too, we should search for:
|
---|
747 | // CleaningLevel1: 3.0
|
---|
748 | //
|
---|
749 | // Warning: The programmer is responsible for the names to be unique in
|
---|
750 | // all Mars classes.
|
---|
751 | //
|
---|
752 | // Return values:
|
---|
753 | // kTRUE: Environment string found
|
---|
754 | // kFALSE: Environment string not found
|
---|
755 | // kERROR: Error occured, eg. environment invalid
|
---|
756 | //
|
---|
757 | // Overload this if you don't want to control the level of setup-string. In
|
---|
758 | // this case ReadEnv gets called with the different possibilities, see TestEnv.
|
---|
759 | //
|
---|
760 | Int_t MParContainer::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
761 | {
|
---|
762 | if (!IsEnvDefined(env, prefix, "", print))
|
---|
763 | return kFALSE;
|
---|
764 |
|
---|
765 | *fLog << warn << "WARNING - " << fName << ": Resource " << prefix << " found, but no " << ClassName() << "::ReadEnv." << endl;
|
---|
766 | return kTRUE;
|
---|
767 | }
|
---|
768 |
|
---|
769 | // --------------------------------------------------------------------------
|
---|
770 | //
|
---|
771 | // Write the contents/setup of a parameter container/task to a TEnv
|
---|
772 | // instance (steering card/setup file).
|
---|
773 | // The key to search for in the file should be of the syntax:
|
---|
774 | // prefix.vname
|
---|
775 | // While vname is a name which is specific for a single setup date
|
---|
776 | // (variable) of this container and prefix is something like:
|
---|
777 | // evtloopname.name
|
---|
778 | // While name is the name of the containers/tasks in the parlist/tasklist
|
---|
779 | //
|
---|
780 | // eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
|
---|
781 | // Job4.MImgCleanStd.CleaningLevel2: 2.5
|
---|
782 | //
|
---|
783 | // If this cannot be found the next step is to search for
|
---|
784 | // MImgCleanStd.CleaningLevel1: 3.0
|
---|
785 | // And if this doesn't exist, too, we should search for:
|
---|
786 | // CleaningLevel1: 3.0
|
---|
787 | //
|
---|
788 | // Warning: The programmer is responsible for the names to be unique in
|
---|
789 | // all Mars classes.
|
---|
790 | //
|
---|
791 | Bool_t MParContainer::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
|
---|
792 | {
|
---|
793 | if (!IsEnvDefined(env, prefix, "", print))
|
---|
794 | return kFALSE;
|
---|
795 |
|
---|
796 | *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but " << ClassName() << "::WriteEnv not overloaded." << endl;
|
---|
797 | return kTRUE;
|
---|
798 | }
|
---|
799 |
|
---|
800 | // --------------------------------------------------------------------------
|
---|
801 | //
|
---|
802 | // Take the prefix and call ReadEnv for:
|
---|
803 | // prefix.containername
|
---|
804 | // prefix.classname
|
---|
805 | // containername
|
---|
806 | // classname
|
---|
807 | //
|
---|
808 | // The existance of an environment variable is done in this order. If
|
---|
809 | // ReadEnv return kTRUE the existance of the container setup is assumed and
|
---|
810 | // the other tests are skipped. If kFALSE is assumed the sequence is
|
---|
811 | // continued. In case of kERROR failing of the setup from a file is assumed.
|
---|
812 | //
|
---|
813 | // Overload this if you want to control the handling of level of setup-string
|
---|
814 | // mentioned above. In this case ReadEnv gets never called if you don't call
|
---|
815 | // it explicitly.
|
---|
816 | //
|
---|
817 | Int_t MParContainer::TestEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
818 | {
|
---|
819 | if (print)
|
---|
820 | *fLog << all << "Testing Prefix+ContName: " << prefix+GetName() << endl;
|
---|
821 | Int_t rc = ReadEnv(env, prefix+GetName(), print);
|
---|
822 | if (rc==kERROR || rc==kTRUE)
|
---|
823 | return rc;
|
---|
824 |
|
---|
825 | // Check For: Job4.MClassName.Varname
|
---|
826 | if (print)
|
---|
827 | *fLog << all << "Testing Prefix+ClasName: " << prefix+ClassName() << endl;
|
---|
828 | rc = ReadEnv(env, prefix+ClassName(), print);
|
---|
829 | if (rc==kERROR || rc==kTRUE)
|
---|
830 | return rc;
|
---|
831 |
|
---|
832 | // Check For: ContainerName.Varname
|
---|
833 | if (print)
|
---|
834 | *fLog << all << "Testing ContName: " << GetName() << endl;
|
---|
835 | rc = ReadEnv(env, GetName(), print);
|
---|
836 | if (rc==kERROR || rc==kTRUE)
|
---|
837 | return rc;
|
---|
838 |
|
---|
839 | // Check For: MClassName.Varname
|
---|
840 | if (print)
|
---|
841 | *fLog << all << "Testing ClassName: " << ClassName() << endl;
|
---|
842 | rc = ReadEnv(env, ClassName(), print);
|
---|
843 | if (rc==kERROR || rc==kTRUE)
|
---|
844 | return rc;
|
---|
845 |
|
---|
846 | // Not found
|
---|
847 | return kFALSE;
|
---|
848 | }
|
---|
849 |
|
---|
850 | Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString prefix, TString postfix, Bool_t print) const
|
---|
851 | {
|
---|
852 | if (!postfix.IsNull())
|
---|
853 | postfix.Insert(0, ".");
|
---|
854 |
|
---|
855 | return IsEnvDefined(env, prefix+postfix, print);
|
---|
856 | }
|
---|
857 |
|
---|
858 | Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString name, Bool_t print) const
|
---|
859 | {
|
---|
860 | if (print)
|
---|
861 | *fLog << all << GetDescriptor() << " - " << name << "... " << flush;
|
---|
862 |
|
---|
863 | if (!((TEnv&)env).Defined(name))
|
---|
864 | {
|
---|
865 | if (print)
|
---|
866 | *fLog << "not found." << endl;
|
---|
867 | return kFALSE;
|
---|
868 | }
|
---|
869 |
|
---|
870 | if (print)
|
---|
871 | *fLog << "found." << endl;
|
---|
872 |
|
---|
873 | return kTRUE;
|
---|
874 | }
|
---|
875 |
|
---|
876 | Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Int_t dflt) const
|
---|
877 | {
|
---|
878 | return GetEnvValue(env, prefix+"."+postfix, dflt);
|
---|
879 | }
|
---|
880 |
|
---|
881 | Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Double_t dflt) const
|
---|
882 | {
|
---|
883 | return GetEnvValue(env, prefix+"."+postfix, dflt);
|
---|
884 | }
|
---|
885 |
|
---|
886 | const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, const char *dflt) const
|
---|
887 | {
|
---|
888 | return GetEnvValue(env, prefix+"."+postfix, dflt);
|
---|
889 | }
|
---|
890 |
|
---|
891 | Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Int_t dflt) const
|
---|
892 | {
|
---|
893 | return ((TEnv&)env).GetValue(prefix, dflt);
|
---|
894 | }
|
---|
895 |
|
---|
896 | Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Double_t dflt) const
|
---|
897 | {
|
---|
898 | return ((TEnv&)env).GetValue(prefix, dflt);
|
---|
899 | }
|
---|
900 |
|
---|
901 | const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, const char *dflt) const
|
---|
902 | {
|
---|
903 | return ((TEnv&)env).GetValue(prefix, dflt);
|
---|
904 | }
|
---|