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@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
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>
|
---|
51 |
|
---|
52 | #include <TMethodCall.h> // TMethodCall, AsciiWrite
|
---|
53 |
|
---|
54 | #include "MDataList.h" // MDataList
|
---|
55 | #include "MDataChain.h" // MDataChain
|
---|
56 | #include "MDataValue.h" // MDataValue
|
---|
57 | #include "MDataMember.h" // MDataMember
|
---|
58 |
|
---|
59 | #include "MLog.h"
|
---|
60 | #include "MLogManip.h"
|
---|
61 |
|
---|
62 | #include "MParList.h"
|
---|
63 |
|
---|
64 | ClassImp(MWriteAsciiFile);
|
---|
65 |
|
---|
66 | using namespace std;
|
---|
67 |
|
---|
68 | // --------------------------------------------------------------------------
|
---|
69 | //
|
---|
70 | // Init. Replaces the same code used in all constructors.
|
---|
71 | //
|
---|
72 | void MWriteAsciiFile::Init(const char *filename, const char *name, const char *title)
|
---|
73 | {
|
---|
74 | fName = name ? name : "MWriteAsciiFile";
|
---|
75 | fTitle = title ? title : "Task to write one container to an ascii file";
|
---|
76 |
|
---|
77 | fNameFile = filename;
|
---|
78 |
|
---|
79 | fOut = new ofstream(fNameFile);
|
---|
80 | }
|
---|
81 |
|
---|
82 | // --------------------------------------------------------------------------
|
---|
83 | //
|
---|
84 | // Specify the name of the ascii output file 'filename' and the name
|
---|
85 | // of the container you want to write. (Normally this is the name
|
---|
86 | // of the class (eg. MHillas) but it can also be a different name which
|
---|
87 | // identifies the container in the parameter list.
|
---|
88 | // Because you cannot write more than one container there is no Add-function
|
---|
89 | // like in MWriteRootFile.
|
---|
90 | //
|
---|
91 | // For Example: MWriteAsciiFile("file.txt", "MHillas");
|
---|
92 | //
|
---|
93 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
|
---|
94 | const char *name, const char *title)
|
---|
95 | {
|
---|
96 | Init(filename, name, title);
|
---|
97 |
|
---|
98 | if (contname)
|
---|
99 | AddColumns(contname);
|
---|
100 | }
|
---|
101 |
|
---|
102 | // --------------------------------------------------------------------------
|
---|
103 | //
|
---|
104 | // Specify a the name of the ascii output file 'filename' and a pointer to
|
---|
105 | // the container you want to write.
|
---|
106 | // Because you cannot write more than one container there is no Add-function
|
---|
107 | // like in MWriteRootFile.
|
---|
108 | //
|
---|
109 | // For Example: MHillas hillas;
|
---|
110 | // MWriteAsciiFile("file.txt", &hillas);
|
---|
111 | //
|
---|
112 | //
|
---|
113 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, MParContainer *cont,
|
---|
114 | const char *name, const char *title)
|
---|
115 | {
|
---|
116 | Init(filename, name, title);
|
---|
117 |
|
---|
118 | if (cont)
|
---|
119 | AddColumns(cont);
|
---|
120 | }
|
---|
121 |
|
---|
122 | // --------------------------------------------------------------------------
|
---|
123 | //
|
---|
124 | // Destructor. Delete the output file if necessary (it is closed
|
---|
125 | // automatically by its destructor.
|
---|
126 | //
|
---|
127 | MWriteAsciiFile::~MWriteAsciiFile()
|
---|
128 | {
|
---|
129 | fAutoDel.SetOwner();
|
---|
130 | delete fOut;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // --------------------------------------------------------------------------
|
---|
134 | //
|
---|
135 | // Return open state of the file
|
---|
136 | //
|
---|
137 | Bool_t MWriteAsciiFile::IsFileOpen() const
|
---|
138 | {
|
---|
139 | return (bool)(*fOut);
|
---|
140 | }
|
---|
141 |
|
---|
142 | // --------------------------------------------------------------------------
|
---|
143 | //
|
---|
144 | // Tries to get all containers from the ParList which were given by name
|
---|
145 | // adds them to the list of pointers to the container which should be
|
---|
146 | // written to the ascii file.
|
---|
147 | //
|
---|
148 | Bool_t MWriteAsciiFile::GetContainer(MParList *plist)
|
---|
149 | {
|
---|
150 | TObject *obj=NULL;
|
---|
151 |
|
---|
152 | TIter Next(&fList);
|
---|
153 | while ((obj=Next()))
|
---|
154 | {
|
---|
155 | //
|
---|
156 | // MData is the highest class in the inheritance tree
|
---|
157 | //
|
---|
158 | if (obj->InheritsFrom(MData::Class()))
|
---|
159 | {
|
---|
160 | if (!((MData*)obj)->PreProcess(plist))
|
---|
161 | return kFALSE;
|
---|
162 | continue;
|
---|
163 | }
|
---|
164 |
|
---|
165 | //
|
---|
166 | // MParContainer is the next class in the inheritance tree
|
---|
167 | //
|
---|
168 | if (obj->InheritsFrom(MParContainer::Class()))
|
---|
169 | continue;
|
---|
170 |
|
---|
171 | //
|
---|
172 | // It is neither a MData nor a MParContainer, it must be a TNamed
|
---|
173 | //
|
---|
174 | TObject *o = plist->FindObject(obj->GetName());
|
---|
175 | if (!o)
|
---|
176 | return kFALSE;
|
---|
177 |
|
---|
178 | fList[fList.IndexOf(obj)] = o;
|
---|
179 | }
|
---|
180 | return kTRUE;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // --------------------------------------------------------------------------
|
---|
184 | //
|
---|
185 | // Check if the containers are ready for writing. If so write them.
|
---|
186 | // The containers are written in one line, one after each other.
|
---|
187 | // If not all containers are written (because of the IsReadyToSave-flag)
|
---|
188 | // a warning message is print.
|
---|
189 | //
|
---|
190 | void MWriteAsciiFile::CheckAndWrite() const
|
---|
191 | {
|
---|
192 | Bool_t written = kFALSE;
|
---|
193 |
|
---|
194 | MParContainer *obj = NULL;
|
---|
195 |
|
---|
196 | Int_t num = fList.GetEntries();
|
---|
197 |
|
---|
198 | TIter Next(&fList);
|
---|
199 | while ((obj=(MParContainer*)Next()))
|
---|
200 | {
|
---|
201 | //
|
---|
202 | // Check for the Write flag
|
---|
203 | //
|
---|
204 | if (!obj->IsReadyToSave())
|
---|
205 | continue;
|
---|
206 |
|
---|
207 | //
|
---|
208 | // Write container to file
|
---|
209 | //
|
---|
210 | if (!obj->AsciiWrite(*fOut))
|
---|
211 | continue;
|
---|
212 |
|
---|
213 | //
|
---|
214 | // Store that at least one container was written
|
---|
215 | //
|
---|
216 | written = kTRUE;
|
---|
217 |
|
---|
218 | num--;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (!written)
|
---|
222 | return;
|
---|
223 |
|
---|
224 | *fOut << endl;
|
---|
225 |
|
---|
226 | if (num!=0)
|
---|
227 | *fLog << warn << "Warning - given number of objects doesn't fit number of written objects." << endl;
|
---|
228 | }
|
---|
229 |
|
---|
230 | // --------------------------------------------------------------------------
|
---|
231 | //
|
---|
232 | // Add a rule to be written as a column to the ascii file.
|
---|
233 | // For more information about rules see MDataChain.
|
---|
234 | //
|
---|
235 | // eg: MWriteAsciiFile::AddColumn("log10(MHillas.fEnergy)/2")
|
---|
236 | //
|
---|
237 | void MWriteAsciiFile::AddColumn(const TString rule)
|
---|
238 | {
|
---|
239 | MDataChain *chain = new MDataChain(rule);
|
---|
240 | fList.Add(chain);
|
---|
241 | }
|
---|
242 |
|
---|
243 | // --------------------------------------------------------------------------
|
---|
244 | //
|
---|
245 | // Add another column to be written to the ascii file. The columns will be
|
---|
246 | // output one after each other in one line.
|
---|
247 | // Specify the name of the data member to be written (eg fWidth) and
|
---|
248 | // a possible scale factor (eg. to transform millimeters to degrees)
|
---|
249 | //
|
---|
250 | // eg:
|
---|
251 | // MMcEvt evt;
|
---|
252 | // MWriteAsciiFile::AddColumn(&evt, "fImpact", 0.01);
|
---|
253 | //
|
---|
254 | void MWriteAsciiFile::AddColumn(MParContainer *cont, const TString member, Double_t scale)
|
---|
255 | {
|
---|
256 | MData *data = new MDataMember(cont, member);
|
---|
257 |
|
---|
258 | if (scale!=1)
|
---|
259 | {
|
---|
260 | MDataList *list = new MDataList('*');
|
---|
261 | MDataValue *val = new MDataValue(scale);
|
---|
262 |
|
---|
263 | list->SetOwner();
|
---|
264 | list->AddToList(data);
|
---|
265 | list->AddToList(val);
|
---|
266 |
|
---|
267 | data = list;
|
---|
268 | }
|
---|
269 | fList.Add(data);
|
---|
270 | fAutoDel.Add(data);
|
---|
271 | }
|
---|
272 |
|
---|
273 | // --------------------------------------------------------------------------
|
---|
274 | //
|
---|
275 | // Add another container (by name) to be written to the ascii file.
|
---|
276 | // The container will be output one after each other in one line.
|
---|
277 | // The output will be done either by MParContainer::AsciiWrite or
|
---|
278 | // by the corresponding overloaded function.
|
---|
279 | //
|
---|
280 | // eg: MWriteAsciiFile::AddColumns("MMcEvt")
|
---|
281 | //
|
---|
282 | void MWriteAsciiFile::AddColumns(const TString cont)
|
---|
283 | {
|
---|
284 | TNamed *name = new TNamed(cont, cont);
|
---|
285 | fList.Add(name);
|
---|
286 | fAutoDel.Add(name);
|
---|
287 | }
|
---|
288 |
|
---|
289 | // --------------------------------------------------------------------------
|
---|
290 | //
|
---|
291 | // Add another container (by pointer) to be written to the ascii file.
|
---|
292 | // The container will be output one after each other in one line.
|
---|
293 | // The output will be done either by MParContainer::AsciiWrite or
|
---|
294 | // by the corresponding overloaded function.
|
---|
295 | //
|
---|
296 | // eg:
|
---|
297 | // MMcEvt evt;
|
---|
298 | // MWriteAsciiFile::AddColumns(&evt);
|
---|
299 | //
|
---|
300 | void MWriteAsciiFile::AddColumns(MParContainer *cont)
|
---|
301 | {
|
---|
302 | fList.Add(cont);
|
---|
303 | }
|
---|