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