| 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 | // Specify the name of the ascii output file 'filename' and the name
|
|---|
| 53 | // of the container you want to write. (Normally this is the name
|
|---|
| 54 | // of the class (eg. MHillas) but it can also be a different name which
|
|---|
| 55 | // identifies the container in the parameter list.
|
|---|
| 56 | //
|
|---|
| 57 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, const char *contname,
|
|---|
| 58 | const char *name, const char *title)
|
|---|
| 59 | : fOut(NULL), fContainer(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 | // Specify a the name of the ascii output file 'filename' and a pointer to
|
|---|
| 71 | // the container you want to write.
|
|---|
| 72 | //
|
|---|
| 73 | MWriteAsciiFile::MWriteAsciiFile(const char *filename, MParContainer *cont,
|
|---|
| 74 | const char *name, const char *title)
|
|---|
| 75 | : fOut(NULL), fContainer(cont)
|
|---|
| 76 | {
|
|---|
| 77 | *fName = name ? name : "MWriteAsciiFile";
|
|---|
| 78 | *fTitle = title ? title : "Task to write one container to an ascii file";
|
|---|
| 79 |
|
|---|
| 80 | fNameFile = filename;
|
|---|
| 81 | fNameContainer = cont->GetName();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | // --------------------------------------------------------------------------
|
|---|
| 85 | //
|
|---|
| 86 | // Destructor. Delete the output file if necessary (it is closed
|
|---|
| 87 | // automatically by its destructor.
|
|---|
| 88 | //
|
|---|
| 89 | MWriteAsciiFile::~MWriteAsciiFile()
|
|---|
| 90 | {
|
|---|
| 91 | if (fOut)
|
|---|
| 92 | delete fOut;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | // --------------------------------------------------------------------------
|
|---|
| 96 | //
|
|---|
| 97 | // Tries to open the given output file. And tries to get a pointer to the
|
|---|
| 98 | // instance of the container which should be written.
|
|---|
| 99 | // If the container has already the HasChanged flag it is immediatly written
|
|---|
| 100 | // to the output file.
|
|---|
| 101 | //
|
|---|
| 102 | Bool_t MWriteAsciiFile::PreProcess (MParList *pList)
|
|---|
| 103 | {
|
|---|
| 104 | //
|
|---|
| 105 | // Try to find the container which should be stored.
|
|---|
| 106 | //
|
|---|
| 107 | if (!fContainer)
|
|---|
| 108 | {
|
|---|
| 109 | fContainer = (MParContainer*)pList->FindObject(fNameContainer);
|
|---|
| 110 | if (!fContainer)
|
|---|
| 111 | {
|
|---|
| 112 | *fLog << dbginf << "Cannot find parameter container '" << fContainer << "'." << endl;
|
|---|
| 113 | return kFALSE;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | //
|
|---|
| 118 | // Try to open the output file
|
|---|
| 119 | //
|
|---|
| 120 | fOut = new ofstream(fNameFile);
|
|---|
| 121 |
|
|---|
| 122 | if (!(*fOut))
|
|---|
| 123 | {
|
|---|
| 124 | *fLog << dbginf << "Cannot open Ascii file '" << fNameFile << "' for writing." << endl;
|
|---|
| 125 | return kFALSE;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | *fLog << "Ascii file '" << fNameFile << "' opened for writing." << endl;
|
|---|
| 129 |
|
|---|
| 130 | //
|
|---|
| 131 | // write the container if it is already in changed state
|
|---|
| 132 | //
|
|---|
| 133 | if (fContainer->HasChanged())
|
|---|
| 134 | fContainer->AsciiWrite(*fOut);
|
|---|
| 135 |
|
|---|
| 136 | return kTRUE;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // --------------------------------------------------------------------------
|
|---|
| 140 | //
|
|---|
| 141 | // Checks if the HasChanged flag of the output container is set. If it is set
|
|---|
| 142 | // the container is instructed to write itself to the ascii file.
|
|---|
| 143 | // (By calling its memberfunction AsciiWrite. You find the Prototype in
|
|---|
| 144 | // MParContainer)
|
|---|
| 145 | //
|
|---|
| 146 | Bool_t MWriteAsciiFile::Process()
|
|---|
| 147 | {
|
|---|
| 148 | if (fContainer->HasChanged())
|
|---|
| 149 | fContainer->AsciiWrite(*fOut);
|
|---|
| 150 |
|
|---|
| 151 | return kTRUE;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // --------------------------------------------------------------------------
|
|---|
| 155 | //
|
|---|
| 156 | // If the output container has the HasChanged flag set, it is written to the
|
|---|
| 157 | // output file (eg. this could be some calculated result)
|
|---|
| 158 | // If the output file object exists, delete it. (The file is closed
|
|---|
| 159 | // automatically in the corresponding destructor)
|
|---|
| 160 | //
|
|---|
| 161 | Bool_t MWriteAsciiFile::PostProcess()
|
|---|
| 162 | {
|
|---|
| 163 | //
|
|---|
| 164 | // check if the container changed state is set
|
|---|
| 165 | //
|
|---|
| 166 | if (fContainer->HasChanged())
|
|---|
| 167 | fContainer->AsciiWrite(*fOut);
|
|---|
| 168 |
|
|---|
| 169 | //
|
|---|
| 170 | // delete (close) file
|
|---|
| 171 | //
|
|---|
| 172 | delete fOut;
|
|---|
| 173 | fOut = NULL;
|
|---|
| 174 |
|
|---|
| 175 | return kTRUE;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|