GammaLib  1.7.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-2017 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 "GTools.hpp"
32 #include "GFilename.hpp"
33 #include "GObservationRegistry.hpp"
34 #include "GException.hpp"
35 #include "GMWLObservation.hpp"
36 #include "GMWLSpectrum.hpp"
37 #include "GMWLException.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 GMWLException::bad_response_type
209  * Specified response in not of type GMWLResponse.
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  if (mwlrsp == NULL) {
220  }
221 
222  // Copy response function
223  m_response = *mwlrsp;
224 
225  // Return
226  return;
227 }
228 
229 
230 /***********************************************************************//**
231  * @brief Read observation from XML element
232  *
233  * @param[in] xml XML element.
234  *
235  * Reads information for a multi-wavelength observation from an XML element.
236  * The expected format of the XML element is
237  *
238  * <observation name="..." id="..." instrument="MWL">
239  * <parameter name="Instrument" value="..."/>
240  * <parameter name="Data" file="..."/>
241  * </observation>
242  *
243  * The extno and extname attributes of the data parameter are optional, and
244  * can be used to indicate the extension number or name from which the
245  * multi-wavelength data should be loaded. If both are given, the extension
246  * number will take precedence and the extension name is ignored.
247  ***************************************************************************/
249 {
250  // Clear observation
251  clear();
252 
253  // Get parameters
254  m_instrument = gammalib::xml_get_attr(G_READ, xml, "Instrument", "value");
255  std::string filename = gammalib::xml_get_attr(G_READ, xml, "Data", "file");
256 
257  // Expand file names
258  filename = gammalib::xml_file_expand(xml, filename);
259 
260  // Load file
261  load(filename);
262 
263  // Return
264  return;
265 }
266 
267 
268 /***********************************************************************//**
269  * @brief Write observation into XML element
270  *
271  * @param[in] xml XML element.
272  *
273  * @exception GException::xml_invalid_parnum
274  * Invalid number of parameters found in XML element.
275  * @exception GException::xml_invalid_parnames
276  * Invalid parameter names found in XML element.
277  *
278  * Writes information for a multi-wavelength observation into an XML element.
279  * The format of the XML element is
280  *
281  * <observation name="..." id="..." instrument="MWL">
282  * <parameter name="Instrument" value="..."/>
283  * <parameter name="Data" file="..."/>
284  * </observation>
285  ***************************************************************************/
287 {
288  // Allocate XML element pointer
289  GXmlElement* par;
290 
291  // Set Instrument parameter
292  par = gammalib::xml_need_par(G_WRITE, xml, "Instrument");
293  par->attribute("value", gammalib::xml_file_reduce(xml, m_instrument));
294 
295  // Set Data parameter
296  par = gammalib::xml_need_par(G_WRITE, xml, "Data");
297  par->attribute("file", gammalib::xml_file_reduce(xml, m_filename));
298 
299  // Return
300  return;
301 }
302 
303 
304 /***********************************************************************//**
305  * @brief Load observation
306  *
307  * @param[in] filename File name.
308  ***************************************************************************/
309 void GMWLObservation::load(const GFilename& filename)
310 {
311  // Clear observation
312  clear();
313 
314  // Allocate spectrum
315  GMWLSpectrum* spec = new GMWLSpectrum;
316  m_events = spec;
317 
318  // Load spectrum
319  spec->load(filename);
320 
321  // Set attributes
322  name("Multi-wavelength observation");
323  id(filename);
325  m_instrument = spec->instrument();
326 
327  // Return
328  return;
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Print multi-wavelength information
334  *
335  * @param[in] chatter Chattiness (defaults to NORMAL).
336  * @return String containing multi-wavelength information.
337  ***************************************************************************/
338 std::string GMWLObservation::print(const GChatter& chatter) const
339 {
340  // Initialise result string
341  std::string result;
342 
343  // Continue only if chatter is not silent
344  if (chatter != SILENT) {
345 
346  // Append header
347  result.append("=== GMWLObservation ===");
348 
349  // Append information
350  result.append("\n"+gammalib::parformat("Name")+name());
351  result.append("\n"+gammalib::parformat("Identifier")+id());
352  result.append("\n"+gammalib::parformat("Instrument")+instrument());
353  result.append("\n"+gammalib::parformat("Statistic")+statistic());
354 
355  // EXPLICIT: Append events
356  if (chatter >= EXPLICIT) {
357  if (m_events != NULL) {
358  result.append("\n"+m_events->print(chatter));
359  }
360  }
361 
362  } // endif: chatter was not silent
363 
364  // Return result
365  return result;
366 }
367 
368 
369 /*==========================================================================
370  = =
371  = Private methods =
372  = =
373  ==========================================================================*/
374 
375 /***********************************************************************//**
376  * @brief Initialise class members
377  *
378  * The instrument name is set here to "MWL" so that the registry has an
379  * instrument type with that name. This may be later overwritten by a
380  * specific instrument.
381  ***************************************************************************/
383 {
384  // Initialise members
385  m_instrument = "MWL";
386  m_filename.clear();
387  m_response.clear();
388 
389  // Overwrite base class statistic
390  m_statistic = "Gaussian";
391 
392  // Return
393  return;
394 }
395 
396 
397 /***********************************************************************//**
398  * @brief Copy class members
399  *
400  * @param[in] obs Observation to be copied
401  ***************************************************************************/
403 {
404  // Copy members
406  m_filename = obs.m_filename;
407  m_response = obs.m_response;
408 
409  // Return
410  return;
411 }
412 
413 
414 /***********************************************************************//**
415  * @brief Delete class members
416  ***************************************************************************/
418 {
419  // Return
420  return;
421 }
422 
423 
424 /*==========================================================================
425  = =
426  = Friends =
427  = =
428  ==========================================================================*/
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:47
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:1713
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:1513
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.
MWL exception handler interface 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:1608
virtual void clear(void)
Clear instance.
Abstract instrument response base class.
Definition: GResponse.hpp:67
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
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:1683
void free_members(void)
Delete class members.