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 (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 HasChanged 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 |
|
---|
48 | ClassImp(MWriteAsciiFile)
|
---|
49 |
|
---|
50 | // --------------------------------------------------------------------------
|
---|
51 | //
|
---|
52 | // Default constructor.
|
---|
53 | // Specify the name of the ascii output file 'filename' and the name
|
---|
54 | // of the container you want to write. (Normally this is the name
|
---|
55 | // of the class (eg. MHillas) but it can also be a different name which
|
---|
56 | // identifies the container in the parameter list.
|
---|
57 | //
|
---|
58 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
|
---|
59 | const char *name, const char *title) : fOut(NULL)
|
---|
60 | {
|
---|
61 | *fName = name ? name : "MWriteAsciiFile";
|
---|
62 | *fTitle = title ? title : "Task to write one container to an ascii file";
|
---|
63 |
|
---|
64 | fNameFile = filename;
|
---|
65 | fNameContainer = contname;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // --------------------------------------------------------------------------
|
---|
69 | //
|
---|
70 | // Destructor. Delete the output file if necessary (it is closed
|
---|
71 | // automatically by its destructor.
|
---|
72 | //
|
---|
73 | MWriteAsciiFile::~MWriteAsciiFile()
|
---|
74 | {
|
---|
75 | if (fOut)
|
---|
76 | delete fOut;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // --------------------------------------------------------------------------
|
---|
80 | //
|
---|
81 | // Tries to open the given output file. And tries to get a pointer to the
|
---|
82 | // instance of the container which should be written.
|
---|
83 | // If the container has already the HasChanged flag it is immediatly written
|
---|
84 | // to the output file.
|
---|
85 | //
|
---|
86 | Bool_t MWriteAsciiFile::PreProcess (MParList *pList)
|
---|
87 | {
|
---|
88 | fOut = new ofstream(fNameFile);
|
---|
89 |
|
---|
90 | if (!(*fOut))
|
---|
91 | {
|
---|
92 | *fLog << dbginf << "Cannot open Ascii file '" << fNameFile << "' for writing." << endl;
|
---|
93 | return kFALSE;
|
---|
94 | }
|
---|
95 |
|
---|
96 | *fLog << "Ascii file '" << fNameFile << "' opened for writing." << endl;
|
---|
97 |
|
---|
98 | fContainer = (MParContainer*)pList->FindObject(fNameContainer);
|
---|
99 | if (!fContainer)
|
---|
100 | {
|
---|
101 | *fLog << dbginf << "Cannot find parameter container '" << fContainer << "'." << endl;
|
---|
102 | return kFALSE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (fContainer->HasChanged())
|
---|
106 | fContainer->AsciiWrite(*fOut);
|
---|
107 |
|
---|
108 | return kTRUE;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // --------------------------------------------------------------------------
|
---|
112 | //
|
---|
113 | // Checks if the HasChanged flag of the output container is set. If it is set
|
---|
114 | // the container is instructed to write itself to the ascii file.
|
---|
115 | // (By calling its memberfunction AsciiWrite. You find the Prototype in
|
---|
116 | // MParContainer)
|
---|
117 | //
|
---|
118 | Bool_t MWriteAsciiFile::Process()
|
---|
119 | {
|
---|
120 | if (fContainer->HasChanged())
|
---|
121 | fContainer->AsciiWrite(*fOut);
|
---|
122 |
|
---|
123 | return kTRUE;
|
---|
124 | }
|
---|
125 |
|
---|
126 | // --------------------------------------------------------------------------
|
---|
127 | //
|
---|
128 | // If the output container has the HasChanged flag set, it is written to the
|
---|
129 | // output file (eg. this could be some calculated result)
|
---|
130 | // If the output file object exists, delete it. (The file is closed
|
---|
131 | // automatically in the corresponding destructor)
|
---|
132 | //
|
---|
133 | Bool_t MWriteAsciiFile::PostProcess()
|
---|
134 | {
|
---|
135 | if (fContainer->HasChanged())
|
---|
136 | fContainer->AsciiWrite(*fOut);
|
---|
137 |
|
---|
138 | delete fOut;
|
---|
139 | fOut = NULL;
|
---|
140 |
|
---|
141 | return kTRUE;
|
---|
142 | }
|
---|
143 |
|
---|