source: trunk/MagicSoft/Mars/mfileio/MWriteAsciiFile.cc@ 1788

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