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

Last change on this file since 20063 was 19688, checked in by tbretz, 5 years ago
Propagate name of camera geometry to the header
File size: 9.4 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 TString fGeometry;
120
121 void OpenTopLevelGroup(const char * fname);
122 void CloseTopLevelGroup();
123
124 Bool_t InitColumns(const TString & tableName,
125 const TString & parentVarName,
126 ofits* fitsTable,
127 void * baseAdr,
128 TClass * classDef);
129 void InitSingleColumn(const TString& tableName,
130 uint32_t count,
131 const std::string& typeName,
132 void* dataPointer,
133 const std::string& columnName,
134 const std::string& unit,
135 const std::string& comment);
136 void writeOneRow(const TString& tableName);
137 void InitAttr(const char* attrName,
138 const char* dataType,
139 void* var,
140 const char* unit=NULL,
141 const char* comment=NULL,
142 ofits* outFile=NULL);
143
144 // MWrite
145 Bool_t IsFileOpen() const
146 {return iTopFitsGroup != fTopFitsGroups.end();}
147 Bool_t CheckAndWrite();
148 Bool_t GetContainer(MParList *pList);
149 const char *GetFileName() const;
150
151 Int_t PreProcess(MParList *pList);
152 Int_t PostProcess();
153
154 void SetupHeaderKeys(MRawRunHeader &header, const char *geometry);
155
156public:
157 //Header keys related stuff
158 template<typename _T>
159 std::string GetFitsString(const _T& value)
160 {
161 std::ostringstream returnVal;
162 std::string typeName = typeid(_T).name();
163 if (typeName == "i" || typeName == "j" || //int
164 typeName == "s" || typeName == "t" || //short
165 typeName == "l" || typeName == "m" || //long
166 typeName == "x" || typeName == "y") //long long
167 returnVal << value;
168 if (typeName == "b")
169 {
170 if (value) returnVal << "T"; else returnVal << "F";
171 }
172 if (typeName == "Ss" || typeName == "PKc" ||
173 (typeName.size() >= 4 && typeName[0] == 'A' && typeName[typeName.size()-1] == 'c' && typeName[typeName.size()-2] == '_'))
174 {
175 returnVal << value;
176 std::string temp = returnVal.str();
177 returnVal.str("");
178 for (std::string::iterator c=temp.begin(); c<temp.end(); c++)
179 if (*c=='\'')
180 temp.insert(c++, '\'');
181 returnVal << "'" << temp << "'";
182 }
183 if (returnVal.str() == "")
184 *fLog << warn << "WARNING: could not construct fits string from \"" << value << "\" typename: " << typeName << std::endl;
185 return returnVal.str();
186 }
187
188 template<typename _T>
189 bool SetHeaderKey(const std::string& key,
190 const _T& value,
191 const std::string& comment="")
192 {
193 //check if header was already written
194 for (std::map<TString, bool>::iterator it=fTableHeaderWritten.begin(); it!= fTableHeaderWritten.end(); it++)
195 if (it->second == true)
196 return false;
197 //headers were not written yet. Add one key
198 std::vector<ofits::Key>::iterator it = fHeaderKeys.begin();
199 //check if the value had been set beforel
200 for (;it!=fHeaderKeys.end();it++)
201 {
202 if (it->key == key)
203 {
204 it->value = GetFitsString(value);
205 it->comment = comment;
206 break;
207 }
208 }
209 //did we find the key ?
210 if (it == fHeaderKeys.end())
211 {//no ? add it !
212 ofits::Key temp;
213 temp.key = key;
214 temp.value = GetFitsString(value);
215 temp.comment = comment;
216 fHeaderKeys.push_back(temp);
217 }
218 return true;
219 }
220private:
221 std::vector<ofits::Key> fHeaderKeys;
222
223 std::string Trim(const std::string &str);
224
225 // MTask
226 Bool_t ReInit(MParList *pList);
227
228 std::vector<std::string> fVetoedColumns;
229 std::map<std::string, uint32_t> fBytesPerSamples;
230
231
232public:
233
234 //Veto a type-mapping functions and variables
235 Bool_t VetoColumn(const std::string& colName);
236 Bool_t SetBytesPerSample(const std::string& colName, uint32_t numBytes);
237
238 enum FILE_MODE {
239 kMultiFiles,
240 kSingleFile
241 } ;
242
243 MWriteFitsFile(const char *fname,
244 FILE_MODE fileMode = kMultiFiles,
245 const Option_t *opt="RECREATE",
246 const char *name=NULL,
247 const char *title=NULL);
248 //Similar contructor to what MWriteRootFiles takes.
249 //Some of the args are not used: comp as they does not make sense in FITS
250 MWriteFitsFile(const Int_t comp,
251 const char* rule,
252 const Option_t* option="RECREATE",
253 const char* fTitle="Untitled",
254 const char* name=NULL,
255 const char* title=NULL);
256
257 ~MWriteFitsFile();
258
259
260 void AddContainer(const char *cName, const char *tName=NULL, Bool_t must=kTRUE, UInt_t max=0);
261 void AddContainer(MParContainer *cont, const char *tName=NULL, Bool_t must=kTRUE, UInt_t max=0);
262
263
264 void AddTree(const char *name, Bool_t force=kTRUE)
265 {
266 AddContainer(Form("MReport%s", name), name, force);
267 AddContainer(Form("MTime%s", name), name, force);
268 }
269
270 void SetGeometry(const TString &geom) { fGeometry = geom; }
271
272 ClassDef(MWriteFitsFile, 0)
273};
274//Specializations for float and doubles (because of the precision handling, I could not deal with it in the main function
275template<>
276std::string MWriteFitsFile::GetFitsString(const double& value);
277template<>
278std::string MWriteFitsFile::GetFitsString(const float& value);
279#endif
Note: See TracBrowser for help on using the repository browser.