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

Last change on this file since 761 was 754, checked in by tbretz, 24 years ago
*** empty log message ***
File size: 7.7 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 (tbretz@uni-sw.gwdg.de)
19!
20! Copyright: MAGIC Software Development, 2000-2001
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 <TNamed.h>
44#include <TClass.h>
45
46#include "MLog.h"
47#include "MLogManip.h"
48
49ClassImp(MParList)
50
51// --------------------------------------------------------------------------
52//
53// default constructor
54// creates an empty list
55//
56MParList::MParList(const char *name, const char *title)
57{
58 *fName = name ? name : "MParList";
59 *fTitle = title ? title : "List of Parameter Containers";
60
61 //
62 // This sets a flag that the list is the owner, which means
63 // that the destructor of the list deletes all it's objects
64 //
65 fAutodelete.SetOwner();
66}
67
68// --------------------------------------------------------------------------
69//
70// copy constructor
71//
72MParList::MParList(MParList &ts)
73{
74 fContainer.AddAll(&ts.fContainer);
75}
76
77// --------------------------------------------------------------------------
78//
79// create the Iterator over the tasklist
80//
81void MParList::SetLogStream(MLog *log)
82{
83 TIter Next(&fContainer);
84
85 MParContainer *cont=NULL;
86
87 //
88 // loop over all tasks for preproccesing
89 //
90 while ( (cont=(MParContainer*)Next()) )
91 cont->SetLogStream(log);
92
93 MParContainer::SetLogStream(log);
94}
95
96// --------------------------------------------------------------------------
97//
98// Add an Container to the list.
99//
100// If 'where' is given, the object will be added after this.
101//
102Bool_t MParList::AddToList(MParContainer *obj, MParContainer *where)
103{
104 //
105 // check if the object (you want to add) exists
106 //
107
108 if (!obj) return kTRUE;
109
110 *fLog << "Adding " << obj->GetName() << " to " << GetName() << "... " << flush;
111 //
112 // check if it is in the list yet
113 //
114 if (fContainer.FindObject(obj))
115 {
116 *fLog << dbginf << "Container already added" << endl;
117 return kTRUE;
118 }
119
120 //
121 // check if you want to add the new parameter container somewhere
122 // special (in that case you specify "where")
123 //
124 if (where)
125 {
126 if (!fContainer.FindObject(where))
127 {
128 *fLog << dbginf << "Cannot find parameter container after which the new one should be added!" << endl;
129 return kFALSE;
130 }
131 }
132
133 fContainer.Add(obj);
134 *fLog << "Done." << endl;
135
136 return kTRUE;
137}
138
139// --------------------------------------------------------------------------
140//
141// Find an object in the list.
142// 'name' is the name of the object you are searching for.
143//
144TObject *MParList::FindObject(const char *name) const
145{
146 return (TObject*)fContainer.FindObject(name);
147}
148
149// --------------------------------------------------------------------------
150//
151// Find an object in the list.
152// 'name' is the name of the object you are searching for.
153// If the object doesn't exist we try to create one from the
154// dictionary. If this isn't possible NULL is returned.
155//
156// By default (you don't specify an object name) the object name is
157// the same as the classname
158//
159// If the classname (default classname) is of the structure
160// "Name;something" - containing a semicolon - evarything which is
161// after the last appearance of a semicolon is stripped to get the
162// Name of the Class. Normally this is used to number your objects.
163// "Name;1", "Name;2", ...
164//
165MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
166{
167 //
168 // If now object name (name of the object to identify it in the
169 // List) is given use it's classname as the objectname
170 //
171 if (!objname)
172 objname = classname;
173
174 //
175 // Check if the classname is a 'numbered' name (like: "MTime;2")
176 // if so strip the number from the classname.
177 //
178 // Becareful: We check for the last occurance of a ';' only and we
179 // also don't check if a number follows or something else.
180 //
181 // Rem: I use a TString to make the code more readyble and to get
182 // the new object deleted automatically
183 //
184 TString cname(classname);
185 const char *semicolon = strrchr(cname, ';');
186
187 if (semicolon)
188 cname.Remove(semicolon-cname);
189
190 //
191 // Try to find a object with this object name which is already
192 // in the List. If we can find one we are done.
193 //
194 MParContainer *pcont = (MParContainer*)FindObject(objname);
195
196 if (pcont)
197 return pcont;
198
199 //
200 // if object is not existing in the list try to create one
201 //
202 *fLog << dbginf << "Object '" << objname << "' of type '" << cname << "' not found... creating." << endl;
203
204 //
205 // try to get class from root environment
206 //
207 TClass *cls = gROOT->GetClass(cname);
208
209 if (!cls)
210 {
211 //
212 // if class is not existing in the root environment
213 //
214 *fLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
215 return NULL;
216 }
217
218 //
219 // create the parameter container of the the given class type
220 //
221 pcont = (MParContainer*)cls->New();
222
223 //
224 // If a name different to the classname was given,
225 // set the new object name of the object
226 //
227 pcont->SetName(objname);
228
229 //
230 // Now add the object to the parameter list
231 //
232 AddToList(pcont);
233
234 //
235 // The object was automatically created. This makes sure, that such an
236 // object is deleted together with the list
237 //
238 fAutodelete.Add(pcont);
239
240 //
241 // Find an object in the list.
242 // 'name' is the name of the object you are searching for.
243 //
244 return pcont;
245}
246
247// --------------------------------------------------------------------------
248//
249// print some information about the current status of MParList
250//
251void MParList::Print(Option_t *t)
252{
253 *fLog << dbginf << "ParList: " << this->GetName() << " <" << this->GetTitle() << ">" << endl;
254 *fLog << endl;
255
256}
257
Note: See TracBrowser for help on using the repository browser.