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