source: trunk/Mars/mbase/MParContainer.cc@ 14444

Last change on this file since 14444 was 12858, checked in by tbretz, 13 years ago
GetDataMember doesn't return offsets of pointers in root 5.32/00 -- replaced.
File size: 35.7 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 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#include "MStatusDisplay.h"
74
75TList *gListOfPrimitives; // forard declaration in MParContainer.h
76
77#undef DEBUG
78//#define DEBUG
79
80ClassImp(MParContainer);
81
82using namespace std;
83
84TObjArray MParContainer::fgListMethodCall;
85
86MParContainer::MParContainer(const char *name, const char *title) :
87 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
88{
89 fgListMethodCall.SetOwner();
90}
91
92MParContainer::MParContainer(const TString &name, const TString &title) :
93 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
94{
95 fgListMethodCall.SetOwner();
96}
97
98// --------------------------------------------------------------------------
99//
100// MParContainer copy ctor
101//
102MParContainer::MParContainer(const MParContainer &named) : TObject()
103{
104 fName = named.fName;
105 fTitle = named.fTitle;
106
107 fLog = named.fLog;
108
109 fReadyToSave = named.fReadyToSave;
110
111 fDisplay = named.fDisplay;
112}
113
114MParContainer::~MParContainer()
115{
116#ifdef DEBUG
117 if (fName.IsNull() || fName==(TString)"MTime")
118 return;
119
120 *fLog << all << "Deleting " << this << " " << GetDescriptor() << endl;
121 if (TestBit(kMustCleanup) && gROOT && gROOT->MustClean())
122 {
123 *fLog << "Recursive Remove..." << flush;
124 if (TestBit(kCanDelete))
125 *fLog << "kCanDelete..." << flush;
126 TIter Next(gROOT->GetListOfCleanups());
127 TObject *o=0;
128 while ((o=Next()))
129 *fLog << dbg << o->GetName() << " [" << o->ClassName() << "]" << endl;
130 *fLog << dbg << "Removing..." << flush;
131 gROOT->GetListOfCleanups()->RecursiveRemove(this);
132 *fLog << "Removed." << endl;
133 }
134#endif
135}
136
137// --------------------------------------------------------------------------
138//
139// MParContainer assignment operator.
140//
141MParContainer& MParContainer::operator=(const MParContainer& rhs)
142{
143 if (this == &rhs)
144 return *this;
145
146 TObject::operator=(rhs);
147
148 fName = rhs.fName;
149 fTitle = rhs.fTitle;
150
151 fLog = rhs.fLog;
152 fReadyToSave = rhs.fReadyToSave;
153
154 return *this;
155}
156
157// --------------------------------------------------------------------------
158//
159// Make a clone of an object using the Streamer facility.
160// If newname is specified, this will be the name of the new object
161//
162TObject *MParContainer::Clone(const char *newname) const
163{
164
165 MParContainer *named = (MParContainer*)TObject::Clone();
166 if (newname && strlen(newname)) named->SetName(newname);
167 return named;
168}
169
170// --------------------------------------------------------------------------
171//
172// Compare two MParContainer objects. Returns 0 when equal, -1 when this is
173// smaller and +1 when bigger (like strcmp).
174//
175Int_t MParContainer::Compare(const TObject *obj) const
176{
177 if (this == obj) return 0;
178 return fName.CompareTo(obj->GetName());
179}
180
181// --------------------------------------------------------------------------
182//
183// Copy this to obj.
184//
185void MParContainer::Copy(TObject &obj)
186#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
187const
188#endif
189{
190 MParContainer &cont = (MParContainer&)obj;
191
192 TObject::Copy(obj);
193
194 cont.fName = fName;
195 cont.fTitle = fTitle;
196
197 cont.fLog = fLog;
198 cont.fReadyToSave = fReadyToSave;
199}
200
201// --------------------------------------------------------------------------
202//
203// Encode MParContainer into output buffer.
204//
205void MParContainer::FillBuffer(char *&buffer)
206{
207 fName.FillBuffer(buffer);
208 fTitle.FillBuffer(buffer);
209}
210
211// --------------------------------------------------------------------------
212//
213// Returns the name of the object. If the name of the object is not the
214// class name it returns the object name and in []-brackets the class name.
215//
216const TString MParContainer::GetDescriptor() const
217{
218 return GetDescriptor(*this);
219}
220
221// --------------------------------------------------------------------------
222//
223// Returns the name of the object. If the name of the object is not the
224// class name it returns the object name and in []-brackets the class name.
225//
226const TString MParContainer::GetDescriptor(const TObject &o)
227{
228 //
229 // Because it returns a (const char*) we cannot return a casted
230 // local TString. The pointer would - immediatly after return -
231 // point to a random memory segment, because the TString has gone.
232 //
233 return (TString)o.GetName()==o.ClassName() ? (TString)o.ClassName() :
234 MString::Format("%s [%s]", o.GetName(), o.ClassName());
235}
236
237// --------------------------------------------------------------------------
238//
239// Check down to base whether the class given in the argument
240// overwrites the function given by name.
241//
242// This function calls itself recursively. If you want to call it,
243// leave out the argument.
244//
245Bool_t MParContainer::Overwrites(const TClass *base, const TObject &obj, const char *name, TClass *cls)
246{
247 if (!cls)
248 cls = obj.IsA();
249
250 //
251 // Check whether we reached the base class MTask
252 //
253 if (cls==base)
254 return kFALSE;
255
256 //
257 // Check whether the class cls overwrites Process
258 //
259 if (cls->GetMethodAny(name))
260 return kTRUE;
261
262 //
263 // If the class itself doesn't overload it check all it's base classes
264 //
265 TBaseClass *basecls=NULL;
266 TIter NextBase(cls->GetListOfBases());
267 while ((basecls=(TBaseClass*)NextBase()))
268 {
269 if (Overwrites(base, obj, name, basecls->GetClassPointer()))
270 return kTRUE;
271 }
272
273 return kFALSE;
274}
275
276
277// --------------------------------------------------------------------------
278//
279// Return a unique name for this container. It is created from
280// the container name and the unique Id. (This is mostly used
281// in the StreamPrimitive member functions)
282//
283const TString MParContainer::GetUniqueName() const
284{
285 TString ret = ToLower(fName);
286
287 if (isdigit(ret[ret.Length()-1]))
288 ret+="_";
289
290 ret+=GetUniqueID();
291
292 return ret;
293}
294
295// --------------------------------------------------------------------------
296//
297// List MParContainer name and title.
298//
299void MParContainer::ls(Option_t *) const
300{
301 TROOT::IndentLevel();
302 *fLog << all << GetDescriptor() << " " << GetTitle() << ": kCanDelete=";
303 *fLog << Int_t(TestBit(kCanDelete)) << endl;
304}
305
306// --------------------------------------------------------------------------
307//
308// Print MParContainer name and title.
309//
310void MParContainer::Print(Option_t *) const
311{
312 *fLog << all << GetDescriptor() << " " << GetTitle() << endl;
313}
314
315// --------------------------------------------------------------------------
316//
317// Change (i.e. set) the name of the MParContainer.
318// WARNING !!
319// If the object is a member of a THashTable, THashList container
320// The HashTable must be Rehashed after SetName
321// For example the list of objects in the current directory is a THashList
322//
323void MParContainer::SetName(const char *name)
324{
325 fName = name;
326 ResetBit(kIsSavedAsPrimitive);
327 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
328}
329
330// --------------------------------------------------------------------------
331//
332// Change (i.e. set) all the MParContainer parameters (name and title).
333// See also WARNING in SetName
334//
335void MParContainer::SetObject(const char *name, const char *title)
336{
337 fName = name;
338 fTitle = title;
339 ResetBit(kIsSavedAsPrimitive);
340 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
341}
342
343// --------------------------------------------------------------------------
344//
345// Change (i.e. set) the title of the MParContainer.
346//
347void MParContainer::SetTitle(const char *title)
348{
349 fTitle = title;
350 ResetBit(kIsSavedAsPrimitive);
351 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
352}
353
354// --------------------------------------------------------------------------
355//
356// Return size of the MParContainer part of the TObject.
357//
358Int_t MParContainer::Sizeof() const
359{
360 Int_t nbytes = fName.Sizeof() + fTitle.Sizeof();
361 return nbytes;
362}
363
364// --------------------------------------------------------------------------
365//
366// Read an object from the current directory. If no name is given
367// the name of this object is used. The final object will have the
368// name of the object read from file.
369//
370Int_t MParContainer::Read(const char *name)
371{
372 const Int_t rc = TObject::Read(name?name:(const char*)fName);
373 if (name)
374 SetName(name);
375 return rc;
376}
377
378// --------------------------------------------------------------------------
379//
380// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
381// container, overload this function.
382//
383void MParContainer::AsciiRead(istream &)
384{
385 *fLog << warn << "To use the the ascii input of " << GetName();
386 *fLog << " you have to overload " << ClassName() << "::AsciiRead." << endl;
387}
388
389// --------------------------------------------------------------------------
390//
391// Write out a data member given as a TDataMember object to an output stream.
392//
393Bool_t MParContainer::WriteDataMember(ostream &out, const TDataMember *member, Double_t scale) const
394{
395 if (!member)
396 return kFALSE;
397
398 if (!member->IsPersistent() || member->Property()&kIsStatic)
399 return kFALSE;
400
401 /*const*/ TMethodCall *call = const_cast<TDataMember*>(member)->GetterMethod(); //FIXME: Root
402 if (!call)
403 {
404 *fLog << warn << "Sorry, no getter method found for " << member->GetName() << endl;
405 return kFALSE;
406 }
407
408 // For debugging: out << member->GetName() << ":";
409
410 switch (call->ReturnType())
411 {
412 case TMethodCall::kLong:
413 Long_t l;
414 call->Execute((void*)this, l); // FIXME: const, root
415 out << l << " ";
416 return kTRUE;
417
418 case TMethodCall::kDouble:
419 Double_t d;
420 call->Execute((void*)this, d); // FIXME: const, root
421 out << (scale*d) << " ";
422 return kTRUE;
423
424 default:
425 //case TMethodCall::kString:
426 //case TMethodCall::kOther:
427 /* someone may want to enhance this? */
428 return kFALSE;
429 }
430}
431
432// --------------------------------------------------------------------------
433//
434// Write out a data member given by name to an output stream.
435//
436Bool_t MParContainer::WriteDataMember(ostream &out, const char *member, Double_t scale) const
437{
438 /*const*/ TClass *cls = IsA()->GetBaseDataMember(member);
439 if (!cls)
440 return kFALSE;
441
442 return WriteDataMember(out, cls->GetDataMember(member), scale);
443}
444
445// --------------------------------------------------------------------------
446//
447// Write out a data member from a given TList of TDataMembers.
448// returns kTRUE when at least one member was successfully written
449//
450Bool_t MParContainer::WriteDataMember(ostream &out, const TList *list) const
451{
452 Bool_t rc = kFALSE;
453
454 TDataMember *data = NULL;
455
456 TIter Next(list);
457 while ((data=(TDataMember*)Next()))
458 rc |= WriteDataMember(out, data);
459
460 return rc;
461}
462
463// --------------------------------------------------------------------------
464//
465// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
466// container, you may overload this function. If you don't overload it
467// the data member of a class are written to the file in the order of
468// appearance in the class header (be more specfic: root dictionary)
469// Only data members which are of integer (Bool_t, Int_t, ...) or
470// floating point (Float_t, Double_t, ...) type are written.
471// returns kTRUE when at least one member was successfully written
472//
473Bool_t MParContainer::AsciiWrite(ostream &out) const
474{
475 // *fLog << warn << "To use the the ascii output of " << GetName();
476 // *fLog << " you have to overload " << ClassName() << "::AsciiWrite." << endl;
477
478 Bool_t rc = WriteDataMember(out, IsA()->GetListOfDataMembers());
479
480 TIter NextBaseClass(IsA()->GetListOfBases());
481 TBaseClass *base;
482 while ((base = (TBaseClass*) NextBaseClass()))
483 {
484 /*const*/ TClass *cls = base->GetClassPointer();
485
486 if (!cls)
487 continue;
488
489 if (cls->GetClassVersion())
490 rc |= WriteDataMember(out, cls->GetListOfDataMembers());
491 }
492
493 return rc;
494}
495
496// --------------------------------------------------------------------------
497//
498// This virtual function is called for all parameter containers which are
499// found in the parameter list automatically each time the tasklist is
500// executed.
501//
502// By overwriting this function you can invalidate the contents of a
503// container before each execution of the tasklist:
504//
505// For example:
506// void MHillas::Reset()
507// {
508// fWidth = -1;
509// }
510//
511// (While -1 is obviously a impossible value for fWidth you can immediatly
512// see - if you Print() the contents of this container - that MHillasCalc
513// has not caluclated the width in this runthrough of the tasklist)
514//
515// Overwriting MParConatiner::Reset() in your container makes it
516// unnecessary to call any Clear() or Reset() manually in your task and
517// you make sure, that you don't keep results of previous runs of your
518// tasklist by chance.
519//
520// MParContainer::Reset() itself does nothing.
521//
522void MParContainer::Reset()
523{
524}
525
526// --------------------------------------------------------------------------
527//
528// Return the pointer to the TClass (from the root dictionary) which
529// corresponds to the class with name name.
530//
531// Make sure, that a new object of this type can be created.
532//
533// In case of failure return NULL
534//
535TClass *MParContainer::GetConstructor(const char *name) const
536{
537 //
538 // try to get class from root environment
539 //
540 TClass *cls = gROOT->GetClass(name);
541 Int_t rc = 0;
542 if (!cls)
543 rc =1;
544 else
545 {
546 if (!cls->Property())
547 rc = 5;
548 if (!cls->Size())
549 rc = 4;
550 if (!cls->IsLoaded())
551 rc = 3;
552 if (!cls->HasDefaultConstructor())
553 rc = 2;
554 }
555
556 if (!rc)
557 return cls;
558
559 *fLog << err << dbginf << GetDescriptor() << " - Cannot create new instance of class '" << name << "': ";
560 switch (rc)
561 {
562 case 1:
563 *fLog << "gROOT->GetClass() returned NULL." << endl;
564 return NULL;
565 case 2:
566 *fLog << "no default constructor." << endl;
567 return NULL;
568 case 3:
569 *fLog << "not loaded." << endl;
570 return NULL;
571 case 4:
572 *fLog << "zero size." << endl;
573 return NULL;
574 case 5:
575 *fLog << "no property." << endl;
576 return NULL;
577 }
578
579 *fLog << "rtlprmft." << endl;
580
581 return NULL;
582}
583
584// --------------------------------------------------------------------------
585//
586// Return a new object of class 'name'. Make sure that the object
587// derives from the class base.
588//
589// In case of failure return NULL
590//
591// The caller is responsible of deleting the object!
592//
593MParContainer *MParContainer::GetNewObject(const char *name, TClass *base) const
594{
595 return base ? GetNewObject(name, base->GetName()) : 0;
596}
597
598// --------------------------------------------------------------------------
599//
600// Return a new object of class 'name'. Make sure that the object
601// derives from the class base.
602//
603// In case of failure return NULL
604//
605// The caller is responsible of deleting the object!
606//
607MParContainer *MParContainer::GetNewObject(const char *name, const char *base) const
608{
609 TClass *cls = GetConstructor(name);
610 if (!cls || !base)
611 return NULL;
612
613 if (!cls->InheritsFrom(base))
614 {
615 *fLog << err;
616 *fLog << dbginf << GetDescriptor() << "Cannot create new instance of class '" << name << "': " << endl;
617 *fLog << "Class " << cls->GetName() << " doesn't inherit from " << base << endl;
618 return NULL;
619 }
620
621 //
622 // create the parameter container of the the given class type
623 //
624 TObject *obj = (TObject*)cls->New();
625 if (!obj)
626 {
627 *fLog << err;
628 *fLog << dbginf << GetDescriptor() << " - Cannot create new instance of class '" << name << "': " << endl;
629 *fLog << " - Class " << cls->GetName() << " has no default constructor." << endl;
630 *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
631 return NULL;
632 }
633
634 return (MParContainer*)obj;
635}
636
637TMethodCall *MParContainer::GetterMethod(const char *name) const
638{
639 const TString n(name);
640 const Int_t pos1 = n.First('.');
641
642 const TString part1 = pos1<0 ? n : n(0, pos1);
643 const TString part2 = pos1<0 ? TString("") : n(pos1+1, n.Length());
644
645 TClass *cls = IsA()->GetBaseDataMember(part1);
646 if (cls)
647 {
648 TDataMember *member = cls->GetDataMember(part1);
649 if (!member)
650 {
651 *fLog << err << "Datamember '" << part1 << "' not in " << GetDescriptor() << endl;
652 return NULL;
653 }
654
655 // This handles returning references of contained objects, eg
656 // class X { TObject fO; TObject &GetO() { return fO; } };
657 if (!member->IsBasic() && !part2.IsNull())
658 {
659 cls = gROOT->GetClass(member->GetTypeName());
660 if (!cls)
661 {
662 *fLog << err << "Datamember " << part1 << " [";
663 *fLog << member->GetTypeName() << "] not in dictionary." << endl;
664 return NULL;
665 }
666 if (!cls->InheritsFrom(MParContainer::Class()))
667 {
668 *fLog << err << "Datamember " << part1 << " [";
669 *fLog << member->GetTypeName() << "] does not inherit from ";
670 *fLog << "MParContainer." << endl;
671 return NULL;
672 }
673
674 const MParContainer *sub = (MParContainer*)((ULong_t)this+member->GetOffset());
675 return sub->GetterMethod(part2);
676 }
677
678 if (member->IsaPointer())
679 {
680 *fLog << warn << "Data-member " << part1 << " is a pointer..." << endl;
681 *fLog << dbginf << "Not yet implemented!" << endl;
682 //TClass *test = gROOT->GetClass(member->GetTypeName());
683 return 0;
684 }
685
686 TMethodCall *call = member->GetterMethod();
687 if (call)
688 return call;
689 }
690
691 *fLog << inf << "No standard access for '" << part1 << "' in ";
692 *fLog << GetDescriptor() << " or one of its base classes." << endl;
693
694 TMethodCall *call = NULL;
695
696 *fLog << "Trying to find MethodCall '" << ClassName();
697 *fLog << "::" << part1 << "' instead..." << flush;
698 call = new TMethodCall(IsA(), part1, "");
699 if (call->GetMethod())
700 {
701 fgListMethodCall.Add(call);
702 *fLog << "found." << endl;
703 return call;
704 }
705 *fLog << endl;
706
707 delete call;
708
709 *fLog << "Trying to find MethodCall '" << ClassName();
710 *fLog << "::Get" << part1 << "' instead..." << flush;
711 call = new TMethodCall(IsA(), (TString)"Get"+part1, "");
712 if (call->GetMethod())
713 {
714 fgListMethodCall.Add(call);
715 *fLog << "found." << endl;
716 return call;
717 }
718 *fLog << endl;
719
720 delete call;
721
722 *fLog << err << "Sorry, no getter method found for " << part1 << endl;
723 return NULL;
724}
725
726void *MParContainer::DataMember(const char *member)
727{
728 TDataMember *m = IsA()->GetDataMember(member);
729 if (m)
730 {
731 const Long_t off = m->GetOffset();
732 if (off>0)
733 return reinterpret_cast<char*>(this)+off;
734 }
735
736 *fLog << err << "Data member '" << member << "' not found in " << GetDescriptor() << endl;
737 return 0;
738}
739
740// --------------------------------------------------------------------------
741//
742// Implementation of SavePrimitive. Used to write the call to a constructor
743// to a macro. In the original root implementation it is used to write
744// gui elements to a macro-file.
745//
746void MParContainer::SavePrimitive(ostream &out, Option_t *)
747{
748 static UInt_t uid = 0;
749
750 if (IsSavedAsPrimitive())
751 return;
752
753 SetUniqueID(uid++);
754 SetBit(kIsSavedAsPrimitive);
755
756 if (gListOfPrimitives && !gListOfPrimitives->FindObject(this))
757 gListOfPrimitives->Add(this);
758
759 StreamPrimitive(out);
760}
761
762void MParContainer::SavePrimitive(ofstream &out, Option_t *)
763{
764 SavePrimitive(static_cast<ostream&>(out));
765}
766
767// --------------------------------------------------------------------------
768//
769// Creates the string written by SavePrimitive and returns it.
770//
771void MParContainer::StreamPrimitive(ostream &out) const
772{
773 out << " // Using MParContainer::StreamPrimitive" << endl;
774 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
775 out << fName << "\", \"" << fTitle << "\");" << endl;
776}
777
778void MParContainer::GetNames(TObjArray &arr) const
779{
780 arr.AddLast(new TNamed(fName, fTitle));
781}
782
783void MParContainer::SetNames(TObjArray &arr)
784{
785 TNamed *name = (TNamed*)arr.First();
786
787 fName = name->GetName();
788 fTitle = name->GetTitle();
789
790 delete arr.Remove(name);
791 arr.Compress();
792}
793
794// --------------------------------------------------------------------------
795//
796// Creates a new instance of this class. The idea is to create a clone of
797// this class in its initial state.
798//
799MParContainer *MParContainer::New() const
800{
801 return (MParContainer*)IsA()->New();
802}
803
804// --------------------------------------------------------------------------
805//
806// Check if an object can be created through gROOT->GetClass(classname)
807// return the correspodning TClass or NULL if this is not possible.
808// A message containing the reason is returned in msg.
809//
810TClass *MParContainer::GetClass(const char *classname, TString &msg)
811{
812 TClass *cls = gROOT->GetClass(classname);
813 Int_t rcc = 0;
814 if (!cls)
815 rcc = 1;
816 else
817 {
818 if (!cls->Property())
819 rcc = 5;
820 if (!cls->Size())
821 rcc = 4;
822 if (!cls->IsLoaded())
823 rcc = 3;
824 if (!cls->HasDefaultConstructor())
825 rcc = 2;
826 }
827
828 // Everything is ok.
829 if (rcc==0)
830 return cls;
831
832 msg += "Cannot create instance of class '";
833 msg += classname;
834 msg += "': ";
835
836 switch (rcc)
837 {
838 case 1:
839 msg += "gROOT->GetClass() returned NULL.";
840 break;
841 case 2:
842 msg += "no default constructor.";
843 break;
844 case 3:
845 msg += "not loaded.";
846 break;
847 case 4:
848 msg += "zero size.";
849 break;
850 case 5:
851 msg += "no property.";
852 break;
853 default:
854 msg += "Unknown error.";
855 break;
856 }
857
858 return 0;
859}
860
861// --------------------------------------------------------------------------
862//
863// Check if an object can be created through gROOT->GetClass(classname)
864// return the correspodning TClass or NULL if this is not possible.
865// A message with the reason is ouput.
866//
867TClass *MParContainer::GetClass(const char *classname, MLog *log)
868{
869 TString msg;
870 TClass *cls = GetClass(classname, msg);
871
872 if (!cls && log)
873 *log << msg << endl;
874
875 return cls;
876}
877
878
879// --------------------------------------------------------------------------
880//
881// Read the contents/setup of a parameter container/task from a TEnv
882// instance (steering card/setup file).
883// The key to search for in the file should be of the syntax:
884// prefix.vname
885// While vname is a name which is specific for a single setup date
886// (variable) of this container and prefix is something like:
887// evtloopname.name
888// While name is the name of the containers/tasks in the parlist/tasklist
889//
890// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
891// Job4.MImgCleanStd.CleaningLevel2: 2.5
892//
893// If this cannot be found the next step is to search for
894// MImgCleanStd.CleaningLevel1: 3.0
895// And if this doesn't exist, too, we should search for:
896// CleaningLevel1: 3.0
897//
898// Warning: The programmer is responsible for the names to be unique in
899// all Mars classes.
900//
901// Return values:
902// kTRUE: Environment string found
903// kFALSE: Environment string not found
904// kERROR: Error occured, eg. environment invalid
905//
906// Overload this if you don't want to control the level of setup-string. In
907// this case ReadEnv gets called with the different possibilities, see TestEnv.
908//
909Int_t MParContainer::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
910{
911 if (!IsEnvDefined(env, prefix, "", print))
912 return kFALSE;
913
914 *fLog << warn << "WARNING - " << fName << ": Resource " << prefix << " found, but no " << ClassName() << "::ReadEnv." << endl;
915 return kTRUE;
916}
917
918// --------------------------------------------------------------------------
919//
920// Write the contents/setup of a parameter container/task to a TEnv
921// instance (steering card/setup file).
922// The key to search for in the file should be of the syntax:
923// prefix.vname
924// While vname is a name which is specific for a single setup date
925// (variable) of this container and prefix is something like:
926// evtloopname.name
927// While name is the name of the containers/tasks in the parlist/tasklist
928//
929// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
930// Job4.MImgCleanStd.CleaningLevel2: 2.5
931//
932// If this cannot be found the next step is to search for
933// MImgCleanStd.CleaningLevel1: 3.0
934// And if this doesn't exist, too, we should search for:
935// CleaningLevel1: 3.0
936//
937// Warning: The programmer is responsible for the names to be unique in
938// all Mars classes.
939//
940Bool_t MParContainer::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
941{
942 if (!IsEnvDefined(env, prefix, "", print))
943 return kFALSE;
944
945 *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but " << ClassName() << "::WriteEnv not overloaded." << endl;
946 return kTRUE;
947}
948
949// --------------------------------------------------------------------------
950//
951// Take the prefix and call ReadEnv for:
952// prefix.containername
953// prefix.classname
954// containername
955// classname
956//
957// The existance of an environment variable is done in this order. If
958// ReadEnv return kTRUE the existance of the container setup is assumed and
959// the other tests are skipped. If kFALSE is assumed the sequence is
960// continued. In case of kERROR failing of the setup from a file is assumed.
961//
962// Overload this if you want to control the handling of level of setup-string
963// mentioned above. In this case ReadEnv gets never called if you don't call
964// it explicitly.
965//
966Int_t MParContainer::TestEnv(const TEnv &env, TString prefix, Bool_t print)
967{
968 if (print)
969 *fLog << all << "Testing Prefix+ContName: " << prefix+GetName() << endl;
970 Int_t rc = ReadEnv(env, prefix+GetName(), print);
971 if (rc==kERROR || rc==kTRUE)
972 return rc;
973
974 // Check For: Job4.MClassName.Varname
975 if (print)
976 *fLog << all << "Testing Prefix+ClassName: " << prefix+ClassName() << endl;
977 rc = ReadEnv(env, prefix+ClassName(), print);
978 if (rc==kERROR || rc==kTRUE)
979 return rc;
980
981 // Check For: ContainerName.Varname
982 if (print)
983 *fLog << all << "Testing ContName: " << GetName() << endl;
984 rc = ReadEnv(env, GetName(), print);
985 if (rc==kERROR || rc==kTRUE)
986 return rc;
987
988 // Check For: MClassName.Varname
989 if (print)
990 *fLog << all << "Testing ClassName: " << ClassName() << endl;
991 rc = ReadEnv(env, ClassName(), print);
992 if (rc==kERROR || rc==kTRUE)
993 return rc;
994
995 // Not found
996 return kFALSE;
997}
998
999// --------------------------------------------------------------------------
1000//
1001// Check if the given resource is defined. If there is a postfix, prefix
1002// the postfix with a dot. Calls IsEnvDefined(env, name, print)
1003//
1004Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString prefix, TString postfix, Bool_t print) const
1005{
1006 if (!postfix.IsNull())
1007 postfix.Insert(0, ".");
1008
1009 return IsEnvDefined(env, prefix+postfix, print);
1010}
1011
1012// --------------------------------------------------------------------------
1013//
1014// If print==kTRUE print information about what's going on. This is necessary
1015// to debug parsing of resource files. Check if a resource "name" is defined
1016// and return kFALSE/kTRUE depending on the result.
1017//
1018Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString name, Bool_t print) const
1019{
1020 if (print)
1021 *fLog << all << GetDescriptor() << " - " << name << "... " << flush;
1022
1023 if (!const_cast<TEnv&>(env).Defined(name))
1024 {
1025 if (print)
1026 *fLog << "not found." << endl;
1027 return kFALSE;
1028 }
1029
1030 if (print)
1031 *fLog << "found." << endl;
1032
1033 return kTRUE;
1034}
1035
1036// --------------------------------------------------------------------------
1037//
1038// Return the resource prefix+"."+postfix from env or deftl if not available.
1039// If prefix IsNull search for postfix only.
1040//
1041Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Int_t dflt) const
1042{
1043 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
1044}
1045
1046// --------------------------------------------------------------------------
1047//
1048// Return the resource prefix+"."+postfix from env or deftl if not available.
1049// If prefix IsNull search for postfix only.
1050//
1051Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Double_t dflt) const
1052{
1053 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
1054}
1055
1056// --------------------------------------------------------------------------
1057//
1058// Return the resource prefix+"."+postfix from env or deftl if not available.
1059// If prefix IsNull search for postfix only.
1060//
1061const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, const char *dflt) const
1062{
1063 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
1064}
1065
1066// --------------------------------------------------------------------------
1067//
1068// Return the resource prefix from env or deftl if not available.
1069//
1070Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Int_t dflt) const
1071{
1072 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1073}
1074
1075// --------------------------------------------------------------------------
1076//
1077// Return the resource prefix from env or deftl if not available.
1078//
1079Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Double_t dflt) const
1080{
1081 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1082}
1083
1084// --------------------------------------------------------------------------
1085//
1086// Return the resource prefix from env or deftl if not available.
1087//
1088const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, const char *dflt) const
1089{
1090 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1091}
1092
1093// --------------------------------------------------------------------------
1094//
1095// Check for the resource prefix+"."+postfix. If it is not available or
1096// prefix IsNull check for the more common resource postfix. If none
1097// is found return the default.
1098//
1099template <class T>
1100T MParContainer::GetEnvValue2Imp(const TEnv &env, const TString &prefix, const TString &postfix, T dftl, Bool_t print) const
1101{
1102 // Check for a dedicated resource (prefix.postfix) first
1103 if (!prefix.IsNull())
1104 {
1105 if (IsEnvDefined(env, prefix, postfix, print))
1106 return GetEnvValue(env, prefix, postfix, dftl);
1107 }
1108
1109 // check for a general resource (postfix)
1110 if (IsEnvDefined(env, postfix, print))
1111 return GetEnvValue(env, postfix, dftl);
1112
1113 // return default
1114 return dftl;
1115}
1116
1117// --------------------------------------------------------------------------
1118//
1119// see template GetEnvValue2Imp
1120//
1121const char *MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, const char *dftl, Bool_t print) const
1122{
1123 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1124}
1125
1126// --------------------------------------------------------------------------
1127//
1128// see template GetEnvValue2Imp
1129//
1130Int_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Int_t dftl, Bool_t print) const
1131{
1132 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1133}
1134
1135// --------------------------------------------------------------------------
1136//
1137// see template GetEnvValue2Imp
1138//
1139Double_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Double_t dftl, Bool_t print) const
1140{
1141 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1142}
1143
1144// --------------------------------------------------------------------------
1145//
1146// This is a wrapper which checks the resource file for an id containing
1147// a %d with different numbers of leading zeros (1 to 8).
1148//
1149// If athe entries in the resource file are not unambiguous a warning
1150// is printed.
1151//
1152TString MParContainer::GetEnvValue3(const TEnv &env, const TString &prefix, TString id, UInt_t num) const
1153{
1154 id.ReplaceAll("%d", "%%0%dd");
1155
1156 TString rc;
1157 for (int i=1; i<9; i++)
1158 {
1159 const TString form = MString::Format(id.Data(), i);
1160 const TString res = MString::Format(form.Data(), num);
1161
1162 const TString str = GetEnvValue2(env, prefix, res, "");
1163
1164 if (str.IsNull())
1165 continue;
1166
1167 if (rc.IsNull())
1168 rc = str;
1169 else
1170 *fLog << warn << "Entry " << res << " ambigous (was also found with less leading zeros)... ignored." << endl;
1171 }
1172
1173 return rc;
1174}
1175
1176// --------------------------------------------------------------------------
1177//
1178// If object to remove is fDisplay set fDisplay to NULL.
1179// If object to remove is fLog set fLog to NULL.
1180// Call TObject::RecursiveRemove
1181//
1182void MParContainer::RecursiveRemove(TObject *obj)
1183{
1184 if (obj==fDisplay)
1185 fDisplay=NULL;
1186
1187 if (obj==fLog)
1188 fLog=NULL;
1189
1190 if (fDisplay)
1191 fDisplay->RecursiveRemove(obj);
1192
1193 if (fLog)
1194 fLog->RecursiveRemove(obj);
1195
1196 TObject::RecursiveRemove(obj);
1197}
1198
Note: See TracBrowser for help on using the repository browser.