source: branches/AddingGoogleTestEnvironment/mfileio/MWriteFitsFile.h@ 19779

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