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