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

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