GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GLATEdisp.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GLATEdisp.cpp - Fermi LAT energy dispersion *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2018 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GLATEdisp.cpp
23  * @brief Fermi LAT energy dispersion class implementation.
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GFilename.hpp"
33 #include "GFits.hpp"
34 #include "GFitsTable.hpp"
35 #include "GFitsBinTable.hpp"
36 #include "GFitsTableFloatCol.hpp"
37 #include "GLATEdisp.hpp"
38 #include "GLATException.hpp"
39 
40 /* __ Method name definitions ____________________________________________ */
41 #define G_READ "GLATEdisp::read(GFits&)"
42 #define G_READ_EDISP "GLATEdisp::read_edisp(GFitsTable&)"
43 
44 /* __ Macros _____________________________________________________________ */
45 
46 /* __ Coding definitions _________________________________________________ */
47 
48 /* __ Debug definitions __________________________________________________ */
49 
50 /* __ Constants __________________________________________________________ */
51 
52 
53 /*==========================================================================
54  = =
55  = Constructors/destructors =
56  = =
57  ==========================================================================*/
58 
59 /***********************************************************************//**
60  * @brief Void constructor
61  ***************************************************************************/
63 {
64  // Initialise class members
65  init_members();
66 
67  // Return
68  return;
69 }
70 
71 
72 /***********************************************************************//**
73  * @brief File constructor
74  *
75  * @param[in] filename FITS file name.
76  * @param[in] evtype Event type.
77  *
78  * Construct instance by loading the energy dispersion information from FITS
79  * file.
80  ***************************************************************************/
81 GLATEdisp::GLATEdisp(const GFilename& filename, const std::string& evtype)
82 {
83  // Initialise class members
84  init_members();
85 
86  // Load energy dispersion from file
87  load(filename, evtype);
88 
89  // Return
90  return;
91 }
92 
93 
94 /***********************************************************************//**
95  * @brief Copy constructor
96  *
97  * @param[in] edisp Energy dispersion.
98  ***************************************************************************/
100 {
101  // Initialise class members
102  init_members();
103 
104  // Copy members
105  copy_members(edisp);
106 
107  // Return
108  return;
109 }
110 
111 
112 /***********************************************************************//**
113  * @brief Destructor
114  ***************************************************************************/
116 {
117  // Free members
118  free_members();
119 
120  // Return
121  return;
122 }
123 
124 
125 /*==========================================================================
126  = =
127  = Operators =
128  = =
129  ==========================================================================*/
130 
131 /***********************************************************************//**
132  * @brief Assignment operator
133  *
134  * @param[in] edisp Energy dispersion.
135  * @return Energy dispersion.
136  ***************************************************************************/
138 {
139  // Execute only if object is not identical
140  if (this != &edisp) {
141 
142  // Free members
143  free_members();
144 
145  // Initialise private members
146  init_members();
147 
148  // Copy members
149  copy_members(edisp);
150 
151  } // endif: object was not identical
152 
153  // Return this object
154  return *this;
155 }
156 
157 
158 /*==========================================================================
159  = =
160  = Public methods =
161  = =
162  ==========================================================================*/
163 
164 /***********************************************************************//**
165  * @brief Clear energy dispersion response
166  *
167  * This method properly resets the object to an initial state.
168  ***************************************************************************/
170 {
171  // Free class members
172  free_members();
173 
174  // Initialise members
175  init_members();
176 
177  // Return
178  return;
179 }
180 
181 
182 /***********************************************************************//**
183  * @brief Clone energy dispersion response
184  *
185  * @return Pointer to deep copy of energy dispersion response.
186  ***************************************************************************/
188 {
189  return new GLATEdisp(*this);
190 }
191 
192 
193 /***********************************************************************//**
194  * @brief Load energy dispersion from FITS file
195  *
196  * @param[in] filename FITS file.
197  * @param[in] evtype Event type.
198  ***************************************************************************/
199 void GLATEdisp::load(const GFilename& filename, const std::string& evtype)
200 {
201  // Open FITS file
202  GFits fits(filename);
203 
204  // Read energy dispersion from file
205  read(fits, evtype);
206 
207  // Return
208  return;
209 }
210 
211 
212 /***********************************************************************//**
213  * @brief Save energy dispersion into FITS file
214  *
215  * @param[in] filename FITS file.
216  * @param[in] clobber Overwrite existing file? (default: false)
217  ***************************************************************************/
218 void GLATEdisp::save(const GFilename& filename, const bool& clobber)
219 {
220  // Create FITS file
221  GFits fits;
222 
223  // Write energy dispersion into file
224  write(fits);
225 
226  // Close FITS file
227  fits.saveto(filename, clobber);
228 
229  // Return
230  return;
231 }
232 
233 
234 /***********************************************************************//**
235  * @brief Read energy dispersion from FITS file
236  *
237  * @param[in] fits FITS file.
238  * @param[in] evtype Event type.
239  *
240  * @todo Implement reading of scaling parameters
241  ***************************************************************************/
242 void GLATEdisp::read(const GFits& fits, const std::string& evtype)
243 {
244  // Clear instance
245  clear();
246 
247  // Store event type
248  m_evtype = evtype;
249 
250  // Set extension names
251  std::string engdisp = gammalib::extname_lat_edisp;
252  std::string escales = gammalib::extname_lat_edisp_scale;
253  if (!fits.contains(engdisp)) {
254  engdisp += "_" + m_evtype;
255  }
256  if (!fits.contains(escales)) {
257  escales += "_" + m_evtype;
258  }
259 
260  // Get pointer to effective area HDU
261  const GFitsTable& hdu_edisp = *fits.table(engdisp);
262  //const GFitsTable& hdu_scale = *fits.table(escales);
263 
264  // Read energy dispersion
265  read_edisp(hdu_edisp);
266 
267  // Return
268  return;
269 }
270 
271 
272 /***********************************************************************//**
273  * @brief Write energy dispersion into FITS file
274  *
275  * @param[in] fits FITS file.
276  ***************************************************************************/
277 void GLATEdisp::write(GFits& fits) const
278 {
279  // Write energy dispersion
280  write_edisp(fits);
281 
282  // Return
283  return;
284 }
285 
286 
287 /***********************************************************************//**
288  * @brief Print energy dispersion information
289  *
290  * @param[in] chatter Chattiness.
291  * @return String containing energy dispersion information.
292  ***************************************************************************/
293 std::string GLATEdisp::print(const GChatter& chatter) const
294 {
295  // Initialise result string
296  std::string result;
297 
298  // Continue only if chatter is not silent
299  if (chatter != SILENT) {
300 
301  // Append header
302  result.append("=== GLATEdisp ===");
303 
304  // Append information
305  result.append("\n"+gammalib::parformat("Number of energy bins") +
307  result.append("\n"+gammalib::parformat("Number of cos theta bins") +
309 
310  } // endif: chatter was not silent
311 
312  // Return result
313  return result;
314 }
315 
316 
317 /*==========================================================================
318  = =
319  = Private methods =
320  = =
321  ==========================================================================*/
322 
323 /***********************************************************************//**
324  * @brief Initialise class members
325  ***************************************************************************/
327 {
328  // Initialise members
329  m_evtype.clear();
331  m_norm.clear();
332  m_ls1.clear();
333  m_scale.clear();
334 
335  // Return
336  return;
337 }
338 
339 
340 /***********************************************************************//**
341  * @brief Copy class members
342  *
343  * @param[in] edisp Energy dispersion.
344  ***************************************************************************/
346 {
347  // Copy members
348  m_evtype = edisp.m_evtype;
349  m_edisp_bins = edisp.m_edisp_bins;
350  m_norm = edisp.m_norm;
351  m_ls1 = edisp.m_ls1;
352  m_scale = edisp.m_scale;
353 
354  // Return
355  return;
356 }
357 
358 
359 /***********************************************************************//**
360  * @brief Delete class members
361  ***************************************************************************/
363 {
364  // Return
365  return;
366 }
367 
368 
369 /***********************************************************************//**
370  * @brief Read energy dispersion from FITS table
371  *
372  * @param[in] hdu FITS table pointer.
373  *
374  * @exception GLATException::inconsistent_response
375  * Inconsistent response table encountered
376  ***************************************************************************/
378 {
379  // Clear arrays
380  m_norm.clear();
381  m_ls1.clear();
382 
383  // Get energy and cos theta binning
384  m_edisp_bins.read(hdu);
385 
386  // Continue only if there are bins
387  int size = m_edisp_bins.size();
388  if (size > 0) {
389 /*
390  // Allocate arrays
391  m_norm.reserve(size);
392  m_ls1.reserve(size);
393 
394  // Get pointer to columns
395  const GFitsTableCol* norm = hdu["NORM"];
396  const GFitsTableCol* ls1 = hdu["LS1"];
397 
398  // Check consistency of columns
399  if (norm->number() != size) {
400  throw GLATException::inconsistent_response(G_READ_EDISP,
401  norm->number(), size);
402  }
403  if (ls1->number() != size) {
404  throw GLATException::inconsistent_response(G_READ_EDISP,
405  ls1->number(), size);
406  }
407 
408  // Copy data
409  for (int i = 0; i < size; ++i) {
410  m_norm.push_back(norm->real(0,i));
411  m_ls1.push_back(ls1->real(0,i));
412  }
413 */
414  } // endif: there were bins
415 
416  // Return
417  return;
418 }
419 
420 
421 /***********************************************************************//**
422  * @brief Write energy dispersion into FITS file
423  *
424  * @param[in] file FITS file.
425  *
426  * This method does not write anything if the instance is empty.
427  ***************************************************************************/
428 void GLATEdisp::write_edisp(GFits& file) const
429 {
430  // Continue only if there are bins
431  int size = m_edisp_bins.size();
432  if (size > 0) {
433 
434  // Allocate new binary table
435  GFitsBinTable hdu_edisp;
436 
437  // Set table attributes
439 
440  // Write boundaries into table
441  m_edisp_bins.write(hdu_edisp);
442 
443 /*
444  // Allocate floating point vector columns
445  GFitsTableFloatCol col_norm = GFitsTableFloatCol("NORM", 1, size);
446  GFitsTableFloatCol col_ls1 = GFitsTableFloatCol("LS1", 1, size);
447 
448  // Fill columns
449  for (int i = 0; i < size; ++i) {
450  col_norm(0,i) = m_norm[i];
451  col_ls1(0,i) = m_ls1[i];
452  }
453 
454  // Append columns to table
455  hdu_edisp.append(col_norm);
456  hdu_edisp.append(col_ls1);
457 */
458  // Append HDU to FITS file
459  file.append(hdu_edisp);
460 
461  } // endif: there were data to write
462 
463  // Return
464  return;
465 }
void read(const GFits &file, const std::string &evtype)
Read energy dispersion from FITS file.
Definition: GLATEdisp.cpp:242
GFitsTable * table(const int &extno)
Get pointer to table HDU.
Definition: GFits.cpp:472
bool contains(const int &extno) const
Check if HDU exists in FITS file.
Definition: GFits.hpp:282
std::vector< double > m_scale
Energy dispersion scaling parameters.
Definition: GLATEdisp.hpp:102
void save(const GFilename &filename, const bool &clobber=false)
Save energy dispersion into FITS file.
Definition: GLATEdisp.cpp:218
GLATEdisp & operator=(const GLATEdisp &edisp)
Assignment operator.
Definition: GLATEdisp.cpp:137
void read(const GFitsTable &hdu)
Read response table from FITS table HDU.
Interface for the Fermi LAT energy dispersion.
Definition: GLATEdisp.hpp:55
void clear(void)
Clear instance.
void write(GFitsTable &hdu) const
Write response table into FITS table.
int size(void) const
Return number of bins in energy dispersion response.
Definition: GLATEdisp.hpp:136
Gammalib tools definition.
FITS table float column class interface definition.
FITS file class.
Definition: GFits.hpp:63
FITS file class interface definition.
void write_edisp(GFits &file) const
Write energy dispersion into FITS file.
Definition: GLATEdisp.cpp:428
std::vector< double > m_ls1
Energy dispersion ...
Definition: GLATEdisp.hpp:101
void write(GFits &file) const
Write energy dispersion into FITS file.
Definition: GLATEdisp.cpp:277
void read_edisp(const GFitsTable &hdu)
Read energy dispersion from FITS table.
Definition: GLATEdisp.cpp:377
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
Definition: GFits.cpp:1267
GLATResponseTable m_edisp_bins
Energy dispersion energy and cos theta binning.
Definition: GLATEdisp.hpp:99
LAT exception handler interface definition.
Filename class.
Definition: GFilename.hpp:62
int ncostheta(void) const
Return number of cosine theta bins in energy dispersion response.
Definition: GLATEdisp.hpp:160
Abstract interface for FITS table.
Definition: GFitsTable.hpp:44
int nenergies(void) const
Return number of energies in energy dispersion response.
Definition: GLATEdisp.hpp:148
GChatter
Definition: GTypemaps.hpp:33
Fermi LAT energy dispersion class definition.
const std::string & extname(void) const
Return extension name.
Definition: GFitsHDU.hpp:162
void free_members(void)
Delete class members.
Definition: GLATEdisp.cpp:362
GLATEdisp(void)
Void constructor.
Definition: GLATEdisp.cpp:62
GLATEdisp * clone(void) const
Clone energy dispersion response.
Definition: GLATEdisp.cpp:187
void clear(void)
Clear energy dispersion response.
Definition: GLATEdisp.cpp:169
std::string print(const GChatter &chatter=NORMAL) const
Print energy dispersion information.
Definition: GLATEdisp.cpp:293
virtual ~GLATEdisp(void)
Destructor.
Definition: GLATEdisp.cpp:115
const std::string extname_lat_edisp
Definition: GLATEdisp.hpp:43
std::vector< double > m_norm
Energy dispersion normalization.
Definition: GLATEdisp.hpp:100
void copy_members(const GLATEdisp &edisp)
Copy class members.
Definition: GLATEdisp.cpp:345
FITS binary table class.
const std::string & evtype(void) const
Return event type.
Definition: GLATEdisp.hpp:124
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
Definition: GFits.cpp:665
FITS binary table class definition.
void load(const GFilename &filename, const std::string &evtype)
Load energy dispersion from FITS file.
Definition: GLATEdisp.cpp:199
const std::string extname_lat_edisp_scale
Definition: GLATEdisp.hpp:44
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
int size(void) const
Return number of bins in response table.
void init_members(void)
Initialise class members.
Definition: GLATEdisp.cpp:326
Filename class interface definition.
std::string m_evtype
Event type.
Definition: GLATEdisp.hpp:98
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413
FITS table abstract base class interface definition.