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

Last change on this file since 4422 was 4081, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 20.2 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-2004
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, AsciiWrite
59
60#include <TEnv.h> // Env::Lookup
61#include <TClass.h> // IsA
62#include <TObjArray.h> // TObjArray
63#include <TBaseClass.h> // GetClassPointer
64#include <TMethodCall.h> // TMethodCall, AsciiWrite
65#include <TDataMember.h> // TDataMember, AsciiWrite
66#include <TVirtualPad.h> // gPad
67
68#include "MString.h"
69
70#include "MLog.h"
71#include "MLogManip.h"
72
73TList *gListOfPrimitives; // forard declaration in MParContainer.h
74
75#undef DEBUG
76//#define DEBUG
77
78ClassImp(MParContainer);
79
80using namespace std;
81
82MParContainer::MParContainer(const char *name, const char *title) :
83 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
84{
85}
86
87MParContainer::MParContainer(const TString &name, const TString &title) :
88 fName(name), fTitle(title), fLog(&gLog), fDisplay(NULL), fReadyToSave(kFALSE)
89{
90}
91
92// --------------------------------------------------------------------------
93//
94// MParContainer copy ctor
95//
96MParContainer::MParContainer(const MParContainer &named)
97{
98 fName = named.fName;
99 fTitle = named.fTitle;
100
101 fLog = named.fLog;
102
103 fReadyToSave = named.fReadyToSave;
104
105 fDisplay = named.fDisplay;
106}
107
108MParContainer::~MParContainer()
109{
110#ifdef DEBUG
111 *fLog << all << "Deleting " << GetDescriptor() << endl;
112#endif
113}
114
115// --------------------------------------------------------------------------
116//
117// MParContainer assignment operator.
118//
119MParContainer& MParContainer::operator=(const MParContainer& rhs)
120{
121 if (this == &rhs)
122 return *this;
123
124 TObject::operator=(rhs);
125
126 fName = rhs.fName;
127 fTitle = rhs.fTitle;
128
129 fLog = rhs.fLog;
130 fReadyToSave = rhs.fReadyToSave;
131
132 return *this;
133}
134
135// --------------------------------------------------------------------------
136//
137// Make a clone of an object using the Streamer facility.
138// If newname is specified, this will be the name of the new object
139//
140TObject *MParContainer::Clone(const char *newname) const
141{
142
143 MParContainer *named = (MParContainer*)TObject::Clone();
144 if (newname && strlen(newname)) named->SetName(newname);
145 return named;
146}
147
148// --------------------------------------------------------------------------
149//
150// Compare two MParContainer objects. Returns 0 when equal, -1 when this is
151// smaller and +1 when bigger (like strcmp).
152//
153Int_t MParContainer::Compare(const TObject *obj) const
154{
155 if (this == obj) return 0;
156 return fName.CompareTo(obj->GetName());
157}
158
159// --------------------------------------------------------------------------
160//
161// Copy this to obj.
162//
163void MParContainer::Copy(TObject &obj)
164#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
165const
166#endif
167{
168 MParContainer &cont = (MParContainer&)obj;
169
170 TObject::Copy(obj);
171
172 cont.fName = fName;
173 cont.fTitle = fTitle;
174
175 cont.fLog = fLog;
176 cont.fReadyToSave = fReadyToSave;
177}
178
179// --------------------------------------------------------------------------
180//
181// Encode MParContainer into output buffer.
182//
183void MParContainer::FillBuffer(char *&buffer)
184{
185 fName.FillBuffer(buffer);
186 fTitle.FillBuffer(buffer);
187}
188
189// --------------------------------------------------------------------------
190//
191// Returns the name of the object. If the name of the object is not the
192// class name it returns the object name and in []-brackets the class name.
193//
194const char *MParContainer::GetDescriptor() const
195{
196 //
197 // Because it returns a (const char*) we cannot return a casted
198 // local TString. The pointer would - immediatly after return -
199 // point to a random memory segment, because the TString has gone.
200 //
201 MString desc;
202 desc.Print("%s [%s]", fName.Data(), ClassName());
203 return fName==ClassName() ? ClassName() : desc.Data();
204}
205
206// --------------------------------------------------------------------------
207//
208// Return a unique name for this container. It is created from
209// the container name and the unique Id. (This is mostly used
210// in the StreamPrimitive member functions)
211//
212const TString MParContainer::GetUniqueName() const
213{
214 TString ret = ToLower(fName);
215
216 if (isdigit(ret[ret.Length()-1]))
217 ret+="_";
218
219 ret+=GetUniqueID();
220
221 return ret;
222}
223
224// --------------------------------------------------------------------------
225//
226// List MParContainer name and title.
227//
228void MParContainer::ls(Option_t *) const
229{
230 TROOT::IndentLevel();
231 *fLog << all << GetDescriptor() << " " << GetTitle() << ": kCanDelete=";
232 *fLog << Int_t(TestBit(kCanDelete)) << endl;
233}
234
235// --------------------------------------------------------------------------
236//
237// Print MParContainer name and title.
238//
239void MParContainer::Print(Option_t *) const
240{
241 *fLog << all << GetDescriptor() << " " << GetTitle() << endl;
242}
243
244// --------------------------------------------------------------------------
245//
246// Change (i.e. set) the name of the MParContainer.
247// WARNING !!
248// If the object is a member of a THashTable, THashList container
249// The HashTable must be Rehashed after SetName
250// For example the list of objects in the current directory is a THashList
251//
252void MParContainer::SetName(const char *name)
253{
254 fName = name;
255 ResetBit(kIsSavedAsPrimitive);
256 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
257}
258
259// --------------------------------------------------------------------------
260//
261// Change (i.e. set) all the MParContainer parameters (name and title).
262// See also WARNING in SetName
263//
264void MParContainer::SetObject(const char *name, const char *title)
265{
266 fName = name;
267 fTitle = title;
268 ResetBit(kIsSavedAsPrimitive);
269 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
270}
271
272// --------------------------------------------------------------------------
273//
274// Change (i.e. set) the title of the MParContainer.
275//
276void MParContainer::SetTitle(const char *title)
277{
278 fTitle = title;
279 ResetBit(kIsSavedAsPrimitive);
280 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
281}
282
283// --------------------------------------------------------------------------
284//
285// Return size of the MParContainer part of the TObject.
286//
287Int_t MParContainer::Sizeof() const
288{
289 Int_t nbytes = fName.Sizeof() + fTitle.Sizeof();
290 return nbytes;
291}
292
293// --------------------------------------------------------------------------
294//
295// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
296// container, overload this function.
297//
298void MParContainer::AsciiRead(ifstream &fin)
299{
300 *fLog << warn << "To use the the ascii input of " << GetName();
301 *fLog << " you have to overload " << ClassName() << "::AsciiRead." << endl;
302}
303
304// --------------------------------------------------------------------------
305//
306// Write out a data member given as a TDataMember object to an output stream.
307//
308Bool_t MParContainer::WriteDataMember(ostream &out, const TDataMember *member, Double_t scale) const
309{
310 if (!member)
311 return kFALSE;
312
313 if (!member->IsPersistent() || member->Property()&kIsStatic)
314 return kFALSE;
315
316 /*const*/ TMethodCall *call = ((TDataMember*)member)->GetterMethod(); //FIXME: Root
317 if (!call)
318 {
319 *fLog << warn << "Sorry, no getter method found for " << member->GetName() << endl;
320 return kFALSE;
321 }
322
323 // For debugging: out << member->GetName() << ":";
324
325 switch (call->ReturnType())
326 {
327 case TMethodCall::kLong:
328 Long_t l;
329 call->Execute((void*)this, l); // FIXME: const, root
330 out << l << " ";
331 return kTRUE;
332
333 case TMethodCall::kDouble:
334 Double_t d;
335 call->Execute((void*)this, d); // FIXME: const, root
336 out << (scale*d) << " ";
337 return kTRUE;
338
339 default:
340 //case TMethodCall::kString:
341 //case TMethodCall::kOther:
342 /* someone may want to enhance this? */
343 return kFALSE;
344 }
345}
346
347// --------------------------------------------------------------------------
348//
349// Write out a data member given by name to an output stream.
350//
351Bool_t MParContainer::WriteDataMember(ostream &out, const char *member, Double_t scale) const
352{
353 /*const*/ TClass *cls = IsA()->GetBaseDataMember(member);
354 if (!cls)
355 return kFALSE;
356
357 return WriteDataMember(out, cls->GetDataMember(member), scale);
358}
359
360// --------------------------------------------------------------------------
361//
362// Write out a data member from a given TList of TDataMembers.
363// returns kTRUE when at least one member was successfully written
364//
365Bool_t MParContainer::WriteDataMember(ostream &out, const TList *list) const
366{
367 Bool_t rc = kFALSE;
368
369 TDataMember *data = NULL;
370
371 TIter Next(list);
372 while ((data=(TDataMember*)Next()))
373 rc |= WriteDataMember(out, data);
374
375 return rc;
376}
377
378// --------------------------------------------------------------------------
379//
380// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
381// container, you may overload this function. If you don't overload it
382// the data member of a class are written to the file in the order of
383// appearance in the class header (be more specfic: root dictionary)
384// Only data members which are of integer (Bool_t, Int_t, ...) or
385// floating point (Float_t, Double_t, ...) type are written.
386// returns kTRUE when at least one member was successfully written
387//
388Bool_t MParContainer::AsciiWrite(ostream &out) const
389{
390 // *fLog << warn << "To use the the ascii output of " << GetName();
391 // *fLog << " you have to overload " << ClassName() << "::AsciiWrite." << endl;
392
393 Bool_t rc = WriteDataMember(out, IsA()->GetListOfDataMembers());
394
395 TIter NextBaseClass(IsA()->GetListOfBases());
396 TBaseClass *base;
397 while ((base = (TBaseClass*) NextBaseClass()))
398 {
399 /*const*/ TClass *cls = base->GetClassPointer();
400
401 if (!cls)
402 continue;
403
404 if (cls->GetClassVersion())
405 rc |= WriteDataMember(out, cls->GetListOfDataMembers());
406 }
407
408 return rc;
409}
410
411TMethodCall *MParContainer::GetterMethod(const char *name) const
412{
413 const TString n(name);
414 const Int_t pos1 = n.First('.');
415
416 const TString part1 = pos1<0 ? n : n(0, pos1);
417 const TString part2 = pos1<0 ? TString("") : n(pos1+1, n.Length());
418
419 TClass *cls = IsA()->GetBaseDataMember(part1);
420 if (cls)
421 {
422 TDataMember *member = cls->GetDataMember(part1);
423 if (!member)
424 {
425 *fLog << err << "Datamember '" << part1 << "' not in " << GetDescriptor() << endl;
426 return NULL;
427 }
428
429 // This handles returning references of contained objects, eg
430 // class X { TObject fO; TObject &GetO() { return fO; } };
431 if (!member->IsBasic() && !part2.IsNull())
432 {
433 cls = gROOT->GetClass(member->GetTypeName());
434 if (!cls)
435 {
436 *fLog << err << "Datamember " << part1 << " [";
437 *fLog << member->GetTypeName() << "] not in dictionary." << endl;
438 return NULL;
439 }
440 if (!cls->InheritsFrom(MParContainer::Class()))
441 {
442 *fLog << err << "Datamember " << part1 << " [";
443 *fLog << member->GetTypeName() << "] does not inherit from ";
444 *fLog << "MParContainer." << endl;
445 return NULL;
446 }
447
448 const MParContainer *sub = (MParContainer*)((ULong_t)this+member->GetOffset());
449 return sub->GetterMethod(part2);
450 }
451
452 if (member->IsaPointer())
453 {
454 *fLog << warn << "Data-member " << part1 << " is a pointer..." << endl;
455 *fLog << dbginf << "Not yet implemented!" << endl;
456 //TClass *test = gROOT->GetClass(member->GetTypeName());
457 return 0;
458 }
459
460 TMethodCall *call = member->GetterMethod();
461 if (call)
462 return call;
463 }
464
465 *fLog << warn << "No standard access for '" << part1 << "' in ";
466 *fLog << GetDescriptor() << " or one of its base classes." << endl;
467
468 TMethodCall *call = NULL;
469
470 *fLog << warn << "Trying to find MethodCall '" << ClassName();
471 *fLog << "::Get" << part1 << "' instead <LEAKS MEMORY>" << endl;
472 call = new TMethodCall(IsA(), (TString)"Get"+part1, "");
473 if (call->GetMethod())
474 return call;
475
476 delete call;
477
478 *fLog << warn << "Trying to find MethodCall '" << ClassName();
479 *fLog << "::" << part1 << "' instead <LEAKS MEMORY>" << endl;
480 call = new TMethodCall(IsA(), part1, "");
481 if (call->GetMethod())
482 return call;
483
484 delete call;
485
486 *fLog << err << "Sorry, no getter method found for " << part1 << endl;
487 return NULL;
488}
489
490// --------------------------------------------------------------------------
491//
492// Implementation of SavePrimitive. Used to write the call to a constructor
493// to a macro. In the original root implementation it is used to write
494// gui elements to a macro-file.
495//
496void MParContainer::SavePrimitive(ofstream &out, Option_t *o)
497{
498 static UInt_t uid = 0;
499
500 if (IsSavedAsPrimitive())
501 return;
502
503 SetUniqueID(uid++);
504 SetBit(kIsSavedAsPrimitive);
505
506 if (gListOfPrimitives && !gListOfPrimitives->FindObject(this))
507 gListOfPrimitives->Add(this);
508
509 StreamPrimitive(out);
510}
511
512// --------------------------------------------------------------------------
513//
514// Creates the string written by SavePrimitive and returns it.
515//
516void MParContainer::StreamPrimitive(ofstream &out) const
517{
518 out << " // Using MParContainer::StreamPrimitive" << endl;
519 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
520 out << fName << "\", \"" << fTitle << "\");" << endl;
521}
522
523void MParContainer::GetNames(TObjArray &arr) const
524{
525 arr.AddLast(new TNamed(fName, fTitle));
526}
527
528void MParContainer::SetNames(TObjArray &arr)
529{
530 TNamed *name = (TNamed*)arr.First();
531
532 fName = name->GetName();
533 fTitle = name->GetTitle();
534
535 delete arr.Remove(name);
536 arr.Compress();
537}
538
539// --------------------------------------------------------------------------
540//
541// Creates a new instance of this class. The idea is to create a clone of
542// this class in its initial state.
543//
544MParContainer *MParContainer::New() const
545{
546 return (MParContainer*)IsA()->New();
547}
548
549// --------------------------------------------------------------------------
550//
551// Read the contents/setup of a parameter container/task from a TEnv
552// instance (steering card/setup file).
553// The key to search for in the file should be of the syntax:
554// prefix.vname
555// While vname is a name which is specific for a single setup date
556// (variable) of this container and prefix is something like:
557// evtloopname.name
558// While name is the name of the containers/tasks in the parlist/tasklist
559//
560// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
561// Job4.MImgCleanStd.CleaningLevel2: 2.5
562//
563// If this cannot be found the next step is to search for
564// MImgCleanStd.CleaningLevel1: 3.0
565// And if this doesn't exist, too, we should search for:
566// CleaningLevel1: 3.0
567//
568// Warning: The programmer is responsible for the names to be unique in
569// all Mars classes.
570//
571Bool_t MParContainer::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
572{
573 if (!IsEnvDefined(env, prefix, "", print))
574 return kFALSE;
575
576 *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but no " << ClassName() << "::ReadEnv." << endl;
577 return kTRUE;
578}
579
580// --------------------------------------------------------------------------
581//
582// Write the contents/setup of a parameter container/task to a TEnv
583// instance (steering card/setup file).
584// The key to search for in the file should be of the syntax:
585// prefix.vname
586// While vname is a name which is specific for a single setup date
587// (variable) of this container and prefix is something like:
588// evtloopname.name
589// While name is the name of the containers/tasks in the parlist/tasklist
590//
591// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
592// Job4.MImgCleanStd.CleaningLevel2: 2.5
593//
594// If this cannot be found the next step is to search for
595// MImgCleanStd.CleaningLevel1: 3.0
596// And if this doesn't exist, too, we should search for:
597// CleaningLevel1: 3.0
598//
599// Warning: The programmer is responsible for the names to be unique in
600// all Mars classes.
601//
602Bool_t MParContainer::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
603{
604 if (!IsEnvDefined(env, prefix, "", print))
605 return kFALSE;
606
607 *fLog << warn << "WARNING - Resource " << prefix+fName << " found, but " << ClassName() << "::WriteEnv not overloaded." << endl;
608 return kTRUE;
609}
610
611Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString prefix, TString postfix, Bool_t print) const
612{
613 if (!postfix.IsNull())
614 postfix.Insert(0, ".");
615
616 return IsEnvDefined(env, prefix+postfix, print);
617}
618
619Bool_t MParContainer::IsEnvDefined(const TEnv &env, TString name, Bool_t print) const
620{
621 if (print)
622 *fLog << all << GetDescriptor() << " - " << name << "... " << flush;
623
624 if (!((TEnv&)env).Defined(name))
625 {
626 if (print)
627 *fLog << "not found." << endl;
628 return kFALSE;
629 }
630
631 if (print)
632 *fLog << "found." << endl;
633
634 return kTRUE;
635}
636
637Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Int_t dflt) const
638{
639 return GetEnvValue(env, prefix+"."+postfix, dflt);
640}
641
642Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, Double_t dflt) const
643{
644 return GetEnvValue(env, prefix+"."+postfix, dflt);
645}
646
647const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, TString postfix, const char *dflt) const
648{
649 return GetEnvValue(env, prefix+"."+postfix, dflt);
650}
651
652Int_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Int_t dflt) const
653{
654 return ((TEnv&)env).GetValue(prefix, dflt);
655}
656
657Double_t MParContainer::GetEnvValue(const TEnv &env, TString prefix, Double_t dflt) const
658{
659 return ((TEnv&)env).GetValue(prefix, dflt);
660}
661
662const char *MParContainer::GetEnvValue(const TEnv &env, TString prefix, const char *dflt) const
663{
664 return ((TEnv&)env).GetValue(prefix, dflt);
665}
Note: See TracBrowser for help on using the repository browser.