source: trunk/MagicSoft/Mars/mbase/MParContainer.cc@ 9110

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