1 | #include <TDataMember.h>
|
---|
2 | #include <TClonesArray.h>
|
---|
3 |
|
---|
4 | #include "MLogManip.h"
|
---|
5 |
|
---|
6 | #include "MArrayS.h"
|
---|
7 | #include "MArrayB.h"
|
---|
8 | #include "MArrayF.h"
|
---|
9 |
|
---|
10 | #include "MRead.h"
|
---|
11 | #include "MParList.h"
|
---|
12 | #include "MStatusDisplay.h"
|
---|
13 |
|
---|
14 | #include "MLog.h"
|
---|
15 | #include "MLogManip.h"
|
---|
16 | #include "MWriteRootFile.h"
|
---|
17 |
|
---|
18 | #include "MWriteFitsFile.h"
|
---|
19 | #include "MFitsArray.h"
|
---|
20 |
|
---|
21 | #include "MTime.h"
|
---|
22 |
|
---|
23 | //for find
|
---|
24 | #include <algorithm>
|
---|
25 |
|
---|
26 | ClassImp(MWriteFitsFile);
|
---|
27 |
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 | map<MUniqueFileId, MTopFitsGroup> MWriteFitsFile::fTopFitsGroups;
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | // ----------------------------------------------------------------------------
|
---|
35 | //
|
---|
36 | // if fileMode == kSingleFile
|
---|
37 | // Opens the top level group file, if it was not already opened by an other
|
---|
38 | // instance of MWriteFitsFile.
|
---|
39 | // if fileMode == kMultiFiles
|
---|
40 | // fname defines the rules to build the output filenames from the current
|
---|
41 | // input filename. The files are opened in the ReInit() method.
|
---|
42 | //
|
---|
43 | // opt = RECREATE : an existing file will be deleted
|
---|
44 | // = NEW : an already existing file will not be deleted and the new
|
---|
45 | // file is not created. Check with IsFileOpen() if the new
|
---|
46 | // top level group could be opened.
|
---|
47 | // name will be the name of the group
|
---|
48 | // title will be written as keyword TITLE into the header
|
---|
49 | //
|
---|
50 | MWriteFitsFile::MWriteFitsFile(const char *fname,
|
---|
51 | FILE_MODE fileMode,
|
---|
52 | const Option_t *opt,
|
---|
53 | const char *name,
|
---|
54 | const char *title)
|
---|
55 | {
|
---|
56 | fOpenOption = opt;
|
---|
57 | if (name && name[0] != 0)
|
---|
58 | fGroupName = name;
|
---|
59 | else
|
---|
60 | fGroupName = "MARS";
|
---|
61 |
|
---|
62 | if (title)
|
---|
63 | fTitle = title;
|
---|
64 |
|
---|
65 | iTopFitsGroup = fTopFitsGroups.end();
|
---|
66 |
|
---|
67 | if (fileMode == kMultiFiles)
|
---|
68 | fRule = fname;
|
---|
69 | else
|
---|
70 | *fLog << err << "The new version of MWriteFitsFile does not support multiple tables in one file. Please use kMultiFiles instead" << endl;
|
---|
71 | //ETIENNE DEPREC
|
---|
72 | // iTopFitsGroup will be set by OpenTopLevelGroup, if a top level
|
---|
73 | // group can be assigned to this MWriteFitsFile.
|
---|
74 | //OpenTopLevelGroup(fname);
|
---|
75 | }
|
---|
76 |
|
---|
77 | MWriteFitsFile::MWriteFitsFile(const Int_t comp,
|
---|
78 | const char* rule,
|
---|
79 | const Option_t* option,
|
---|
80 | const char* ftitle,
|
---|
81 | const char* name,
|
---|
82 | const char* title)
|
---|
83 | {
|
---|
84 | fOpenOption = option;
|
---|
85 | if (name && name[0] != 0)
|
---|
86 | fGroupName = name;
|
---|
87 | else
|
---|
88 | fGroupName = "MARS";
|
---|
89 | if (title) fTitle = ftitle;
|
---|
90 | iTopFitsGroup = fTopFitsGroups.end();
|
---|
91 | //always kMultiFiles
|
---|
92 | fRule = rule;
|
---|
93 | }
|
---|
94 | // ----------------------------------------------------------------------------
|
---|
95 | //
|
---|
96 | // Closes the top level group, it is is not used by an other instance of
|
---|
97 | // MWriteFitsFile.
|
---|
98 | // std::map<TString, std::ofits*>
|
---|
99 | MWriteFitsFile::~MWriteFitsFile()
|
---|
100 | {
|
---|
101 | for (std::map<TString, ofits*>::iterator it=fFitsTables.begin(); it!= fFitsTables.end();it++)
|
---|
102 | {
|
---|
103 | if (it->second != NULL)
|
---|
104 | {
|
---|
105 | it->second->close();
|
---|
106 | delete it->second;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | // CloseTopLevelGroup();
|
---|
110 | DeleteArrayHelper();
|
---|
111 | }
|
---|
112 |
|
---|
113 | // ----------------------------------------------------------------------------
|
---|
114 | //
|
---|
115 | // If the same file is already used by an other instance of MWriteFitsFile,
|
---|
116 | // the user counter is increasesd.
|
---|
117 | // Else: If the file exist and option is "RECREATE", the file is deleted and
|
---|
118 | // recreated.
|
---|
119 | // Finally the file with an empty group file is created.
|
---|
120 | //
|
---|
121 | void MWriteFitsFile::OpenTopLevelGroup(const char * fname)
|
---|
122 | {
|
---|
123 | // get rid of environment variables, ~, ... in filename
|
---|
124 | char * expandedFileName = gSystem->ExpandPathName(fname);
|
---|
125 |
|
---|
126 | // get unique identifier of the new file
|
---|
127 | struct stat fileId;
|
---|
128 | if (stat(expandedFileName, &fileId) == 0)
|
---|
129 | {
|
---|
130 | // file exist already
|
---|
131 | iTopFitsGroup = fTopFitsGroups.find(MUniqueFileId(fileId.st_dev, fileId.st_ino));
|
---|
132 | if (iTopFitsGroup != fTopFitsGroups.end())
|
---|
133 | {
|
---|
134 | *fLog << err << "You are trying to use an already existing file. Please choose a different file name (" << fname << ")" << endl;
|
---|
135 | exit(0);
|
---|
136 | // this group is already open. we use it as well
|
---|
137 | *fLog << inf2 << "Found open file '" << GetFileName() << "'... re-using." << endl;
|
---|
138 | iTopFitsGroup->second.IncreaseUsage();
|
---|
139 | delete [] expandedFileName;
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // file exist, but we cannot use
|
---|
144 | if (fOpenOption == "RECREATE")
|
---|
145 | // remove the file
|
---|
146 | unlink(expandedFileName);
|
---|
147 |
|
---|
148 | else if (fOpenOption == "NEW")
|
---|
149 | {
|
---|
150 | *fLog << err << "File '" << expandedFileName << "' exist already." << endl;
|
---|
151 | delete [] expandedFileName;
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | *fLog << err << "Unsupported option (" << fOpenOption.Data() << ") to open file " <<
|
---|
157 | expandedFileName << endl;
|
---|
158 | delete [] expandedFileName;
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | // file does not yet exist or was deleted
|
---|
164 |
|
---|
165 | // try {
|
---|
166 | // AstroRootGroup topGroup;
|
---|
167 | // char wrTitle[fTitle.Length() + 1];
|
---|
168 | // if (fTitle.Length() > 0)
|
---|
169 | // {
|
---|
170 | // strcpy(wrTitle, fTitle.Data());
|
---|
171 | // topGroup.InitAttr("TITLE", "A", wrTitle);
|
---|
172 | // }
|
---|
173 |
|
---|
174 | //ETIENNE do not open top level group as using one file per table
|
---|
175 | //I kept this code active anyway, because it is checked elsewhere in the code that iTopFitsGroup is set.
|
---|
176 | ofits* topGroup = NULL;
|
---|
177 | // ofits* topGroup = new ofits();
|
---|
178 | // TString dol = expandedFileName;
|
---|
179 | // topGroup->open(dol.Data());
|
---|
180 |
|
---|
181 |
|
---|
182 | // store this new file in fTopFitsGroups and get the iterator to the
|
---|
183 | // new group
|
---|
184 | stat(expandedFileName, &fileId);
|
---|
185 | iTopFitsGroup = fTopFitsGroups.insert(pair<MUniqueFileId, MTopFitsGroup>
|
---|
186 | (MUniqueFileId(fileId.st_dev, fileId.st_ino),
|
---|
187 | MTopFitsGroup(expandedFileName, topGroup) )).first;
|
---|
188 |
|
---|
189 | delete [] expandedFileName;
|
---|
190 |
|
---|
191 | }
|
---|
192 |
|
---|
193 | // --------------------------------------------------------------------------
|
---|
194 | //
|
---|
195 | // Reduces the use - counter of the top level group. Closes the group
|
---|
196 | // table, if the counter is 0, i.e. no other MWriteFitsFile instance uses
|
---|
197 | // the same top level group.
|
---|
198 | //
|
---|
199 |
|
---|
200 | void MWriteFitsFile::CloseTopLevelGroup()
|
---|
201 | {
|
---|
202 | if (iTopFitsGroup != fTopFitsGroups.end())
|
---|
203 | {
|
---|
204 | iTopFitsGroup->second.DecreaseUsage();
|
---|
205 | if (iTopFitsGroup->second.GetNumUsage() <= 0)
|
---|
206 | fTopFitsGroups.erase(iTopFitsGroup);
|
---|
207 |
|
---|
208 | iTopFitsGroup = fTopFitsGroups.end();
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | // --------------------------------------------------------------------------
|
---|
213 | //
|
---|
214 | // returns the name of the top level group file. It may be expanded if the
|
---|
215 | // name, given by the user, contained environment variables of the ~ sign.
|
---|
216 | // If no top level group is open, "no_open_file" is returned.
|
---|
217 | //
|
---|
218 | const char * MWriteFitsFile::GetFileName() const
|
---|
219 | {
|
---|
220 | if (IsFileOpen())
|
---|
221 | return iTopFitsGroup->second.GetFileName().Data();
|
---|
222 | else
|
---|
223 | return "no_open_file";
|
---|
224 | }
|
---|
225 |
|
---|
226 | // --------------------------------------------------------------------------
|
---|
227 | //
|
---|
228 | // Add a new Container to list of containers which should be written to the
|
---|
229 | // file. Give the name of the container which will identify the container
|
---|
230 | // in the parameterlist. tname is the name of the FITS table to which the
|
---|
231 | // container should be written (Remark: one table can hold more than one
|
---|
232 | // container). The default is the same name as the container name.
|
---|
233 | // If "mus"t is set to kTRUE (the default), then the container must exist
|
---|
234 | // in the parameterlist. The container will be ignored, if "must" is set to
|
---|
235 | // kFALSE and the container does not exist in the parameterlist.
|
---|
236 | //
|
---|
237 | void MWriteFitsFile::AddContainer(const char *cName,
|
---|
238 | const char *tName,
|
---|
239 | Bool_t must, UInt_t max)
|
---|
240 | {
|
---|
241 | if (cName == NULL || strlen(cName) == 0)
|
---|
242 | {
|
---|
243 | *fLog << warn << "Warning - container name in method MWriteFitsFile::"
|
---|
244 | "AddContainer not defined... ignored" << endl;
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | // if tName is not defined, we use cName as the table name
|
---|
249 | TString tableName;
|
---|
250 | if (tName == NULL || strlen(tName) == 0)
|
---|
251 | tableName = cName;
|
---|
252 | else
|
---|
253 | tableName = tName;
|
---|
254 |
|
---|
255 | // try to insert this container to the list of sub-tables. Write a
|
---|
256 | // warning, if the same container name was already defined and ignore
|
---|
257 | // this one.
|
---|
258 | if (fSubTables[tableName].insert( pair<TString, MFitsSubTable>
|
---|
259 | (cName, MFitsSubTable(must))).second == false)
|
---|
260 | {
|
---|
261 | *fLog << warn << "Warning - Container '"<< cName <<"' in table '" <<
|
---|
262 | tableName.Data() << "' already scheduled... ignored." << endl;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | // --------------------------------------------------------------------------
|
---|
267 | //
|
---|
268 | // Add a new Container to list of containers which should be written to the
|
---|
269 | // file. tname is the name of the FITS table to which the
|
---|
270 | // container should be written (Remark: one table can hold more than one
|
---|
271 | // container). The default is the same name as the container name.
|
---|
272 | // "must" is ignored. It is just for compatibility with MWriteRootFiles.
|
---|
273 | //
|
---|
274 | void MWriteFitsFile::AddContainer(MParContainer *cont, const char *tName,
|
---|
275 | Bool_t must, UInt_t max)
|
---|
276 | {
|
---|
277 | if (cont == NULL)
|
---|
278 | {
|
---|
279 | *fLog << warn << "Warning - container in method MWriteFitsFile::"
|
---|
280 | "AddContainer not defined... ignored" << endl;
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | TString tableName;
|
---|
285 | if (tName == NULL || strlen(tName) == 0)
|
---|
286 | tableName = cont->IsA()->GetName();
|
---|
287 | else
|
---|
288 | tableName = tName;
|
---|
289 |
|
---|
290 | TString cName = cont->IsA()->GetName();
|
---|
291 | if (fSubTables[tableName].insert( pair<TString, MFitsSubTable>
|
---|
292 | (cName, MFitsSubTable(cont, must))).second == false)
|
---|
293 | {
|
---|
294 | *fLog << warn << "Warning - Container '"<< cName.Data() <<
|
---|
295 | "' in table '" << tableName.Data() <<
|
---|
296 | "' already scheduled... ignored." << endl;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | // --------------------------------------------------------------------------
|
---|
301 | //
|
---|
302 | // Tries to open the given output file.
|
---|
303 | //
|
---|
304 | Int_t MWriteFitsFile::PreProcess(MParList *pList)
|
---|
305 | {
|
---|
306 | if (fRule.Length() != 0)
|
---|
307 | // the file is not yet open
|
---|
308 | return kTRUE;
|
---|
309 | //
|
---|
310 | // test whether file is now open or not
|
---|
311 | //
|
---|
312 | if (!IsFileOpen())
|
---|
313 | {
|
---|
314 | *fLog << err << dbginf << "Cannot open file '" << GetFileName() << "'" << endl;
|
---|
315 | return kFALSE;
|
---|
316 | }
|
---|
317 |
|
---|
318 | *fLog << inf << "File '" << GetFileName() << "' open for writing." << endl;
|
---|
319 |
|
---|
320 | //
|
---|
321 | // Get the containers (pointers) from the parameter list you want to write
|
---|
322 | //
|
---|
323 | if (!GetContainer(pList))
|
---|
324 | return kFALSE;
|
---|
325 |
|
---|
326 | //
|
---|
327 | // write the container if it is already in changed state
|
---|
328 | //
|
---|
329 | return CheckAndWrite();
|
---|
330 |
|
---|
331 | }
|
---|
332 |
|
---|
333 | Int_t MWriteFitsFile::PostProcess()
|
---|
334 | {
|
---|
335 | bool rc = true;
|
---|
336 | for (std::map<TString, ofits*>::iterator it=fFitsTables.begin(); it!= fFitsTables.end();it++)
|
---|
337 | {
|
---|
338 | rc &= it->second->close();
|
---|
339 | delete it->second;
|
---|
340 | it->second = NULL;
|
---|
341 | }
|
---|
342 |
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 | void MWriteFitsFile::SetupHeaderKeys(MRawRunHeader &header)
|
---|
347 | {
|
---|
348 | const MTime now(-1);
|
---|
349 | SetHeaderKey("ISMC",true,"Bool if File is Montecarlo File");
|
---|
350 | SetHeaderKey("TELESCOP", "FACT", "");
|
---|
351 | SetHeaderKey("PACKAGE", "MARS Cheobs", "");
|
---|
352 | SetHeaderKey("VERSION", "1.0", "");
|
---|
353 | SetHeaderKey("CREATOR", "Ceres", "");
|
---|
354 | SetHeaderKey("EXTREL", 1., "");
|
---|
355 | SetHeaderKey("COMPILED", __DATE__" " __TIME__, "");
|
---|
356 | //SetHeaderKey("REVISION", "0", "");
|
---|
357 | SetHeaderKey("ORIGIN", "FACT", "");
|
---|
358 | SetHeaderKey("DATE", now.GetStringFmt("%Y-%m-%dT%H:%M:%S").Data(), "");
|
---|
359 | SetHeaderKey("NIGHT", now.GetNightAsInt(), "");
|
---|
360 | SetHeaderKey("TIMESYS", "UTC", "");
|
---|
361 | SetHeaderKey("TIMEUNIT", "d", "");
|
---|
362 | SetHeaderKey("MJDREF", 40587, "");
|
---|
363 | //SetHeaderKey("BLDVER", 1, "");
|
---|
364 | SetHeaderKey("RUNID", header.GetRunNumber(), "");
|
---|
365 | SetHeaderKey("NBOARD", 40, "");
|
---|
366 | SetHeaderKey("NPIX", header.GetNumPixel(), "");
|
---|
367 | SetHeaderKey("NROI", header.GetNumSamplesHiGain(), "");
|
---|
368 | SetHeaderKey("NROITM", 0, "");
|
---|
369 | SetHeaderKey("TMSHIFT", 0, "");
|
---|
370 | SetHeaderKey("CAMERA", "MGeomCamFACT", "Montecarlo File");
|
---|
371 | SetHeaderKey("DAQ", "DRS4", "Montecarlo File");
|
---|
372 |
|
---|
373 | // FTemme: ADCRANGE and ADC have to be calculated, using the values for
|
---|
374 | // the fadctype.
|
---|
375 | // SetHeaderKey("ADCRANGE", 2000, "Dynamic range in mV");
|
---|
376 | // SetHeaderKey("ADC", 12, "Resolution in bits");
|
---|
377 |
|
---|
378 | switch(header.GetRunType())
|
---|
379 | {
|
---|
380 | case MRawRunHeader::kRTData|MRawRunHeader::kRTMonteCarlo:
|
---|
381 | SetHeaderKey("RUNTYPE", "data", "");
|
---|
382 | break;
|
---|
383 | case MRawRunHeader::kRTPedestal|MRawRunHeader::kRTMonteCarlo:
|
---|
384 | SetHeaderKey("RUNTYPE", "pedestal", "");
|
---|
385 | break;
|
---|
386 | case MRawRunHeader::kRTCalibration|MRawRunHeader::kRTMonteCarlo:
|
---|
387 | SetHeaderKey("RUNTYPE", "calibration", "");
|
---|
388 | break;
|
---|
389 | }
|
---|
390 | // SetHeaderKey("ID", 777, "Board 0: Board ID");
|
---|
391 | // SetHeaderKey("FMVER", 532, "Board 0: Firmware Version");
|
---|
392 | // SetHeaderKey("DNA", "0", "");
|
---|
393 | // SetHeaderKey("BOARD", 0, "");
|
---|
394 | // SetHeaderKey("PRESC", 40, "");
|
---|
395 | // SetHeaderKey("PHASE", 0, "");
|
---|
396 | // SetHeaderKey("DAC0", 26500, "");
|
---|
397 | // SetHeaderKey("DAC1", 0, "");
|
---|
398 | // SetHeaderKey("DAC2", 0, "");
|
---|
399 | // SetHeaderKey("DAC3", 0, "");
|
---|
400 | // SetHeaderKey("DAC4", 28800, "");
|
---|
401 | // SetHeaderKey("DAC5", 28800, "");
|
---|
402 | // SetHeaderKey("DAC6", 28800, "");
|
---|
403 | // SetHeaderKey("DAC7", 28800, "");
|
---|
404 | SetHeaderKey("REFCLK", header.GetFreqSampling(), "");
|
---|
405 | SetHeaderKey("DRSCALIB", false, "");
|
---|
406 | // SetHeaderKey("TSTARTI", 0, "");
|
---|
407 | // SetHeaderKey("TSTARTF", 0., "");
|
---|
408 | // SetHeaderKey("TSTOPI", 0, "");
|
---|
409 | // SetHeaderKey("TSTOPF", 0., "");
|
---|
410 | // SetHeaderKey("DATE-OBS", "1970-01-01T00:00:00", "");
|
---|
411 | // SetHeaderKey("DATE-END", "1970-01-01T00:00:00", "");
|
---|
412 | // SetHeaderKey("NTRG", 0, "");
|
---|
413 | // SetHeaderKey("NTRGPED", 0, "");
|
---|
414 | // SetHeaderKey("NTRGLPE", 0, "");
|
---|
415 | // SetHeaderKey("NTRGTIM", 0, "");
|
---|
416 | // SetHeaderKey("NTRGLPI", 0, "");
|
---|
417 | // SetHeaderKey("NTRGEXT1", 0, "");
|
---|
418 | // SetHeaderKey("NTRGEXT2", 0, "");
|
---|
419 | // SetHeaderKey("NTRGMISC", 0, "");
|
---|
420 | }
|
---|
421 |
|
---|
422 | template<>
|
---|
423 | std::string MWriteFitsFile::GetFitsString(const double& value)
|
---|
424 | {
|
---|
425 | std::ostringstream returnVal;
|
---|
426 | returnVal << std::setprecision(value>1e-100 && value<1e100 ? 15 : 14) << value;
|
---|
427 | std::string temp = returnVal.str();
|
---|
428 | std::replace(temp.begin(), temp.end(), 'e', 'E');
|
---|
429 | if (temp.find_first_of('E')==std::string::npos && temp.find_first_of('.')==std::string::npos)
|
---|
430 | temp += ".";
|
---|
431 | return temp;
|
---|
432 | }
|
---|
433 | template<>
|
---|
434 | std::string MWriteFitsFile::GetFitsString(const float& value)
|
---|
435 | {
|
---|
436 | return GetFitsString((double)(value));
|
---|
437 | }
|
---|
438 | // --------------------------------------------------------------------------
|
---|
439 | //
|
---|
440 | // Opens all FITS files for all container, which were registered with the
|
---|
441 | // AddContainer() - methods. The container is taken from the pList, if it
|
---|
442 | // was registered just by its name.
|
---|
443 | // Calling the AddContainer() after calling this method will not add new
|
---|
444 | // containers to the list.
|
---|
445 | //
|
---|
446 | Bool_t MWriteFitsFile::GetContainer(MParList *pList)
|
---|
447 | {
|
---|
448 | if (iTopFitsGroup == fTopFitsGroups.end())
|
---|
449 | // something went wrong while the top level group was created
|
---|
450 | return kFALSE;
|
---|
451 |
|
---|
452 |
|
---|
453 | // remove the extension from the filename
|
---|
454 | // this has been disabled for now
|
---|
455 | char fileNameNoExt[strlen(GetFileName()) + 1];
|
---|
456 | strcpy(fileNameNoExt, GetFileName());
|
---|
457 | // char * pos = strrchr(fileNameNoExt, '.');
|
---|
458 | // if (pos) *pos = 0;
|
---|
459 | //*fLog << inf <<"Filename no ext: " << fileNameNoExt << endl;
|
---|
460 | // loop over all FITS tables which have to be created.
|
---|
461 | map<TString, map<TString, MFitsSubTable> >::iterator i_table =
|
---|
462 | fSubTables.begin();
|
---|
463 | while (i_table != fSubTables.end())
|
---|
464 | {
|
---|
465 |
|
---|
466 | ofits* fitsTable = new ofits();
|
---|
467 | fitsTable->AllowCommentsTrimming(true);
|
---|
468 | TString dol = fileNameNoExt;
|
---|
469 | //get rid of the ".root" extension in the file name (if any)
|
---|
470 | if (dol(dol.Length()-5, dol.Length()) == ".root")
|
---|
471 | {
|
---|
472 | dol = dol(0, dol.Length()-5);
|
---|
473 | }
|
---|
474 | if (dol(dol.Length()-5, dol.Length()) == ".fits")
|
---|
475 | {
|
---|
476 | dol = dol(0, dol.Length()-5);
|
---|
477 | }
|
---|
478 | dol += "_";
|
---|
479 | dol += i_table->first;
|
---|
480 | dol += ".fits";
|
---|
481 | fitsTable->open(dol.Data());
|
---|
482 | *fLog << inf << "Opening FITS file: " << dol.Data() << endl;
|
---|
483 | fFitsTables[i_table->first] = fitsTable;
|
---|
484 | fTableObjectCreated[i_table->first] = true;
|
---|
485 | fTableHeaderWritten[i_table->first] = false;
|
---|
486 |
|
---|
487 | // loop over all containers, which define a sub-table of the current table
|
---|
488 | Int_t num = 0;
|
---|
489 | map<TString, MFitsSubTable>::iterator i_subTable = i_table->second.begin();
|
---|
490 | while (i_subTable != i_table->second.end())
|
---|
491 | {
|
---|
492 |
|
---|
493 | MFitsSubTable & subTable = i_subTable->second;
|
---|
494 | if (subTable.GetContainer() == NULL)
|
---|
495 | {
|
---|
496 | // container address is not yet known
|
---|
497 | const char * cname = i_subTable->first.Data();
|
---|
498 | MParContainer *cont = (MParContainer*)pList->FindObject(cname);
|
---|
499 | if (!cont)
|
---|
500 | {
|
---|
501 | // no corresponding container is available in pList
|
---|
502 | if (subTable.MustHave())
|
---|
503 | {
|
---|
504 | *fLog << err << "Cannot find parameter container '" << cname << "'." << endl;
|
---|
505 | return kFALSE;
|
---|
506 | }
|
---|
507 |
|
---|
508 | // we can ignore this container, delete it from the map
|
---|
509 | *fLog << inf2 << "Unnecessary parameter container '" << cname << "' not found..." << endl;
|
---|
510 | map<TString, MFitsSubTable>::iterator i_tmp = i_subTable;
|
---|
511 | i_subTable++;
|
---|
512 | i_table->second.erase(i_tmp);
|
---|
513 | continue;
|
---|
514 | }
|
---|
515 |
|
---|
516 | // we have our container address and can use it
|
---|
517 | subTable.SetContainer(cont);
|
---|
518 | }
|
---|
519 |
|
---|
520 | // initialize all columns of the sub-table, defined by the current container
|
---|
521 | TString containerName = i_subTable->second.GetContainer()->GetName();
|
---|
522 | TClass * cl = i_subTable->second.GetContainer()->IsA();
|
---|
523 | if (!InitColumns(i_table->first, containerName + ".", fitsTable,
|
---|
524 | i_subTable->second.GetContainer(), cl) )
|
---|
525 | return kFALSE;
|
---|
526 |
|
---|
527 | InitAttr(Form("CLNAME%d", num),
|
---|
528 | "A",
|
---|
529 | (void*)i_subTable->first.Data(),
|
---|
530 | NULL,
|
---|
531 | "MARS container name",
|
---|
532 | fitsTable);
|
---|
533 |
|
---|
534 | InitAttr(Form("CLTYPE%d", num),
|
---|
535 | "A",
|
---|
536 | (void*)i_subTable->second.GetContainer()->ClassName(),
|
---|
537 | NULL,
|
---|
538 | "MARS container class",
|
---|
539 | fitsTable);
|
---|
540 |
|
---|
541 | num++;
|
---|
542 | i_subTable++;
|
---|
543 | }
|
---|
544 |
|
---|
545 | // in case not all sub-tables were removed, we can now create the FITS table
|
---|
546 | if (i_table->second.size() > 0)
|
---|
547 | {
|
---|
548 | // create the DOL of the table. It will be something like:
|
---|
549 | // fileNameNoExt_dataName.fits[dataName]
|
---|
550 | TString dol2 = fileNameNoExt;
|
---|
551 | dol2 += "_";
|
---|
552 | dol2 += i_table->first;
|
---|
553 | dol2 += ".fits";
|
---|
554 |
|
---|
555 | if (fOpenOption == "RECREATE")
|
---|
556 | // remove the file
|
---|
557 | unlink(dol2.Data());
|
---|
558 |
|
---|
559 | dol2 += "[";
|
---|
560 | dol2 += i_table->first;
|
---|
561 | dol2 += "]";
|
---|
562 | // *fLog << err << "Reiner would have opened fits file: " << dol.Data() << endl;
|
---|
563 | //exit(0);
|
---|
564 | //fitsTable->open(dol.Data());
|
---|
565 |
|
---|
566 | // attach the new table to the top level group
|
---|
567 | //TODO make sure that this attach below is not needed...
|
---|
568 | //iTopFitsGroup->second.Attach(fitsTable);
|
---|
569 | }
|
---|
570 |
|
---|
571 | i_table++;
|
---|
572 | }
|
---|
573 |
|
---|
574 | return kTRUE;
|
---|
575 | }
|
---|
576 | void MWriteFitsFile::InitAttr(const char* attrName,
|
---|
577 | const char* dataType,
|
---|
578 | void* var,
|
---|
579 | const char* unit,
|
---|
580 | const char* comment,
|
---|
581 | ofits* outFile)
|
---|
582 | {
|
---|
583 | if (outFile == NULL)
|
---|
584 | return;
|
---|
585 | string dts(dataType);
|
---|
586 | string ans(attrName);
|
---|
587 | string cs;
|
---|
588 | if (comment != NULL)
|
---|
589 | cs = string(comment);
|
---|
590 | else
|
---|
591 | cs = "";
|
---|
592 | ostringstream val;
|
---|
593 | if ((dts == "bool") || (dts == "Bool_t") || (dts == "L"))
|
---|
594 | {
|
---|
595 | outFile->SetBool(ans, ((bool*)(var))[0], cs);
|
---|
596 | return;
|
---|
597 | }
|
---|
598 | if ((dts == "char") || (dts == "Char_t") || (dts == "S"))
|
---|
599 | {
|
---|
600 | val << ((char*)(var))[0];
|
---|
601 | outFile->SetStr(ans, val.str(), cs);
|
---|
602 | return;
|
---|
603 | }
|
---|
604 | if ((dts == "unsigned char") || (dts == "UChar_t") || (dts == "B"))
|
---|
605 | {
|
---|
606 | val << ((unsigned char*)(var))[0];
|
---|
607 | outFile->SetStr(ans, val.str(), cs);
|
---|
608 | return;
|
---|
609 | }
|
---|
610 | if ((dts == "short") || (dts == "Short_t") || (dts == "I"))
|
---|
611 | {
|
---|
612 | val << ((short*)(var))[0];
|
---|
613 | outFile->SetStr(ans, val.str(), cs);
|
---|
614 | return;
|
---|
615 | }
|
---|
616 | if ((dts == "unsigned short") || (dts == "UShort_t") || (dts == "U"))
|
---|
617 | {
|
---|
618 | val << ((unsigned short*)(var))[0];
|
---|
619 | outFile->SetStr(ans, val.str(), cs);
|
---|
620 | return;
|
---|
621 | }
|
---|
622 | if ((dts == "int") || (dts == "Int_t") || (dts == "V"))
|
---|
623 | {
|
---|
624 | outFile->SetInt(ans, ((int*)(var))[0], cs);
|
---|
625 | return;
|
---|
626 | }
|
---|
627 | if ((dts == "unsigned int") || (dts == "UInt_t") || (dts == "V"))
|
---|
628 | {
|
---|
629 | outFile->SetInt(ans, ((unsigned int*)(var))[0], cs);
|
---|
630 | return;
|
---|
631 | }
|
---|
632 | if ((dts == "long long") || (dts == "Long64_t") || (dts == "K"))
|
---|
633 | {
|
---|
634 | outFile->SetInt(ans, ((long long*)(var))[0], cs);
|
---|
635 | return;
|
---|
636 | }
|
---|
637 | if ((dts == "unsigned long long") || (dts == "ULong64_t") || (dts == "W"))
|
---|
638 | {
|
---|
639 | val << ((unsigned long long*)(var))[0];
|
---|
640 | outFile->SetStr(ans, val.str(), cs);
|
---|
641 | return;
|
---|
642 | }
|
---|
643 | if ((dts == "float") || (dts == "TFloat_t") || (dts == "E"))
|
---|
644 | {
|
---|
645 | outFile->SetFloat(ans, ((float*)(var))[0], cs);
|
---|
646 | return;
|
---|
647 | }
|
---|
648 | if ((dts == "double") || (dts == "TDouble_t") || (dts == "D"))
|
---|
649 | {
|
---|
650 | outFile->SetFloat(ans, ((double*)(var))[0], cs);
|
---|
651 | return;
|
---|
652 | }
|
---|
653 | if ((dts == "char*") || (dts == "A"))
|
---|
654 | {
|
---|
655 | outFile->SetStr(ans, string((char*)(var)), cs);
|
---|
656 | return;
|
---|
657 | }
|
---|
658 | //trigger error
|
---|
659 | *fLog << err << "Format string not recognized while adding header entry " << ans << " with type " << dts << endl;
|
---|
660 | }
|
---|
661 | // --------------------------------------------------------------------------
|
---|
662 | //
|
---|
663 | // This method is called when new data should be written to the FITS table.
|
---|
664 | // In general only one row is written. (There are exceptions for Arrays).
|
---|
665 | // A new row to a FITS table is written if at least for one container
|
---|
666 | // (one sub-table) the ReadyToSave - flag is set.
|
---|
667 | //
|
---|
668 | Bool_t MWriteFitsFile::CheckAndWrite()
|
---|
669 | {
|
---|
670 | // try {
|
---|
671 |
|
---|
672 | // loop over all tables
|
---|
673 | map<TString, map<TString, MFitsSubTable> >::iterator i_table =
|
---|
674 | fSubTables.begin();
|
---|
675 | while (i_table != fSubTables.end())
|
---|
676 | {
|
---|
677 | // is this table open?
|
---|
678 | if (fFitsTables.find(i_table->first) != fFitsTables.end())
|
---|
679 | {
|
---|
680 |
|
---|
681 | // loop over all sub-tables
|
---|
682 | map<TString, MFitsSubTable>::iterator i_subTable = i_table->second.begin();
|
---|
683 | while (i_subTable != i_table->second.end())
|
---|
684 | {
|
---|
685 |
|
---|
686 | if (i_subTable->second.GetContainer()->IsReadyToSave())
|
---|
687 | {
|
---|
688 | // first write the TClonesArray and set the size of the arrays
|
---|
689 | list<MArrayHelperBase*> & clHelper = fClHelper[i_table->first];
|
---|
690 | list<MArrayHelperBase*>::iterator i_clHelper = clHelper.begin();
|
---|
691 | while (i_clHelper != clHelper.end())
|
---|
692 | {
|
---|
693 | // write all entries in the TClonesArray in its FITS table
|
---|
694 | //removed this write because I do it elsewhere. is that alright ?
|
---|
695 | // (*i_clHelper)->Write();
|
---|
696 | i_clHelper++;
|
---|
697 | }
|
---|
698 |
|
---|
699 | // write one line to this table
|
---|
700 | writeOneRow(i_table->first);
|
---|
701 | // fFitsTables[i_table->first].Write();
|
---|
702 | break;
|
---|
703 | }
|
---|
704 | i_subTable++;
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | i_table++;
|
---|
709 | }
|
---|
710 | // }
|
---|
711 | // catch (exception &e)
|
---|
712 | // {
|
---|
713 | // *fLog << err << e.what() << endl;
|
---|
714 | // return kFALSE;
|
---|
715 | // }
|
---|
716 |
|
---|
717 | return kTRUE;
|
---|
718 | }
|
---|
719 |
|
---|
720 | string MWriteFitsFile::Trim(const string &str)
|
---|
721 | {
|
---|
722 | // Trim Both leading and trailing spaces
|
---|
723 | const size_t first = str.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
|
---|
724 | const size_t last = str.find_last_not_of(' '); // Find the first character position from reverse af
|
---|
725 |
|
---|
726 | // if all spaces or empty return an empty string
|
---|
727 | if (string::npos==first || string::npos==last)
|
---|
728 | return string();
|
---|
729 |
|
---|
730 | return str.substr(first, last-first+1);
|
---|
731 | }
|
---|
732 |
|
---|
733 | Bool_t MWriteFitsFile::VetoColumn(const std::string& colName)
|
---|
734 | {
|
---|
735 | for (std::vector<string>::iterator it=fVetoedColumns.begin(); it != fVetoedColumns.end(); it++)
|
---|
736 | if (*it == colName)
|
---|
737 | {
|
---|
738 | *fLog << warn << "Warning: column " << colName << " is being vetoed twice" << endl;
|
---|
739 | return kFALSE;
|
---|
740 | }
|
---|
741 | fVetoedColumns.push_back(colName);
|
---|
742 | return kTRUE;
|
---|
743 | }
|
---|
744 |
|
---|
745 | Bool_t MWriteFitsFile::SetBytesPerSample(const std::string& colName, uint32_t numBytes)
|
---|
746 | {
|
---|
747 | for (map<string, uint32_t>::iterator it=fBytesPerSamples.begin(); it!=fBytesPerSamples.end(); it++)
|
---|
748 | if (it->first == colName)
|
---|
749 | {
|
---|
750 | *fLog << warn << "Warning: column " << colName << " bytes per sample is being redefined twice" << endl;
|
---|
751 | return kFALSE;
|
---|
752 | }
|
---|
753 | if (numBytes != 1 && numBytes != 2 && numBytes != 4 && numBytes != 8)
|
---|
754 | {
|
---|
755 | *fLog << warn << "Only powers of two are allowed for types re-mapping." << endl;
|
---|
756 | return kFALSE;
|
---|
757 | }
|
---|
758 | fBytesPerSamples[colName] = numBytes;
|
---|
759 | return kTRUE;
|
---|
760 | }
|
---|
761 |
|
---|
762 | // --------------------------------------------------------------------------
|
---|
763 | //
|
---|
764 | // Initialize all columns in "fitsTable" of the class "classDef". The data
|
---|
765 | // of this class are stored in a buffer, beginning at "baseAdr".
|
---|
766 | //
|
---|
767 | Bool_t MWriteFitsFile::InitColumns(const TString & tableName,
|
---|
768 | const TString & parentVarName,
|
---|
769 | ofits* fitsTable,
|
---|
770 | void * baseAdr,
|
---|
771 | TClass * classDef)
|
---|
772 | {
|
---|
773 | // get all data members of the class
|
---|
774 | TList * dataMembers = classDef->GetListOfDataMembers();
|
---|
775 | TIter next(dataMembers);
|
---|
776 | TDataMember * dataMember;
|
---|
777 |
|
---|
778 | // loop over all data members
|
---|
779 | while ((dataMember = (TDataMember*)next()) != NULL)
|
---|
780 | {
|
---|
781 | if (!dataMember->IsPersistent())
|
---|
782 | // don't store this variable
|
---|
783 | continue;
|
---|
784 |
|
---|
785 | #if ROOT_VERSION_CODE < ROOT_VERSION(6,00,00)
|
---|
786 | if (dataMember->Property() & ( G__BIT_ISENUM | G__BIT_ISSTATIC))
|
---|
787 | // we cannot store this
|
---|
788 | continue;
|
---|
789 | #else
|
---|
790 | if (dataMember->Property() & ( EProperty::kIsEnum | kIsStatic))
|
---|
791 | // we cannot store this
|
---|
792 | continue;
|
---|
793 | #endif
|
---|
794 |
|
---|
795 | if (strcmp(dataMember->GetTrueTypeName(), "TClass*") == 0)
|
---|
796 | // we don't want to store this.
|
---|
797 | continue;
|
---|
798 |
|
---|
799 | // is it an array of more than 1 dimension?
|
---|
800 | if (dataMember->GetArrayDim() > 1)
|
---|
801 | {
|
---|
802 | *fLog << err << "Two and more dimensional arrays of member variables"
|
---|
803 | " are not supported." << endl;
|
---|
804 | *fLog << "See variable " << dataMember->GetName() <<
|
---|
805 | " in container " << classDef->GetName() << endl;
|
---|
806 | return kFALSE;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 | // replace � by **2 in the comment field
|
---|
811 | string comment(dataMember->GetTitle());
|
---|
812 | string::size_type pos1, pos2;
|
---|
813 | if ((pos1 = comment.find('\xb2')) != string::npos)
|
---|
814 | comment.replace(pos1, 1, "**2");
|
---|
815 |
|
---|
816 | // get the optional mapping to the fits column names
|
---|
817 | string fitsOptions="";
|
---|
818 | string unit="unit";
|
---|
819 |
|
---|
820 | pos1 = comment.find("{fits: ");
|
---|
821 | pos2 = comment.find('}');
|
---|
822 | std::map<string, string> fitsTokens;
|
---|
823 | if (pos1 != string::npos && pos2 != string::npos)
|
---|
824 | {
|
---|
825 | fitsOptions=comment.substr(pos1+7, (pos2-pos1)-7);
|
---|
826 | //do we have more than one option ?
|
---|
827 | string::size_type pos3= fitsOptions.find_first_of(';');
|
---|
828 | string::size_type pos4 = string::npos;
|
---|
829 | string key="";
|
---|
830 | string value="";
|
---|
831 | string keyValue="";
|
---|
832 | while (pos3 != string::npos)
|
---|
833 | {//we have at least 2 options left
|
---|
834 | // *fLog << err << "fitsOptions: " << fitsOptions << endl;
|
---|
835 | keyValue = fitsOptions.substr(0,pos3);
|
---|
836 | // *fLog << err << "keyValue: " << keyValue << endl;
|
---|
837 | pos4 = keyValue.find('=');
|
---|
838 | if (pos4 == string::npos)
|
---|
839 | {
|
---|
840 | *fLog << err << "Error while parsing comment \"" << comment << "\" from variable " << parentVarName + dataMember->GetName() << endl;
|
---|
841 | return kFALSE;
|
---|
842 | }
|
---|
843 | key = Trim(keyValue.substr(0, pos4));
|
---|
844 | value = Trim(keyValue.substr(pos4+1, pos3));
|
---|
845 | fitsTokens[key] = value;
|
---|
846 | // *fLog << err << "key: " << key << " value: " << value << endl;
|
---|
847 | fitsOptions = fitsOptions.substr(pos3+1, fitsOptions.size());
|
---|
848 | pos3 = fitsOptions.find_first_of(';');
|
---|
849 | }
|
---|
850 | // *fLog << err << "fitsOptions: " << fitsOptions << endl;
|
---|
851 | keyValue = fitsOptions;
|
---|
852 | pos4 = keyValue.find('=');
|
---|
853 | if (pos4 == string::npos)
|
---|
854 | {
|
---|
855 | *fLog << err << "Error while parsing comment \"" << comment << "\" from variable " << parentVarName + dataMember->GetName() << endl;
|
---|
856 | return kFALSE;
|
---|
857 | }
|
---|
858 | key = Trim(keyValue.substr(0, pos4));
|
---|
859 | value = Trim(keyValue.substr(pos4+1, pos3));
|
---|
860 | fitsTokens[key] = value;
|
---|
861 | // *fLog << err << "key: " << key << " value: " << value << endl;
|
---|
862 | }
|
---|
863 |
|
---|
864 | TString colName = parentVarName + dataMember->GetName();
|
---|
865 |
|
---|
866 | if (fitsTokens.find("unit") != fitsTokens.end())
|
---|
867 | unit = fitsTokens["unit"];
|
---|
868 | if (fitsTokens.find("name") != fitsTokens.end())
|
---|
869 | colName = fitsTokens["name"];
|
---|
870 |
|
---|
871 | //check for column veto
|
---|
872 | if (std::find(fVetoedColumns.begin(), fVetoedColumns.end(), colName.Data())!=fVetoedColumns.end())
|
---|
873 | {
|
---|
874 | *fLog << inf << "Vetoing column " << colName.Data() << endl;
|
---|
875 | continue;
|
---|
876 | }
|
---|
877 |
|
---|
878 | // set the array size
|
---|
879 | TString dataType = dataMember->GetTrueTypeName();
|
---|
880 |
|
---|
881 | uint32_t dataLength = 0;
|
---|
882 | switch (dataMember->GetArrayDim())
|
---|
883 | {
|
---|
884 | case 0:
|
---|
885 | dataLength = 1;
|
---|
886 | break;
|
---|
887 | case 1:
|
---|
888 | dataLength = dataMember->GetMaxIndex(0);
|
---|
889 | break;
|
---|
890 | default:
|
---|
891 | *fLog << err << "n-dimensional array should have been discarded already " << colName.Data();
|
---|
892 | break;
|
---|
893 | };
|
---|
894 | // if (dataLength == 0)
|
---|
895 | // continue;
|
---|
896 |
|
---|
897 | #if ROOT_VERSION_CODE < ROOT_VERSION(6,00,00)
|
---|
898 | if (dataMember->Property() & G__BIT_ISCLASS)
|
---|
899 | #else
|
---|
900 | if (dataMember->Property() & EProperty::kIsClass)
|
---|
901 | #endif
|
---|
902 | {
|
---|
903 | // special treatment for classes
|
---|
904 | const char *ptr = reinterpret_cast<char*>(baseAdr) + dataMember->GetOffset();
|
---|
905 |
|
---|
906 | if (strncmp(dataMember->GetTrueTypeName(), "MArray", 6) == 0)
|
---|
907 | {
|
---|
908 | if (strcmp(dataMember->GetTrueTypeName(), "MArrayS*") == 0)
|
---|
909 | {
|
---|
910 | const MArrayS *arr = *reinterpret_cast<MArrayS* const*>(ptr);
|
---|
911 | InitSingleColumn(tableName,
|
---|
912 | arr->GetSize(),
|
---|
913 | "UShort_t",
|
---|
914 | (char*)arr->GetArray(),
|
---|
915 | colName.Data(),
|
---|
916 | unit,
|
---|
917 | comment);
|
---|
918 | }
|
---|
919 | else if (strcmp(dataMember->GetTrueTypeName(), "MArrayB*") == 0)
|
---|
920 | {
|
---|
921 | const MArrayB *arr = *reinterpret_cast<MArrayB* const*>(ptr);
|
---|
922 | InitSingleColumn(tableName,
|
---|
923 | arr->GetSize(),
|
---|
924 | "UChar_t",
|
---|
925 | (char*)arr->GetArray(),
|
---|
926 | colName.Data(),
|
---|
927 | unit,
|
---|
928 | comment);
|
---|
929 | }
|
---|
930 | else if (strcmp(dataMember->GetTrueTypeName(), "MArrayF*") == 0)
|
---|
931 | {
|
---|
932 | const MArrayF *arr = *reinterpret_cast<MArrayF* const*>(ptr);
|
---|
933 | InitSingleColumn(tableName,
|
---|
934 | arr->GetSize(),
|
---|
935 | "TFloat_t",
|
---|
936 | (char*)arr->GetArray(),
|
---|
937 | colName.Data(),
|
---|
938 | unit,
|
---|
939 | comment);
|
---|
940 | }
|
---|
941 |
|
---|
942 | else {
|
---|
943 | *fLog << err << dataMember->GetTrueTypeName() << " not yet implemented." << endl;
|
---|
944 | return kFALSE;
|
---|
945 | }
|
---|
946 |
|
---|
947 |
|
---|
948 | continue;
|
---|
949 | }
|
---|
950 | else if (strcmp(dataMember->GetTrueTypeName(), "TClonesArray") == 0)
|
---|
951 | {
|
---|
952 | *fLog << warn << "I'm skipping the TClonesArray for now" << endl;
|
---|
953 | continue;
|
---|
954 | // each TClonesArray requires a FITS table by itself.
|
---|
955 | MClonesArrayHelper * clHelper;
|
---|
956 |
|
---|
957 | const TClonesArray * cloneArray = reinterpret_cast<const TClonesArray*>(ptr);
|
---|
958 | Bool_t status;
|
---|
959 | clHelper = new MClonesArrayHelper(cloneArray, fLog, status);
|
---|
960 | if (!status) return status;
|
---|
961 |
|
---|
962 | fClHelper[tableName].push_back(clHelper);
|
---|
963 |
|
---|
964 | // add one column in the parent table of the TClonesArray to store the
|
---|
965 | // number of entries in the TClonesArray.
|
---|
966 | InitSingleColumn(tableName,
|
---|
967 | 1,
|
---|
968 | "UInt_t",
|
---|
969 | clHelper->GetArraySizePtr(),
|
---|
970 | colName.Data(),
|
---|
971 | unit,
|
---|
972 | comment);
|
---|
973 |
|
---|
974 | // initialize the columns of the new FITS table, which will store the
|
---|
975 | // data entries of the TClonesArray
|
---|
976 | if (InitColumns(TString("noName"),
|
---|
977 | colName + "_",
|
---|
978 | fitsTable,
|
---|
979 | clHelper->GetDataBuffer(),
|
---|
980 | cloneArray->GetClass())
|
---|
981 | == kFALSE)
|
---|
982 | return kFALSE;
|
---|
983 |
|
---|
984 | // the columns are initialized. We can create the FITS table
|
---|
985 | if (clHelper->OpenFitsTable(GetFileName(), dataMember->GetName(),
|
---|
986 | fOpenOption, fLog) == kFALSE)
|
---|
987 | return kFALSE;
|
---|
988 | }
|
---|
989 |
|
---|
990 | else
|
---|
991 | {
|
---|
992 | // the current container has a variable of an other class. We create
|
---|
993 | // also columns of this other class in the same table
|
---|
994 | TClass * newClassDef = TClass::GetClass(dataMember->GetTrueTypeName(), kFALSE, kTRUE);
|
---|
995 | if (newClassDef)
|
---|
996 | {
|
---|
997 | if (InitColumns(tableName, colName + ".", fitsTable, (char*)baseAdr + dataMember->GetOffset(),
|
---|
998 | newClassDef) == kFALSE)
|
---|
999 | return kFALSE;
|
---|
1000 | }
|
---|
1001 | else
|
---|
1002 | *fLog << warn << "Cannot write data of class " << colName + "." + dataMember->GetTrueTypeName() << endl;
|
---|
1003 | }
|
---|
1004 | continue;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | InitSingleColumn(tableName,
|
---|
1008 | dataLength,
|
---|
1009 | dataType.Data(),
|
---|
1010 | (char*)baseAdr + dataMember->GetOffset(),
|
---|
1011 | colName.Data(),
|
---|
1012 | unit,
|
---|
1013 | comment);
|
---|
1014 |
|
---|
1015 | }
|
---|
1016 | return kTRUE;
|
---|
1017 | }
|
---|
1018 | void MWriteFitsFile::InitSingleColumn(const TString& tableName,
|
---|
1019 | uint32_t count,
|
---|
1020 | const string& typeName,
|
---|
1021 | void* dataPointer,
|
---|
1022 | const string& columnName,
|
---|
1023 | const string& unit,
|
---|
1024 | const string& comment)
|
---|
1025 | {
|
---|
1026 | if (!fTableObjectCreated[tableName])
|
---|
1027 | {
|
---|
1028 | *fLog << err << "ERROR: Cannot init column " << columnName << " before assigning object: " << tableName << endl;
|
---|
1029 | return;
|
---|
1030 | }
|
---|
1031 | int typeFound = 0;
|
---|
1032 | char typeChar = '0';
|
---|
1033 | if ((typeName == "bool") || (typeName == "Bool_t") || (typeName == "L"))
|
---|
1034 | {
|
---|
1035 | typeChar = 'L';
|
---|
1036 | typeFound++;
|
---|
1037 | }
|
---|
1038 | if ((typeName == "char") || (typeName == "Char_t") || (typeName == "S"))
|
---|
1039 | {
|
---|
1040 | typeChar = 'A';
|
---|
1041 | typeFound++;
|
---|
1042 | }
|
---|
1043 | if ((typeName == "unsigned char") || (typeName == "UChar_t") || (typeName == "B"))
|
---|
1044 | {
|
---|
1045 | typeChar = 'A';
|
---|
1046 | // *fLog << warn << "Converting unsigned char to char in fits file" << endl;
|
---|
1047 | typeFound++;
|
---|
1048 | }
|
---|
1049 | if ((typeName == "short") || (typeName == "Short_t") || (typeName == "I"))
|
---|
1050 | {
|
---|
1051 | typeChar = 'I';
|
---|
1052 | typeFound++;
|
---|
1053 | }
|
---|
1054 | if ((typeName == "unsigned short") || (typeName == "UShort_t") || (typeName == "U"))
|
---|
1055 | {
|
---|
1056 | typeChar = 'I';
|
---|
1057 | // *fLog << warn << "Converting unsigned short to short in fits file" << endl;
|
---|
1058 | typeFound++;
|
---|
1059 | }
|
---|
1060 | if ((typeName == "int") || (typeName == "Int_t") || (typeName == "V"))
|
---|
1061 | {
|
---|
1062 | typeChar = 'J';
|
---|
1063 | typeFound++;
|
---|
1064 | }
|
---|
1065 | if ((typeName == "unsigned int") || (typeName == "UInt_t") || (typeName == "V"))
|
---|
1066 | {
|
---|
1067 | typeChar = 'J';
|
---|
1068 | // *fLog << warn << "Converting unsigned int to int in fits file" << endl;
|
---|
1069 | typeFound++;
|
---|
1070 | }
|
---|
1071 | if ((typeName == "long long") || (typeName == "Long64_t") || (typeName == "K"))
|
---|
1072 | {
|
---|
1073 | typeChar = 'K';
|
---|
1074 | typeFound++;
|
---|
1075 | }
|
---|
1076 | if ((typeName == "unsigned long long") || (typeName == "ULong64_t") || (typeName == "W"))
|
---|
1077 | {
|
---|
1078 | typeChar = 'K';
|
---|
1079 | // *fLog << warn << "Converting unsigned long to long in fits file" << endl;
|
---|
1080 | typeFound++;
|
---|
1081 | }
|
---|
1082 | if ((typeName == "float") || (typeName=="TFloat_t") || (typeName == "E"))
|
---|
1083 | {
|
---|
1084 | typeChar = 'E';
|
---|
1085 | typeFound++;
|
---|
1086 | }
|
---|
1087 | if ((typeName == "double") || (typeName == "TDouble_t") || (typeName == "D"))
|
---|
1088 | {
|
---|
1089 | typeChar = 'D';
|
---|
1090 | typeFound++;
|
---|
1091 | }
|
---|
1092 | // if ((typeName == "char*") || (typeName == "A"))
|
---|
1093 | // {
|
---|
1094 | // typeFound++;
|
---|
1095 | // }
|
---|
1096 | if (typeFound != 1)
|
---|
1097 | {
|
---|
1098 | *fLog << err << "We have a problem with the data type: " << typeName << endl;
|
---|
1099 | return;
|
---|
1100 | }
|
---|
1101 | uint32_t colWidth = 0;
|
---|
1102 | switch (typeChar)
|
---|
1103 | {
|
---|
1104 | case 'L': colWidth = 1*count; break;
|
---|
1105 | case 'A': colWidth = 1*count; break;
|
---|
1106 | case 'B': colWidth = 1*count; break;
|
---|
1107 | case 'I': colWidth = 2*count; break;
|
---|
1108 | case 'J': colWidth = 4*count; break;
|
---|
1109 | case 'K': colWidth = 8*count; break;
|
---|
1110 | case 'E': colWidth = 4*count; break;
|
---|
1111 | case 'D': colWidth = 8*count; break;
|
---|
1112 | default:
|
---|
1113 | *fLog << err << "ERROR: typeChar could not be resolved to an actual type" << endl;
|
---|
1114 | };
|
---|
1115 | //check for type remapping here
|
---|
1116 | if (fBytesPerSamples.find(columnName) != fBytesPerSamples.end())
|
---|
1117 | {
|
---|
1118 | if (typeChar != 'A')
|
---|
1119 | {
|
---|
1120 | *fLog << err << "Attempt to remap type " << typeChar << " to " << fBytesPerSamples[columnName] << " is only allowed on bytes (variable name: " << columnName << "). Ignoring column" << endl;
|
---|
1121 | return;
|
---|
1122 | }
|
---|
1123 | uint32_t bytesPerSample = fBytesPerSamples.find(columnName)->second;
|
---|
1124 | if (colWidth%bytesPerSample != 0)
|
---|
1125 | {
|
---|
1126 | *fLog << err << "Type remapping cannot be done using " << bytesPerSample << " bytes per sample on an array of char of size " << colWidth << ". Ignoring column " << columnName << endl;
|
---|
1127 | return;
|
---|
1128 | }
|
---|
1129 | switch (bytesPerSample)
|
---|
1130 | {
|
---|
1131 | case 1: count = count/1; typeChar = 'A'; break;
|
---|
1132 | case 2: count = count/2; typeChar = 'I'; break;
|
---|
1133 | case 4: count = count/4; typeChar = 'J'; break;
|
---|
1134 | case 8: count = count/8; typeChar = 'K'; break;
|
---|
1135 | default:
|
---|
1136 | *fLog << err << "ERROR: num bytes per sample = " << bytesPerSample << " should have been forbidden already" << endl;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | fDataPointers[tableName].push_back(dataPointer);
|
---|
1142 | fTypeChars[tableName].push_back(typeChar);
|
---|
1143 | fColSizes[tableName].push_back(count);
|
---|
1144 | fColWidth[tableName].push_back(colWidth);
|
---|
1145 |
|
---|
1146 | //FIXME ofits does not allow for much liberty regarding the size of the column names.
|
---|
1147 | //Truncating them badly here, will probably cause other problems -> Modify ofits.h instead
|
---|
1148 | string truncatedName=columnName.substr((columnName.size()>40)?columnName.size()-40:0,columnName.size());
|
---|
1149 | string truncatedComment = comment.substr((comment.size()>10)?comment.size()-10:0,comment.size());
|
---|
1150 | // *fLog << warn << "In table " << tableName << " Adding column |" << truncatedName << "| |" << truncatedComment << "| |" << count << "| |" << typeChar;
|
---|
1151 | // *fLog << warn << "| Real: "<< columnName << " comment: " << comment << endl;
|
---|
1152 | fFitsTables[tableName]->AddColumn(count, typeChar, truncatedName, unit, truncatedComment);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | void MWriteFitsFile::writeOneRow(const TString& tableName)
|
---|
1156 | {
|
---|
1157 | if (!fTableHeaderWritten[tableName])
|
---|
1158 | {
|
---|
1159 | for (vector<ofits::Key>::const_iterator it = fHeaderKeys.begin(); it != fHeaderKeys.end(); it++)
|
---|
1160 | fFitsTables[tableName]->SetRaw(it->key, it->value, it->comment);
|
---|
1161 | fFitsTables[tableName]->WriteTableHeader(tableName.Data());
|
---|
1162 | fTableHeaderWritten[tableName] = true;
|
---|
1163 | }
|
---|
1164 | if (!fTableObjectCreated[tableName])
|
---|
1165 | {
|
---|
1166 | *fLog << err << "This is not good. Please initialize the fits table before writing to it: " << tableName << endl;
|
---|
1167 | return;
|
---|
1168 | }
|
---|
1169 | //first calculate the size of one row
|
---|
1170 | uint32_t rowWidth = 0;
|
---|
1171 | for (uint32_t i=0;i<fTypeChars[tableName].size();i++)
|
---|
1172 | rowWidth += fColWidth[tableName][i];
|
---|
1173 | unsigned char* tempBuffer = new unsigned char[rowWidth];
|
---|
1174 | //then copy the data to be written contiguously
|
---|
1175 | uint32_t bytesCounter = 0;
|
---|
1176 | for (uint32_t i=0;i<fDataPointers[tableName].size();i++)
|
---|
1177 | {
|
---|
1178 | memcpy(&tempBuffer[bytesCounter], fDataPointers[tableName][i], fColWidth[tableName][i]);
|
---|
1179 | bytesCounter+=fColWidth[tableName][i];
|
---|
1180 | }
|
---|
1181 | if (fFitsTables[tableName]->WriteRow(tempBuffer, bytesCounter) == false)
|
---|
1182 | *fLog << err << "Error while writing to FITS table " << tableName << endl;
|
---|
1183 | else
|
---|
1184 | fFitsTables[tableName]->FlushNumRows();
|
---|
1185 |
|
---|
1186 | delete[] tempBuffer;
|
---|
1187 | }
|
---|
1188 | Bool_t MWriteFitsFile::ReInit(MParList *pList)
|
---|
1189 | {
|
---|
1190 | if (fRule.Length() == 0)
|
---|
1191 | // there is not rule defined. We keep the old file
|
---|
1192 | return MWriteFile::ReInit(pList);
|
---|
1193 |
|
---|
1194 | MRead *read = (MRead*)pList->FindTask("MRead");
|
---|
1195 | if (!read)
|
---|
1196 | {
|
---|
1197 | *fLog << err;
|
---|
1198 | *fLog << "ERROR: No Task 'MRead' found in the tasklist. This task is" << endl;
|
---|
1199 | *fLog << " necessary to get the filename. Without a read-filename" << endl;
|
---|
1200 | *fLog << " no output-filename can be created... abort." << endl;
|
---|
1201 | *fLog << endl;
|
---|
1202 | return kFALSE;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 |
|
---|
1206 | // close the current files
|
---|
1207 | CloseTopLevelGroup();
|
---|
1208 | for (std::map<TString,ofits*>::iterator it=fFitsTables.begin(); it!=fFitsTables.end(); it++)
|
---|
1209 | {
|
---|
1210 | (it->second)->close();
|
---|
1211 | delete it->second;
|
---|
1212 | }
|
---|
1213 | fFitsTables.clear();
|
---|
1214 | fDataPointers.clear();
|
---|
1215 | fTypeChars.clear();
|
---|
1216 | fColSizes.clear();
|
---|
1217 | fColWidth.clear();
|
---|
1218 | fTableObjectCreated.clear();
|
---|
1219 | fTableHeaderWritten.clear();
|
---|
1220 | DeleteArrayHelper();
|
---|
1221 | fClHelper.clear();
|
---|
1222 |
|
---|
1223 | // get new filename
|
---|
1224 | const TString readFileName = read->GetFullFileName();
|
---|
1225 | const TString newname = MWriteRootFile::SubstituteName(fRule, readFileName);
|
---|
1226 |
|
---|
1227 | // create new files
|
---|
1228 | OpenTopLevelGroup(newname.Data());
|
---|
1229 | if (!IsFileOpen())
|
---|
1230 | return kFALSE;
|
---|
1231 |
|
---|
1232 |
|
---|
1233 | MRawRunHeader* header = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
|
---|
1234 | SetupHeaderKeys(*header);
|
---|
1235 |
|
---|
1236 | if (GetContainer(pList) == kFALSE)
|
---|
1237 | return kFALSE;
|
---|
1238 |
|
---|
1239 | // do, what has to be done in ReInit.
|
---|
1240 | return MWriteFile::ReInit(pList);
|
---|
1241 |
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | void MWriteFitsFile::DeleteArrayHelper()
|
---|
1245 | {
|
---|
1246 | map<TString, list<MArrayHelperBase *> >::iterator i_helper1 = fClHelper.begin();
|
---|
1247 | while (i_helper1 != fClHelper.end())
|
---|
1248 | {
|
---|
1249 | list<MArrayHelperBase *>::iterator i_helper2 = i_helper1->second.begin();
|
---|
1250 | while(i_helper2 != i_helper1->second.end())
|
---|
1251 | {
|
---|
1252 | delete *i_helper2;
|
---|
1253 |
|
---|
1254 | i_helper2++;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | i_helper1++;
|
---|
1258 | }
|
---|
1259 | }
|
---|