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