51#define G_CONSTRUCTOR "GSPIModelDataSpace::GSPIModelDataSpace("\
52 "GSPIObservation&, std::string&, std::string&, int&)"
53#define G_EVAL1 "GSPIModelDataSpace::eval(GEvent&, GObservation&, bool&)"
54#define G_EVAL2 "GSPIModelDataSpace::eval(GObservation&, GMatrixSparse*)"
55#define G_NPRED "GSPIModelDataSpace::npred(GEnergy&, GTime&, GObservation&)"
56#define G_MC "GSPIModelDataSpace::mc(GObservation&, GRan&)"
57#define G_SETUP "GSPIModelDataSpace::setup(Observation&)"
58#define G_READ "GSPIModelDataSpace::read(GXmlElement&)"
59#define G_WRITE "GSPIModelDataSpace::write(GXmlElement&)"
60#define G_GET_DATE_TIME "GSPIModelDataSpace::get_date_time(std::string&)"
110 const std::string& name,
111 const std::string& method,
212 if (
this != &model) {
295 const bool& gradients)
const
304 std::string cls = std::string(
typeid(obs.
events()).name());
305 std::string msg =
"Events of type \""+cls+
"\" are not an INTEGRAL/SPI "
306 "event cube. Please specify an INTEGRAL/SPI observation "
307 "with an event cube as argument.";
314 std::string cls = std::string(
typeid(&event).
name());
315 std::string msg =
"Event of type \""+cls+
"\" is not an INTEGRAL/SPI "
316 "event bin. Please specify an INTEGRAL/SPI event "
340 int index = bin->
index();
343 int ipar =
m_map[index];
361 double grad = (par->
is_free()) ? value * par->
scale() : 0.0;
376 #if defined(G_NAN_CHECK)
378 std::cout <<
"*** ERROR: GSPIModelDataSpace::eval";
379 std::cout <<
"(index=" << index <<
"):";
380 std::cout <<
" NaN/Inf encountered";
381 std::cout <<
" (value=" << value;
382 std::cout <<
", scale=" <<
scale;
383 std::cout <<
")" << std::endl;
420 const int& offset)
const
428 std::string cls = std::string(
typeid(obs.
events()).name());
429 std::string msg =
"Events of type \""+cls+
"\" are not an INTEGRAL/SPI "
430 "event cube. Please specify an INTEGRAL/SPI observation "
431 "with an event cube as argument.";
437 int nevents = cube->
size();
438 int nmodels = cube->
models();
441 bool grad = ((gradients != NULL) && (npars > 0));
445 if (gradients->
columns() < npars+offset) {
447 " columns in gradient matrix is smaller than "
450 ". Please provide a compatible gradient matrix.";
453 if (gradients->
rows() != nevents) {
455 " rows in gradient matrix differs from number "
457 "observation. Please provide a compatible "
464 double** tmp_values = NULL;
465 int** tmp_rows = NULL;
466 int* tmp_number = NULL;
478 tmp_values =
new double*[npars];
479 tmp_rows =
new int*[npars];
480 tmp_number =
new int[npars];
481 tmp_inx =
new int[npars];
484 for (
int i = 0; i < npars; ++i) {
485 tmp_values[i] = NULL;
492 for (
int k = 0; k < nevents; ++k) {
500 for (
int i = 0; i < npars; ++i) {
501 if (tmp_number[i] > 0) {
502 tmp_values[i] =
new double[tmp_number[i]];
503 tmp_rows[i] =
new int[tmp_number[i]];
522 for (
int i = 0; i < npars; ++i) {
531 for (
int k = 0; k < nevents; ++k) {
552 values[k] = value * par->
value();
558 double gradient = (par->
is_free()) ? value * par->
scale() : 0.0;
562 tmp_values[ipar][tmp_inx[ipar]] = gradient;
563 tmp_rows[ipar][tmp_inx[ipar]] = k;
579 for (
int i = 0; i < npars; ++i) {
580 if (tmp_number[i] > 0) {
581 gradients->
column(i+offset, tmp_values[i], tmp_rows[i], tmp_number[i]);
586 if (tmp_values != NULL) {
587 for (
int i = 0; i < npars; ++i) {
588 if (tmp_values[i] != NULL)
delete [] tmp_values[i];
590 delete [] tmp_values;
594 if (tmp_rows != NULL) {
595 for (
int i = 0; i < npars; ++i) {
596 if (tmp_rows[i] != NULL)
delete [] tmp_rows[i];
602 if (tmp_number != NULL)
delete [] tmp_number;
603 if (tmp_inx != NULL)
delete [] tmp_inx;
626 #if defined(G_DEBUG_SETUP)
627 std::cout <<
"GSPIModelDataSpace::setup_model: ";
628 std::cout <<
"observation pointer differs";
629 std::cout << std::endl;
634 if (spi_obs == NULL) {
635 std::string cls = std::string(
typeid(&obs).
name());
636 std::string msg =
"Observation of type \""+cls+
"\" is not an "
637 "INTEGRAL/SPI observation. Please specify an "
638 "INTEGRAL/SPI observation as argument.";
656 #if defined(G_DEBUG_SETUP)
658 std::cout <<
"GSPIModelDataSpace::setup: ";
659 std::cout <<
"no method defined.";
660 std::cout << std::endl;
667 #if defined(G_DEBUG_SETUP)
669 std::cout <<
"GSPIModelDataSpace::setup: ";
670 std::cout <<
"no valid event cube found in observation";
671 std::cout << std::endl;
695 const GTime& obsTime,
760 int npars = xml.
elements(
"parameter");
763 for (
int i = 0; i < npars; ++i) {
772 parameter.
read(*par);
820 for (
int k = 0; k < n; ++k) {
831 src = xml.
append(
"source");
841 std::string msg =
"Invalid model type \""+src->
attribute(
"type")+
"\" "
842 "found in XML file. Model type \""+
type()+
"\" "
852 for (
int i = 0; i < npars; ++i) {
859 std::string msg =
"Invalid number of parameters "+
861 " found in XML file, but model requires exactly "+
867 for (
int i = 0; i < npars; ++i) {
904 result.append(
"=== GSPIModelDataSpace ===");
914 for (
int i = 0; i <
size(); ++i) {
1014 for (
int i = 0; i < npars; ++i) {
1039 int n_events = cube->
size();
1040 int n_pt = cube->
naxis(0);
1041 int n_det = cube->
naxis(1);
1042 int n_eng = cube->
naxis(2);
1045 std::vector<int> pt_indices(n_pt, 0);
1046 std::vector<int> det_indices(n_det, 0);
1047 std::vector<int> eng_indices(n_eng, 0);
1050 std::vector<std::string> pt_names;
1051 std::vector<std::string> det_names;
1052 std::vector<std::string> eng_names;
1068 int npt = (pt_indices.size() > 0) ? pt_indices[pt_indices.size()-1] + 1 : 0;
1069 int ndet = (det_indices.size() > 0) ? det_indices[det_indices.size()-1] + 1 : 0;
1070 int neng = (eng_indices.size() > 0) ? eng_indices[eng_indices.size()-1] + 1 : 0;
1071 int npars = npt * ndet * neng;
1074 #if defined(G_DEBUG_SETUP_MODEL)
1075 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1076 std::cout <<
"determined pointing, detector and energy indices:" << std::endl;
1077 std::cout <<
"- npt = " << npt << std::endl;
1078 std::cout <<
"- ndet = " << ndet << std::endl;
1079 std::cout <<
"- neng = " << neng << std::endl;
1080 std::cout <<
"- npars = " << npars << std::endl;
1087 std::vector<GModelPar> parameters;
1088 parameters.reserve(npars);
1093 for (
int ipt = 0; ipt < npt; ++ipt) {
1094 for (
int idet = 0; idet < ndet; ++idet) {
1095 for (
int ieng = 0; ieng < neng; ++ieng) {
1098 std::string par_name;
1105 if (eng_names.size() > 0) {
1106 par_name +=
" " + eng_names[ieng];
1108 if (det_names.size() > 0) {
1109 par_name +=
" " + det_names[idet];
1111 if (pt_names.size() > 0) {
1112 par_name +=
" " + pt_names[ipt];
1124 #if defined(G_DEBUG_SETUP_MODEL)
1125 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1126 std::cout <<
"copy old parameter \"";
1127 std::cout <<
m_parameters[i].name() <<
"\"" << std::endl;
1143 parameters.push_back(par);
1144 #if defined(G_DEBUG_SETUP_MODEL)
1145 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1146 std::cout <<
"allocate new parameter \"";
1147 std::cout << par_name <<
"\"" << std::endl;
1162 #if defined(G_DEBUG_SETUP_MODEL)
1163 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1164 std::cout <<
"allocated parameter map:" << std::endl;
1165 std::cout <<
"- n_pt = " << n_pt << std::endl;
1166 std::cout <<
"- n_det = " << n_det << std::endl;
1167 std::cout <<
"- n_eng = " << n_eng << std::endl;
1168 std::cout <<
"- m_map_size = " <<
m_map_size << std::endl;
1172 for (
int i_pt = 0; i_pt < n_pt; ++i_pt) {
1173 for (
int i_det = 0; i_det < n_det; ++i_det) {
1174 for (
int i_eng = 0; i_eng < n_eng; ++i_eng) {
1180 int index = pt_indices[i_pt] * ndet * neng +
1181 det_indices[i_det] * neng +
1185 int i_map = (i_pt * n_det + i_det) * n_eng + i_eng;
1188 m_map[i_map] = index;
1191 #if defined(G_DEBUG_SETUP_MODEL_MAP)
1192 std::cout <<
"- [" << i_pt <<
"," << i_det <<
"," << i_eng <<
"]";
1193 std::cout <<
" = " << index << std::endl;
1210 std::vector<int> n_uses(n_pars, 0);
1211 for (
int i = 0; i < n_events; ++i) {
1213 int ipar =
m_map[i];
1219 for (
int i = 0; i < n_pars; ++i) {
1220 if (n_uses[i] == 0) {
1222 #if defined(G_DEBUG_SETUP_MODEL_FIX)
1223 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1224 std::cout <<
"parameter \"" <<
m_parameters[i].name() <<
"\" ";
1225 std::cout <<
"not used, fix parameter." << std::endl;
1248 std::vector<int>* indices,
1249 std::vector<std::string>* names)
1296 std::vector<int>* indices,
1297 std::vector<std::string>* names)
1328 std::vector<int>* indices,
1329 std::vector<std::string>* names)
1354 std::vector<int>* indices,
1355 std::vector<std::string>* names)
1358 int npt = indices->size();
1361 for (
int ipt = 0; ipt < npt; ++ipt) {
1362 (*indices)[ipt] = ipt;
1381 std::vector<int>* indices,
1382 std::vector<std::string>* names)
1385 std::vector<std::string> orbits;
1388 int npt = indices->size();
1391 for (
int ipt = 0; ipt < npt; ++ipt) {
1397 std::string orbit = cube->
ptid(ipt).substr(0,4);
1401 for (
int i = 0; i < orbits.size(); ++i) {
1402 if (orbit == orbits[i]) {
1410 index = orbits.size();
1411 orbits.push_back(orbit);
1412 names->push_back(
"O" + orbit);
1416 (*indices)[ipt] = index;
1436 std::vector<int>* indices,
1437 std::vector<std::string>* names,
1442 double tstart = cube->
gti().tstart().secs();
1443 double tbin = cube->
gti().telapse();
1444 int nbins = int(tbin / time + 0.5);
1448 tbin /= double(nbins);
1451 int npt = indices->size();
1454 for (
int ipt = 0; ipt < npt; ++ipt) {
1457 double t = 0.5 * (cube->
gti().tstart(ipt).secs() +
1458 cube->
gti().tstop(ipt).secs());
1461 int index = int((t - tstart) / tbin);
1465 else if (index >= nbins) {
1470 (*indices)[ipt] = index;
1475 for (
int index = 0, used_index = 0; index < nbins; ++index) {
1479 for (
int ipt = 0; ipt < npt; ++ipt) {
1480 if ((*indices)[ipt] == index) {
1481 (*indices)[ipt] = used_index;
1516 std::vector<int>* indices,
1517 std::vector<std::string>* names)
1523 for (
int i = 0; i < times.
size(); ++i) {
1543 std::vector<int>* indices,
1544 std::vector<std::string>* names)
1550 for (
int i = 0; i < times.
size(); ++i) {
1569 std::vector<int>* indices,
1570 std::vector<std::string>* names)
1573 int ndet = indices->size();
1576 for (
int idet = 0; idet < ndet; ++idet) {
1577 (*indices)[idet] = idet;
1596 std::vector<int>* indices,
1597 std::vector<std::string>* names)
1600 int ndet = indices->size();
1603 for (
int idet = 0; idet < ndet; ++idet) {
1606 int detid = cube->
dir(0, idet).
detid();
1609 if (detid >=0 && detid < 19) {
1610 (*indices)[idet] = 0;
1612 else if (detid >= 19 && detid < 61) {
1613 (*indices)[idet] = 1;
1615 else if (detid >= 61 && detid < 85) {
1616 (*indices)[idet] = 2;
1618 else if (detid >= 85 && detid < 104) {
1619 (*indices)[idet] = 3;
1621 else if (detid >= 104 && detid < 123) {
1622 (*indices)[idet] = 4;
1624 else if (detid >= 123 && detid < 142) {
1625 (*indices)[idet] = 5;
1631 for (
int index = 0, used_index = 0; index < 6; ++index) {
1635 for (
int idet = 0; idet < ndet; ++idet) {
1636 if ((*indices)[idet] == index) {
1637 (*indices)[idet] = used_index;
1671 std::vector<int>* indices,
1672 std::vector<std::string>* names)
1675 int neng = indices->size();
1678 for (
int ieng = 0; ieng < neng; ++ieng) {
1679 (*indices)[ieng] = ieng;
1700 size_t start = method.find(
"date") + 4;
1704 size_t stop = std::string::npos;
1705 if ((stop = method.find(
"min", start)) != std::string::npos) {
1708 else if ((stop = method.find(
"hour", start)) != std::string::npos) {
1711 else if ((stop = method.find(
"day", start)) != std::string::npos) {
1714 else if ((stop = method.find(
"week", start)) != std::string::npos) {
1717 else if ((stop = method.find(
"month", start)) != std::string::npos) {
1720 else if ((stop = method.find(
"year", start)) != std::string::npos) {
1725 if (stop == std::string::npos) {
1726 std::string msg =
"Method string \""+method+
"\" does not contain "
1727 "time units for \"DATE\" method. Please specify one "
1728 "of the following units: min, hour, day, week, "
1753 std::vector<int>* indices,
1754 std::vector<std::string>* names,
1756 const std::string& reason)
const
1759 #if defined(G_DEBUG_SPLIT_POINTING)
1760 std::cout <<
"GSPIModelDataSpace::split_pointing_indices: ";
1761 std::cout << this->
name() <<
" (" <<
m_method <<
") at ";
1762 std::cout << time.
utc() <<
" due to " << reason << std::endl;
1766 int npt = indices->size();
1773 for (
int ipt = 0; ipt < npt; ++ipt) {
1774 if ((*indices)[ipt] > igrp_last) {
1775 igrp_last = (*indices)[ipt];
1783 int igrp_current = (*indices)[ipt_start];
1784 GTime tstart = cube->
gti().tstart(ipt_start);
1785 GTime tstop = cube->
gti().tstop(ipt_start);
1788 for (
int ipt = 0; ipt < npt; ++ipt) {
1792 if ((*indices)[ipt] == igrp_current) {
1793 tstop = cube->
gti().tstop(ipt);
1802 if (((*indices)[ipt] != igrp_current) || (ipt == npt-1)) {
1806 if ((time > tstart) && (time < tstop)) {
1812 for (
int kpt = ipt_start; kpt <= ipt_stop; ++kpt) {
1813 if (cube->
gti().tstart(kpt) > time) {
1814 (*indices)[kpt] = igrp_last;
1826 if (names->size() == 0) {
1827 names->push_back(
"");
1829 names->push_back(reason);
1832 #if defined(G_DEBUG_SPLIT_POINTING)
1833 std::cout <<
"Insert new pointing group at pointing ";
1834 std::cout << ipt <<
" (" << nnew <<
")" << std::endl;
1843 igrp_current = (*indices)[ipt];
1844 tstart = cube->
gti().tstart(ipt);
1845 tstop = cube->
gti().tstop(ipt);
Exception handler interface definition.
Sparse matrix class definition.
Model parameter class interface definition.
Model registry class definition.
INTEGRAL/SPI event bin class definition.
const GSPIModelDataSpace g_spi_data_space_seed
INTEGRAL/SPI data space model interface definition.
INTEGRAL/SPI observation class definition.
Time container class definition.
Vector class interface definition.
Class that handles energies in a unit independent way.
Abstract interface for the event classes.
void gti(const GGti >i)
Set Good Time Intervals.
const int & rows(void) const
Return number of matrix rows.
const int & columns(void) const
Return number of matrix columns.
Sparse matrix class interface definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Abstract data model class.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
virtual GModelData & operator=(const GModelData &model)
Assignment operator.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Interface definition for the model registry class.
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
std::vector< std::string > m_instruments
Instruments to which model applies.
std::vector< GModelPar * > m_pars
Pointers to all model parameters.
void write_attributes(GXmlElement &xml) const
Write model attributes.
std::string m_name
Model name.
void free_members(void)
Delete class members.
void init_members(void)
Initialise class members.
std::string print_attributes(void) const
Print model attributes.
std::vector< int > m_eval_inx
int size(void) const
Return number of parameters in model.
bool has_par(const std::string &name) const
Checks if parameter name exists.
std::string instruments(void) const
Returns instruments to which model applies.
void read_attributes(const GXmlElement &xml)
Read model attributes.
const std::string & name(void) const
Return parameter name.
Abstract observation base class.
virtual GEvents * events(void)
Return events.
void computed_gradient(const GModel &model, const GModelPar &par) const
Signals that an analytical gradient was computed for a model parameter.
bool is_free(void) const
Signal if parameter is free.
void free(void)
Free a parameter.
const double & scale(void) const
Return parameter scale.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
const double & factor_gradient(void) const
Return parameter factor gradient.
double gradient(void) const
Return parameter gradient.
void clear(void)
Clear parameter.
double value(void) const
Return parameter value.
const std::string & name(void) const
Return parameter name.
Random number generator class.
INTEGRAL/SPI event bin class.
const double & model(const int &index) const
Return model value.
virtual double size(void) const
Return size of event bin.
const int & index(void) const
Return event bin index.
INTEGRAL/SPI event bin container class.
const GSPIInstDir & dir(const int &ipt, const int &idet) const
Return instrument direction.
const double & event_model(const int &ievent, const int &imodel) const
Return value of model for event bin.
const double & event_size(const int &ievent) const
Return size of event bin.
int models(void) const
Return number of models.
virtual int naxis(const int &axis) const
Return number of bins in axis.
virtual int size(void) const
Return event cube size.
const std::string & ptid(const int &ipt) const
Return pointing identifier.
void detid(const int &detid)
Set detector identifier.
INTEGRAL/SPI data space model.
void setup_evtclass(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices and names for "evtclass" method.
virtual void setup(const GObservation &obs) const
Model setup hook.
int * m_map
Parameter map.
void split_pointing_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names, const GTime &time, const std::string &reason) const
Split pointing indices and names at given time.
double get_date_time(const std::string &method) const
Get time scale from method string.
std::string m_method
Fitting method.
void setup_date(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names, const double &time)
Setup pointing indices and names for "date" method.
void init_members(void)
Initialise class members.
void copy_members(const GSPIModelDataSpace &model)
Copy class members.
void add_gedfail(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Modify pointing indices and names for "gedfail" method.
int m_index
Index of model in event bins.
virtual GSPIModelDataSpace & operator=(const GSPIModelDataSpace &model)
Assignment operator.
void setup_detector_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices.
void setup_energy_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup energy indices.
int m_map_size
Size of parameter map.
virtual GSPIModelDataSpace * clone(void) const
Clone instance.
void add_gedanneal(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Modify pointing indices and names for "gedanneal" method.
void setup_dete(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices and names for "dete" method.
void free_members(void)
Delete class members.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print model information.
GSPIModelDataSpace(void)
Void constructor.
void setup_pointing_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices.
void set_pointers(void)
Set pointers.
virtual double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const
Evaluate function.
GSPIObservation * m_obs
SPI observation.
virtual void read(const GXmlElement &xml)
Read model from XML element.
virtual double npred(const GEnergy &obsEng, const GTime &obsTime, const GObservation &obs) const
Return spatially integrated data model.
virtual std::string type(void) const
Return model type.
virtual void clear(void)
Clear instance.
void setup_ebin(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup energy indices and names for "ebin" method.
std::vector< GModelPar > m_parameters
Model parameters.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual GSPIEventCube * mc(const GObservation &obs, GRan &ran) const
Return simulated list of events.
void setup_orbit(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices and names for "orbit" method.
void setup_point(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices and names for "point" method.
void setup_pars(GSPIEventCube *cube)
Setup parameters.
virtual ~GSPIModelDataSpace(void)
Destructor.
INTEGRAL/SPI observation class.
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
int size(void) const
Return number of times.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
virtual GXmlNode * append(const GXmlNode &node)
Append XML child node.
virtual GXmlElement * element(const int &index)
Return pointer to GXMLElement child.
virtual int elements(void) const
Return number of GXMLElement children of node.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
bool is_infinite(const double &x)
Signal if argument is infinite.
bool is_notanumber(const double &x)
Signal if argument is not a number.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
double todouble(const std::string &arg)
Convert string into double precision value.
std::string tolower(const std::string &s)
Convert string to lower case.
int toint(const std::string &arg)
Convert string into integer value.
bool contains(const std::string &str, const std::string &substring)
Checks if a substring is in a string.
GTimes spi_annealing_start_times(void)
Return start time of annealing operations.
GTimes spi_gedfail_times(void)
Return times of detector failures.