| 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->GetName()[0]=='\0')
|
|---|
| 156 | {
|
|---|
| 157 | if (!cont->AsciiWrite(*fOut))
|
|---|
| 158 | continue;
|
|---|
| 159 | }
|
|---|
| 160 | else
|
|---|
| 161 | {
|
|---|
| 162 | if (!cont->WriteDataMember(*fOut, memb->GetName(), memb->GetScale()))
|
|---|
| 163 | continue;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | *fOut << " ";
|
|---|
| 167 | written = kTRUE;
|
|---|
| 168 |
|
|---|
| 169 | num--;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if (!written)
|
|---|
| 173 | return;
|
|---|
| 174 |
|
|---|
| 175 | *fOut << endl;
|
|---|
| 176 |
|
|---|
| 177 | if (num!=0)
|
|---|
| 178 | *fLog << warn << "Warning - given number of containers doesn't fit number of written containers." << endl;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | // --------------------------------------------------------------------------
|
|---|
| 182 | //
|
|---|
| 183 | // Return open state of the file
|
|---|
| 184 | //
|
|---|
| 185 | Bool_t MWriteAsciiFile::IsFileOpen() const
|
|---|
| 186 | {
|
|---|
| 187 | return (bool)(*fOut);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | // --------------------------------------------------------------------------
|
|---|
| 191 | //
|
|---|
| 192 | // Tries to get all containers from the ParList which were given by name
|
|---|
| 193 | // adds them to the list of pointers to the container which should be
|
|---|
| 194 | // written to the ascii file.
|
|---|
| 195 | //
|
|---|
| 196 | Bool_t MWriteAsciiFile::GetContainer(MParList *pList)
|
|---|
| 197 | {
|
|---|
| 198 | MScale *obj = NULL;
|
|---|
| 199 |
|
|---|
| 200 | TIter Next(&fContNames);
|
|---|
| 201 |
|
|---|
| 202 | while ((obj=(MScale*)Next()))
|
|---|
| 203 | {
|
|---|
| 204 | const char *name = obj->GetName();
|
|---|
| 205 |
|
|---|
| 206 | MParContainer *cont = (MParContainer*)pList->FindObject(name, "MParContainer");
|
|---|
| 207 | if (!cont)
|
|---|
| 208 | {
|
|---|
| 209 | *fLog << err << dbginf << "Cannot find parameter container '" << name << "'." << endl;
|
|---|
| 210 | return kFALSE;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | AddContainer(cont, obj->GetTitle(), obj->GetScale());
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | return kTRUE;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | // --------------------------------------------------------------------------
|
|---|
| 220 | //
|
|---|
| 221 | // Add another container (by name) to be written to the ascii file.
|
|---|
| 222 | // The container will be output one after each other in one line.
|
|---|
| 223 | // If you want to write only one data member of the container
|
|---|
| 224 | // specify the name of the data member (eg. fAlpha) Make sure,
|
|---|
| 225 | // that a "GetteMethod" for this data type exists (strip the f and
|
|---|
| 226 | // replace it by Get)
|
|---|
| 227 | // If you specify a single data member you can add a scale-factor which
|
|---|
| 228 | // is (in case of the data member being a floating point value) multiplied
|
|---|
| 229 | // with the data member value. This is usefull if you are want to
|
|---|
| 230 | // change the scale (unit) of a data member for writing (eg.
|
|---|
| 231 | // writing degrees for the hillas parameters instead of the internally
|
|---|
| 232 | // used millimeters)
|
|---|
| 233 | //
|
|---|
| 234 | void MWriteAsciiFile::AddContainer(const char *cname, const char *member, Double_t scale)
|
|---|
| 235 | {
|
|---|
| 236 | MScale *name = new MScale(cname, member, scale);
|
|---|
| 237 | fContNames.AddLast(name);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | // --------------------------------------------------------------------------
|
|---|
| 241 | //
|
|---|
| 242 | // Add another container (by pointer) to be written to the ascii file.
|
|---|
| 243 | // The container will be output one after each other in one line.
|
|---|
| 244 | // If you want to write only one data member of the container
|
|---|
| 245 | // specify the name of the data member (eg. fAlpha) Make sure,
|
|---|
| 246 | // that a "GetteMethod" for this data type exists (strip the f and
|
|---|
| 247 | // replace it by Get)
|
|---|
| 248 | // If you specify a single data member you can add a scale-factor which
|
|---|
| 249 | // is (in case of the data member being a floating point value) multiplied
|
|---|
| 250 | // with the data member value. This is usefull if you are want to
|
|---|
| 251 | // change the scale (unit) of a data member for writing (eg.
|
|---|
| 252 | // writing degrees for the hillas parameters instead of the internally
|
|---|
| 253 | // used millimeters)
|
|---|
| 254 | //
|
|---|
| 255 | void MWriteAsciiFile::AddContainer(MParContainer *cont, const char *member, Double_t scale)
|
|---|
| 256 | {
|
|---|
| 257 | fContainer.AddLast(cont);
|
|---|
| 258 |
|
|---|
| 259 | MScale *name = new MScale(member, "", scale);
|
|---|
| 260 | fMembers.AddLast(name);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|