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

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