source: trunk/MagicSoft/Mars/mbase/MWriteAsciiFile.cc@ 1176

Last change on this file since 1176 was 1176, checked in by tbretz, 23 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 5.8 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@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
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 writte to the ascii file if its ReadyToSave flag is //
35// set (MParContainer) //
36// //
37/////////////////////////////////////////////////////////////////////////////
38
39#include "MWriteAsciiFile.h"
40
41#include <fstream.h>
42
43#include "MLog.h"
44#include "MLogManip.h"
45
46#include "MParList.h"
47
48ClassImp(MWriteAsciiFile);
49
50void MWriteAsciiFile::Init(const char *filename, const char *name, const char *title)
51{
52 fName = name ? name : "MWriteAsciiFile";
53 fTitle = title ? title : "Task to write one container to an ascii file";
54
55 fNameFile = filename;
56
57 fOut = new ofstream(fNameFile);
58}
59
60// --------------------------------------------------------------------------
61//
62// Specify the name of the ascii output file 'filename' and the name
63// of the container you want to write. (Normally this is the name
64// of the class (eg. MHillas) but it can also be a different name which
65// identifies the container in the parameter list.
66// Because you cannot write more than one container there is no Add-function
67// like in MWriteRootFile.
68//
69// For Example: MWriteAsciiFile("file.txt", "MHillas");
70//
71MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
72 const char *name, const char *title)
73{
74 Init(filename, name, title);
75
76 AddContainer(contname);
77}
78
79// --------------------------------------------------------------------------
80//
81// Specify a the name of the ascii output file 'filename' and a pointer to
82// the container you want to write.
83// Because you cannot write more than one container there is no Add-function
84// like in MWriteRootFile.
85//
86// For Example: MHillas hillas;
87// MWriteAsciiFile("file.txt", &hillas);
88//
89//
90MWriteAsciiFile::MWriteAsciiFile(const char *filename, MParContainer *cont,
91 const char *name, const char *title)
92{
93 Init(filename, name, title);
94
95 AddContainer(cont);
96}
97
98// --------------------------------------------------------------------------
99//
100// Destructor. Delete the output file if necessary (it is closed
101// automatically by its destructor.
102//
103MWriteAsciiFile::~MWriteAsciiFile()
104{
105 fContNames.SetOwner();
106
107 delete fOut;
108}
109
110// --------------------------------------------------------------------------
111//
112// Check if our container is ready for writing. If so write it.
113//
114void MWriteAsciiFile::CheckAndWrite() const
115{
116 Bool_t written = kFALSE;
117
118 MParContainer *cont = NULL;
119
120 TIter Next(&fContainer);
121
122 while ((cont=(MParContainer*)Next()))
123 {
124 if (!cont->IsReadyToSave())
125 continue;
126
127 cont->AsciiWrite(*fOut);
128 *fOut << " ";
129 written = kTRUE;
130 }
131
132 if (written)
133 *fOut << endl;
134}
135
136// --------------------------------------------------------------------------
137//
138// Return open state of the file
139//
140Bool_t MWriteAsciiFile::IsFileOpen() const
141{
142 return (bool)(*fOut);
143}
144
145// --------------------------------------------------------------------------
146//
147// If the container is yet unknown and the name of it is known only, try
148// to get the container from the parameter list.
149//
150Bool_t MWriteAsciiFile::GetContainer(MParList *pList)
151{
152 TObject *obj = NULL;
153
154 TIter Next(&fContNames);
155
156 while ((obj=Next()))
157 {
158 const char *name = obj->GetName();
159
160 MParContainer *cont = (MParContainer*)pList->FindObject(name);
161 if (!cont)
162 {
163 *fLog << err << dbginf << "Cannot find parameter container '" << name << "'." << endl;
164 return kFALSE;
165 }
166
167 if (!cont->InheritsFrom(MParContainer::Class()))
168 {
169 *fLog << err << dbginf << "'" << name << "' doesn't inherit from MParContainer." << endl;
170 return kFALSE;
171
172 }
173
174 AddContainer(cont);
175 }
176
177 return kTRUE;
178}
179
180// --------------------------------------------------------------------------
181//
182//
183void MWriteAsciiFile::AddContainer(const char *cname)
184{
185 TNamed *named = new TNamed(cname, "");
186 fContNames.AddLast(named);
187}
188
189// --------------------------------------------------------------------------
190//
191//
192void MWriteAsciiFile::AddContainer(MParContainer *cont)
193{
194 fContainer.AddLast(cont);
195}
196
Note: See TracBrowser for help on using the repository browser.