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 |
|
---|
49 | ClassImp(MParList);
|
---|
50 |
|
---|
51 | // --------------------------------------------------------------------------
|
---|
52 | //
|
---|
53 | // default constructor
|
---|
54 | // creates an empty list
|
---|
55 | //
|
---|
56 | MParList::MParList(const char *name, const char *title)
|
---|
57 | {
|
---|
58 | *fName = name ? name : "MParList";
|
---|
59 | *fTitle = title ? title : "A 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 | //
|
---|
72 | MParList::MParList(MParList &ts)
|
---|
73 | {
|
---|
74 | fContainer.AddAll(&ts.fContainer);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | //
|
---|
79 | // create the Iterator over the tasklist
|
---|
80 | //
|
---|
81 | void 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 | //
|
---|
102 | Bool_t MParList::AddToList(MParContainer *cont, MParContainer *where)
|
---|
103 | {
|
---|
104 | //
|
---|
105 | // check if the object (you want to add) exists
|
---|
106 | //
|
---|
107 |
|
---|
108 | if (!cont)
|
---|
109 | return kFALSE;
|
---|
110 |
|
---|
111 | //
|
---|
112 | // Get Name of new container
|
---|
113 | //
|
---|
114 | const char *name = cont->GetName();
|
---|
115 |
|
---|
116 | //
|
---|
117 | // Check if the new container is already existing in the list
|
---|
118 | //
|
---|
119 | const TObject *objn = fContainer.FindObject(name);
|
---|
120 | const TObject *objt = fContainer.FindObject(cont);
|
---|
121 |
|
---|
122 | if (objn || objt)
|
---|
123 | {
|
---|
124 | //
|
---|
125 | // If the container is already in the list ignore it.
|
---|
126 | //
|
---|
127 | if (objt || objn==cont)
|
---|
128 | {
|
---|
129 | *fLog << dbginf << "Warning: Container '" << cont->GetName() << ", 0x" << (void*)cont;
|
---|
130 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
131 | return kTRUE;
|
---|
132 | }
|
---|
133 |
|
---|
134 | //
|
---|
135 | // Otherwise add it to the list, but print a warning message
|
---|
136 | //
|
---|
137 | *fLog << dbginf << "Warning: Container with the same name '" << cont->GetName();
|
---|
138 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
139 | *fLog << "You may not be able to get a pointer to container task by name." << endl;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //
|
---|
143 | // check if you want to add the new parameter container somewhere
|
---|
144 | // special (in that case you specify "where")
|
---|
145 | //
|
---|
146 | if (where)
|
---|
147 | {
|
---|
148 | if (!fContainer.FindObject(where))
|
---|
149 | {
|
---|
150 | *fLog << dbginf << "Error: Cannot find parameter container after which the new one should be added!" << endl;
|
---|
151 | return kFALSE;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | *fLog << "Adding " << name << " to " << GetName() << "... " << flush;
|
---|
156 |
|
---|
157 | fContainer.Add(cont);
|
---|
158 | *fLog << "Done." << endl;
|
---|
159 |
|
---|
160 | return kTRUE;
|
---|
161 | }
|
---|
162 |
|
---|
163 | // --------------------------------------------------------------------------
|
---|
164 | //
|
---|
165 | // Find an object in the list.
|
---|
166 | // 'name' is the name of the object you are searching for.
|
---|
167 | //
|
---|
168 | TObject *MParList::FindObject(const char *name) const
|
---|
169 | {
|
---|
170 | return fContainer.FindObject(name);
|
---|
171 | }
|
---|
172 |
|
---|
173 | // --------------------------------------------------------------------------
|
---|
174 | //
|
---|
175 | // check if the object is in the list or not
|
---|
176 | //
|
---|
177 | TObject *MParList::FindObject(TObject *obj) const
|
---|
178 | {
|
---|
179 | return fContainer.FindObject(obj);
|
---|
180 | }
|
---|
181 |
|
---|
182 | // --------------------------------------------------------------------------
|
---|
183 | //
|
---|
184 | // Find an object in the list.
|
---|
185 | // 'name' is the name of the object you are searching for.
|
---|
186 | // If the object doesn't exist we try to create one from the
|
---|
187 | // dictionary. If this isn't possible NULL is returned.
|
---|
188 | //
|
---|
189 | // An object which was created automatically is deleted automatically in
|
---|
190 | // the destructor of the parameter list, too. This means, that if an
|
---|
191 | // object should survive (eg. Histograms) you MUST create it by yourself
|
---|
192 | // and add it to the parameter list.
|
---|
193 | //
|
---|
194 | // By default (you don't specify an object name) the object name is
|
---|
195 | // the same as the classname
|
---|
196 | //
|
---|
197 | // If the classname (default classname) is of the structure
|
---|
198 | // "Name;something" - containing a semicolon - evarything which is
|
---|
199 | // after the last appearance of a semicolon is stripped to get the
|
---|
200 | // Name of the Class. Normally this is used to number your objects.
|
---|
201 | // "Name;1", "Name;2", ...
|
---|
202 | //
|
---|
203 | MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
|
---|
204 | {
|
---|
205 | //
|
---|
206 | // If now object name (name of the object to identify it in the
|
---|
207 | // List) is given use it's classname as the objectname
|
---|
208 | //
|
---|
209 | if (!objname)
|
---|
210 | objname = classname;
|
---|
211 |
|
---|
212 | //
|
---|
213 | // Check if the classname is a 'numbered' name (like: "MTime;2")
|
---|
214 | // if so strip the number from the classname.
|
---|
215 | //
|
---|
216 | // Becareful: We check for the last occurance of a ';' only and we
|
---|
217 | // also don't check if a number follows or something else.
|
---|
218 | //
|
---|
219 | // Rem: I use a TString to make the code more readyble and to get
|
---|
220 | // the new object deleted automatically
|
---|
221 | //
|
---|
222 | TString cname(classname);
|
---|
223 | const char *semicolon = strrchr(cname, ';');
|
---|
224 |
|
---|
225 | if (semicolon)
|
---|
226 | cname.Remove(semicolon-cname);
|
---|
227 |
|
---|
228 | //
|
---|
229 | // Try to find a object with this object name which is already
|
---|
230 | // in the List. If we can find one we are done.
|
---|
231 | //
|
---|
232 | MParContainer *pcont = (MParContainer*)FindObject(objname);
|
---|
233 |
|
---|
234 | if (pcont)
|
---|
235 | return pcont;
|
---|
236 |
|
---|
237 | //
|
---|
238 | // if object is not existing in the list try to create one
|
---|
239 | //
|
---|
240 | *fLog << dbginf << "Object '" << objname << "' of type '" << cname << "' not found... creating." << endl;
|
---|
241 |
|
---|
242 | //
|
---|
243 | // try to get class from root environment
|
---|
244 | //
|
---|
245 | TClass *cls = gROOT->GetClass(cname);
|
---|
246 |
|
---|
247 | if (!cls)
|
---|
248 | {
|
---|
249 | //
|
---|
250 | // if class is not existing in the root environment
|
---|
251 | //
|
---|
252 | *fLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
|
---|
253 | return NULL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | //
|
---|
257 | // create the parameter container of the the given class type
|
---|
258 | //
|
---|
259 | pcont = (MParContainer*)cls->New();
|
---|
260 |
|
---|
261 | //
|
---|
262 | // If a name different to the classname was given,
|
---|
263 | // set the new object name of the object
|
---|
264 | //
|
---|
265 | pcont->SetName(objname);
|
---|
266 |
|
---|
267 | //
|
---|
268 | // Now add the object to the parameter list
|
---|
269 | //
|
---|
270 | AddToList(pcont);
|
---|
271 |
|
---|
272 | //
|
---|
273 | // The object was automatically created. This makes sure, that such an
|
---|
274 | // object is deleted together with the list
|
---|
275 | //
|
---|
276 | fAutodelete.Add(pcont);
|
---|
277 |
|
---|
278 | //
|
---|
279 | // Find an object in the list.
|
---|
280 | // 'name' is the name of the object you are searching for.
|
---|
281 | //
|
---|
282 | return pcont;
|
---|
283 | }
|
---|
284 |
|
---|
285 | // --------------------------------------------------------------------------
|
---|
286 | //
|
---|
287 | // print some information about the current status of MParList
|
---|
288 | //
|
---|
289 | void MParList::Print(Option_t *t)
|
---|
290 | {
|
---|
291 | *fLog << dbginf << "ParList: " << this->GetName() << " <" << this->GetTitle() << ">" << endl;
|
---|
292 | *fLog << endl;
|
---|
293 | }
|
---|
294 |
|
---|
295 | // --------------------------------------------------------------------------
|
---|
296 | //
|
---|
297 | // Sets the flags off all containers in the list (and the list
|
---|
298 | // itself) to unchanged
|
---|
299 | //
|
---|
300 | void MParList::SetReadyToSave(Bool_t flag)
|
---|
301 | {
|
---|
302 | TIter Next(&fContainer);
|
---|
303 |
|
---|
304 | MParContainer *cont=NULL;
|
---|
305 |
|
---|
306 | //
|
---|
307 | // loop over all tasks for preproccesing
|
---|
308 | //
|
---|
309 | while ( (cont=(MParContainer*)Next()) )
|
---|
310 | cont->SetReadyToSave(flag);
|
---|
311 |
|
---|
312 | MParContainer::SetReadyToSave(flag);
|
---|
313 | }
|
---|
314 |
|
---|
315 | void MParList::Reset()
|
---|
316 | {
|
---|
317 | TIter Next(&fContainer);
|
---|
318 |
|
---|
319 | MParContainer *cont=NULL;
|
---|
320 |
|
---|
321 | //
|
---|
322 | // loop over all tasks for preproccesing
|
---|
323 | //
|
---|
324 | while ((cont=(MParContainer*)Next()))
|
---|
325 | cont->Reset();
|
---|
326 | }
|
---|