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