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

Last change on this file since 2925 was 2173, checked in by tbretz, 21 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-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
64ClassImp(MWriteAsciiFile);
65
66using namespace std;
67
68// --------------------------------------------------------------------------
69//
70// Init. Replaces the same code used in all constructors.
71//
72void 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//
93MWriteAsciiFile::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//
113MWriteAsciiFile::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//
127MWriteAsciiFile::~MWriteAsciiFile()
128{
129 fAutoDel.SetOwner();
130 delete fOut;
131}
132
133// --------------------------------------------------------------------------
134//
135// Return open state of the file
136//
137Bool_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//
148Bool_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//
190void 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 if (!obj->IsReadyToSave())
202 continue;
203
204 if (!obj->AsciiWrite(*fOut))
205 continue;
206
207 written = kTRUE;
208
209 num--;
210 }
211
212 if (!written)
213 return;
214
215 *fOut << endl;
216
217 if (num!=0)
218 *fLog << warn << "Warning - given number of objects doesn't fit number of written objects." << endl;
219}
220
221// --------------------------------------------------------------------------
222//
223// Add a rule to be written as a column to the ascii file.
224// For more information about rules see MDataChain.
225//
226// eg: MWriteAsciiFile::AddColumn("log10(MHillas.fEnergy)/2")
227//
228void MWriteAsciiFile::AddColumn(const TString rule)
229{
230 MDataChain *chain = new MDataChain(rule);
231 fList.Add(chain);
232}
233
234// --------------------------------------------------------------------------
235//
236// Add another column to be written to the ascii file. The columns will be
237// output one after each other in one line.
238// Specify the name of the data member to be written (eg fWidth) and
239// a possible scale factor (eg. to transform millimeters to degrees)
240//
241// eg:
242// MMcEvt evt;
243// MWriteAsciiFile::AddColumn(&evt, "fImpact", 0.01);
244//
245void MWriteAsciiFile::AddColumn(MParContainer *cont, const TString member, Double_t scale)
246{
247 MData *data = new MDataMember(cont, member);
248
249 if (scale!=1)
250 {
251 MDataList *list = new MDataList('*');
252 MDataValue *val = new MDataValue(scale);
253
254 list->SetOwner();
255 list->AddToList(data);
256 list->AddToList(val);
257
258 data = list;
259 }
260 fList.Add(data);
261 fAutoDel.Add(data);
262}
263
264// --------------------------------------------------------------------------
265//
266// Add another container (by name) to be written to the ascii file.
267// The container will be output one after each other in one line.
268// The output will be done either by MParContainer::AsciiWrite or
269// by the corresponding overloaded function.
270//
271// eg: MWriteAsciiFile::AddColumns("MMcEvt")
272//
273void MWriteAsciiFile::AddColumns(const TString cont)
274{
275 TNamed *name = new TNamed(cont, cont);
276 fList.Add(name);
277 fAutoDel.Add(name);
278}
279
280// --------------------------------------------------------------------------
281//
282// Add another container (by pointer) to be written to the ascii file.
283// The container will be output one after each other in one line.
284// The output will be done either by MParContainer::AsciiWrite or
285// by the corresponding overloaded function.
286//
287// eg:
288// MMcEvt evt;
289// MWriteAsciiFile::AddColumns(&evt);
290//
291void MWriteAsciiFile::AddColumns(MParContainer *cont)
292{
293 fList.Add(cont);
294}
Note: See TracBrowser for help on using the repository browser.