GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GMWLObservation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GMWLObservation.cpp - Multi-wavelength observation class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-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 GMWLObservation.cpp
23  * @brief Multi-wavelength observation class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <typeinfo>
32 #include "GTools.hpp"
33 #include "GFilename.hpp"
34 #include "GObservationRegistry.hpp"
35 #include "GException.hpp"
36 #include "GMWLObservation.hpp"
37 #include "GMWLSpectrum.hpp"
38 
39 /* __ Globals ____________________________________________________________ */
41 const GObservationRegistry g_obs_mwl_registry(&g_obs_mwl_seed);
42 
43 /* __ Method name definitions ____________________________________________ */
44 #define G_RESPONSE "GMWLObservation::response(GResponse&)"
45 #define G_READ "GMWLObservation::read(GXmlElement&)"
46 #define G_WRITE "GMWLObservation::write(GXmlElement&)"
47 
48 /* __ Macros _____________________________________________________________ */
49 
50 /* __ Coding definitions _________________________________________________ */
51 
52 /* __ Debug definitions __________________________________________________ */
53 
54 
55 /*==========================================================================
56  = =
57  = Constructors/destructors =
58  = =
59  ==========================================================================*/
60 
61 /***********************************************************************//**
62  * @brief Void constructor
63  *
64  * Creates instance of an undefined observation.
65  ***************************************************************************/
67 {
68  // Initialise members
69  init_members();
70 
71  // Return
72  return;
73 }
74 
75 
76 /***********************************************************************//**
77  * @brief File constructor
78  *
79  * @param[in] filename File name.
80  *
81  * Creates instance from file.
82  ***************************************************************************/
84 {
85  // Initialise members
86  init_members();
87 
88  // Load observation
89  load(filename);
90 
91  // Return
92  return;
93 }
94 
95 
96 /***********************************************************************//**
97  * @brief Copy constructor
98  *
99  * @param[in] obs Observation.
100  *
101  * Creates instance by copying an existing observation.
102  ***************************************************************************/
104 {
105  // Initialise members
106  init_members();
107 
108  // Copy members
109  copy_members(obs);
110 
111  // Return
112  return;
113 }
114 
115 
116 /***********************************************************************//**
117  * @brief Destructor
118  *
119  * Destroy instance.
120  ***************************************************************************/
122 {
123  // Free members
124  free_members();
125 
126  // Return
127  return;
128 }
129 
130 
131 /*==========================================================================
132  = =
133  = Operators =
134  = =
135  ==========================================================================*/
136 
137 /***********************************************************************//**
138  * @brief Assignment operator
139  *
140  * @param[in] obs Observation.
141  *
142  * Copies observation into the instance.
143  ***************************************************************************/
145 {
146  // Execute only if object is not identical
147  if (this != &obs) {
148 
149  // Copy base class members
150  this->GObservation::operator=(obs);
151 
152  // Free members
153  free_members();
154 
155  // Initialise members
156  init_members();
157 
158  // Copy members
159  copy_members(obs);
160 
161  } // endif: object was not identical
162 
163  // Return this object
164  return *this;
165 }
166 
167 
168 /*==========================================================================
169  = =
170  = Public methods =
171  = =
172  ==========================================================================*/
173 
174 /***********************************************************************//**
175  * @brief Clear instance
176  *
177  * This method properly resets the instance to an initial state.
178  ***************************************************************************/
180 {
181  // Free class members (base and derived classes, derived class first)
182  free_members();
184 
185  // Initialise members
187  init_members();
188 
189  // Return
190  return;
191 }
192 
193 
194 /***********************************************************************//**
195  * @brief Clone instance
196 ***************************************************************************/
198 {
199  return new GMWLObservation(*this);
200 }
201 
202 
203 /***********************************************************************//**
204  * @brief Set response function
205  *
206  * @param[in] rsp Response function.
207  *
208  * @exception GException::invalid_argument
209  * Response @p rsp in not a MWL response.
210  *
211  * Sets the response function for the observation. The argument has to be of
212  * type GMWLResponse, otherwise an exception is thrown.
213  ***************************************************************************/
215 {
216  // Get pointer on MWL response
217  const GMWLResponse* mwlrsp = dynamic_cast<const GMWLResponse*>(&rsp);
218 
219  // If pointer is not valid then throw an exception
220  if (mwlrsp == NULL) {
221  std::string cls = std::string(typeid(&rsp).name());
222  std::string msg = "Invalid response type \""+cls+"\" specified. "
223  "Please specify a \"GMWLResponse\" instance as "
224  "argument.";
226  }
227 
228  // Copy response function
229  m_response = *mwlrsp;
230 
231  // Return
232  return;
233 }
234 
235 
236 /***********************************************************************//**
237  * @brief Read observation from XML element
238  *
239  * @param[in] xml XML element.
240  *
241  * Reads information for a multi-wavelength observation from an XML element.
242  * The expected format of the XML element is
243  *
244  * <observation name="..." id="..." instrument="MWL">
245  * <parameter name="Instrument" value="..."/>
246  * <parameter name="Data" file="..."/>
247  * </observation>
248  *
249  * The extno and extname attributes of the data parameter are optional, and
250  * can be used to indicate the extension number or name from which the
251  * multi-wavelength data should be loaded. If both are given, the extension
252  * number will take precedence and the extension name is ignored.
253  ***************************************************************************/
255 {
256  // Clear observation
257  clear();
258 
259  // Get parameters
260  m_instrument = gammalib::xml_get_attr(G_READ, xml, "Instrument", "value");
261  std::string filename = gammalib::xml_get_attr(G_READ, xml, "Data", "file");
262 
263  // Expand file names
264  filename = gammalib::xml_file_expand(xml, filename);
265 
266  // Load file
267  load(filename);
268 
269  // Return
270  return;
271 }
272 
273 
274 /***********************************************************************//**
275  * @brief Write observation into XML element
276  *
277  * @param[in] xml XML element.
278  *
279  * Writes information for a multi-wavelength observation into an XML element.
280  * The format of the XML element is
281  *
282  * <observation name="..." id="..." instrument="MWL">
283  * <parameter name="Instrument" value="..."/>
284  * <parameter name="Data" file="..."/>
285  * </observation>
286  ***************************************************************************/
288 {
289  // Allocate XML element pointer
290  GXmlElement* par;
291 
292  // Set Instrument parameter
293  par = gammalib::xml_need_par(G_WRITE, xml, "Instrument");
294  par->attribute("value", gammalib::xml_file_reduce(xml, m_instrument));
295 
296  // Set Data parameter
297  par = gammalib::xml_need_par(G_WRITE, xml, "Data");
298  par->attribute("file", gammalib::xml_file_reduce(xml, m_filename));
299 
300  // Return
301  return;
302 }
303 
304 
305 /***********************************************************************//**
306  * @brief Load observation
307  *
308  * @param[in] filename File name.
309  ***************************************************************************/
310 void GMWLObservation::load(const GFilename& filename)
311 {
312  // Clear observation
313  clear();
314 
315  // Allocate spectrum
316  GMWLSpectrum* spec = new GMWLSpectrum;
317  m_events = spec;
318 
319  // Load spectrum
320  spec->load(filename);
321 
322  // Set attributes
323  name("Multi-wavelength observation");
324  id(filename);
326  m_instrument = spec->instrument();
327 
328  // Return
329  return;
330 }
331 
332 
333 /***********************************************************************//**
334  * @brief Print multi-wavelength information
335  *
336  * @param[in] chatter Chattiness (defaults to NORMAL).
337  * @return String containing multi-wavelength information.
338  ***************************************************************************/
339 std::string GMWLObservation::print(const GChatter& chatter) const
340 {
341  // Initialise result string
342  std::string result;
343 
344  // Continue only if chatter is not silent
345  if (chatter != SILENT) {
346 
347  // Append header
348  result.append("=== GMWLObservation ===");
349 
350  // Append information
351  result.append("\n"+gammalib::parformat("Name")+name());
352  result.append("\n"+gammalib::parformat("Identifier")+id());
353  result.append("\n"+gammalib::parformat("Instrument")+instrument());
354  result.append("\n"+gammalib::parformat("Statistic")+statistic());
355 
356  // EXPLICIT: Append events
357  if (chatter >= EXPLICIT) {
358  if (m_events != NULL) {
359  result.append("\n"+m_events->print(chatter));
360  }
361  }
362 
363  } // endif: chatter was not silent
364 
365  // Return result
366  return result;
367 }
368 
369 
370 /*==========================================================================
371  = =
372  = Private methods =
373  = =
374  ==========================================================================*/
375 
376 /***********************************************************************//**
377  * @brief Initialise class members
378  *
379  * The instrument name is set here to "MWL" so that the registry has an
380  * instrument type with that name. This may be later overwritten by a
381  * specific instrument.
382  ***************************************************************************/
384 {
385  // Initialise members
386  m_instrument = "MWL";
387  m_filename.clear();
388  m_response.clear();
389 
390  // Overwrite base class statistic
391  m_statistic = "Gaussian";
392 
393  // Return
394  return;
395 }
396 
397 
398 /***********************************************************************//**
399  * @brief Copy class members
400  *
401  * @param[in] obs Observation to be copied
402  ***************************************************************************/
404 {
405  // Copy members
407  m_filename = obs.m_filename;
408  m_response = obs.m_response;
409 
410  // Return
411  return;
412 }
413 
414 
415 /***********************************************************************//**
416  * @brief Delete class members
417  ***************************************************************************/
419 {
420  // Return
421  return;
422 }
423 
424 
425 /*==========================================================================
426  = =
427  = Friends =
428  = =
429  ==========================================================================*/
const GFilename & filename(void) const
Return filename.
const std::string & statistic(void) const
Return optimizer statistic.
Multi-wavelength response class.
virtual GMWLObservation * clone(void) const
Clone instance.
std::string m_instrument
Instrument name.
Multi-wavelength spectrum class interface definition.
GEvents * m_events
Pointer to event container.
XML element node class.
Definition: GXmlElement.hpp:48
Gammalib tools definition.
const std::string & instrument(void) const
Return instrument name.
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition: GTools.cpp:1946
virtual ~GMWLObservation(void)
Destructor.
Multi-wavelength spectrum class interface.
void free_members(void)
Delete class members.
#define G_READ
#define G_WRITE
const std::string & id(void) const
Return observation identifier.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print multi-wavelength information.
virtual void clear(void)
Clear instance.
const GMWLObservation g_obs_mwl_seed
Filename class.
Definition: GFilename.hpp:62
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1637
Interface definition for the observation registry class.
const std::string & name(void) const
Return observation name.
void copy_members(const GMWLObservation &obs)
Copy class members.
Observation registry class definition.
Interface class for multi-wavelength observations.
GChatter
Definition: GTypemaps.hpp:33
void init_members(void)
Initialise class members.
Abstract observation base class.
GMWLResponse m_response
Response function.
virtual void read(const GXmlElement &xml)
Read observation from XML element.
#define G_RESPONSE
GFilename m_filename
Filename.
virtual GMWLObservation & operator=(const GMWLObservation &obs)
Assignment operator.
void clear(void)
Clear file name.
Definition: GFilename.cpp:188
virtual std::string instrument(void) const
Return instrument name.
void init_members(void)
Initialise class members.
std::string m_statistic
Optimizer statistic.
Multi-wavelength observation class interface definition.
virtual const GMWLResponse * response(void) const
Return response.
virtual GObservation & operator=(const GObservation &obs)
Assignment operator.
virtual void write(GXmlElement &xml) const
Write observation into XML element.
Exception handler interface definition.
std::string xml_get_attr(const std::string &origin, const GXmlElement &xml, const std::string &name, const std::string &attribute)
Return attribute value for a given parameter in XML element.
Definition: GTools.cpp:1738
virtual void clear(void)
Clear instance.
Abstract instrument response base class.
Definition: GResponse.hpp:77
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
GMWLObservation(void)
Void constructor.
void load(const GFilename &filename)
Load observation.
Filename class interface definition.
virtual void load(const GFilename &filename)
Load spectrum.
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition: GTools.cpp:1889
void free_members(void)
Delete class members.