GammaLib  2.1.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-2021 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 "GException.hpp"
32 #include "GTools.hpp"
33 #include "GFilename.hpp"
34 #include "GFits.hpp"
35 #include "GFitsTable.hpp"
36 #include "GFitsBinTable.hpp"
37 #include "GFitsTableFloatCol.hpp"
38 #include "GLATEdisp.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  /*
257  if (!fits.contains(escales)) {
258  escales += "_" + m_evtype;
259  }
260  */
261 
262  // Get pointer to effective area HDU
263  const GFitsTable& hdu_edisp = *fits.table(engdisp);
264  //const GFitsTable& hdu_scale = *fits.table(escales);
265 
266  // Read energy dispersion
267  read_edisp(hdu_edisp);
268 
269  // Return
270  return;
271 }
272 
273 
274 /***********************************************************************//**
275  * @brief Write energy dispersion into FITS file
276  *
277  * @param[in] fits FITS file.
278  ***************************************************************************/
279 void GLATEdisp::write(GFits& fits) const
280 {
281  // Write energy dispersion
282  write_edisp(fits);
283 
284  // Return
285  return;
286 }
287 
288 
289 /***********************************************************************//**
290  * @brief Print energy dispersion information
291  *
292  * @param[in] chatter Chattiness.
293  * @return String containing energy dispersion information.
294  ***************************************************************************/
295 std::string GLATEdisp::print(const GChatter& chatter) const
296 {
297  // Initialise result string
298  std::string result;
299 
300  // Continue only if chatter is not silent
301  if (chatter != SILENT) {
302 
303  // Append header
304  result.append("=== GLATEdisp ===");
305 
306  // Append information
307  result.append("\n"+gammalib::parformat("Number of energy bins") +
309  result.append("\n"+gammalib::parformat("Number of cos theta bins") +
311 
312  } // endif: chatter was not silent
313 
314  // Return result
315  return result;
316 }
317 
318 
319 /*==========================================================================
320  = =
321  = Private methods =
322  = =
323  ==========================================================================*/
324 
325 /***********************************************************************//**
326  * @brief Initialise class members
327  ***************************************************************************/
329 {
330  // Initialise members
331  m_evtype.clear();
333  m_norm.clear();
334  m_ls1.clear();
335  m_scale.clear();
336 
337  // Return
338  return;
339 }
340 
341 
342 /***********************************************************************//**
343  * @brief Copy class members
344  *
345  * @param[in] edisp Energy dispersion.
346  ***************************************************************************/
348 {
349  // Copy members
350  m_evtype = edisp.m_evtype;
351  m_edisp_bins = edisp.m_edisp_bins;
352  m_norm = edisp.m_norm;
353  m_ls1 = edisp.m_ls1;
354  m_scale = edisp.m_scale;
355 
356  // Return
357  return;
358 }
359 
360 
361 /***********************************************************************//**
362  * @brief Delete class members
363  ***************************************************************************/
365 {
366  // Return
367  return;
368 }
369 
370 
371 /***********************************************************************//**
372  * @brief Read energy dispersion from FITS table
373  *
374  * @param[in] table FITS table.
375  *
376  * @exception GException::invalid_argument
377  * Inconsistent response table encountered
378  *
379  * @todo Implement reading of energy dispersion table
380  ***************************************************************************/
382 {
383  // Clear arrays
384  m_norm.clear();
385  m_ls1.clear();
386 
387  // Get energy and cos theta binning
388  m_edisp_bins.read(table);
389 
390  // Continue only if there are bins
391  int size = m_edisp_bins.size();
392  if (size > 0) {
393 /*
394  // Allocate arrays
395  m_norm.reserve(size);
396  m_ls1.reserve(size);
397 
398  // Get pointer to columns
399  const GFitsTableCol* norm = table["NORM"];
400  const GFitsTableCol* ls1 = table["LS1"];
401 
402  // Check consistency of columns
403  if (norm->number() != size) {
404  std::string msg = "Number of elements in \"NORM\" column ("+
405  gammalib::str(norm->number())+") is incompatible "
406  "with the expected size ("+
407  gammalib::str(size)+"). Please specify a valid "
408  "point spread function table.";
409  throw GException::invalid_argument(G_READ, msg);
410  }
411  if (ls1->number() != size) {
412  std::string msg = "Number of elements in \"LS1\" column ("+
413  gammalib::str(ls1->number())+") is incompatible "
414  "with the expected size ("+
415  gammalib::str(size)+"). Please specify a valid "
416  "point spread function table.";
417  throw GException::invalid_argument(G_READ, msg);
418  }
419 
420  // Copy data
421  for (int i = 0; i < size; ++i) {
422  m_norm.push_back(norm->real(0,i));
423  m_ls1.push_back(ls1->real(0,i));
424  }
425 */
426  } // endif: there were bins
427 
428  // Return
429  return;
430 }
431 
432 
433 /***********************************************************************//**
434  * @brief Write energy dispersion into FITS file
435  *
436  * @param[in] file FITS file.
437  *
438  * This method does not write anything if the instance is empty.
439  ***************************************************************************/
440 void GLATEdisp::write_edisp(GFits& file) const
441 {
442  // Continue only if there are bins
443  int size = m_edisp_bins.size();
444  if (size > 0) {
445 
446  // Allocate new binary table
447  GFitsBinTable hdu_edisp;
448 
449  // Set table attributes
451 
452  // Write boundaries into table
453  m_edisp_bins.write(hdu_edisp);
454 
455 /*
456  // Allocate floating point vector columns
457  GFitsTableFloatCol col_norm = GFitsTableFloatCol("NORM", 1, size);
458  GFitsTableFloatCol col_ls1 = GFitsTableFloatCol("LS1", 1, size);
459 
460  // Fill columns
461  for (int i = 0; i < size; ++i) {
462  col_norm(0,i) = m_norm[i];
463  col_ls1(0,i) = m_ls1[i];
464  }
465 
466  // Append columns to table
467  hdu_edisp.append(col_norm);
468  hdu_edisp.append(col_ls1);
469 */
470  // Append HDU to FITS file
471  file.append(hdu_edisp);
472 
473  } // endif: there were data to write
474 
475  // Return
476  return;
477 }
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:482
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:440
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:279
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
Definition: GFits.cpp:1293
GLATResponseTable m_edisp_bins
Energy dispersion energy and cos theta binning.
Definition: GLATEdisp.hpp:99
Filename class.
Definition: GFilename.hpp:62
void read_edisp(const GFitsTable &table)
Read energy dispersion from FITS table.
Definition: GLATEdisp.cpp:381
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:364
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:295
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:347
FITS binary table class.
Exception handler interface definition.
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:678
FITS binary table class definition.
void load(const GFilename &filename, const std::string &evtype)
Load energy dispersion from FITS file.
Definition: GLATEdisp.cpp:199
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
int size(void) const
Return number of bins in response table.
void init_members(void)
Initialise class members.
Definition: GLATEdisp.cpp:328
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:489
FITS table abstract base class interface definition.