source: trunk/Mars/mfileio/MWriteAsciiFile.cc@ 10075

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