source: trunk/Mars/mbase/MParContainer.cc@ 12392

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