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-2001
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MParList //
|
---|
28 | // //
|
---|
29 | // This class contains a list of different parameter containers. //
|
---|
30 | // //
|
---|
31 | // A parameter container is an object which is derived from //
|
---|
32 | // MParContainer. //
|
---|
33 | // //
|
---|
34 | // Normally a parameter container is used for data exchange between two //
|
---|
35 | // tasks at runtime. //
|
---|
36 | // //
|
---|
37 | // You can add every parameter container (Named object) to the //
|
---|
38 | // instance and access it from somewhere else via its Name. //
|
---|
39 | // //
|
---|
40 | /////////////////////////////////////////////////////////////////////////////
|
---|
41 | #include "MParList.h"
|
---|
42 |
|
---|
43 | #include <TNamed.h>
|
---|
44 | #include <TClass.h>
|
---|
45 | #include <TOrdCollection.h>
|
---|
46 |
|
---|
47 | #include "MLog.h"
|
---|
48 | #include "MLogManip.h"
|
---|
49 |
|
---|
50 | ClassImp(MParList);
|
---|
51 |
|
---|
52 | // --------------------------------------------------------------------------
|
---|
53 | //
|
---|
54 | // default constructor
|
---|
55 | // creates an empty list
|
---|
56 | //
|
---|
57 | MParList::MParList(const char *name, const char *title)
|
---|
58 | {
|
---|
59 | fName = name ? name : "MParList";
|
---|
60 | fTitle = title ? title : "A list of Parameter Containers";
|
---|
61 |
|
---|
62 | //
|
---|
63 | // This sets a flag that the list is the owner, which means
|
---|
64 | // that the destructor of the list deletes all it's objects
|
---|
65 | //
|
---|
66 | fContainer = new TOrdCollection;
|
---|
67 | fAutodelete = new TOrdCollection;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // --------------------------------------------------------------------------
|
---|
71 | //
|
---|
72 | // Copy constructor. It copies all entries of the parameter list, but it
|
---|
73 | // takes care of, that the automatically created entries are only deleted
|
---|
74 | // once. (doesn't copy the list which holds the automatically created
|
---|
75 | // entries)
|
---|
76 | //
|
---|
77 | MParList::MParList(MParList &ts)
|
---|
78 | {
|
---|
79 | fContainer->AddAll(ts.fContainer);
|
---|
80 | }
|
---|
81 |
|
---|
82 | // --------------------------------------------------------------------------
|
---|
83 | //
|
---|
84 | // If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
|
---|
85 | // by the destructor
|
---|
86 | //
|
---|
87 | MParList::~MParList()
|
---|
88 | {
|
---|
89 | //
|
---|
90 | // Case:
|
---|
91 | // 1) MParList is owner of the containers:
|
---|
92 | // All container are stored in fContainer, and become deleted by
|
---|
93 | // 'delete fContainer'. Some of these containers, which were
|
---|
94 | // created automatically are stored in fAutodelete, too. To prevent
|
---|
95 | // double deletion this containers are not deleted by the destructor
|
---|
96 | // of fAutodelete.
|
---|
97 | // 2) MParList is not owner of the containers:
|
---|
98 | // The containers which were Added by AddToList are not touched.
|
---|
99 | // Only the containers which were created automatically are also
|
---|
100 | // automatically deleted.
|
---|
101 | //
|
---|
102 | TestBit(kIsOwner) ? fContainer->SetOwner() : fAutodelete->SetOwner();
|
---|
103 |
|
---|
104 | delete fContainer;
|
---|
105 | delete fAutodelete;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // --------------------------------------------------------------------------
|
---|
109 | //
|
---|
110 | // If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
|
---|
111 | // by the destructor
|
---|
112 | //
|
---|
113 | void MParList::SetOwner(Bool_t enable)
|
---|
114 | {
|
---|
115 | enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // --------------------------------------------------------------------------
|
---|
119 | //
|
---|
120 | // Set the logging streamer of the parameter list and all contained
|
---|
121 | // parameter containers
|
---|
122 | //
|
---|
123 | void MParList::SetLogStream(MLog *log)
|
---|
124 | {
|
---|
125 | fContainer->ForEach(MParContainer, SetLogStream)(log);
|
---|
126 | MParContainer::SetLogStream(log);
|
---|
127 | }
|
---|
128 |
|
---|
129 | // --------------------------------------------------------------------------
|
---|
130 | //
|
---|
131 | // Add a single container to the list.
|
---|
132 | //
|
---|
133 | // If 'where' is given, the object will be added after this.
|
---|
134 | //
|
---|
135 | Bool_t MParList::AddToList(MParContainer *cont, MParContainer *where)
|
---|
136 | {
|
---|
137 | //
|
---|
138 | // check if the object (you want to add) exists
|
---|
139 | //
|
---|
140 |
|
---|
141 | if (!cont)
|
---|
142 | return kFALSE;
|
---|
143 |
|
---|
144 | //
|
---|
145 | // Get Name of new container
|
---|
146 | //
|
---|
147 | const char *name = cont->GetName();
|
---|
148 |
|
---|
149 | //
|
---|
150 | // Check if the new container is already existing in the list
|
---|
151 | //
|
---|
152 | const TObject *objn = fContainer->FindObject(name);
|
---|
153 | const TObject *objt = fContainer->FindObject(cont);
|
---|
154 |
|
---|
155 | if (objn || objt)
|
---|
156 | {
|
---|
157 | //
|
---|
158 | // If the container is already in the list ignore it.
|
---|
159 | //
|
---|
160 | if (objt || objn==cont)
|
---|
161 | {
|
---|
162 | *fLog << warn << dbginf << "Warning: Container '" << cont->GetName() << ", 0x" << (void*)cont;
|
---|
163 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
164 | return kTRUE;
|
---|
165 | }
|
---|
166 |
|
---|
167 | //
|
---|
168 | // Otherwise add it to the list, but print a warning message
|
---|
169 | //
|
---|
170 | *fLog << warn << dbginf << "Warning: Container with the same name '" << cont->GetName();
|
---|
171 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
172 | *fLog << "You may not be able to get a pointer to container task by name." << endl;
|
---|
173 | }
|
---|
174 |
|
---|
175 | //
|
---|
176 | // check if you want to add the new parameter container somewhere
|
---|
177 | // special (in that case you specify "where")
|
---|
178 | //
|
---|
179 | if (where)
|
---|
180 | {
|
---|
181 | if (!fContainer->FindObject(where))
|
---|
182 | {
|
---|
183 | *fLog << dbginf << "Error: Cannot find parameter container after which the new one should be added!" << endl;
|
---|
184 | return kFALSE;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | *fLog << inf << "Adding " << name << " to " << GetName() << "... " << flush;
|
---|
189 |
|
---|
190 | fContainer->Add(cont);
|
---|
191 | *fLog << "Done." << endl;
|
---|
192 |
|
---|
193 | return kTRUE;
|
---|
194 | }
|
---|
195 |
|
---|
196 | // --------------------------------------------------------------------------
|
---|
197 | //
|
---|
198 | // Add all entries of the TObjArray to the list.
|
---|
199 | //
|
---|
200 | void MParList::AddToList(TObjArray *list)
|
---|
201 | {
|
---|
202 | //
|
---|
203 | // check if the object (you want to add) exists
|
---|
204 | //
|
---|
205 | if (!list)
|
---|
206 | return;
|
---|
207 |
|
---|
208 | TObjArrayIter Next(list);
|
---|
209 |
|
---|
210 | MParContainer *cont = NULL;
|
---|
211 | while ((cont=(MParContainer*)Next()))
|
---|
212 | AddToList(cont);
|
---|
213 | }
|
---|
214 |
|
---|
215 | // --------------------------------------------------------------------------
|
---|
216 | //
|
---|
217 | // Find an object in the list.
|
---|
218 | // 'name' is the name of the object you are searching for.
|
---|
219 | //
|
---|
220 | TObject *MParList::FindObject(const char *name) const
|
---|
221 | {
|
---|
222 | return fContainer->FindObject(name);
|
---|
223 | }
|
---|
224 |
|
---|
225 | // --------------------------------------------------------------------------
|
---|
226 | //
|
---|
227 | // check if the object is in the list or not
|
---|
228 | //
|
---|
229 | TObject *MParList::FindObject(const TObject *obj) const
|
---|
230 | {
|
---|
231 | return fContainer->FindObject(obj);
|
---|
232 | }
|
---|
233 |
|
---|
234 | // --------------------------------------------------------------------------
|
---|
235 | //
|
---|
236 | // returns the ClassName without anything which is behind that last ';' in
|
---|
237 | // string.
|
---|
238 | //
|
---|
239 | TString MParList::GetClassName(const char *classname)
|
---|
240 | {
|
---|
241 | TString cname(classname);
|
---|
242 | const char *semicolon = strrchr(cname, ';');
|
---|
243 |
|
---|
244 | if (semicolon)
|
---|
245 | cname.Remove(semicolon-cname);
|
---|
246 |
|
---|
247 | return cname;
|
---|
248 | }
|
---|
249 |
|
---|
250 | // --------------------------------------------------------------------------
|
---|
251 | //
|
---|
252 | // returns the ObjectName. It is created from a class and object name.
|
---|
253 | // If no object name is given the objectname is the same than the
|
---|
254 | // class name. Leading dots are removed from the object name
|
---|
255 | //
|
---|
256 | TString MParList::GetObjectName(const char *classname, const char *objname)
|
---|
257 | {
|
---|
258 | TString cname(classname);
|
---|
259 | const char *semicolon = strrchr(cname, ';');
|
---|
260 |
|
---|
261 | TString oname(objname ? objname : classname);
|
---|
262 |
|
---|
263 | if (semicolon)
|
---|
264 | {
|
---|
265 | //
|
---|
266 | // Remove leading dots from objectname (eg. "MMcTrig;5.")
|
---|
267 | //
|
---|
268 | Int_t sz = oname.Sizeof()-2;
|
---|
269 |
|
---|
270 | while (sz>=0 && oname[sz]=='.')
|
---|
271 | oname.Remove(sz--);
|
---|
272 | }
|
---|
273 | return oname;
|
---|
274 | }
|
---|
275 |
|
---|
276 | // --------------------------------------------------------------------------
|
---|
277 | //
|
---|
278 | // Find an object in the list.
|
---|
279 | // 'name' is the name of the object you are searching for.
|
---|
280 | // If the object doesn't exist we try to create one from the
|
---|
281 | // dictionary. If this isn't possible NULL is returned.
|
---|
282 | //
|
---|
283 | // An object which was created automatically is deleted automatically in
|
---|
284 | // the destructor of the parameter list, too. This means, that if an
|
---|
285 | // object should survive (eg. Histograms) you MUST create it by yourself
|
---|
286 | // and add it to the parameter list.
|
---|
287 | //
|
---|
288 | // By default (you don't specify an object name) the object name is
|
---|
289 | // the same as the classname
|
---|
290 | //
|
---|
291 | // If the classname (default classname) is of the structure
|
---|
292 | // "Name;something" - containing a semicolon - evarything which is
|
---|
293 | // after the last appearance of a semicolon is stripped to get the
|
---|
294 | // Name of the Class. Normally this is used to number your objects.
|
---|
295 | // "Name;1", "Name;2", ... If a semicolon is detected leading dots
|
---|
296 | // are stripped from the object-name (eg. "name;5.")
|
---|
297 | //
|
---|
298 | MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
|
---|
299 | {
|
---|
300 | //
|
---|
301 | // If now object name (name of the object to identify it in the
|
---|
302 | // List) is given use it's classname as the objectname
|
---|
303 | //
|
---|
304 |
|
---|
305 | //
|
---|
306 | // Check if the classname is a 'numbered' name (like: "MTime;2")
|
---|
307 | // if so strip the number from the classname.
|
---|
308 | //
|
---|
309 | // Becareful: We check for the last occurance of a ';' only and we
|
---|
310 | // also don't check if a number follows or something else.
|
---|
311 | //
|
---|
312 | // Rem: I use a TString to make the code more readyble and to get
|
---|
313 | // the new object deleted automatically
|
---|
314 | //
|
---|
315 | TString cname = GetClassName(classname);
|
---|
316 | TString oname = GetObjectName(classname, objname);
|
---|
317 |
|
---|
318 | //
|
---|
319 | // Try to find a object with this object name which is already
|
---|
320 | // in the List. If we can find one we are done.
|
---|
321 | //
|
---|
322 | MParContainer *pcont = (MParContainer*)FindObject(oname);
|
---|
323 |
|
---|
324 | if (pcont)
|
---|
325 | return pcont;
|
---|
326 |
|
---|
327 | //
|
---|
328 | // if object is not existing in the list try to create one
|
---|
329 | //
|
---|
330 | *fLog << inf << "Object '" << oname << "' [" << cname << "] not yet in " << GetName() << "... creating." << endl;
|
---|
331 |
|
---|
332 | //
|
---|
333 | // try to get class from root environment
|
---|
334 | //
|
---|
335 | TClass *cls = gROOT->GetClass(cname);
|
---|
336 |
|
---|
337 | if (!cls)
|
---|
338 | {
|
---|
339 | //
|
---|
340 | // if class is not existing in the root environment
|
---|
341 | //
|
---|
342 | *fLog << err << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
|
---|
343 | return NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | //
|
---|
347 | // create the parameter container of the the given class type
|
---|
348 | //
|
---|
349 | pcont = (MParContainer*)cls->New();
|
---|
350 |
|
---|
351 | //
|
---|
352 | // Set the name of the container
|
---|
353 | //
|
---|
354 | pcont->SetName(oname);
|
---|
355 |
|
---|
356 | //
|
---|
357 | // Now add the object to the parameter list
|
---|
358 | //
|
---|
359 | AddToList(pcont);
|
---|
360 |
|
---|
361 | //
|
---|
362 | // The object was automatically created. This makes sure, that such an
|
---|
363 | // object is deleted together with the list
|
---|
364 | //
|
---|
365 | fAutodelete->Add(pcont);
|
---|
366 |
|
---|
367 | //
|
---|
368 | // Find an object in the list.
|
---|
369 | // 'name' is the name of the object you are searching for.
|
---|
370 | //
|
---|
371 | return pcont;
|
---|
372 | }
|
---|
373 |
|
---|
374 | // --------------------------------------------------------------------------
|
---|
375 | //
|
---|
376 | // print some information about the current status of MParList
|
---|
377 | //
|
---|
378 | void MParList::Print(Option_t *t) const
|
---|
379 | {
|
---|
380 | *fLog << all << GetDescriptor() << endl;
|
---|
381 | *fLog << setfill('-') << setw(strlen(GetDescriptor())+2) << "" << endl;
|
---|
382 | MParContainer *obj = NULL;
|
---|
383 | TIter Next(fContainer);
|
---|
384 | while ((obj=(MParContainer*)Next()))
|
---|
385 | *fLog << " " << obj->GetDescriptor() << endl;
|
---|
386 | *fLog << endl;
|
---|
387 | }
|
---|
388 |
|
---|
389 | // --------------------------------------------------------------------------
|
---|
390 | //
|
---|
391 | // Sets the flags off all containers in the list (and the list
|
---|
392 | // itself) to unchanged
|
---|
393 | //
|
---|
394 | void MParList::SetReadyToSave(Bool_t flag)
|
---|
395 | {
|
---|
396 | fContainer->ForEach(MParContainer, SetReadyToSave)(flag);
|
---|
397 | MParContainer::SetReadyToSave(flag);
|
---|
398 | }
|
---|
399 |
|
---|
400 | // --------------------------------------------------------------------------
|
---|
401 | //
|
---|
402 | // Reset all containers in the list
|
---|
403 | //
|
---|
404 | void MParList::Reset()
|
---|
405 | {
|
---|
406 | fContainer->ForEach(MParContainer, Reset)();
|
---|
407 | }
|
---|
408 |
|
---|
409 | // --------------------------------------------------------------------------
|
---|
410 | //
|
---|
411 | // This finds numbered objects. The objects are returned in a copy of a
|
---|
412 | // TObjArray.
|
---|
413 | //
|
---|
414 | // If from only is given (or to=0) object are assumed numbered
|
---|
415 | // from 1 to from.
|
---|
416 | //
|
---|
417 | TObjArray MParList::FindObjectList(const char *name, UInt_t first, const UInt_t last) const
|
---|
418 | {
|
---|
419 | TObjArray list;
|
---|
420 |
|
---|
421 | if (first>0 && last<first)
|
---|
422 | {
|
---|
423 | *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
|
---|
424 | return list;
|
---|
425 | }
|
---|
426 |
|
---|
427 | const UInt_t len = strlen(name);
|
---|
428 |
|
---|
429 | char *auxname = new char[len+7];
|
---|
430 | strcpy(auxname, name);
|
---|
431 |
|
---|
432 | if (first==0 && last!=0)
|
---|
433 | first = 1;
|
---|
434 |
|
---|
435 | //
|
---|
436 | // If only 'from' is specified the number of entries are ment
|
---|
437 | //
|
---|
438 | for (UInt_t i=first; i<=last; i++)
|
---|
439 | {
|
---|
440 | if (first!=0 || last!=0)
|
---|
441 | sprintf(auxname+len, ";%d", i);
|
---|
442 |
|
---|
443 | TObject *obj = FindObject(auxname);
|
---|
444 | if (!obj)
|
---|
445 | continue;
|
---|
446 |
|
---|
447 | list.AddLast(obj);
|
---|
448 | }
|
---|
449 | delete auxname;
|
---|
450 |
|
---|
451 | return list;
|
---|
452 | }
|
---|
453 |
|
---|
454 | // --------------------------------------------------------------------------
|
---|
455 | //
|
---|
456 | // This finds numbered objects. The objects are returned in a copy of a
|
---|
457 | // TObjArray. If one of the objects doesn't exist it is created from the
|
---|
458 | // meaning of cname and oname (s. FindCreateObj)
|
---|
459 | //
|
---|
460 | // If from only is given (or to=0) object are assumed numbered
|
---|
461 | // from 1 to from.
|
---|
462 | //
|
---|
463 | TObjArray MParList::FindCreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *oname)
|
---|
464 | {
|
---|
465 | TObjArray list;
|
---|
466 |
|
---|
467 | if (first>0 && last<first)
|
---|
468 | {
|
---|
469 | *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
|
---|
470 | return list;
|
---|
471 | }
|
---|
472 |
|
---|
473 | const UInt_t len = strlen(cname);
|
---|
474 |
|
---|
475 | char *auxname = new char[len+7];
|
---|
476 | strcpy(auxname, cname);
|
---|
477 |
|
---|
478 | //
|
---|
479 | // If only 'from' is specified the number of entries are ment
|
---|
480 | //
|
---|
481 | if (first==0 && last!=0)
|
---|
482 | first = 1;
|
---|
483 |
|
---|
484 | for (UInt_t i=first; i<=last; i++)
|
---|
485 | {
|
---|
486 | if (first!=0 || last!=0)
|
---|
487 | sprintf(auxname+len, ";%d", i);
|
---|
488 |
|
---|
489 | TObject *obj = FindCreateObj(auxname, oname);
|
---|
490 | if (!obj)
|
---|
491 | break;
|
---|
492 |
|
---|
493 | list.AddLast(obj);
|
---|
494 | }
|
---|
495 | delete auxname;
|
---|
496 |
|
---|
497 | return list;
|
---|
498 | }
|
---|
499 |
|
---|
500 | // --------------------------------------------------------------------------
|
---|
501 | //
|
---|
502 | // This finds numbered objects. The objects are returned in a copy of a
|
---|
503 | // TObjArray. If one of the objects doesn't exist it is created from the
|
---|
504 | // meaning of cname and oname (s. FindCreateObj)
|
---|
505 | //
|
---|
506 | // If from only is given (or to=0) object are assumed numbered
|
---|
507 | // from 1 to from.
|
---|
508 | //
|
---|
509 | // Remark: Because it is static the object are only created and not added to
|
---|
510 | // the parameter list. You must also take care of deleting these objects!
|
---|
511 | // This function is mainly made for use in root macros. Don't use it in
|
---|
512 | // compiled programs if you are not 100% sure what you are doing.
|
---|
513 | //
|
---|
514 | TObjArray MParList::CreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *oname)
|
---|
515 | {
|
---|
516 | TObjArray list;
|
---|
517 |
|
---|
518 | if (first>0 && last<first)
|
---|
519 | {
|
---|
520 | gLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
|
---|
521 | return list;
|
---|
522 | }
|
---|
523 |
|
---|
524 | //
|
---|
525 | // try to get class from root environment
|
---|
526 | //
|
---|
527 | TClass *cls = gROOT->GetClass(cname);
|
---|
528 |
|
---|
529 | if (!cls)
|
---|
530 | {
|
---|
531 | //
|
---|
532 | // if class is not existing in the root environment
|
---|
533 | //
|
---|
534 | gLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
|
---|
535 | return list;
|
---|
536 | }
|
---|
537 |
|
---|
538 | const UInt_t len = strlen(cname);
|
---|
539 |
|
---|
540 | char *auxname = new char[len+7];
|
---|
541 | strcpy(auxname, cname);
|
---|
542 |
|
---|
543 | //
|
---|
544 | // If only 'from' is specified the number of entries are ment
|
---|
545 | //
|
---|
546 | if (first==0 && last!=0)
|
---|
547 | first = 1;
|
---|
548 |
|
---|
549 | for (UInt_t i=first; i<=last; i++)
|
---|
550 | {
|
---|
551 | if (first!=0 || last!=0)
|
---|
552 | sprintf(auxname+len, ";%d", i);
|
---|
553 |
|
---|
554 | //
|
---|
555 | // create the parameter container of the the given class type
|
---|
556 | //
|
---|
557 | MParContainer *pcont = (MParContainer*)cls->New();
|
---|
558 |
|
---|
559 | //
|
---|
560 | // Set the name of the container
|
---|
561 | //
|
---|
562 | pcont->SetName(auxname);
|
---|
563 |
|
---|
564 | //
|
---|
565 | // Add new object to the return list
|
---|
566 | //
|
---|
567 | list.AddLast(pcont);
|
---|
568 | }
|
---|
569 | delete auxname;
|
---|
570 |
|
---|
571 | return list;
|
---|
572 | }
|
---|