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

Last change on this file since 8795 was 8724, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 31.8 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MParContainer
28//
29// The MParContainer class is the base class for all MARS parameter
30// containers. At the moment it is almost the same than ROOT's TNamed.
31// A TNamed contains the essential elements (name, title)
32// to identify a derived object in lists like our MParList or MTaskList.
33// The main difference is that the name and title isn't stored and read
34// to and from root files ("//!")
35//
36// MParContainer has several enhancements compared to TNamed:
37// - GetDescriptor(): returns name and class type
38// - GetUniqueName(): returns a unique name (used in StreamPrimitive)
39// - SetLogStream(MLog *lg): Set a logging stream to which loggingis stored
40// - Reset(): Reset content of class in an eventloop
41// - IsReadyToSave(): The contents are ready to be saved to a file
42// - IsSavedAsPrimitive(): A unique name for this instance is already
43// existing
44// - SetVariables(): Can be overloaded if the containers stores
45// coefficients (to be used in fits)
46// - SetDisplay(): Set a display for redirecting graphical output
47// - GetNames(): Get Name/Title from instance and store it in
48// a TObjArray (used to store the names of the
49// conteiners in a file
50// - SetNames(): vice versa
51// - ReadEnv(), WriteEnv(): Function which is used for automatical setup
52// IsEnvDefined() from a TEnv file
53//
54//////////////////////////////////////////////////////////////////////////////
55#include "MParContainer.h"
56
57#include <ctype.h> // isdigit
58#include <fstream> // ofstream
59
60#include <TEnv.h> // Env::Lookup
61#include <TClass.h> // IsA
62#include <TObjArray.h> // TObjArray
63#include <TBaseClass.h> // GetClassPointer
64#include <TMethodCall.h> // TMethodCall, AsciiWrite
65#include <TDataMember.h> // TDataMember, AsciiWrite
66#include <TVirtualPad.h> // gPad
67
68#include "MString.h"
69
70#include "MLog.h"
71#include "MLogManip.h"
72
73#include "MStatusDisplay.h"
74
75TList *gListOfPrimitives; // forard declaration in MParContainer.h
76
77#undef DEBUG
78//#define DEBUG
79
80ClassImp(MParContainer);
81
82using namespace std;
83
84TObjArray MParContainer::fgListMethodCall;
85
86MParContainer::MParContainer(const char *name, const char *title) :
87 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
88{
89 fgListMethodCall.SetOwner();
90}
91
92MParContainer::MParContainer(const TString &name, const TString &title) :
93 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
94{
95 fgListMethodCall.SetOwner();
96}
97
98// --------------------------------------------------------------------------
99//
100// MParContainer copy ctor
101//
102MParContainer::MParContainer(const MParContainer &named) : TObject()
103{
104 fName = named.fName;
105 fTitle = named.fTitle;
106
107 fLog = named.fLog;
108
109 fReadyToSave = named.fReadyToSave;
110
111 fDisplay = named.fDisplay;
112}
113
114MParContainer::~MParContainer()
115{
116#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 = ((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// Read the contents/setup of a parameter container/task from a TEnv
753// instance (steering card/setup file).
754// The key to search for in the file should be of the syntax:
755// prefix.vname
756// While vname is a name which is specific for a single setup date
757// (variable) of this container and prefix is something like:
758// evtloopname.name
759// While name is the name of the containers/tasks in the parlist/tasklist
760//
761// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
762// Job4.MImgCleanStd.CleaningLevel2: 2.5
763//
764// If this cannot be found the next step is to search for
765// MImgCleanStd.CleaningLevel1: 3.0
766// And if this doesn't exist, too, we should search for:
767// CleaningLevel1: 3.0
768//
769// Warning: The programmer is responsible for the names to be unique in
770// all Mars classes.
771//
772// Return values:
773// kTRUE: Environment string found
774// kFALSE: Environment string not found
775// kERROR: Error occured, eg. environment invalid
776//
777// Overload this if you don't want to control the level of setup-string. In
778// this case ReadEnv gets called with the different possibilities, see TestEnv.
779//
780Int_t MParContainer::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
781{
782 if (!IsEnvDefined(env, prefix, "", print))
783 return kFALSE;
784
785 *fLog << warn << "WARNING - " << fName << ": Resource " << prefix << " found, but no " << ClassName() << "::ReadEnv." << endl;
786 return kTRUE;
787}
788
789// --------------------------------------------------------------------------
790//
791// Write the contents/setup of a parameter container/task to a TEnv
792// instance (steering card/setup file).
793// The key to search for in the file should be of the syntax:
794// prefix.vname
795// While vname is a name which is specific for a single setup date
796// (variable) of this container and prefix is something like:
797// evtloopname.name
798// While name is the name of the containers/tasks in the parlist/tasklist
799//
800// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
801// Job4.MImgCleanStd.CleaningLevel2: 2.5
802//
803// If this cannot be found the next step is to search for
804// MImgCleanStd.CleaningLevel1: 3.0
805// And if this doesn't exist, too, we should search for:
806// CleaningLevel1: 3.0
807//
808// Warning: The programmer is responsible for the names to be unique in
809// all Mars classes.
810//
811Bool_t MParContainer::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
812{
813 if (!IsEnvDefined(env, prefix, "", print))
814 return kFALSE;
815
816 *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but " << ClassName() << "::WriteEnv not overloaded." << endl;
817 return kTRUE;
818}
819
820// --------------------------------------------------------------------------
821//
822// Take the prefix and call ReadEnv for:
823// prefix.containername
824// prefix.classname
825// containername
826// classname
827//
828// The existance of an environment variable is done in this order. If
829// ReadEnv return kTRUE the existance of the container setup is assumed and
830// the other tests are skipped. If kFALSE is assumed the sequence is
831// continued. In case of kERROR failing of the setup from a file is assumed.
832//
833// Overload this if you want to control the handling of level of setup-string
834// mentioned above. In this case ReadEnv gets never called if you don't call
835// it explicitly.
836//
837Int_t MParContainer::TestEnv(const TEnv &env, TString prefix, Bool_t print)
838{
839 if (print)
840 *fLog << all << "Testing Prefix+ContName: " << prefix+GetName() << endl;
841 Int_t rc = ReadEnv(env, prefix+GetName(), print);
842 if (rc==kERROR || rc==kTRUE)
843 return rc;
844
845 // Check For: Job4.MClassName.Varname
846 if (print)
847 *fLog << all << "Testing Prefix+ClasName: " << prefix+ClassName() << endl;
848 rc = ReadEnv(env, prefix+ClassName(), print);
849 if (rc==kERROR || rc==kTRUE)
850 return rc;
851
852 // Check For: ContainerName.Varname
853 if (print)
854 *fLog << all << "Testing ContName: " << GetName() << endl;
855 rc = ReadEnv(env, GetName(), print);
856 if (rc==kERROR || rc==kTRUE)
857 return rc;
858
859 // Check For: MClassName.Varname
860 if (print)
861 *fLog << all << "Testing ClassName: " << ClassName() << endl;
862 rc = ReadEnv(env, ClassName(), print);
863 if (rc==kERROR || rc==kTRUE)
864 return rc;
865
866 // Not found
867 return kFALSE;
868}
869
870// --------------------------------------------------------------------------
871//
872// Check if the given resource is defined. If there is a postfix, prefix
873// the postfix with a dot. Calls IsEnvDefined(env, name, print)
874//
875Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString prefix, TString postfix, Bool_t print) const
876{
877 if (!postfix.IsNull())
878 postfix.Insert(0, ".");
879
880 return IsEnvDefined(env, prefix+postfix, print);
881}
882
883// --------------------------------------------------------------------------
884//
885// If print==kTRUE print information about what's going on. This is necessary
886// to debug parsing of resource files. Check if a resource "name" is defined
887// and return kFALSE/kTRUE depending on the result.
888//
889Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString name, Bool_t print) const
890{
891 if (print)
892 *fLog << all << GetDescriptor() << " - " << name << "... " << flush;
893
894 if (!const_cast<TEnv&>(env).Defined(name))
895 {
896 if (print)
897 *fLog << "not found." << endl;
898 return kFALSE;
899 }
900
901 if (print)
902 *fLog << "found." << endl;
903
904 return kTRUE;
905}
906
907// --------------------------------------------------------------------------
908//
909// Return the resource prefix+"."+postfix from env or deftl if not available.
910// If prefix IsNull search for postfix only.
911//
912Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Int_t dflt) const
913{
914 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
915}
916
917// --------------------------------------------------------------------------
918//
919// Return the resource prefix+"."+postfix from env or deftl if not available.
920// If prefix IsNull search for postfix only.
921//
922Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Double_t dflt) const
923{
924 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
925}
926
927// --------------------------------------------------------------------------
928//
929// Return the resource prefix+"."+postfix from env or deftl if not available.
930// If prefix IsNull search for postfix only.
931//
932const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, const char *dflt) const
933{
934 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
935}
936
937// --------------------------------------------------------------------------
938//
939// Return the resource prefix from env or deftl if not available.
940//
941Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Int_t dflt) const
942{
943 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
944}
945
946// --------------------------------------------------------------------------
947//
948// Return the resource prefix from env or deftl if not available.
949//
950Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Double_t dflt) const
951{
952 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
953}
954
955// --------------------------------------------------------------------------
956//
957// Return the resource prefix from env or deftl if not available.
958//
959const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, const char *dflt) const
960{
961 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
962}
963
964// --------------------------------------------------------------------------
965//
966// Check for the resource prefix+"."+postfix. If it is not available or
967// prefix IsNull check for the more common resource postfix. If none
968// is found return the default.
969//
970template <class T>
971T MParContainer::GetEnvValue2Imp(const TEnv &env, const TString &prefix, const TString &postfix, T dftl, Bool_t print) const
972{
973 // Check for a dedicated resource (prefix.postfix) first
974 if (!prefix.IsNull())
975 {
976 if (IsEnvDefined(env, prefix, postfix, print))
977 return GetEnvValue(env, prefix, postfix, dftl);
978 }
979
980 // check for a general resource (postfix)
981 if (IsEnvDefined(env, postfix, print))
982 return GetEnvValue(env, postfix, dftl);
983
984 // return default
985 return dftl;
986}
987
988// --------------------------------------------------------------------------
989//
990// see template GetEnvValue2Imp
991//
992const char *MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, const char *dftl, Bool_t print) const
993{
994 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
995}
996
997// --------------------------------------------------------------------------
998//
999// see template GetEnvValue2Imp
1000//
1001Int_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Int_t dftl, Bool_t print) const
1002{
1003 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1004}
1005
1006// --------------------------------------------------------------------------
1007//
1008// see template GetEnvValue2Imp
1009//
1010Double_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Double_t dftl, Bool_t print) const
1011{
1012 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1013}
1014
1015// --------------------------------------------------------------------------
1016//
1017// If object to remove is fDisplay set fDisplay to NULL.
1018// If object to remove is fLog set fLog to NULL.
1019// Call TObject::RecursiveRemove
1020//
1021void MParContainer::RecursiveRemove(TObject *obj)
1022{
1023 if (obj==fDisplay)
1024 fDisplay=NULL;
1025
1026 if (obj==fLog)
1027 fLog=NULL;
1028
1029 if (fDisplay)
1030 fDisplay->RecursiveRemove(obj);
1031
1032 if (fLog)
1033 fLog->RecursiveRemove(obj);
1034
1035 TObject::RecursiveRemove(obj);
1036}
1037
Note: See TracBrowser for help on using the repository browser.