source: trunk/MagicSoft/Mars/mbase/MParList.cc@ 1299

Last change on this file since 1299 was 1283, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 18.5 KB
Line 
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 <TNamed.h>
44#include <TClass.h>
45#include <TOrdCollection.h>
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50ClassImp(MParList);
51
52// --------------------------------------------------------------------------
53//
54// default constructor
55// creates an empty list
56//
57MParList::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//
77MParList::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//
87MParList::~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//
113void 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//
123void 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//
135Bool_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//
200void 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//
220TObject *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//
229TObject *MParList::FindObject(const TObject *obj) const
230{
231 return fContainer->FindObject(obj);
232}
233
234// --------------------------------------------------------------------------
235//
236// Find an object in the list and check for the correct inheritance.
237// 'name' is the name of the object you are searching for.
238//
239TObject *MParList::FindObject(const char *name, const char *classname) const
240{
241 TObject *obj = fContainer->FindObject(name);
242
243 if (!obj)
244 return NULL;
245
246 if (obj->InheritsFrom(classname))
247 return obj;
248
249 *fLog << dbginf << warn << "Found object '" << name << "' doesn't ";
250 *fLog << "inherit from " << "'" << classname << "'" << endl;
251 return NULL;
252}
253
254// --------------------------------------------------------------------------
255//
256// check if the object is in the list or not and check for the correct
257// inheritance
258//
259TObject *MParList::FindObject(const TObject *obj, const char *classname) const
260{
261 TObject *nobj = fContainer->FindObject(obj);
262
263 if (!nobj)
264 return NULL;
265
266 if (nobj->InheritsFrom(classname))
267 return nobj;
268
269 *fLog << dbginf << warn << "Found object '" << nobj->GetName() << "' ";
270 *fLog << "doesn't inherit from " << "'" << classname << "'" << endl;
271 return NULL;
272}
273
274// --------------------------------------------------------------------------
275//
276// returns the ClassName without anything which is behind that last ';' in
277// string.
278//
279TString MParList::GetClassName(const char *classname)
280{
281 TString cname(classname);
282 const char *semicolon = strrchr(cname, ';');
283
284 if (semicolon)
285 cname.Remove(semicolon-cname);
286
287 return cname;
288}
289
290// --------------------------------------------------------------------------
291//
292// returns the ObjectName. It is created from a class and object name.
293// If no object name is given the objectname is the same than the
294// class name. Leading dots are removed from the object name
295//
296TString MParList::GetObjectName(const char *classname, const char *objname)
297{
298 TString cname(classname);
299 const char *semicolon = strrchr(cname, ';');
300
301 TString oname(objname ? objname : classname);
302
303 if (semicolon)
304 {
305 //
306 // Remove leading dots from objectname (eg. "MMcTrig;5.")
307 //
308 Int_t sz = oname.Sizeof()-2;
309
310 while (sz>=0 && oname[sz]=='.')
311 oname.Remove(sz--);
312 }
313 return oname;
314}
315
316// --------------------------------------------------------------------------
317//
318// Find an object in the list.
319// 'name' is the name of the object you are searching for.
320// If the object doesn't exist we try to create one from the
321// dictionary. If this isn't possible NULL is returned.
322//
323// An object which was created automatically is deleted automatically in
324// the destructor of the parameter list, too. This means, that if an
325// object should survive (eg. Histograms) you MUST create it by yourself
326// and add it to the parameter list.
327//
328// By default (you don't specify an object name) the object name is
329// the same as the classname
330//
331// If the classname (default classname) is of the structure
332// "Name;something" - containing a semicolon - evarything which is
333// after the last appearance of a semicolon is stripped to get the
334// Name of the Class. Normally this is used to number your objects.
335// "Name;1", "Name;2", ... If a semicolon is detected leading dots
336// are stripped from the object-name (eg. "name;5.")
337//
338MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
339{
340 //
341 // If now object name (name of the object to identify it in the
342 // List) is given use it's classname as the objectname
343 //
344
345 //
346 // Check if the classname is a 'numbered' name (like: "MTime;2")
347 // if so strip the number from the classname.
348 //
349 // Becareful: We check for the last occurance of a ';' only and we
350 // also don't check if a number follows or something else.
351 //
352 // Rem: I use a TString to make the code more readyble and to get
353 // the new object deleted automatically
354 //
355 TString cname = GetClassName(classname);
356 TString oname = GetObjectName(classname, objname);
357
358 //
359 // Try to find a object with this object name which is already
360 // in the List. If we can find one we are done.
361 //
362 MParContainer *pcont = (MParContainer*)FindObject(oname);
363
364 if (pcont)
365 {
366 if (pcont->InheritsFrom(cname))
367 return pcont;
368
369 *fLog << err << "Warning: Object '" << oname << "' found in list doesn't inherit from " << cname << "." << endl;
370 return NULL;
371 }
372
373 //
374 // if object is not existing in the list try to create one
375 //
376 *fLog << inf << "Object '" << oname << "' [" << cname << "] not yet in " << GetName() << "... creating." << endl;
377
378 //
379 // try to get class from root environment
380 //
381 TClass *cls = gROOT->GetClass(cname);
382 if (!cls)
383 {
384 //
385 // if class is not existing in the root environment
386 //
387 *fLog << err << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
388 return NULL;
389 }
390
391 //
392 // create the parameter container of the the given class type
393 //
394 pcont = (MParContainer*)cls->New();
395 if (!pcont)
396 {
397 *fLog << err << dbginf << "Cannot create new instance of class '" << cname << "' (Maybe no def. constructor)" << endl;
398 return NULL;
399 }
400
401 //
402 // Set the name of the container
403 //
404 pcont->SetName(oname);
405
406 //
407 // Now add the object to the parameter list
408 //
409 AddToList(pcont);
410
411 //
412 // The object was automatically created. This makes sure, that such an
413 // object is deleted together with the list
414 //
415 fAutodelete->Add(pcont);
416
417 //
418 // Find an object in the list.
419 // 'name' is the name of the object you are searching for.
420 //
421 return pcont;
422}
423
424// --------------------------------------------------------------------------
425//
426// print some information about the current status of MParList
427//
428void MParList::Print(Option_t *t) const
429{
430 *fLog << all << " " << GetDescriptor() << endl;
431 *fLog << setfill('-') << setw(strlen(GetDescriptor())+2) << "" << endl;
432 MParContainer *obj = NULL;
433 TIter Next(fContainer);
434 while ((obj=(MParContainer*)Next()))
435 *fLog << " " << obj->GetDescriptor() << endl;
436 *fLog << endl;
437}
438
439// --------------------------------------------------------------------------
440//
441// Sets the flags off all containers in the list (and the list
442// itself) to unchanged
443//
444void MParList::SetReadyToSave(Bool_t flag)
445{
446 fContainer->ForEach(MParContainer, SetReadyToSave)(flag);
447 MParContainer::SetReadyToSave(flag);
448}
449
450// --------------------------------------------------------------------------
451//
452// Reset all containers in the list
453//
454void MParList::Reset()
455{
456 fContainer->ForEach(MParContainer, Reset)();
457}
458
459// --------------------------------------------------------------------------
460//
461// This finds numbered objects. The objects are returned in a copy of a
462// TObjArray.
463//
464// If from only is given (or to=0) object are assumed numbered
465// from 1 to from.
466//
467TObjArray MParList::FindObjectList(const char *name, UInt_t first, const UInt_t last) const
468{
469 TObjArray list;
470
471 if (first>0 && last<first)
472 {
473 *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
474 return list;
475 }
476
477 const UInt_t len = strlen(name);
478
479 char *auxname = new char[len+7];
480 strcpy(auxname, name);
481
482 if (first==0 && last!=0)
483 first = 1;
484
485 //
486 // If only 'from' is specified the number of entries are ment
487 //
488 for (UInt_t i=first; i<=last; i++)
489 {
490 if (first!=0 || last!=0)
491 sprintf(auxname+len, ";%d", i);
492
493 TObject *obj = FindObject(auxname);
494 if (!obj)
495 continue;
496
497 list.AddLast(obj);
498 }
499 delete auxname;
500
501 return list;
502}
503
504// --------------------------------------------------------------------------
505//
506// This finds numbered objects. The objects are returned in a copy of a
507// TObjArray. If one of the objects doesn't exist it is created from the
508// meaning of cname and oname (s. FindCreateObj)
509//
510// If from only is given (or to=0) object are assumed numbered
511// from 1 to from.
512//
513TObjArray MParList::FindCreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *oname)
514{
515 TObjArray list;
516
517 if (first>0 && last<first)
518 {
519 *fLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
520 return list;
521 }
522
523 const UInt_t len = strlen(cname);
524
525 char *auxname = new char[len+7];
526 strcpy(auxname, cname);
527
528 //
529 // If only 'from' is specified the number of entries are ment
530 //
531 if (first==0 && last!=0)
532 first = 1;
533
534 for (UInt_t i=first; i<=last; i++)
535 {
536 if (first!=0 || last!=0)
537 sprintf(auxname+len, ";%d", i);
538
539 TObject *obj = FindCreateObj(auxname, oname);
540 if (!obj)
541 break;
542
543 list.AddLast(obj);
544 }
545 delete auxname;
546
547 return list;
548}
549
550// --------------------------------------------------------------------------
551//
552// This finds numbered objects. The objects are returned in a copy of a
553// TObjArray. If one of the objects doesn't exist it is created from the
554// meaning of cname and oname (s. FindCreateObj)
555//
556// If from only is given (or to=0) object are assumed numbered
557// from 1 to from.
558//
559// Remark: Because it is static the object are only created and not added to
560// the parameter list. You must also take care of deleting these objects!
561// This function is mainly made for use in root macros. Don't use it in
562// compiled programs if you are not 100% sure what you are doing.
563//
564TObjArray MParList::CreateObjList(const char *cname, UInt_t first, const UInt_t last, const char *oname)
565{
566 TObjArray list;
567
568 if (first>0 && last<first)
569 {
570 gLog << err << dbginf << "Cannot create entries backwards (last<first)...skipped." << endl;
571 return list;
572 }
573
574 //
575 // try to get class from root environment
576 //
577 TClass *cls = gROOT->GetClass(cname);
578 if (!cls)
579 {
580 //
581 // if class is not existing in the root environment
582 //
583 gLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
584 return list;
585 }
586
587 const UInt_t len = strlen(cname);
588
589 char *auxname = new char[len+7];
590 strcpy(auxname, cname);
591
592 //
593 // If only 'from' is specified the number of entries are ment
594 //
595 if (first==0 && last!=0)
596 first = 1;
597
598 for (UInt_t i=first; i<=last; i++)
599 {
600 if (first!=0 || last!=0)
601 sprintf(auxname+len, ";%d", i);
602
603 //
604 // create the parameter container of the the given class type
605 //
606 MParContainer *pcont = (MParContainer*)cls->New();
607 if (pcont)
608 {
609 gLog << err << dbginf << "Cannot create new instance of class '" << cname << "' (Maybe no def. constructor)" << endl;
610 return list;
611 }
612
613 //
614 // Set the name of the container
615 //
616 pcont->SetName(auxname);
617
618 //
619 // Add new object to the return list
620 //
621 list.AddLast(pcont);
622 }
623 delete auxname;
624
625 return list;
626}
Note: See TracBrowser for help on using the repository browser.