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 (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 |
|
---|
46 | #include "MLog.h"
|
---|
47 | #include "MLogManip.h"
|
---|
48 |
|
---|
49 | ClassImp(MParList);
|
---|
50 |
|
---|
51 | // --------------------------------------------------------------------------
|
---|
52 | //
|
---|
53 | // default constructor
|
---|
54 | // creates an empty list
|
---|
55 | //
|
---|
56 | MParList::MParList(const char *name, const char *title)
|
---|
57 | {
|
---|
58 | *fName = name ? name : "MParList";
|
---|
59 | *fTitle = title ? title : "A list of Parameter Containers";
|
---|
60 |
|
---|
61 | //
|
---|
62 | // This sets a flag that the list is the owner, which means
|
---|
63 | // that the destructor of the list deletes all it's objects
|
---|
64 | //
|
---|
65 | fAutodelete.SetOwner();
|
---|
66 | }
|
---|
67 |
|
---|
68 | // --------------------------------------------------------------------------
|
---|
69 | //
|
---|
70 | // copy constructor
|
---|
71 | //
|
---|
72 | MParList::MParList(MParList &ts)
|
---|
73 | {
|
---|
74 | fContainer.AddAll(&ts.fContainer);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | //
|
---|
79 | // create the Iterator over the tasklist
|
---|
80 | //
|
---|
81 | void MParList::SetLogStream(MLog *log)
|
---|
82 | {
|
---|
83 | TIter Next(&fContainer);
|
---|
84 |
|
---|
85 | MParContainer *cont=NULL;
|
---|
86 |
|
---|
87 | //
|
---|
88 | // loop over all tasks for preproccesing
|
---|
89 | //
|
---|
90 | while ( (cont=(MParContainer*)Next()) )
|
---|
91 | cont->SetLogStream(log);
|
---|
92 |
|
---|
93 | MParContainer::SetLogStream(log);
|
---|
94 | }
|
---|
95 |
|
---|
96 | // --------------------------------------------------------------------------
|
---|
97 | //
|
---|
98 | // Add an Container to the list.
|
---|
99 | //
|
---|
100 | // If 'where' is given, the object will be added after this.
|
---|
101 | //
|
---|
102 | Bool_t MParList::AddToList(MParContainer *cont, MParContainer *where)
|
---|
103 | {
|
---|
104 | //
|
---|
105 | // check if the object (you want to add) exists
|
---|
106 | //
|
---|
107 |
|
---|
108 | if (!cont)
|
---|
109 | return kFALSE;
|
---|
110 |
|
---|
111 | //
|
---|
112 | // Get Name of new container
|
---|
113 | //
|
---|
114 | const char *name = cont->GetName();
|
---|
115 |
|
---|
116 | //
|
---|
117 | // Check if the new container is already existing in the list
|
---|
118 | //
|
---|
119 | const TObject *objn = fContainer.FindObject(name);
|
---|
120 | const TObject *objt = fContainer.FindObject(cont);
|
---|
121 |
|
---|
122 | if (objn || objt)
|
---|
123 | {
|
---|
124 | //
|
---|
125 | // If the container is already in the list ignore it.
|
---|
126 | //
|
---|
127 | if (objt || objn==cont)
|
---|
128 | {
|
---|
129 | *fLog << dbginf << "Warning: Container '" << cont->GetName() << ", 0x" << (void*)cont;
|
---|
130 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
131 | return kTRUE;
|
---|
132 | }
|
---|
133 |
|
---|
134 | //
|
---|
135 | // Otherwise add it to the list, but print a warning message
|
---|
136 | //
|
---|
137 | *fLog << dbginf << "Warning: Container with the same name '" << cont->GetName();
|
---|
138 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
139 | *fLog << "You may not be able to get a pointer to container task by name." << endl;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //
|
---|
143 | // check if you want to add the new parameter container somewhere
|
---|
144 | // special (in that case you specify "where")
|
---|
145 | //
|
---|
146 | if (where)
|
---|
147 | {
|
---|
148 | if (!fContainer.FindObject(where))
|
---|
149 | {
|
---|
150 | *fLog << dbginf << "Error: Cannot find parameter container after which the new one should be added!" << endl;
|
---|
151 | return kFALSE;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | *fLog << "Adding " << name << " to " << GetName() << "... " << flush;
|
---|
156 |
|
---|
157 | fContainer.Add(cont);
|
---|
158 | *fLog << "Done." << endl;
|
---|
159 |
|
---|
160 | return kTRUE;
|
---|
161 | }
|
---|
162 |
|
---|
163 | // --------------------------------------------------------------------------
|
---|
164 | //
|
---|
165 | // Find an object in the list.
|
---|
166 | // 'name' is the name of the object you are searching for.
|
---|
167 | //
|
---|
168 | TObject *MParList::FindObject(const char *name) const
|
---|
169 | {
|
---|
170 | return fContainer.FindObject(name);
|
---|
171 | }
|
---|
172 |
|
---|
173 | // --------------------------------------------------------------------------
|
---|
174 | //
|
---|
175 | // check if the object is in the list or not
|
---|
176 | //
|
---|
177 | TObject *MParList::FindObject(TObject *obj) const
|
---|
178 | {
|
---|
179 | return fContainer.FindObject(obj);
|
---|
180 | }
|
---|
181 |
|
---|
182 | // --------------------------------------------------------------------------
|
---|
183 | //
|
---|
184 | // Find an object in the list.
|
---|
185 | // 'name' is the name of the object you are searching for.
|
---|
186 | // If the object doesn't exist we try to create one from the
|
---|
187 | // dictionary. If this isn't possible NULL is returned.
|
---|
188 | //
|
---|
189 | // An object which was created automatically is deleted automatically in
|
---|
190 | // the destructor of the parameter list, too. This means, that if an
|
---|
191 | // object should survive (eg. Histograms) you MUST create it by yourself
|
---|
192 | // and add it to the parameter list.
|
---|
193 | //
|
---|
194 | // By default (you don't specify an object name) the object name is
|
---|
195 | // the same as the classname
|
---|
196 | //
|
---|
197 | // If the classname (default classname) is of the structure
|
---|
198 | // "Name;something" - containing a semicolon - evarything which is
|
---|
199 | // after the last appearance of a semicolon is stripped to get the
|
---|
200 | // Name of the Class. Normally this is used to number your objects.
|
---|
201 | // "Name;1", "Name;2", ... If a semicolon is detected leading dots
|
---|
202 | // are stripped from the object-name (eg. "name;5.")
|
---|
203 | //
|
---|
204 | MParContainer *MParList::FindCreateObj(const char *classname, const char *objname)
|
---|
205 | {
|
---|
206 | //
|
---|
207 | // If now object name (name of the object to identify it in the
|
---|
208 | // List) is given use it's classname as the objectname
|
---|
209 | //
|
---|
210 | if (!objname)
|
---|
211 | objname = classname;
|
---|
212 |
|
---|
213 | //
|
---|
214 | // Check if the classname is a 'numbered' name (like: "MTime;2")
|
---|
215 | // if so strip the number from the classname.
|
---|
216 | //
|
---|
217 | // Becareful: We check for the last occurance of a ';' only and we
|
---|
218 | // also don't check if a number follows or something else.
|
---|
219 | //
|
---|
220 | // Rem: I use a TString to make the code more readyble and to get
|
---|
221 | // the new object deleted automatically
|
---|
222 | //
|
---|
223 | TString cname(classname);
|
---|
224 | const char *semicolon = strrchr(cname, ';');
|
---|
225 |
|
---|
226 | TString oname(objname);
|
---|
227 |
|
---|
228 | if (semicolon)
|
---|
229 | {
|
---|
230 | cname.Remove(semicolon-cname);
|
---|
231 |
|
---|
232 | //
|
---|
233 | // Remove leading dots from objectname (eg. "MMcTrig;5.")
|
---|
234 | //
|
---|
235 | Int_t sz = oname.Sizeof()-2;
|
---|
236 |
|
---|
237 | while (sz>=0 && oname[sz]=='.')
|
---|
238 | oname.Remove(sz--);
|
---|
239 | }
|
---|
240 |
|
---|
241 | //
|
---|
242 | // Try to find a object with this object name which is already
|
---|
243 | // in the List. If we can find one we are done.
|
---|
244 | //
|
---|
245 | MParContainer *pcont = (MParContainer*)FindObject(oname);
|
---|
246 |
|
---|
247 | if (pcont)
|
---|
248 | return pcont;
|
---|
249 |
|
---|
250 | //
|
---|
251 | // if object is not existing in the list try to create one
|
---|
252 | //
|
---|
253 | *fLog << dbginf << "Object '" << oname << "' of type '" << cname << "' not found... creating." << endl;
|
---|
254 |
|
---|
255 | //
|
---|
256 | // try to get class from root environment
|
---|
257 | //
|
---|
258 | TClass *cls = gROOT->GetClass(cname);
|
---|
259 |
|
---|
260 | if (!cls)
|
---|
261 | {
|
---|
262 | //
|
---|
263 | // if class is not existing in the root environment
|
---|
264 | //
|
---|
265 | *fLog << dbginf << "Class '" << cname << "' not existing in dictionary." << endl;
|
---|
266 | return NULL;
|
---|
267 | }
|
---|
268 |
|
---|
269 | //
|
---|
270 | // create the parameter container of the the given class type
|
---|
271 | //
|
---|
272 | pcont = (MParContainer*)cls->New();
|
---|
273 |
|
---|
274 | //
|
---|
275 | // If a name different to the classname was given,
|
---|
276 | // set the new object name of the object
|
---|
277 | //
|
---|
278 | pcont->SetName(oname);
|
---|
279 |
|
---|
280 | //
|
---|
281 | // Now add the object to the parameter list
|
---|
282 | //
|
---|
283 | AddToList(pcont);
|
---|
284 |
|
---|
285 | //
|
---|
286 | // The object was automatically created. This makes sure, that such an
|
---|
287 | // object is deleted together with the list
|
---|
288 | //
|
---|
289 | fAutodelete.Add(pcont);
|
---|
290 |
|
---|
291 | //
|
---|
292 | // Find an object in the list.
|
---|
293 | // 'name' is the name of the object you are searching for.
|
---|
294 | //
|
---|
295 | return pcont;
|
---|
296 | }
|
---|
297 |
|
---|
298 | // --------------------------------------------------------------------------
|
---|
299 | //
|
---|
300 | // print some information about the current status of MParList
|
---|
301 | //
|
---|
302 | void MParList::Print(Option_t *t)
|
---|
303 | {
|
---|
304 | *fLog << dbginf << "ParList: " << this->GetName() << " <" << this->GetTitle() << ">" << endl;
|
---|
305 | *fLog << endl;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // --------------------------------------------------------------------------
|
---|
309 | //
|
---|
310 | // Sets the flags off all containers in the list (and the list
|
---|
311 | // itself) to unchanged
|
---|
312 | //
|
---|
313 | void MParList::SetReadyToSave(Bool_t flag)
|
---|
314 | {
|
---|
315 | TIter Next(&fContainer);
|
---|
316 |
|
---|
317 | MParContainer *cont=NULL;
|
---|
318 |
|
---|
319 | //
|
---|
320 | // loop over all tasks for preproccesing
|
---|
321 | //
|
---|
322 | while ( (cont=(MParContainer*)Next()) )
|
---|
323 | cont->SetReadyToSave(flag);
|
---|
324 |
|
---|
325 | MParContainer::SetReadyToSave(flag);
|
---|
326 | }
|
---|
327 |
|
---|
328 | void MParList::Reset()
|
---|
329 | {
|
---|
330 | TIter Next(&fContainer);
|
---|
331 |
|
---|
332 | MParContainer *cont=NULL;
|
---|
333 |
|
---|
334 | //
|
---|
335 | // loop over all tasks for preproccesing
|
---|
336 | //
|
---|
337 | while ((cont=(MParContainer*)Next()))
|
---|
338 | cont->Reset();
|
---|
339 | }
|
---|