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 06/2001 <mailto:tbretz@uni-sw.gwdg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2002
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MWriteAsciiFile //
|
---|
28 | // //
|
---|
29 | // If you want to store a single container into an Ascii file you have //
|
---|
30 | // to use this class. You must know the name of the file you wanne write //
|
---|
31 | // (you should know it) and the name of the container you want to write. //
|
---|
32 | // This can be the name of the class or a given name, which identifies //
|
---|
33 | // the container in a parameter container list (MParList). //
|
---|
34 | // The container is written to the ascii file if its ReadyToSave flag is //
|
---|
35 | // set (MParContainer) //
|
---|
36 | // //
|
---|
37 | // You can write more than one container in one line of the file, see //
|
---|
38 | // AddContainer. //
|
---|
39 | // //
|
---|
40 | // You can also write single data members of a container (like fWidth //
|
---|
41 | // of MHillas). For more details see AddContainer. Make sure, that a //
|
---|
42 | // getter method for the data member exist. The name of the method //
|
---|
43 | // must be the same than the data member itself, but the f must be //
|
---|
44 | // replaced by a Get. //
|
---|
45 | // //
|
---|
46 | /////////////////////////////////////////////////////////////////////////////
|
---|
47 |
|
---|
48 | #include "MWriteAsciiFile.h"
|
---|
49 |
|
---|
50 | #include <fstream.h>
|
---|
51 |
|
---|
52 | #include <TClass.h> // IsA
|
---|
53 | #include <TMethodCall.h> // TMethodCall, AsciiWrite
|
---|
54 | #include <TDataMember.h> // TDataMember, AsciiWrite
|
---|
55 |
|
---|
56 | #include "MLog.h"
|
---|
57 | #include "MLogManip.h"
|
---|
58 |
|
---|
59 | #include "MParList.h"
|
---|
60 |
|
---|
61 | ClassImp(MWriteAsciiFile);
|
---|
62 |
|
---|
63 | // --------------------------------------------------------------------------
|
---|
64 | //
|
---|
65 | // Init. Replaces the same code used in all constructors.
|
---|
66 | //
|
---|
67 | void MWriteAsciiFile::Init(const char *filename, const char *name, const char *title)
|
---|
68 | {
|
---|
69 | fName = name ? name : "MWriteAsciiFile";
|
---|
70 | fTitle = title ? title : "Task to write one container to an ascii file";
|
---|
71 |
|
---|
72 | fNameFile = filename;
|
---|
73 |
|
---|
74 | fOut = new ofstream(fNameFile);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | //
|
---|
79 | // Specify the name of the ascii output file 'filename' and the name
|
---|
80 | // of the container you want to write. (Normally this is the name
|
---|
81 | // of the class (eg. MHillas) but it can also be a different name which
|
---|
82 | // identifies the container in the parameter list.
|
---|
83 | // Because you cannot write more than one container there is no Add-function
|
---|
84 | // like in MWriteRootFile.
|
---|
85 | //
|
---|
86 | // For Example: MWriteAsciiFile("file.txt", "MHillas");
|
---|
87 | //
|
---|
88 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
|
---|
89 | const char *name, const char *title)
|
---|
90 | {
|
---|
91 | Init(filename, name, title);
|
---|
92 |
|
---|
93 | if (contname)
|
---|
94 | AddContainer(contname);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // --------------------------------------------------------------------------
|
---|
98 | //
|
---|
99 | // Specify a the name of the ascii output file 'filename' and a pointer to
|
---|
100 | // the container you want to write.
|
---|
101 | // Because you cannot write more than one container there is no Add-function
|
---|
102 | // like in MWriteRootFile.
|
---|
103 | //
|
---|
104 | // For Example: MHillas hillas;
|
---|
105 | // MWriteAsciiFile("file.txt", &hillas);
|
---|
106 | //
|
---|
107 | //
|
---|
108 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, MParContainer *cont,
|
---|
109 | const char *name, const char *title)
|
---|
110 | {
|
---|
111 | Init(filename, name, title);
|
---|
112 |
|
---|
113 | if (cont)
|
---|
114 | AddContainer(cont);
|
---|
115 | }
|
---|
116 |
|
---|
117 | // --------------------------------------------------------------------------
|
---|
118 | //
|
---|
119 | // Destructor. Delete the output file if necessary (it is closed
|
---|
120 | // automatically by its destructor.
|
---|
121 | //
|
---|
122 | MWriteAsciiFile::~MWriteAsciiFile()
|
---|
123 | {
|
---|
124 | fContNames.SetOwner();
|
---|
125 | fMembers.SetOwner();
|
---|
126 |
|
---|
127 | delete fOut;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // --------------------------------------------------------------------------
|
---|
131 | //
|
---|
132 | // Check if the containers are ready for writing. If so write them.
|
---|
133 | // The containers are written in one line, one after each other.
|
---|
134 | // If not all containers are written (because of the IsReadyToSave-flag)
|
---|
135 | // a warning message is print.
|
---|
136 | //
|
---|
137 | void MWriteAsciiFile::CheckAndWrite() const
|
---|
138 | {
|
---|
139 | Bool_t written = kFALSE;
|
---|
140 |
|
---|
141 | MParContainer *cont = NULL;
|
---|
142 |
|
---|
143 | Int_t num = fContainer.GetEntries();
|
---|
144 |
|
---|
145 | TIter NextCont(&fContainer);
|
---|
146 | TIter NextMemb(&fMembers);
|
---|
147 |
|
---|
148 | while ((cont=(MParContainer*)NextCont()))
|
---|
149 | {
|
---|
150 | const TObject *memb = NextMemb();
|
---|
151 |
|
---|
152 | if (!cont->IsReadyToSave())
|
---|
153 | continue;
|
---|
154 |
|
---|
155 | if (memb->GetName()[0]=='\0')
|
---|
156 | cont->AsciiWrite(*fOut);
|
---|
157 | else
|
---|
158 | {
|
---|
159 | if (!cont->WriteDataMember(*fOut, memb->GetName()))
|
---|
160 | continue;
|
---|
161 | }
|
---|
162 |
|
---|
163 | *fOut << " ";
|
---|
164 | written = kTRUE;
|
---|
165 |
|
---|
166 | num--;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (written)
|
---|
170 | *fOut << endl;
|
---|
171 |
|
---|
172 | if (num!=0)
|
---|
173 | *fLog << warn << "Warning - given number of containers doesn't fit number of written containers." << endl;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // --------------------------------------------------------------------------
|
---|
177 | //
|
---|
178 | // Return open state of the file
|
---|
179 | //
|
---|
180 | Bool_t MWriteAsciiFile::IsFileOpen() const
|
---|
181 | {
|
---|
182 | return (bool)(*fOut);
|
---|
183 | }
|
---|
184 |
|
---|
185 | // --------------------------------------------------------------------------
|
---|
186 | //
|
---|
187 | // Tries to get all containers from the ParList which were given by name
|
---|
188 | // adds them to the list of pointers to the container which should be
|
---|
189 | // written to the ascii file.
|
---|
190 | //
|
---|
191 | Bool_t MWriteAsciiFile::GetContainer(MParList *pList)
|
---|
192 | {
|
---|
193 | TObject *obj = NULL;
|
---|
194 |
|
---|
195 | TIter Next(&fContNames);
|
---|
196 |
|
---|
197 | while ((obj=Next()))
|
---|
198 | {
|
---|
199 | const char *name = obj->GetName();
|
---|
200 |
|
---|
201 | MParContainer *cont = (MParContainer*)pList->FindObject(name, "MParContainer");
|
---|
202 | if (!cont)
|
---|
203 | {
|
---|
204 | *fLog << err << dbginf << "Cannot find parameter container '" << name << "'." << endl;
|
---|
205 | return kFALSE;
|
---|
206 | }
|
---|
207 |
|
---|
208 | AddContainer(cont, obj->GetTitle());
|
---|
209 | }
|
---|
210 |
|
---|
211 | return kTRUE;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // --------------------------------------------------------------------------
|
---|
215 | //
|
---|
216 | // Add another container (by name) to be written to the ascii file.
|
---|
217 | // The container will be output one after each other in one line.
|
---|
218 | // If you want to write only one data member of the container
|
---|
219 | // specify the name of the data member (eg. fAlpha) Make sure,
|
---|
220 | // that a "GetteMethod" for this data type exists (strif the f and
|
---|
221 | // replace it by Get)
|
---|
222 | //
|
---|
223 | void MWriteAsciiFile::AddContainer(const char *cname, const char *member)
|
---|
224 | {
|
---|
225 | TNamed *named = new TNamed(cname, member);
|
---|
226 | fContNames.AddLast(named);
|
---|
227 | }
|
---|
228 |
|
---|
229 | // --------------------------------------------------------------------------
|
---|
230 | //
|
---|
231 | // Add another container (by pointer) to be written to the ascii file.
|
---|
232 | // The container will be output one after each other in one line.
|
---|
233 | // If you want to write only one data member of the container
|
---|
234 | // specify the name of the data member (eg. fAlpha) Make sure,
|
---|
235 | // that a "GetteMethod" for this data type exists (strif the f and
|
---|
236 | // replace it by Get)
|
---|
237 | //
|
---|
238 | void MWriteAsciiFile::AddContainer(MParContainer *cont, const char *member)
|
---|
239 | {
|
---|
240 | fContainer.AddLast(cont);
|
---|
241 |
|
---|
242 | TNamed *named = new TNamed(member, "");
|
---|
243 | fMembers.AddLast(named);
|
---|
244 | }
|
---|
245 |
|
---|