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

Last change on this file since 3414 was 3336, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 9.5 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//
190Bool_t 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 {
223 *fOut << endl;
224
225 if (num!=0)
226 *fLog << warn << "Warning - given number of objects doesn't fit number of written objects." << endl;
227 }
228 return kTRUE;
229}
230
231// --------------------------------------------------------------------------
232//
233// Add a rule to be written as a column to the ascii file.
234// For more information about rules see MDataChain.
235//
236// eg: MWriteAsciiFile::AddColumn("log10(MHillas.fEnergy)/2")
237//
238void MWriteAsciiFile::AddColumn(const TString rule)
239{
240 MDataChain *chain = new MDataChain(rule);
241 fList.Add(chain);
242}
243
244// --------------------------------------------------------------------------
245//
246// Add another column to be written to the ascii file. The columns will be
247// output one after each other in one line.
248// Specify the name of the data member to be written (eg fWidth) and
249// a possible scale factor (eg. to transform millimeters to degrees)
250//
251// eg:
252// MMcEvt evt;
253// MWriteAsciiFile::AddColumn(&evt, "fImpact", 0.01);
254//
255void MWriteAsciiFile::AddColumn(MParContainer *cont, const TString member, Double_t scale)
256{
257 MData *data = new MDataMember(cont, member);
258
259 if (scale!=1)
260 {
261 MDataList *list = new MDataList('*');
262 MDataValue *val = new MDataValue(scale);
263
264 list->SetOwner();
265 list->AddToList(data);
266 list->AddToList(val);
267
268 data = list;
269 }
270 fList.Add(data);
271 fAutoDel.Add(data);
272}
273
274// --------------------------------------------------------------------------
275//
276// Add another container (by name) to be written to the ascii file.
277// The container will be output one after each other in one line.
278// The output will be done either by MParContainer::AsciiWrite or
279// by the corresponding overloaded function.
280//
281// eg: MWriteAsciiFile::AddColumns("MMcEvt")
282//
283void MWriteAsciiFile::AddColumns(const TString cont)
284{
285 TNamed *name = new TNamed(cont, cont);
286 fList.Add(name);
287 fAutoDel.Add(name);
288}
289
290// --------------------------------------------------------------------------
291//
292// Add another container (by pointer) to be written to the ascii file.
293// The container will be output one after each other in one line.
294// The output will be done either by MParContainer::AsciiWrite or
295// by the corresponding overloaded function.
296//
297// eg:
298// MMcEvt evt;
299// MWriteAsciiFile::AddColumns(&evt);
300//
301void MWriteAsciiFile::AddColumns(MParContainer *cont)
302{
303 fList.Add(cont);
304}
Note: See TracBrowser for help on using the repository browser.