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

Last change on this file since 11412 was 9578, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 35.4 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
726// --------------------------------------------------------------------------
727//
728// Implementation of SavePrimitive. Used to write the call to a constructor
729// to a macro. In the original root implementation it is used to write
730// gui elements to a macro-file.
731//
732void MParContainer::SavePrimitive(ostream &out, Option_t *)
733{
734 static UInt_t uid = 0;
735
736 if (IsSavedAsPrimitive())
737 return;
738
739 SetUniqueID(uid++);
740 SetBit(kIsSavedAsPrimitive);
741
742 if (gListOfPrimitives && !gListOfPrimitives->FindObject(this))
743 gListOfPrimitives->Add(this);
744
745 StreamPrimitive(out);
746}
747
748void MParContainer::SavePrimitive(ofstream &out, Option_t *)
749{
750 SavePrimitive(static_cast<ostream&>(out));
751}
752
753// --------------------------------------------------------------------------
754//
755// Creates the string written by SavePrimitive and returns it.
756//
757void MParContainer::StreamPrimitive(ostream &out) const
758{
759 out << " // Using MParContainer::StreamPrimitive" << endl;
760 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
761 out << fName << "\", \"" << fTitle << "\");" << endl;
762}
763
764void MParContainer::GetNames(TObjArray &arr) const
765{
766 arr.AddLast(new TNamed(fName, fTitle));
767}
768
769void MParContainer::SetNames(TObjArray &arr)
770{
771 TNamed *name = (TNamed*)arr.First();
772
773 fName = name->GetName();
774 fTitle = name->GetTitle();
775
776 delete arr.Remove(name);
777 arr.Compress();
778}
779
780// --------------------------------------------------------------------------
781//
782// Creates a new instance of this class. The idea is to create a clone of
783// this class in its initial state.
784//
785MParContainer *MParContainer::New() const
786{
787 return (MParContainer*)IsA()->New();
788}
789
790// --------------------------------------------------------------------------
791//
792// Check if an object can be created through gROOT->GetClass(classname)
793// return the correspodning TClass or NULL if this is not possible.
794// A message containing the reason is returned in msg.
795//
796TClass *MParContainer::GetClass(const char *classname, TString &msg)
797{
798 TClass *cls = gROOT->GetClass(classname);
799 Int_t rcc = 0;
800 if (!cls)
801 rcc = 1;
802 else
803 {
804 if (!cls->Property())
805 rcc = 5;
806 if (!cls->Size())
807 rcc = 4;
808 if (!cls->IsLoaded())
809 rcc = 3;
810 if (!cls->HasDefaultConstructor())
811 rcc = 2;
812 }
813
814 // Everything is ok.
815 if (rcc==0)
816 return cls;
817
818 msg += "Cannot create instance of class '";
819 msg += classname;
820 msg += "': ";
821
822 switch (rcc)
823 {
824 case 1:
825 msg += "gROOT->GetClass() returned NULL.";
826 break;
827 case 2:
828 msg += "no default constructor.";
829 break;
830 case 3:
831 msg += "not loaded.";
832 break;
833 case 4:
834 msg += "zero size.";
835 break;
836 case 5:
837 msg += "no property.";
838 break;
839 default:
840 msg += "Unknown error.";
841 break;
842 }
843
844 return 0;
845}
846
847// --------------------------------------------------------------------------
848//
849// Check if an object can be created through gROOT->GetClass(classname)
850// return the correspodning TClass or NULL if this is not possible.
851// A message with the reason is ouput.
852//
853TClass *MParContainer::GetClass(const char *classname, MLog *log)
854{
855 TString msg;
856 TClass *cls = GetClass(classname, msg);
857
858 if (!cls && log)
859 *log << msg << endl;
860
861 return cls;
862}
863
864
865// --------------------------------------------------------------------------
866//
867// Read the contents/setup of a parameter container/task from a TEnv
868// instance (steering card/setup file).
869// The key to search for in the file should be of the syntax:
870// prefix.vname
871// While vname is a name which is specific for a single setup date
872// (variable) of this container and prefix is something like:
873// evtloopname.name
874// While name is the name of the containers/tasks in the parlist/tasklist
875//
876// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
877// Job4.MImgCleanStd.CleaningLevel2: 2.5
878//
879// If this cannot be found the next step is to search for
880// MImgCleanStd.CleaningLevel1: 3.0
881// And if this doesn't exist, too, we should search for:
882// CleaningLevel1: 3.0
883//
884// Warning: The programmer is responsible for the names to be unique in
885// all Mars classes.
886//
887// Return values:
888// kTRUE: Environment string found
889// kFALSE: Environment string not found
890// kERROR: Error occured, eg. environment invalid
891//
892// Overload this if you don't want to control the level of setup-string. In
893// this case ReadEnv gets called with the different possibilities, see TestEnv.
894//
895Int_t MParContainer::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
896{
897 if (!IsEnvDefined(env, prefix, "", print))
898 return kFALSE;
899
900 *fLog << warn << "WARNING - " << fName << ": Resource " << prefix << " found, but no " << ClassName() << "::ReadEnv." << endl;
901 return kTRUE;
902}
903
904// --------------------------------------------------------------------------
905//
906// Write the contents/setup of a parameter container/task to a TEnv
907// instance (steering card/setup file).
908// The key to search for in the file should be of the syntax:
909// prefix.vname
910// While vname is a name which is specific for a single setup date
911// (variable) of this container and prefix is something like:
912// evtloopname.name
913// While name is the name of the containers/tasks in the parlist/tasklist
914//
915// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
916// Job4.MImgCleanStd.CleaningLevel2: 2.5
917//
918// If this cannot be found the next step is to search for
919// MImgCleanStd.CleaningLevel1: 3.0
920// And if this doesn't exist, too, we should search for:
921// CleaningLevel1: 3.0
922//
923// Warning: The programmer is responsible for the names to be unique in
924// all Mars classes.
925//
926Bool_t MParContainer::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
927{
928 if (!IsEnvDefined(env, prefix, "", print))
929 return kFALSE;
930
931 *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but " << ClassName() << "::WriteEnv not overloaded." << endl;
932 return kTRUE;
933}
934
935// --------------------------------------------------------------------------
936//
937// Take the prefix and call ReadEnv for:
938// prefix.containername
939// prefix.classname
940// containername
941// classname
942//
943// The existance of an environment variable is done in this order. If
944// ReadEnv return kTRUE the existance of the container setup is assumed and
945// the other tests are skipped. If kFALSE is assumed the sequence is
946// continued. In case of kERROR failing of the setup from a file is assumed.
947//
948// Overload this if you want to control the handling of level of setup-string
949// mentioned above. In this case ReadEnv gets never called if you don't call
950// it explicitly.
951//
952Int_t MParContainer::TestEnv(const TEnv &env, TString prefix, Bool_t print)
953{
954 if (print)
955 *fLog << all << "Testing Prefix+ContName: " << prefix+GetName() << endl;
956 Int_t rc = ReadEnv(env, prefix+GetName(), print);
957 if (rc==kERROR || rc==kTRUE)
958 return rc;
959
960 // Check For: Job4.MClassName.Varname
961 if (print)
962 *fLog << all << "Testing Prefix+ClassName: " << prefix+ClassName() << endl;
963 rc = ReadEnv(env, prefix+ClassName(), print);
964 if (rc==kERROR || rc==kTRUE)
965 return rc;
966
967 // Check For: ContainerName.Varname
968 if (print)
969 *fLog << all << "Testing ContName: " << GetName() << endl;
970 rc = ReadEnv(env, GetName(), print);
971 if (rc==kERROR || rc==kTRUE)
972 return rc;
973
974 // Check For: MClassName.Varname
975 if (print)
976 *fLog << all << "Testing ClassName: " << ClassName() << endl;
977 rc = ReadEnv(env, ClassName(), print);
978 if (rc==kERROR || rc==kTRUE)
979 return rc;
980
981 // Not found
982 return kFALSE;
983}
984
985// --------------------------------------------------------------------------
986//
987// Check if the given resource is defined. If there is a postfix, prefix
988// the postfix with a dot. Calls IsEnvDefined(env, name, print)
989//
990Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString prefix, TString postfix, Bool_t print) const
991{
992 if (!postfix.IsNull())
993 postfix.Insert(0, ".");
994
995 return IsEnvDefined(env, prefix+postfix, print);
996}
997
998// --------------------------------------------------------------------------
999//
1000// If print==kTRUE print information about what's going on. This is necessary
1001// to debug parsing of resource files. Check if a resource "name" is defined
1002// and return kFALSE/kTRUE depending on the result.
1003//
1004Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString name, Bool_t print) const
1005{
1006 if (print)
1007 *fLog << all << GetDescriptor() << " - " << name << "... " << flush;
1008
1009 if (!const_cast<TEnv&>(env).Defined(name))
1010 {
1011 if (print)
1012 *fLog << "not found." << endl;
1013 return kFALSE;
1014 }
1015
1016 if (print)
1017 *fLog << "found." << endl;
1018
1019 return kTRUE;
1020}
1021
1022// --------------------------------------------------------------------------
1023//
1024// Return the resource prefix+"."+postfix from env or deftl if not available.
1025// If prefix IsNull search for postfix only.
1026//
1027Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Int_t dflt) const
1028{
1029 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
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//
1037Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Double_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//
1047const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, const char *dflt) const
1048{
1049 return GetEnvValue(env, prefix.IsNull()?postfix:(prefix+"."+postfix), dflt);
1050}
1051
1052// --------------------------------------------------------------------------
1053//
1054// Return the resource prefix from env or deftl if not available.
1055//
1056Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Int_t dflt) const
1057{
1058 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1059}
1060
1061// --------------------------------------------------------------------------
1062//
1063// Return the resource prefix from env or deftl if not available.
1064//
1065Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Double_t dflt) const
1066{
1067 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1068}
1069
1070// --------------------------------------------------------------------------
1071//
1072// Return the resource prefix from env or deftl if not available.
1073//
1074const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, const char *dflt) const
1075{
1076 return const_cast<TEnv&>(env).GetValue(prefix, dflt);
1077}
1078
1079// --------------------------------------------------------------------------
1080//
1081// Check for the resource prefix+"."+postfix. If it is not available or
1082// prefix IsNull check for the more common resource postfix. If none
1083// is found return the default.
1084//
1085template <class T>
1086T MParContainer::GetEnvValue2Imp(const TEnv &env, const TString &prefix, const TString &postfix, T dftl, Bool_t print) const
1087{
1088 // Check for a dedicated resource (prefix.postfix) first
1089 if (!prefix.IsNull())
1090 {
1091 if (IsEnvDefined(env, prefix, postfix, print))
1092 return GetEnvValue(env, prefix, postfix, dftl);
1093 }
1094
1095 // check for a general resource (postfix)
1096 if (IsEnvDefined(env, postfix, print))
1097 return GetEnvValue(env, postfix, dftl);
1098
1099 // return default
1100 return dftl;
1101}
1102
1103// --------------------------------------------------------------------------
1104//
1105// see template GetEnvValue2Imp
1106//
1107const char *MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, const char *dftl, Bool_t print) const
1108{
1109 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1110}
1111
1112// --------------------------------------------------------------------------
1113//
1114// see template GetEnvValue2Imp
1115//
1116Int_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Int_t dftl, Bool_t print) const
1117{
1118 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1119}
1120
1121// --------------------------------------------------------------------------
1122//
1123// see template GetEnvValue2Imp
1124//
1125Double_t MParContainer::GetEnvValue2(const TEnv &env, const TString &prefix, const TString &postfix, Double_t dftl, Bool_t print) const
1126{
1127 return GetEnvValue2Imp(env, prefix, postfix, dftl, print);
1128}
1129
1130// --------------------------------------------------------------------------
1131//
1132// This is a wrapper which checks the resource file for an id containing
1133// a %d with different numbers of leading zeros (1 to 8).
1134//
1135// If athe entries in the resource file are not unambiguous a warning
1136// is printed.
1137//
1138TString MParContainer::GetEnvValue3(const TEnv &env, const TString &prefix, TString id, UInt_t num) const
1139{
1140 id.ReplaceAll("%d", "%%0%dd");
1141
1142 TString rc;
1143 for (int i=1; i<9; i++)
1144 {
1145 const TString form = MString::Format(id.Data(), i);
1146 const TString res = MString::Format(form.Data(), num);
1147
1148 const TString str = GetEnvValue2(env, prefix, res, "");
1149
1150 if (str.IsNull())
1151 continue;
1152
1153 if (rc.IsNull())
1154 rc = str;
1155 else
1156 *fLog << warn << "Entry " << res << " ambigous (was also found with less leading zeros)... ignored." << endl;
1157 }
1158
1159 return rc;
1160}
1161
1162// --------------------------------------------------------------------------
1163//
1164// If object to remove is fDisplay set fDisplay to NULL.
1165// If object to remove is fLog set fLog to NULL.
1166// Call TObject::RecursiveRemove
1167//
1168void MParContainer::RecursiveRemove(TObject *obj)
1169{
1170 if (obj==fDisplay)
1171 fDisplay=NULL;
1172
1173 if (obj==fLog)
1174 fLog=NULL;
1175
1176 if (fDisplay)
1177 fDisplay->RecursiveRemove(obj);
1178
1179 if (fLog)
1180 fLog->RecursiveRemove(obj);
1181
1182 TObject::RecursiveRemove(obj);
1183}
1184
Note: See TracBrowser for help on using the repository browser.