source: trunk/Mars/mfileio/MWriteFitsFile.h@ 15658

Last change on this file since 15658 was 15277, checked in by lyard, 12 years ago
added fits files header key support
File size: 9.1 KB
Line 
1#ifndef MARS_MWreitFitsFile
2#define MARS_MWreitFitsFile
3
4#include <list>
5#include <vector>
6#include <map>
7
8#include <TClass.h>
9
10#ifndef MARS_MWriteFile
11#include "MWriteFile.h"
12#endif
13
14#ifndef MARS_MTopFitsGroup
15#include "MTopFitsGroup.h"
16#endif
17
18#ifndef MARS_MFitsArray
19#include "MFitsArray.h"
20#endif
21
22///////////////////////////////////////////////////////////////////////////////
23// Information of one MParContainer, which data should be written into all or
24// just a few columns of one FITS table
25class MFitsSubTable
26{
27 // the container, which data should be written to the FITS sub-table.
28 MParContainer *fContainer;
29
30 // used only if the Container is defined by its name.
31 // if kTRUE must exist in the MParList of the program
32 // if kFALSE the container is ignored. if it is not in the
33 // MParList of the program
34 Bool_t fMust;
35
36public:
37 MFitsSubTable( Bool_t must)
38 : fContainer(NULL), fMust(must)
39 {}
40
41 MFitsSubTable(MParContainer * container, Bool_t must)
42 : fContainer(container), fMust(must)
43 {}
44
45
46 MFitsSubTable(const MFitsSubTable & subTable)
47 : fContainer(subTable.fContainer), fMust(subTable.fMust)
48 {}
49
50 Bool_t MustHave() {return fMust;}
51 MParContainer * GetContainer() {return fContainer;}
52 void SetContainer(MParContainer * cont)
53 {fContainer = cont;}
54
55};
56
57
58
59///////////////////////////////////////////////////////////////////////////////
60// A class to write data of MParContainer into FITS files.
61class MWriteFitsFile : public MWriteFile
62{
63private:
64 // all (global) currently open top FITS groups.
65 // Note: several instances of MWriteFitsFile can write to the same
66 // top level FITS group (a top level FITS group corresponds to
67 // one ROOT file)
68 static std::map<MUniqueFileId, MTopFitsGroup> fTopFitsGroups;
69
70 // pointer to the top level FITS group of this instance
71 std::map<MUniqueFileId, MTopFitsGroup>::iterator iTopFitsGroup;
72
73 // the key (TString) of following maps is the FITS table name, which
74 // corresponds to one TTree
75
76 // all main FITS table. Children tables to store data of
77 // TClonesArray are not in this map.
78 //ETIENNE ofits objects cannot be copied. So store pointers instead
79 std::map<TString, ofits*> fFitsTables;
80
81 // all information needed to write data of TClonesArray to their
82 // own FITS table. Note: one parent FTIS table may have several
83 // children tables, each for one TClonesArray.
84 std::map<TString, std::list<MArrayHelperBase *> > fClHelper;
85
86 // all information about the MParContainer, which data are stored
87 // in the same FITS table. The key of map<TString, MFitsSubTable>
88 // is the class name of the MParContainer
89 std::map<TString, std::map<TString, MFitsSubTable> > fSubTables;
90
91 std::map<TString, std::vector<void*> > fDataPointers;
92 std::map<TString, std::vector<char> > fTypeChars;
93 std::map<TString, std::vector<uint32_t> > fColSizes;
94 std::map<TString, std::vector<uint32_t> > fColWidth;
95 std::map<TString, bool> fTableObjectCreated;
96 std::map<TString, bool> fTableHeaderWritten;
97
98 void DeleteArrayHelper();
99
100 // rule to construct an output file-name from the input-filename
101 TString fRule;
102
103 TString fOpenOption; // RECREATE or NEW
104 TString fGroupName; // name of top level group
105 TString fTitle; // title of top level group
106
107 void OpenTopLevelGroup(const char * fname);
108 void CloseTopLevelGroup();
109
110 Bool_t InitColumns(const TString & tableName,
111 const TString & parentVarName,
112 ofits* fitsTable,
113 void * baseAdr,
114 TClass * classDef);
115 void InitSingleColumn(const TString& tableName,
116 uint32_t count,
117 const std::string& typeName,
118 void* dataPointer,
119 const std::string& columnName,
120 const std::string& unit,
121 const std::string& comment);
122 void writeOneRow(const TString& tableName);
123 void InitAttr(const char* attrName,
124 const char* dataType,
125 void* var,
126 const char* unit=NULL,
127 const char* comment=NULL,
128 ofits* outFile=NULL);
129
130 // MWrite
131 Bool_t IsFileOpen() const
132 {return iTopFitsGroup != fTopFitsGroups.end();}
133 Bool_t CheckAndWrite();
134 Bool_t GetContainer(MParList *pList);
135 const char *GetFileName() const;
136
137 Int_t PreProcess(MParList *pList);
138 Int_t PostProcess();
139
140 //Header keys related stuff
141 template<typename _T>
142 std::string GetFitsString(const _T& value)
143 {
144 std::ostringstream returnVal;
145 std::string typeName = typeid(_T).name();
146 if (typeName == "i" || typeName == "j" || //int
147 typeName == "s" || typeName == "t" || //short
148 typeName == "l" || typeName == "m" || //long
149 typeName == "x" || typeName == "y") //long long
150 returnVal << value;
151
152 if (typeName == "b")
153 {
154 if (value) returnVal << "T"; else returnVal << "F";
155 }
156 if (typeName == "Ss" || typeName == "PKc" ||
157 (typeName.size() >= 4 && typeName[0] == 'A' && typeName[typeName.size()-1] == 'c' && typeName[typeName.size()-2] == '_'))
158 {
159 returnVal << value;
160 std::string temp = returnVal.str();
161 returnVal.str("");
162 for (std::string::iterator c=temp.begin(); c<temp.end(); c++)
163 if (*c=='\'')
164 temp.insert(c++, '\'');
165 returnVal << "'" << temp << "'";
166 }
167 if (returnVal.str() == "")
168 *fLog << warn << "WARNING: could not construct fits string from \"" << value << "\" typename: " << typeName << endl;
169 return returnVal.str();
170 }
171
172public:
173 template<typename _T>
174 bool SetHeaderKey(const std::string& key,
175 const _T& value,
176 const std::string& comment="")
177 {
178 //check if header was already written
179 for (std::map<TString, bool>::iterator it=fTableHeaderWritten.begin(); it!= fTableHeaderWritten.end(); it++)
180 if (it->second == true)
181 return false;
182 //headers were not written yet. Add one key
183 std::vector<ofits::Key>::iterator it = fHeaderKeys.begin();
184 //check if the value had been set beforel
185 for (;it!=fHeaderKeys.end();it++)
186 {
187 if (it->key == key)
188 {
189 it->value = GetFitsString(value);
190 it->comment = comment;
191 break;
192 }
193 }
194 //did we find the key ?
195 if (it == fHeaderKeys.end())
196 {//no ? add it !
197 ofits::Key temp;
198 temp.key = key;
199 temp.value = GetFitsString(value);
200 temp.comment = comment;
201 fHeaderKeys.push_back(temp);
202 }
203 return true;
204 }
205private:
206 std::vector<ofits::Key> fHeaderKeys;
207
208 std::string Trim(const std::string &str);
209
210 // MTask
211 Bool_t ReInit(MParList *pList);
212
213 std::vector<std::string> fVetoedColumns;
214 std::map<std::string, uint32_t> fBytesPerSamples;
215
216
217public:
218
219 //Veto a type-mapping functions and variables
220 Bool_t VetoColumn(const std::string& colName);
221 Bool_t SetBytesPerSample(const std::string& colName, uint32_t numBytes);
222
223 enum FILE_MODE {
224 kMultiFiles,
225 kSingleFile
226 } ;
227
228 MWriteFitsFile(const char *fname,
229 FILE_MODE fileMode = kMultiFiles,
230 const Option_t *opt="RECREATE",
231 const char *name=NULL,
232 const char *title=NULL);
233 //Similar contructor to what MWriteRootFiles takes.
234 //Some of the args are not used: comp as they does not make sense in FITS
235 MWriteFitsFile(const Int_t comp,
236 const char* rule,
237 const Option_t* option="RECREATE",
238 const char* fTitle="Untitled",
239 const char* name=NULL,
240 const char* title=NULL);
241
242 ~MWriteFitsFile();
243
244
245 void AddContainer(const char *cName, const char *tName=NULL, Bool_t must=kTRUE);
246 void AddContainer(MParContainer *cont, const char *tName=NULL, Bool_t must=kTRUE);
247
248
249 void AddTree(const char *name, Bool_t force=kTRUE)
250 {
251 AddContainer(Form("MReport%s", name), name, force);
252 AddContainer(Form("MTime%s", name), name, force);
253 }
254
255 ClassDef(MWriteFitsFile, 0)
256};
257//Specializations for float and doubles (because of the precision handling, I could not deal with it in the main function
258template<>
259std::string MWriteFitsFile::GetFitsString(const double& value);
260template<>
261std::string MWriteFitsFile::GetFitsString(const float& value);
262#endif
Note: See TracBrowser for help on using the repository browser.