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 | // 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.h>
|
---|
51 |
|
---|
52 | #include <TClass.h> // IsA
|
---|
53 | #include <TMethodCall.h> // TMethodCall, AsciiWrite
|
---|
54 | #include <TDataMember.h> // TDataMember, AsciiWrite
|
---|
55 |
|
---|
56 | #include "MLog.h"
|
---|
57 | #include "MLogManip.h"
|
---|
58 |
|
---|
59 | #include "MParList.h"
|
---|
60 |
|
---|
61 | ClassImp(MWriteAsciiFile);
|
---|
62 |
|
---|
63 | // --------------------------------------------------------------------------
|
---|
64 | //
|
---|
65 | // Init. Replaces the same code used in all constructors.
|
---|
66 | //
|
---|
67 | void MWriteAsciiFile::Init(const char *filename, const char *name, const char *title)
|
---|
68 | {
|
---|
69 | fName = name ? name : "MWriteAsciiFile";
|
---|
70 | fTitle = title ? title : "Task to write one container to an ascii file";
|
---|
71 |
|
---|
72 | fNameFile = filename;
|
---|
73 |
|
---|
74 | fOut = new ofstream(fNameFile);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | //
|
---|
79 | // Specify the name of the ascii output file 'filename' and the name
|
---|
80 | // of the container you want to write. (Normally this is the name
|
---|
81 | // of the class (eg. MHillas) but it can also be a different name which
|
---|
82 | // identifies the container in the parameter list.
|
---|
83 | // Because you cannot write more than one container there is no Add-function
|
---|
84 | // like in MWriteRootFile.
|
---|
85 | //
|
---|
86 | // For Example: MWriteAsciiFile("file.txt", "MHillas");
|
---|
87 | //
|
---|
88 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
|
---|
89 | const char *name, const char *title)
|
---|
90 | {
|
---|
91 | Init(filename, name, title);
|
---|
92 |
|
---|
93 | if (contname)
|
---|
94 | AddContainer(contname);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // --------------------------------------------------------------------------
|
---|
98 | //
|
---|
99 | // Specify a the name of the ascii output file 'filename' and a pointer to
|
---|
100 | // the container you want to write.
|
---|
101 | // Because you cannot write more than one container there is no Add-function
|
---|
102 | // like in MWriteRootFile.
|
---|
103 | //
|
---|
104 | // For Example: MHillas hillas;
|
---|
105 | // MWriteAsciiFile("file.txt", &hillas);
|
---|
106 | //
|
---|
107 | //
|
---|
108 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, MParContainer *cont,
|
---|
109 | const char *name, const char *title)
|
---|
110 | {
|
---|
111 | Init(filename, name, title);
|
---|
112 |
|
---|
113 | if (cont)
|
---|
114 | AddContainer(cont);
|
---|
115 | }
|
---|
116 |
|
---|
117 | // --------------------------------------------------------------------------
|
---|
118 | //
|
---|
119 | // Destructor. Delete the output file if necessary (it is closed
|
---|
120 | // automatically by its destructor.
|
---|
121 | //
|
---|
122 | MWriteAsciiFile::~MWriteAsciiFile()
|
---|
123 | {
|
---|
124 | fContNames.SetOwner();
|
---|
125 | fMembers.SetOwner();
|
---|
126 |
|
---|
127 | delete fOut;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // --------------------------------------------------------------------------
|
---|
131 | //
|
---|
132 | // Check if the containers are ready for writing. If so write them.
|
---|
133 | // The containers are written in one line, one after each other.
|
---|
134 | // If not all containers are written (because of the IsReadyToSave-flag)
|
---|
135 | // a warning message is print.
|
---|
136 | //
|
---|
137 | void MWriteAsciiFile::CheckAndWrite() const
|
---|
138 | {
|
---|
139 | Bool_t written = kFALSE;
|
---|
140 |
|
---|
141 | MParContainer *cont = NULL;
|
---|
142 |
|
---|
143 | Int_t num = fContainer.GetEntries();
|
---|
144 |
|
---|
145 | TIter NextCont(&fContainer);
|
---|
146 | TIter NextMemb(&fMembers);
|
---|
147 |
|
---|
148 | while ((cont=(MParContainer*)NextCont()))
|
---|
149 | {
|
---|
150 | const MScale *memb = (MScale*)NextMemb();
|
---|
151 |
|
---|
152 | if (!cont->IsReadyToSave())
|
---|
153 | continue;
|
---|
154 |
|
---|
155 | if (memb->GetTitle()[0]=='\0')
|
---|
156 | {
|
---|
157 | if (!cont->AsciiWrite(*fOut))
|
---|
158 | continue;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | if (!cont->WriteDataMember(*fOut, memb->GetTitle(), memb->GetScale()))
|
---|
163 | continue;
|
---|
164 | }
|
---|
165 |
|
---|
166 | written = kTRUE;
|
---|
167 |
|
---|
168 | num--;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (!written)
|
---|
172 | return;
|
---|
173 |
|
---|
174 | *fOut << endl;
|
---|
175 |
|
---|
176 | if (num!=0)
|
---|
177 | *fLog << warn << "Warning - given number of containers doesn't fit number of written containers." << endl;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // --------------------------------------------------------------------------
|
---|
181 | //
|
---|
182 | // Return open state of the file
|
---|
183 | //
|
---|
184 | Bool_t MWriteAsciiFile::IsFileOpen() const
|
---|
185 | {
|
---|
186 | return (bool)(*fOut);
|
---|
187 | }
|
---|
188 |
|
---|
189 | // --------------------------------------------------------------------------
|
---|
190 | //
|
---|
191 | // Tries to get all containers from the ParList which were given by name
|
---|
192 | // adds them to the list of pointers to the container which should be
|
---|
193 | // written to the ascii file.
|
---|
194 | //
|
---|
195 | Bool_t MWriteAsciiFile::GetContainer(MParList *pList)
|
---|
196 | {
|
---|
197 | MScale *obj = NULL;
|
---|
198 |
|
---|
199 | TIter Next(&fContNames);
|
---|
200 |
|
---|
201 | while ((obj=(MScale*)Next()))
|
---|
202 | {
|
---|
203 | const char *name = obj->GetName();
|
---|
204 |
|
---|
205 | MParContainer *cont = (MParContainer*)pList->FindObject(name, "MParContainer");
|
---|
206 | if (!cont)
|
---|
207 | {
|
---|
208 | *fLog << err << dbginf << "Cannot find parameter container '" << name << "'." << endl;
|
---|
209 | return kFALSE;
|
---|
210 | }
|
---|
211 |
|
---|
212 | AddContainer(cont, obj->GetTitle(), obj->GetScale());
|
---|
213 | }
|
---|
214 |
|
---|
215 | return kTRUE;
|
---|
216 | }
|
---|
217 |
|
---|
218 | // --------------------------------------------------------------------------
|
---|
219 | //
|
---|
220 | // Add another container (by name) to be written to the ascii file.
|
---|
221 | // The container will be output one after each other in one line.
|
---|
222 | // If you want to write only one data member of the container
|
---|
223 | // specify the name of the data member (eg. fAlpha) Make sure,
|
---|
224 | // that a "GetteMethod" for this data type exists (strip the f and
|
---|
225 | // replace it by Get)
|
---|
226 | // If you specify a single data member you can add a scale-factor which
|
---|
227 | // is (in case of the data member being a floating point value) multiplied
|
---|
228 | // with the data member value. This is usefull if you are want to
|
---|
229 | // change the scale (unit) of a data member for writing (eg.
|
---|
230 | // writing degrees for the hillas parameters instead of the internally
|
---|
231 | // used millimeters)
|
---|
232 | //
|
---|
233 | void MWriteAsciiFile::AddContainer(const char *cname, const char *member, Double_t scale)
|
---|
234 | {
|
---|
235 | MScale *name = new MScale(cname, member, scale);
|
---|
236 | fContNames.AddLast(name);
|
---|
237 | }
|
---|
238 |
|
---|
239 | // --------------------------------------------------------------------------
|
---|
240 | //
|
---|
241 | // Add another container (by pointer) to be written to the ascii file.
|
---|
242 | // The container will be output one after each other in one line.
|
---|
243 | // If you want to write only one data member of the container
|
---|
244 | // specify the name of the data member (eg. fAlpha) Make sure,
|
---|
245 | // that a "GetteMethod" for this data type exists (strip the f and
|
---|
246 | // replace it by Get)
|
---|
247 | // If you specify a single data member you can add a scale-factor which
|
---|
248 | // is (in case of the data member being a floating point value) multiplied
|
---|
249 | // with the data member value. This is usefull if you are want to
|
---|
250 | // change the scale (unit) of a data member for writing (eg.
|
---|
251 | // writing degrees for the hillas parameters instead of the internally
|
---|
252 | // used millimeters)
|
---|
253 | //
|
---|
254 | void MWriteAsciiFile::AddContainer(MParContainer *cont, const char *member, Double_t scale)
|
---|
255 | {
|
---|
256 | fContainer.AddLast(cont);
|
---|
257 |
|
---|
258 | MScale *name = new MScale("", member, scale);
|
---|
259 | fMembers.AddLast(name);
|
---|
260 | }
|
---|
261 |
|
---|