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

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