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 |
|
---|
51 | ClassImp(MWriteAsciiFile);
|
---|
52 |
|
---|
53 | // --------------------------------------------------------------------------
|
---|
54 | //
|
---|
55 | // Init. Replaces the same code used in all constructors.
|
---|
56 | //
|
---|
57 | void 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 | //
|
---|
78 | MWriteAsciiFile::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 | //
|
---|
97 | MWriteAsciiFile::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 | //
|
---|
110 | MWriteAsciiFile::~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 | //
|
---|
124 | void MWriteAsciiFile::CheckAndWrite() const
|
---|
125 | {
|
---|
126 | Bool_t written = kFALSE;
|
---|
127 |
|
---|
128 | MParContainer *cont = NULL;
|
---|
129 |
|
---|
130 | Int_t num = fContainer.GetSize();
|
---|
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 | *fOut << 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 | //
|
---|
157 | Bool_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 | //
|
---|
168 | Bool_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);
|
---|
179 | if (!cont)
|
---|
180 | {
|
---|
181 | *fLog << err << dbginf << "Cannot find parameter container '" << name << "'." << endl;
|
---|
182 | return kFALSE;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (!cont->InheritsFrom(MParContainer::Class()))
|
---|
186 | {
|
---|
187 | *fLog << err << dbginf << "'" << name << "' doesn't inherit from MParContainer." << endl;
|
---|
188 | return kFALSE;
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | AddContainer(cont);
|
---|
193 | }
|
---|
194 |
|
---|
195 | return kTRUE;
|
---|
196 | }
|
---|
197 |
|
---|
198 | // --------------------------------------------------------------------------
|
---|
199 | //
|
---|
200 | // Add another container (by name) to be written to the ascii file.
|
---|
201 | // The container will be output one after each other in one line.
|
---|
202 | //
|
---|
203 | void MWriteAsciiFile::AddContainer(const char *cname)
|
---|
204 | {
|
---|
205 | TNamed *named = new TNamed(cname, "");
|
---|
206 | fContNames.AddLast(named);
|
---|
207 | }
|
---|
208 |
|
---|
209 | // --------------------------------------------------------------------------
|
---|
210 | //
|
---|
211 | // Add another container (by pointer) to be written to the ascii file.
|
---|
212 | // The container will be output one after each other in one line.
|
---|
213 | //
|
---|
214 | void MWriteAsciiFile::AddContainer(MParContainer *cont)
|
---|
215 | {
|
---|
216 | fContainer.AddLast(cont);
|
---|
217 | }
|
---|
218 |
|
---|