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_SETUP "GSPIModelDataSpace::setup(Observation&)"
56#define G_READ "GSPIModelDataSpace::read(GXmlElement&)"
57#define G_WRITE "GSPIModelDataSpace::write(GXmlElement&)"
58#define G_GET_DATE_TIME "GSPIModelDataSpace::get_date_time(std::string&)"
111 const std::string& name,
112 const std::string& method,
131 int num_models = cube->
models();
132 if (index < 0 || index >= num_models) {
224 if (
this != &model) {
306 const bool& gradients)
const
315 std::string cls = std::string(
typeid(&event).
name());
316 std::string msg =
"Event of type \""+cls+
"\" is not an INTEGRAL/SPI "
317 "event bin. Please specify an INTEGRAL/SPI event "
338 int index = bin->
index();
341 int ipar =
m_map[index];
358 double grad = (par->
is_free()) ? value * par->
scale() : 0.0;
373 #if defined(G_NAN_CHECK)
375 std::cout <<
"*** ERROR: GSPIModelDataSpace::eval";
376 std::cout <<
"(index=" << index <<
"):";
377 std::cout <<
" NaN/Inf encountered";
378 std::cout <<
" (value=" << value;
379 std::cout <<
", scale=" <<
scale;
380 std::cout <<
")" << std::endl;
417 const int& offset)
const
425 std::string cls = std::string(
typeid(obs.
events()).name());
426 std::string msg =
"Events of type \""+cls+
"\" are not an INTEGRAL/SPI "
427 "event cube. Please specify an INTEGRAL/SPI observation "
428 "with an event cube as argument.";
434 int nevents = cube->
size();
435 int nmodels = cube->
models();
438 bool grad = ((gradients != NULL) && (npars > 0));
442 if (gradients->
columns() < npars+offset) {
444 " columns in gradient matrix is smaller than "
447 ". Please provide a compatible gradient matrix.";
450 if (gradients->
rows() != nevents) {
452 " rows in gradient matrix differs from number "
454 "observation. Please provide a compatible "
461 double** tmp_values = NULL;
462 int** tmp_rows = NULL;
463 int* tmp_number = NULL;
475 tmp_values =
new double*[npars];
476 tmp_rows =
new int*[npars];
477 tmp_number =
new int[npars];
478 tmp_inx =
new int[npars];
481 for (
int i = 0; i < npars; ++i) {
482 tmp_values[i] = NULL;
489 for (
int k = 0; k < nevents; ++k) {
497 for (
int i = 0; i < npars; ++i) {
498 if (tmp_number[i] > 0) {
499 tmp_values[i] =
new double[tmp_number[i]];
500 tmp_rows[i] =
new int[tmp_number[i]];
516 for (
int i = 0; i < npars; ++i) {
525 for (
int k = 0; k < nevents; ++k) {
545 values[k] = value * par->
value();
551 double gradient = (par->
is_free()) ? value * par->
scale() : 0.0;
555 tmp_values[ipar][tmp_inx[ipar]] = gradient;
556 tmp_rows[ipar][tmp_inx[ipar]] = k;
572 for (
int i = 0; i < npars; ++i) {
573 if (tmp_number[i] > 0) {
574 gradients->
column(i+offset, tmp_values[i], tmp_rows[i], tmp_number[i]);
579 if (tmp_values != NULL) {
580 for (
int i = 0; i < npars; ++i) {
581 if (tmp_values[i] != NULL)
delete [] tmp_values[i];
583 delete [] tmp_values;
587 if (tmp_rows != NULL) {
588 for (
int i = 0; i < npars; ++i) {
589 if (tmp_rows[i] != NULL)
delete [] tmp_rows[i];
595 if (tmp_number != NULL)
delete [] tmp_number;
596 if (tmp_inx != NULL)
delete [] tmp_inx;
618 #if defined(G_DEBUG_SETUP)
619 std::cout <<
"GSPIModelDataSpace::setup_model: ";
620 std::cout <<
"observation pointer differs";
621 std::cout << std::endl;
626 if (spi_obs == NULL) {
627 std::string cls = std::string(
typeid(&obs).
name());
628 std::string msg =
"Observation of type \""+cls+
"\" is not an "
629 "INTEGRAL/SPI observation. Please specify an "
630 "INTEGRAL/SPI observation as argument.";
651 #if defined(G_DEBUG_SETUP)
653 std::cout <<
"GSPIModelDataSpace::setup: ";
654 std::cout <<
"no method defined.";
655 std::cout << std::endl;
662 #if defined(G_DEBUG_SETUP)
664 std::cout <<
"GSPIModelDataSpace::setup: ";
665 std::cout <<
"model index " <<
m_index <<
" is not in valid ";
666 std::cout <<
"range.";
667 std::cout << std::endl;
674 #if defined(G_DEBUG_SETUP)
676 std::cout <<
"GSPIModelDataSpace::setup: ";
677 std::cout <<
"no valid event cube found in observation";
678 std::cout << std::endl;
704 const GTime& obsTime,
763 int npars = xml.
elements(
"parameter");
766 for (
int i = 0; i < npars; ++i) {
775 parameter.
read(*par);
823 for (
int k = 0; k < n; ++k) {
834 src = xml.
append(
"source");
844 std::string msg =
"Invalid model type \""+src->
attribute(
"type")+
"\" "
845 "found in XML file. Model type \""+
type()+
"\" "
855 for (
int i = 0; i < npars; ++i) {
862 std::string msg =
"Invalid number of parameters "+
864 " found in XML file, but model requires exactly "+
870 for (
int i = 0; i < npars; ++i) {
907 result.append(
"=== GSPIModelDataSpace ===");
917 for (
int i = 0; i <
size(); ++i) {
1017 for (
int i = 0; i < npars; ++i) {
1042 int n_events = cube->
size();
1043 int n_pt = cube->
naxis(0);
1044 int n_det = cube->
naxis(1);
1045 int n_eng = cube->
naxis(2);
1048 std::vector<int> pt_indices(n_pt, 0);
1049 std::vector<int> det_indices(n_det, 0);
1050 std::vector<int> eng_indices(n_eng, 0);
1053 std::vector<std::string> pt_names;
1054 std::vector<std::string> det_names;
1055 std::vector<std::string> eng_names;
1071 int npt = (pt_indices.size() > 0) ? pt_indices[pt_indices.size()-1] + 1 : 0;
1072 int ndet = (det_indices.size() > 0) ? det_indices[det_indices.size()-1] + 1 : 0;
1073 int neng = (eng_indices.size() > 0) ? eng_indices[eng_indices.size()-1] + 1 : 0;
1074 int npars = npt * ndet * neng;
1077 #if defined(G_DEBUG_SETUP_MODEL)
1078 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1079 std::cout <<
"determined pointing, detector and energy indices:" << std::endl;
1080 std::cout <<
"- npt = " << npt << std::endl;
1081 std::cout <<
"- ndet = " << ndet << std::endl;
1082 std::cout <<
"- neng = " << neng << std::endl;
1083 std::cout <<
"- npars = " << npars << std::endl;
1090 std::vector<GModelPar> parameters;
1091 parameters.reserve(npars);
1096 for (
int ipt = 0; ipt < npt; ++ipt) {
1097 for (
int idet = 0; idet < ndet; ++idet) {
1098 for (
int ieng = 0; ieng < neng; ++ieng) {
1101 std::string par_name;
1108 if (eng_names.size() > 0) {
1109 par_name +=
" " + eng_names[ieng];
1111 if (det_names.size() > 0) {
1112 par_name +=
" " + det_names[idet];
1114 if (pt_names.size() > 0) {
1115 par_name +=
" " + pt_names[ipt];
1127 #if defined(G_DEBUG_SETUP_MODEL)
1128 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1129 std::cout <<
"copy old parameter \"";
1130 std::cout <<
m_parameters[i].name() <<
"\"" << std::endl;
1146 parameters.push_back(par);
1147 #if defined(G_DEBUG_SETUP_MODEL)
1148 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1149 std::cout <<
"allocate new parameter \"";
1150 std::cout << par_name <<
"\"" << std::endl;
1165 #if defined(G_DEBUG_SETUP_MODEL)
1166 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1167 std::cout <<
"allocated parameter map:" << std::endl;
1168 std::cout <<
"- n_pt = " << n_pt << std::endl;
1169 std::cout <<
"- n_det = " << n_det << std::endl;
1170 std::cout <<
"- n_eng = " << n_eng << std::endl;
1171 std::cout <<
"- m_map_size = " <<
m_map_size << std::endl;
1175 for (
int i_pt = 0; i_pt < n_pt; ++i_pt) {
1176 for (
int i_det = 0; i_det < n_det; ++i_det) {
1177 for (
int i_eng = 0; i_eng < n_eng; ++i_eng) {
1183 int index = pt_indices[i_pt] * ndet * neng +
1184 det_indices[i_det] * neng +
1188 int i_map = (i_pt * n_det + i_det) * n_eng + i_eng;
1191 m_map[i_map] = index;
1194 #if defined(G_DEBUG_SETUP_MODEL_MAP)
1195 std::cout <<
"- [" << i_pt <<
"," << i_det <<
"," << i_eng <<
"]";
1196 std::cout <<
" = " << index << std::endl;
1213 std::vector<int> n_uses(n_pars, 0);
1214 for (
int i = 0; i < n_events; ++i) {
1216 int ipar =
m_map[i];
1222 for (
int i = 0; i < n_pars; ++i) {
1223 if (n_uses[i] == 0) {
1225 #if defined(G_DEBUG_SETUP_MODEL_FIX)
1226 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
1227 std::cout <<
"parameter \"" <<
m_parameters[i].name() <<
"\" ";
1228 std::cout <<
"not used, fix parameter." << std::endl;
1251 std::vector<int>* indices,
1252 std::vector<std::string>* names)
1299 std::vector<int>* indices,
1300 std::vector<std::string>* names)
1331 std::vector<int>* indices,
1332 std::vector<std::string>* names)
1357 std::vector<int>* indices,
1358 std::vector<std::string>* names)
1361 int npt = indices->size();
1364 for (
int ipt = 0; ipt < npt; ++ipt) {
1365 (*indices)[ipt] = ipt;
1384 std::vector<int>* indices,
1385 std::vector<std::string>* names)
1388 std::vector<std::string> orbits;
1391 int npt = indices->size();
1394 for (
int ipt = 0; ipt < npt; ++ipt) {
1400 std::string orbit = cube->
ptid(ipt).substr(0,4);
1404 for (
int i = 0; i < orbits.size(); ++i) {
1405 if (orbit == orbits[i]) {
1413 index = orbits.size();
1414 orbits.push_back(orbit);
1415 names->push_back(
"O" + orbit);
1419 (*indices)[ipt] = index;
1439 std::vector<int>* indices,
1440 std::vector<std::string>* names,
1445 double tstart = cube->
gti().tstart().secs();
1446 double tbin = cube->
gti().telapse();
1447 int nbins = int(tbin / time + 0.5);
1451 tbin /= double(nbins);
1454 int npt = indices->size();
1457 for (
int ipt = 0; ipt < npt; ++ipt) {
1460 double t = 0.5 * (cube->
gti().tstart(ipt).secs() +
1461 cube->
gti().tstop(ipt).secs());
1464 int index = int((t - tstart) / tbin);
1468 else if (index >= nbins) {
1473 (*indices)[ipt] = index;
1478 for (
int index = 0, used_index = 0; index < nbins; ++index) {
1482 for (
int ipt = 0; ipt < npt; ++ipt) {
1483 if ((*indices)[ipt] == index) {
1484 (*indices)[ipt] = used_index;
1519 std::vector<int>* indices,
1520 std::vector<std::string>* names)
1526 for (
int i = 0; i < times.
size(); ++i) {
1546 std::vector<int>* indices,
1547 std::vector<std::string>* names)
1553 for (
int i = 0; i < times.
size(); ++i) {
1572 std::vector<int>* indices,
1573 std::vector<std::string>* names)
1576 int ndet = indices->size();
1579 for (
int idet = 0; idet < ndet; ++idet) {
1580 (*indices)[idet] = idet;
1599 std::vector<int>* indices,
1600 std::vector<std::string>* names)
1603 int ndet = indices->size();
1606 for (
int idet = 0; idet < ndet; ++idet) {
1609 int detid = cube->
dir(0, idet).
detid();
1612 if (detid >=0 && detid < 19) {
1613 (*indices)[idet] = 0;
1615 else if (detid >= 19 && detid < 61) {
1616 (*indices)[idet] = 1;
1618 else if (detid >= 61 && detid < 85) {
1619 (*indices)[idet] = 2;
1621 else if (detid >= 85 && detid < 104) {
1622 (*indices)[idet] = 3;
1624 else if (detid >= 104 && detid < 123) {
1625 (*indices)[idet] = 4;
1627 else if (detid >= 123 && detid < 142) {
1628 (*indices)[idet] = 5;
1634 for (
int index = 0, used_index = 0; index < 6; ++index) {
1638 for (
int idet = 0; idet < ndet; ++idet) {
1639 if ((*indices)[idet] == index) {
1640 (*indices)[idet] = used_index;
1674 std::vector<int>* indices,
1675 std::vector<std::string>* names)
1678 int neng = indices->size();
1681 for (
int ieng = 0; ieng < neng; ++ieng) {
1682 (*indices)[ieng] = ieng;
1703 size_t start = method.find(
"date") + 4;
1707 size_t stop = std::string::npos;
1708 if ((stop = method.find(
"min", start)) != std::string::npos) {
1711 else if ((stop = method.find(
"hour", start)) != std::string::npos) {
1714 else if ((stop = method.find(
"day", start)) != std::string::npos) {
1717 else if ((stop = method.find(
"week", start)) != std::string::npos) {
1720 else if ((stop = method.find(
"month", start)) != std::string::npos) {
1723 else if ((stop = method.find(
"year", start)) != std::string::npos) {
1728 if (stop == std::string::npos) {
1729 std::string msg =
"Method string \""+method+
"\" does not contain "
1730 "time units for \"DATE\" method. Please specify one "
1731 "of the following units: min, hour, day, week, "
1756 std::vector<int>* indices,
1757 std::vector<std::string>* names,
1759 const std::string& reason)
const
1762 #if defined(G_DEBUG_SPLIT_POINTING)
1763 std::cout <<
"GSPIModelDataSpace::split_pointing_indices: ";
1764 std::cout << time.
utc() <<
" " << reason << std::endl;
1768 int npt = indices->size();
1775 for (
int ipt = 0; ipt < npt; ++ipt) {
1776 if ((*indices)[ipt] > igrp_last) {
1777 igrp_last = (*indices)[ipt];
1785 int igrp_current = (*indices)[ipt_start];
1786 GTime tstart = cube->
gti().tstart(ipt_start);
1787 GTime tstop = cube->
gti().tstop(ipt_start);
1790 for (
int ipt = 0; ipt < npt; ++ipt) {
1794 if ((*indices)[ipt] == igrp_current) {
1795 tstop = cube->
gti().tstop(ipt);
1804 if (((*indices)[ipt] != igrp_current) || (ipt == npt-1)) {
1808 if ((time > tstart) && (time < tstop)) {
1814 for (
int kpt = ipt_start; kpt <= ipt_stop; ++kpt) {
1815 if (cube->
gti().tstart(kpt) > time) {
1816 (*indices)[kpt] = igrp_last;
1828 if (names->size() == 0) {
1829 names->push_back(
"");
1831 names->push_back(reason);
1839 igrp_current = (*indices)[ipt];
1840 tstart = cube->
gti().tstart(ipt);
1841 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.