source: trunk/MagicSoft/Mars/mbase/MParContainer.cc@ 1218

Last change on this file since 1218 was 1218, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 8.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// MParContainer //
28// //
29// The MParContainer class is the base class for all MARS parameter //
30// containers. At the moment it is almost the same than ROOT's TNamed. //
31// A TNamed contains the essential elements (name, title) //
32// to identify a derived object in lists like our MParList or MTaskList. //
33// The main difference is that the name and title isn't stored and read //
34// to and from root files ("//!") //
35// //
36//////////////////////////////////////////////////////////////////////////////
37#include "MParContainer.h"
38
39#include <fstream.h> // ofstream, AsciiWrite
40
41#include <TClass.h> // IsA
42#include <TROOT.h> // TROOT::Identlevel
43#include <TMethodCall.h> // TMethodCall, AsciiWrite
44#include <TDataMember.h> // TDataMember, AsciiWrite
45#include <TVirtualPad.h> // gPad
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50ClassImp(MParContainer);
51
52// --------------------------------------------------------------------------
53//
54// MParContainer copy ctor
55//
56MParContainer::MParContainer(const MParContainer &named)
57{
58 fName = named.fName;
59 fTitle = named.fTitle;
60
61 fLog = named.fLog;
62
63 fReadyToSave = named.fReadyToSave;
64}
65
66// --------------------------------------------------------------------------
67//
68// MParContainer assignment operator.
69//
70MParContainer& MParContainer::operator=(const MParContainer& rhs)
71{
72 if (this == &rhs)
73 return *this;
74
75 TObject::operator=(rhs);
76
77 fName = rhs.fName;
78 fTitle = rhs.fTitle;
79
80 fLog = rhs.fLog;
81 fReadyToSave = rhs.fReadyToSave;
82
83 return *this;
84}
85
86// --------------------------------------------------------------------------
87//
88// Make a clone of an object using the Streamer facility.
89// If newname is specified, this will be the name of the new object
90//
91TObject *MParContainer::Clone(const char *newname) const
92{
93
94 MParContainer *named = (MParContainer*)TObject::Clone();
95 if (newname && strlen(newname)) named->SetName(newname);
96 return named;
97}
98
99// --------------------------------------------------------------------------
100//
101// Compare two MParContainer objects. Returns 0 when equal, -1 when this is
102// smaller and +1 when bigger (like strcmp).
103//
104Int_t MParContainer::Compare(const TObject *obj) const
105{
106 if (this == obj) return 0;
107 return fName.CompareTo(obj->GetName());
108}
109
110// --------------------------------------------------------------------------
111//
112// Copy this to obj.
113//
114void MParContainer::Copy(TObject &obj)
115{
116 MParContainer &cont = (MParContainer&)obj;
117
118 TObject::Copy(obj);
119
120 cont.fName = fName;
121 cont.fTitle = fTitle;
122
123 cont.fLog = fLog;
124 cont.fReadyToSave = fReadyToSave;
125}
126
127// --------------------------------------------------------------------------
128//
129// Encode MParContainer into output buffer.
130//
131void MParContainer::FillBuffer(char *&buffer)
132{
133 fName.FillBuffer(buffer);
134 fTitle.FillBuffer(buffer);
135}
136
137// --------------------------------------------------------------------------
138//
139// List MParContainer name and title.
140//
141void MParContainer::ls(Option_t *) const
142{
143 TROOT::IndentLevel();
144 *fLog << all << GetDescriptor() << " " << GetTitle() << ": kCanDelete=";
145 *fLog << Int_t(TestBit(kCanDelete)) << endl;
146}
147
148// --------------------------------------------------------------------------
149//
150// Print MParContainer name and title.
151//
152void MParContainer::Print(Option_t *) const
153{
154 *fLog << all << GetDescriptor() << " " << GetTitle() << endl;
155}
156
157// --------------------------------------------------------------------------
158//
159// Change (i.e. set) the name of the MParContainer.
160// WARNING !!
161// If the object is a member of a THashTable, THashList container
162// The HashTable must be Rehashed after SetName
163// For example the list of objects in the current directory is a THashList
164//
165void MParContainer::SetName(const char *name)
166{
167 fName = name;
168 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
169}
170
171// --------------------------------------------------------------------------
172//
173// Change (i.e. set) all the MParContainer parameters (name and title).
174// See also WARNING in SetName
175//
176void MParContainer::SetObject(const char *name, const char *title)
177{
178 fName = name;
179 fTitle = title;
180 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
181}
182
183// --------------------------------------------------------------------------
184//
185// Change (i.e. set) the title of the MParContainer.
186//
187void MParContainer::SetTitle(const char *title)
188{
189 fTitle = title;
190 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
191}
192
193// --------------------------------------------------------------------------
194//
195// Return size of the MParContainer part of the TObject.
196//
197Int_t MParContainer::Sizeof() const
198{
199 Int_t nbytes = fName.Sizeof() + fTitle.Sizeof();
200 return nbytes;
201}
202
203// --------------------------------------------------------------------------
204//
205// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
206// container, overload this function.
207//
208void MParContainer::AsciiRead(ifstream &fin)
209{
210 *fLog << warn << "To use the the ascii input of " << GetName();
211 *fLog << " you have to overload " << ClassName() << "::AsciiRead." << endl;
212}
213
214// --------------------------------------------------------------------------
215//
216// Write out a data member given as a TDataMember object to an output stream.
217//
218Bool_t MParContainer::WriteDataMember(ostream &out, TDataMember *member) const
219{
220 if (!member)
221 return kFALSE;
222
223 if (!member->IsPersistent())
224 return kFALSE;
225
226 /*const*/ TMethodCall *call = member->GetterMethod(); //FIXME: Root
227 if (!call)
228 return kFALSE;
229
230 switch (call->ReturnType())
231 {
232 case TMethodCall::kLong:
233 Long_t l;
234 call->Execute((void*)this, l); // FIXME: const, root
235 out << l << " ";
236 return kTRUE;
237
238 case TMethodCall::kDouble:
239 Double_t d;
240 call->Execute((void*)this, d); // FIXME: const, root
241 out << d << " ";
242 return kTRUE;
243
244 case TMethodCall::kOther:
245 /* someone may want to enhance this? */
246 return kFALSE;
247 }
248
249 return kFALSE;
250}
251
252// --------------------------------------------------------------------------
253//
254// Write out a data member given by name to an output stream.
255//
256Bool_t MParContainer::WriteDataMember(ostream &out, const char *member) const
257{
258 return WriteDataMember(out, IsA()->GetDataMember(member));
259}
260
261// --------------------------------------------------------------------------
262//
263// If you want to use Ascii-Input/-Output (eg. MWriteAsciiFile) of a
264// container, you may overload this function. If you don't overload it
265// the data member of a class are written to the file in the order of
266// appearance in the class header (be more specfic: root dictionary)
267// Only data members which are of integer (Bool_t, Int_t, ...) or
268// floating point (Float_t, Double_t, ...) type are written.
269//
270void MParContainer::AsciiWrite(ostream &out) const
271{
272 // *fLog << warn << "To use the the ascii output of " << GetName();
273 // *fLog << " you have to overload " << ClassName() << "::AsciiWrite." << endl;
274
275 TDataMember *data = NULL;
276
277 TIter Next(IsA()->GetListOfDataMembers());
278 while ((data=(TDataMember*)Next()))
279 WriteDataMember(out, data);
280}
Note: See TracBrowser for help on using the repository browser.