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

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