source: trunk/MagicSoft/Mars/mbase/MParList.cc@ 9345

Last change on this file since 9345 was 9345, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 29.0 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@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26// //
27// MParList //
28// //
29// This class contains a list of different parameter containers. //
30// //
31// A parameter container is an object which is derived from //
32// MParContainer. //
33// //
34// Normally a parameter container is used for data exchange between two //
35// tasks at runtime. //
36// //
37// You can add every parameter container (Named object) to the //
38// instance and access it from somewhere else via its Name. //
39// //
40/////////////////////////////////////////////////////////////////////////////
41#include "MParList.h"
42
43#include <fstream> // ofstream, SavePrimitive
44
45#include <TNamed.h>
46#include <TClass.h>
47#include <TOrdCollection.h>
48
49#include "MLog.h"
50#include "MLogManip.h"
51
52#include "MIter.h"
53#include "MParEnv.h"
54#include "MTaskList.h"
55
56ClassImp(MParList);
57
58using namespace std;
59
60static const TString gsDefName = "MParList";
61static const TString gsDefTitle = "A list of Parameter Containers";
62
63// --------------------------------------------------------------------------
64//
65// creates an empty list
66//
67void MParList::Init(const char *name, const char *title)
68{
69 fName = name ? name : gsDefName.Data();
70 fTitle = title ? title : gsDefTitle.Data();
71
72 //
73 // This sets a flag that the list is the owner, which means
74 // that the destructor of the list deletes all it's objects
75 //
76 fContainer = new TOrdCollection;
77 fAutodelete = new TOrdCollection;
78
79 gROOT->GetListOfCleanups()->Add(fContainer);
80 gROOT->GetListOfCleanups()->Add(fAutodelete);
81 fContainer->SetBit(kMustCleanup);
82 fAutodelete->SetBit(kMustCleanup);
83}
84
85
86// --------------------------------------------------------------------------
87//
88// default constructor
89// creates an empty list
90//
91MParList::MParList(const char *name, const char *title)
92{
93 Init(name, title);
94}
95
96// --------------------------------------------------------------------------
97//
98// Copy constructor. It copies all entries of the parameter list, but it
99// takes care of, that the automatically created entries are only deleted
100// once. (doesn't copy the list which holds the automatically created
101// entries)
102//
103MParList::MParList(const MParList &ts, const char *name, const char *title) : MParContainer()
104{
105 Init(name, title);
106
107 fContainer->AddAll(ts.fContainer);
108}
109
110// --------------------------------------------------------------------------
111//
112// If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
113// by the destructor
114//
115MParList::~MParList()
116{
117 //
118 // Case:
119 // 1) MParList is owner of the containers:
120 // All container are stored in fContainer, and become deleted by
121 // 'delete fContainer'. Some of these containers, which were
122 // created automatically are stored in fAutodelete, too. To prevent
123 // double deletion this containers are not deleted by the destructor
124 // of fAutodelete.
125 // 2) MParList is not owner of the containers:
126 // The containers which were Added by AddToList are not touched.
127 // Only the containers which were created automatically are also
128 // automatically deleted.
129 //
130 IsOwner() ? fContainer->SetOwner() : fAutodelete->SetOwner();
131
132 TIter Next(fContainer);
133 TObject *o=0;
134 while ((o=Next()))
135 if (o->TestBit(kCanDelete))
136 delete fContainer->Remove(o);
137
138 // FIXME? If fContainer is owner do we have to remove the object
139 // from fAutodelete due to the access when checking for a
140 // garbage collection?
141 delete fContainer;
142 delete fAutodelete;
143}
144
145// --------------------------------------------------------------------------
146//
147// If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
148// by the destructor
149//
150void MParList::SetOwner(Bool_t enable)
151{
152 enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
153}
154
155// --------------------------------------------------------------------------
156//
157// Set the logging streamer of the parameter list and all contained
158// parameter containers
159//
160void MParList::SetLogStream(MLog *log)
161{
162 fContainer->R__FOR_EACH(MParContainer, SetLogStream)(log);
163 MParContainer::SetLogStream(log);
164}
165
166void MParList::SetDisplay(MStatusDisplay *d)
167{
168 fContainer->R__FOR_EACH(MParContainer, SetDisplay)(d);
169 MParContainer::SetDisplay(d);
170}
171
172// --------------------------------------------------------------------------
173//
174// Add a single container to the list.
175//
176// If 'where' is given, the object will be added after this.
177//
178Bool_t MParList::AddToList(MParContainer *cont, MParContainer *where)
179{
180 //
181 // check if the object (you want to add) exists
182 //
183 if (!cont)
184 return kFALSE;
185
186 //
187 // Get Name of new container
188 //
189 const char *name = cont->GetName();
190
191 //
192 // Check if the new container is already existing in the list
193 //
194 const TObject *objn = fContainer->FindObject(name);
195 const TObject *objt = fContainer->FindObject(cont);
196
197 if (objn || objt)
198 {
199 //
200 // If the container is already in the list ignore it.
201 //
202 if (objt || objn==cont)
203 {
204 *fLog << warn << dbginf << "Warning: Container '" << cont->GetName() << ", 0x" << (void*)cont;
205 *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
206 return kTRUE;
207 }
208
209 //
210 // Otherwise add it to the list, but print a warning message
211 //
212 *fLog << warn << dbginf << "Warning: Container with the same name '" << cont->GetName();
213 *fLog << "' already existing in '" << GetName() << "'." << endl;
214 *fLog << "You may not be able to get a pointer to container task by name." << endl;
215 }
216
217 //
218 // check if you want to add the new parameter container somewhere
219 // special (in that case you specify "where")
220 //
221 if (where)
222 {
223 if (!fContainer->FindObject(where))
224 {
225 *fLog << dbginf << "Error: Cannot find parameter container after which the new one should be added!" << endl;
226 return kFALSE;
227 }
228 }
229
230 if (!cont->InheritsFrom(MParContainer::Class()))
231 {
232 *fLog << dbginf << "Error: Cantainer MUST derive from MParContainer!" << endl;
233 return kFALSE;
234 }
235
236 *fLog << inf3 << "Adding " << name << " to " << GetName() << "... " << flush;
237
238 cont->SetBit(kMustCleanup);
239 fContainer->Add(cont);
240 *fLog << "done." << endl;
241
242 return kTRUE;
243}
244
245// --------------------------------------------------------------------------
246//
247// Add all entries which derive from MParContainer
248// of the TObjArray to the list.
249//
250void MParList::AddToList(TObjArray *list)
251{
252 //
253 // check if the object (you want to add) exists
254 //
255 if (!list)
256 return;
257
258 TIter Next(list);
259
260 TObject *cont = NULL;
261 while ((cont=Next()))
262 {
263 if (!dynamic_cast<MParContainer*>(cont))
264 continue;
265
266 cont->SetBit(kMustCleanup);
267 AddToList(cont);
268 }
269}
270
271// --------------------------------------------------------------------------
272//
273// Find an object with the same name in the list and replace it with
274// the new one. If the kIsOwner flag is set and the object was not
275// created automatically, the object is deleted.
276//
277Bool_t MParList::Replace(MParContainer *cont)
278{
279 //
280 // check if the object (you want to add) exists
281 //
282 if (!cont)
283 return kFALSE;
284
285 TObject *obj = FindObject(cont->GetName());
286 if (!obj)
287 {
288 *fLog << warn << "No object with the same name '";
289 *fLog << cont->GetName() << "' in list... adding." << endl;
290 return AddToList(cont);
291 }
292
293 fContainer->Remove(obj);
294
295 if (IsOwner() && !fAutodelete->FindObject(obj))
296 delete obj;
297
298 *fLog << inf2 << "MParContainer '" << cont->GetName() << "' found and replaced..." << endl;
299
300 return AddToList(cont);
301}
302
303// --------------------------------------------------------------------------
304//
305// Find an object with the same name in the list and remove it.
306// If the kIsOwner flag is set and the object was not created
307// automatically, the object is deleted.
308//
309void MParList::Remove(MParContainer *cont)
310{
311 //
312 // check if the object (you want to add) exists
313 //
314 if (!cont)
315 return;
316
317 TObject *obj = fContainer->Remove(cont);
318
319 fContainer->RecursiveRemove(obj);
320
321 // if (!obj)
322 // {
323 // *fLog << warn << "Object not found in list..." << endl;
324 // return;
325 //}
326
327// *fLog << inf << "MParContainer '" << cont->GetName() << "' removed..." << endl;
328
329 if (obj && IsOwner() && !fAutodelete->FindObject(obj))
330 delete obj;
331}
332
333// --------------------------------------------------------------------------
334//
335// Call MParContainer::RecursiveRemove
336// Call fContainer->RecursiveRemove
337//
338void MParList::RecursiveRemove(TObject *obj)
339{
340 MParContainer::RecursiveRemove(obj);
341
342 if (obj==fContainer)
343 fContainer = NULL;
344
345 if (fContainer)
346 fContainer->RecursiveRemove(obj);
347}
348
349// --------------------------------------------------------------------------
350//
351// Find an object in the list.
352// 'name' is the name of the object you are searching for.
353//
354TObject *MParList::FindObject(const char *name) const
355{
356 TObject *obj = fContainer->FindObject(name);
357 const MParEnv *env = dynamic_cast<const MParEnv*>(obj);
358 return env ? env->GetCont() : obj;
359}
360
361// --------------------------------------------------------------------------
362//
363// check if the object is in the list or not
364//
365TObject *MParList::FindObject(const TObject *obj) const
366{
367 TObject *ret = fContainer->FindObject(obj);
368 const MParEnv *env = dynamic_cast<const MParEnv*>(ret);
369 return env ? env->GetCont() : ret;
370}
371
372// --------------------------------------------------------------------------
373//
374// Find an object in the list and check for the correct inheritance.
375// 'name' is the name of the object you are searching for.
376//
377// In words: Find object name and check whether it inherits from classname
378//
379TObject *MParList::FindObject(const char *name, const char *classname) const
380{
381 TObject *obj = FindObject(name);
382
383 if (!obj)
384 return NULL;
385
386 if (obj->InheritsFrom(classname))
387 return obj;
388
389 *fLog << dbginf << warn << "Found object '" << name << "' doesn't ";
390 *fLog << "inherit from " << "'" << classname << "' but from '";
391 *fLog << obj->ClassName() << "'" << endl;
392 return NULL;
393}
394
395// --------------------------------------------------------------------------
396//
397// check if the object is in the list or not and check for the correct
398// inheritance
399//
400TObject *MParList::FindObject(const TObject *obj, const char *classname) const
401{
402 TObject *nobj = FindObject(obj);
403
404 if (!nobj)
405 return NULL;
406
407 if (nobj->InheritsFrom(classname))
408 return nobj;
409
410 *fLog << dbginf << warn << "Found object '" << nobj->GetName() << "' ";
411 *fLog << "doesn't inherit from " << "'" << classname << "'" << endl;
412 return NULL;
413}
414
415// --------------------------------------------------------------------------
416//
417// Searches for the tasklist tlist (default: MTaskList) and returns
418// a task with the given name found in this list. If one of both isn't
419// found NULL is returned
420//
421MTask *MParList::FindTask(const char *name, const char *tlist) const
422{
423 TObject *l = FindObject(tlist, "MTaskList");
424 return (MTask*)(l ? l->FindObject(name) : NULL);
425}
426
427// --------------------------------------------------------------------------
428//
429// Find a tasklist which contains a task with name name
430//
431MTaskList *MParList::FindTaskListWithTask(const char *name) const
432{
433 TIter Next(fContainer);
434 TObject *o=0;
435 while ((o=Next()))
436 {
437 MTaskList *l1 = dynamic_cast<MTaskList*>(o);
438 if (!l1)
439 continue;
440
441 MTaskList *l2 = l1->FindTaskList(name);
442 if (l2)
443 return l2;
444 }
445 return 0;
446}
447
448// --------------------------------------------------------------------------
449//
450// Find a tasklist which contains a task task
451//
452MTaskList *MParList::FindTaskListWithTask(const MTask *task) const
453{
454 TIter Next(fContainer);
455 TObject *o=0;
456 while ((o=Next()))
457 {
458 MTaskList *l1 = dynamic_cast<MTaskList*>(o);
459 if (!l1)
460 continue;
461
462 MTaskList *l2 = l1->FindTaskList(task);
463 if (l2)
464 return l2;
465 }
466 return 0;
467}
468
469// --------------------------------------------------------------------------
470//
471// returns the ClassName without anything which is behind that last ';' in
472// string.
473//
474TString MParList::GetClassName(const char *classname)
475{
476 TString cname(classname);
477 const char *semicolon = strrchr(cname, ';');
478
479 if (semicolon)
480 cname.Remove(semicolon-cname);
481
482 return cname;
483}
484
485// --------------------------------------------------------------------------
486//
487// returns the ObjectName. It is created from a class and object name.
488// If no object name is given the objectname is the same than the
489// class name. Leading dots are removed from the object name
490//
491TString MParList::GetObjectName(const char *classname, const char *objname)
492{
493 TString cname(classname);
494 const char *semicolon = strrchr(cname, ';');
495
496 TString oname(objname ? objname : classname);
497
498 if (semicolon)
499 {
500 //
501 // Remove leading dots from objectname (eg. "MMcTrig;5.")
502 //
503 Int_t sz = oname.Sizeof()-2;
504
505 while (sz>=0 && oname[sz]=='.')
506 oname.Remove(sz--);
507 }
508 return oname;
509}
510
511// --------------------------------------------------------------------------
512//
513// Find an object in the list.
514// 'name' is the name of the object you are searching for.
515// If the object doesn't exist we try to create one from the
516// dictionary. If this isn't possible NULL is returned.
517//
518// An object which was created automatically is deleted automatically in
519// the destructor of the parameter list, too. This means, that if an
520// object should survive (eg. Histograms) you MUST create it by yourself
521// and add it to the parameter list.
522//
523// By default (you don't specify an object name) the object name is
524// the same as the classname
525//
526// If the classname (default classname) is of the structure
527// "Name;something" - containing a semicolon - evarything which is
528// after the last appearance of a semicolon is stripped to get the
529// Name of the Class. Normally this is used to number your objects.
530// "Name;1", "Name;2", ... If a semicolon is detected leading dots
531// are stripped from the object-name (eg. "name;5.")
532//
533// In words: Create object of type classname and set its name to objname.
534// If an object with objname already exists return it.
535//
536MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
537{
538 //
539 // If now object name (name of the object to identify it in the
540 // List) is given use it's classname as the objectname
541 //
542
543 //
544 // Check if the classname is a 'numbered' name (like: "MTime;2")
545 // if so strip the number from the classname.
546 //
547 // Becareful: We check for the last occurance of a ';' only and we
548 // also don't check if a number follows or something else.
549 //
550 // Rem: I use a TString to make the code more readyble and to get
551 // the new object deleted automatically
552 //
553 TString cname = GetClassName(classname);
554 TString oname = GetObjectName(classname, objname);
555
556 //
557 // Try to find a object with this object name which is already
558 // in the List. If we can find one we are done.
559 //
560 MParContainer *pcont = (MParContainer*)FindObject(oname);
561
562 if (pcont)
563 {
564 if (pcont->InheritsFrom(cname))
565 return pcont;
566
567 *fLog << err << "Warning: Object '" << oname << "' found in list doesn't inherit from " << cname << "." << endl;
568 return NULL;
569 }
570
571 //
572 // if object is not existing in the list try to create one
573 //
574 *fLog << inf2 << "Object '" << oname << "' ";
575 if (oname!=cname)
576 *fLog << "[" << cname << "] ";
577 *fLog << "not yet in " << GetName() << "... creating." << endl;
578
579 //
580 // try to get class from root environment
581 //
582 *fLog << err;
583 TClass *cls = GetClass(cname, fLog);
584 if (!cls)
585 return NULL;
586
587 if (!cls->InheritsFrom(MParContainer::Class()))
588 {
589 *fLog << err << dbginf << "Cannot create new instance of class '" << cname << "': " << endl;
590 *fLog << "Class doesn't inherit from MParContainer." << endl;
591 return NULL;
592 }
593
594 //
595 // create the parameter container of the the given class type
596 //
597 pcont = (MParContainer*)cls->New();
598 if (!pcont)
599 {
600 *fLog << err << dbginf << "Cannot create new instance of class '" << cname << "': " << endl;
601 *fLog << " - Class has no default constructor." << endl;
602 *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
603 return NULL;
604 }
605
606 //
607 // Set the name of the container
608 //
609 pcont->SetName(oname);
610
611 //
612 // Now add the object to the parameter list
613 //
614 AddToList(pcont);
615
616 //
617 // The object was automatically created. This makes sure, that such an
618 // object is deleted together with the list
619 //
620 fAutodelete->Add(pcont);
621
622 //
623 // Find an object in the list.
624 // 'name' is the name of the object you are searching for.
625 //
626 return pcont;
627}
628
629// --------------------------------------------------------------------------
630//
631// print some information about the current status of MParList
632//
633void MParList::Print(Option_t *) const
634{
635 *fLog << all << underline << GetDescriptor() << ":" << endl;
636
637 MParContainer *obj = NULL;
638 MIter Next(fContainer);
639 while ((obj=Next()))
640 {
641 *fLog << " " << obj->GetDescriptor();
642 if (fAutodelete->FindObject(obj))
643 *fLog << " <autodel>";
644 *fLog << endl;
645 }
646 *fLog << endl;
647}
648
649// --------------------------------------------------------------------------
650//
651// Sets the flags off all containers in the list (and the list
652// itself) to unchanged
653//
654void MParList::SetReadyToSave(Bool_t flag)
655{
656 fContainer->R__FOR_EACH(MParContainer, SetReadyToSave)(flag);
657 MParContainer::SetReadyToSave(flag);
658}
659
660// --------------------------------------------------------------------------
661//
662// Reset all containers in the list
663//
664void MParList::Reset()
665{
666 fContainer->R__FOR_EACH(MParContainer, Reset)();
667}
668
669// --------------------------------------------------------------------------
670//
671// This finds numbered objects. The objects are returned in a copy of a
672// TObjArray.
673//
674// If from only is given (or to=0) object are assumed numbered
675// from 1 to from.
676//
677TObjArray MParList::FindObjectList(const char *name, UInt_t first, const UInt_t last) const
678{
679 TObjArray list;
680
681 if (first>0 && last<first)
682 {
683 *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
684 return list;
685 }
686
687 const UInt_t len = strlen(name);
688
689 char *auxname = new char[len+7];
690 strcpy(auxname, name);
691
692 if (first==0 && last!=0)
693 first = 1;
694
695 //
696 // If only 'from' is specified the number of entries are ment
697 //
698 for (UInt_t i=first; i<=last; i++)
699 {
700 if (first!=0 || last!=0)
701 sprintf(auxname+len, ";%d", i);
702
703 TObject *obj = FindObject(auxname);
704 if (!obj)
705 continue;
706
707 list.AddLast(obj);
708 }
709 delete auxname;
710
711 return list;
712}
713
714// --------------------------------------------------------------------------
715//
716// This finds numbered objects. The objects are returned in a copy of a
717// TObjArray. If one of the objects doesn't exist it is created from the
718// meaning of cname and oname (s. FindCreateObj)
719//
720// If from only is given (or to=0) object are assumed numbered
721// from 1 to from.
722//
723TObjArray MParList::FindCreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *oname)
724{
725 TObjArray list;
726
727 if (first>0 && last<first)
728 {
729 *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
730 return list;
731 }
732
733 const UInt_t len = strlen(cname);
734
735 char *auxname = new char[len+7];
736 strcpy(auxname, cname);
737
738 //
739 // If only 'from' is specified the number of entries are ment
740 //
741 if (first==0 && last!=0)
742 first = 1;
743
744 for (UInt_t i=first; i<=last; i++)
745 {
746 if (first!=0 || last!=0)
747 sprintf(auxname+len, ";%d", i);
748
749 TObject *obj = FindCreateObj(auxname, oname);
750 if (!obj)
751 break;
752
753 list.AddLast(obj);
754 }
755 delete auxname;
756
757 return list;
758}
759
760// --------------------------------------------------------------------------
761//
762// This finds numbered objects. The objects are returned in a copy of a
763// TObjArray. If one of the objects doesn't exist it is created from the
764// meaning of cname and oname (s. FindCreateObj)
765//
766// If from only is given (or to=0) object are assumed numbered
767// from 1 to from.
768//
769// Remark: Because it is static the object are only created and not added to
770// the parameter list. You must also take care of deleting these objects!
771// This function is mainly made for use in root macros. Don't use it in
772// compiled programs if you are not 100% sure what you are doing.
773//
774TObjArray MParList::CreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *)
775{
776 TObjArray list;
777
778 if (first>0 && last<first)
779 {
780 gLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
781 return list;
782 }
783
784 //
785 // try to get class from root environment
786 //
787 gLog << err;
788 TClass *cls = GetClass(cname, &gLog);
789 if (!cls)
790 return list;
791
792 if (!cls->InheritsFrom(MParContainer::Class()))
793 {
794 gLog << err << dbginf << "Cannot create new instance of class '" << cname << "': " << endl;
795 gLog << "Class doesn't inherit from MParContainer." << endl;
796 return list;
797 }
798
799 const UInt_t len = strlen(cname);
800
801 char *auxname = new char[len+7];
802 strcpy(auxname, cname);
803
804 //
805 // If only 'from' is specified the number of entries are ment
806 //
807 if (first==0 && last!=0)
808 first = 1;
809
810 for (UInt_t i=first; i<=last; i++)
811 {
812 if (first!=0 || last!=0)
813 sprintf(auxname+len, ";%d", i);
814
815 //
816 // create the parameter container of the the given class type
817 //
818 MParContainer *pcont = (MParContainer*)cls->New();
819 if (!pcont)
820 {
821 gLog << err << dbginf << "Cannot create new instance of class '" << cname << "' (Maybe no def. constructor)" << endl;
822 return list;
823 }
824
825 //
826 // Set the name of the container
827 //
828 pcont->SetName(auxname);
829
830 //
831 // Add new object to the return list
832 //
833 list.AddLast(pcont);
834 }
835 delete auxname;
836
837 return list;
838}
839
840void MParList::SavePrimitive(ostream &out, Option_t *)
841{
842 Bool_t saved = IsSavedAsPrimitive();
843
844 MParContainer::SavePrimitive(out);
845
846 MIter Next(fContainer);
847
848 MParContainer *cont = NULL;
849 while ((cont=Next()))
850 {
851 //
852 // Because it was automatically created don't store its primitive
853 // I guess it will be automatically created again
854 //
855 if (fAutodelete->FindObject(cont) || cont->IsSavedAsPrimitive())
856 continue;
857
858 cont->SavePrimitive(out, "");
859
860 out << " " << GetUniqueName() << ".";
861 out << (cont->InheritsFrom("MTaskList") && saved ? "Replace" : "AddToList");
862 out << "(&" << cont->GetUniqueName() << ");" << endl << endl;
863 }
864}
865
866void MParList::SavePrimitive(ofstream &out, Option_t *o)
867{
868 SavePrimitive(static_cast<ostream&>(out), o);
869}
870
871// --------------------------------------------------------------------------
872//
873// Implementation of SavePrimitive. Used to write the call to a constructor
874// to a macro. In the original root implementation it is used to write
875// gui elements to a macro-file.
876//
877void MParList::StreamPrimitive(ostream &out) const
878{
879 out << " MParList " << GetUniqueName();
880 if (fName!=gsDefName || fTitle!=gsDefTitle)
881 {
882 out << "(\"" << fName << "\"";
883 if (fTitle!=gsDefTitle)
884 out << ", \"" << fTitle << "\"";
885 out <<")";
886 }
887 out << ";" << endl << endl;
888}
889
890// --------------------------------------------------------------------------
891//
892// Adds one TNamed object per object in the list. The TNamed object must
893// be deleted by the user.
894//
895void MParList::GetNames(TObjArray &arr) const
896{
897 MParContainer::GetNames(arr);
898 fContainer->R__FOR_EACH(MParContainer, GetNames)(arr);
899}
900
901// --------------------------------------------------------------------------
902//
903// Sets name and title of each object in the list from the objects in
904// the array.
905//
906void MParList::SetNames(TObjArray &arr)
907{
908 MParContainer::SetNames(arr);
909 fContainer->R__FOR_EACH(MParContainer, SetNames)(arr);
910}
911
912// --------------------------------------------------------------------------
913//
914// Read the contents/setup of a parameter container/task from a TEnv
915// instance (steering card/setup file).
916// The key to search for in the file should be of the syntax:
917// prefix.vname
918// While vname is a name which is specific for a single setup date
919// (variable) of this container and prefix is something like:
920// evtloopname.name
921// While name is the name of the containers/tasks in the parlist/tasklist
922//
923// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
924// Job4.MImgCleanStd.CleaningLevel2: 2.5
925//
926// If this cannot be found the next step is to search for
927// MImgCleanStd.CleaningLevel1: 3.0
928// And if this doesn't exist, too, we search for:
929// CleaningLevel1: 3.0
930//
931// Warning: The programmer is responsible for the names to be unique in
932// all Mars classes.
933//
934Int_t MParList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
935{
936 if (print)
937 *fLog << all << "MParList::ReadEnv: " << prefix << endl;
938
939 MParContainer *cont = NULL;
940
941 MIter Next(fContainer);
942 while ((cont=Next()))
943 {
944 if (cont->InheritsFrom("MTaskList"))
945 {
946 if (cont->ReadEnv(env, prefix, print)==kERROR)
947 return kERROR;
948 continue;
949 }
950
951 if (cont->TestEnv(env, prefix, print)==kERROR)
952 return kERROR;
953 }
954
955 return kTRUE;
956}
957
958// --------------------------------------------------------------------------
959//
960// Write the contents/setup of a parameter container/task to a TEnv
961// instance (steering card/setup file).
962// The key to search for in the file should be of the syntax:
963// prefix.vname
964// While vname is a name which is specific for a single setup date
965// (variable) of this container and prefix is something like:
966// evtloopname.name
967// While name is the name of the containers/tasks in the parlist/tasklist
968//
969// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
970// Job4.MImgCleanStd.CleaningLevel2: 2.5
971//
972// If this cannot be found the next step is to search for
973// MImgCleanStd.CleaningLevel1: 3.0
974// And if this doesn't exist, too, we search for:
975// CleaningLevel1: 3.0
976//
977// Warning: The programmer is responsible for the names to be unique in
978// all Mars classes.
979//
980Bool_t MParList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
981{
982 MParContainer *cont = NULL;
983
984 MIter Next(fContainer);
985 while ((cont=Next()))
986 if (!cont->WriteEnv(env, prefix, print))
987 return kFALSE;
988 return kTRUE;
989}
990
991// --------------------------------------------------------------------------
992//
993// Can be used to create an iterator over all containers, eg:
994// MParList plist;
995// TIter Next(plist); // Be aware: Use a object here rather than a pointer!
996// TObject *o=0;
997// while ((o=Next()))
998// {
999// [...]
1000// }
1001//
1002MParList::operator TIterator*() const
1003{
1004 return new TOrdCollectionIter(fContainer);
1005}
Note: See TracBrowser for help on using the repository browser.