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

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