46 const GModelRegistry g_spi_data_space_registry(&g_spi_data_space_seed);
49 #define G_CONSTRUCTOR "GSPIModelDataSpace::GSPIModelDataSpace("\
50 "GSPIObservation&, std::string&, std::string&, int&)"
51 #define G_EVAL "GSPIModelDataSpace::eval(GEvent&, GObservation&, bool&)"
52 #define G_READ "GSPIModelDataSpace::read(GXmlElement&)"
53 #define G_WRITE "GSPIModelDataSpace::write(GXmlElement&)"
54 #define G_SETUP_MODEL "GSPIModelDataSpace::setup_model(Observation&)"
55 #define G_GET_DATE_TIME "GSPIModelDataSpace::get_date_time(std::string&)"
106 const std::string& name,
107 const std::string& method,
126 int num_models = cube->
models();
127 if (index < 0 || index >= num_models) {
219 if (
this != &model) {
301 const bool& gradients)
const
310 std::string cls = std::string(
typeid(&event).
name());
311 std::string msg =
"Event of type \""+cls+
"\" is not an INTEGRAL/SPI "
312 "event bin. Please specify an INTEGRAL/SPI event "
333 int index = bin->
index();
336 int ipar =
m_map[index];
353 double grad = (par->
is_free()) ? value * par->
scale() : 0.0;
368 #if defined(G_NAN_CHECK)
370 std::cout <<
"*** ERROR: GSPIModelDataSpace::eval";
371 std::cout <<
"(index=" << index <<
"):";
372 std::cout <<
" NaN/Inf encountered";
373 std::cout <<
" (value=" << value;
374 std::cout <<
", scale=" <<
scale;
375 std::cout <<
")" << std::endl;
401 const GTime& obsTime,
460 int npars = xml.
elements(
"parameter");
463 for (
int i = 0; i < npars; ++i) {
472 parameter.
read(*par);
517 for (
int k = 0; k < n; ++k) {
528 src = xml.
append(
"source");
538 std::string msg =
"Invalid model type \""+src->
attribute(
"type")+
"\" "
539 "found in XML file. Model type \""+
type()+
"\" "
549 for (
int i = 0; i < npars; ++i) {
556 std::string msg =
"Invalid number of parameters "+
558 " found in XML file, but model requires exactly "+
564 for (
int i = 0; i < npars; ++i) {
601 result.append(
"=== GSPIModelDataSpace ===");
611 for (
int i = 0; i <
size(); ++i) {
711 for (
int i = 0; i < npars; ++i) {
733 #if defined(G_DEBUG_SETUP_MODEL)
734 std::cout <<
"GSPIModelDataSpace::setup_model: ";
735 std::cout <<
"observation pointer differs";
736 std::cout << std::endl;
741 if (spi_obs == NULL) {
742 std::string cls = std::string(
typeid(&obs).
name());
743 std::string msg =
"Observation of type \""+cls+
"\" is not an "
744 "INTEGRAL/SPI observation. Please specify an "
745 "INTEGRAL/SPI observation as argument.";
758 if (
m_index >= 0 && m_index < cube->models()) {
766 #if defined(G_DEBUG_SETUP_MODEL)
768 std::cout <<
"GSPIModelDataSpace::setup_model: ";
769 std::cout <<
"no method defined.";
770 std::cout << std::endl;
777 #if defined(G_DEBUG_SETUP_MODEL)
779 std::cout <<
"GSPIModelDataSpace::setup_model: ";
780 std::cout <<
"model index " <<
m_index <<
" is not in valid ";
781 std::cout <<
"range.";
782 std::cout << std::endl;
789 #if defined(G_DEBUG_SETUP_MODEL)
791 std::cout <<
"GSPIModelDataSpace::setup_model: ";
792 std::cout <<
"no valid event cube found in observation";
793 std::cout << std::endl;
820 int n_pt = cube->
naxis(0);
821 int n_det = cube->
naxis(1);
822 int n_eng = cube->
naxis(2);
825 std::vector<int> pt_indices(n_pt, 0);
826 std::vector<int> det_indices(n_det, 0);
827 std::vector<int> eng_indices(n_eng, 0);
830 std::vector<std::string> pt_names;
831 std::vector<std::string> det_names;
832 std::vector<std::string> eng_names;
848 int npt = (pt_indices.size() > 0) ? pt_indices[pt_indices.size()-1] + 1 : 0;
849 int ndet = (det_indices.size() > 0) ? det_indices[det_indices.size()-1] + 1 : 0;
850 int neng = (eng_indices.size() > 0) ? eng_indices[eng_indices.size()-1] + 1 : 0;
851 int npars = npt * ndet * neng;
854 #if defined(G_DEBUG_SETUP_MODEL)
855 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
856 std::cout <<
"determined pointing, detector and energy indices:" << std::endl;
857 std::cout <<
"- npt = " << npt << std::endl;
858 std::cout <<
"- ndet = " << ndet << std::endl;
859 std::cout <<
"- neng = " << neng << std::endl;
860 std::cout <<
"- npars = " << npars << std::endl;
867 std::vector<GModelPar> parameters;
868 parameters.reserve(npars);
873 for (
int ipt = 0; ipt < npt; ++ipt) {
874 for (
int idet = 0; idet < ndet; ++idet) {
875 for (
int ieng = 0; ieng < neng; ++ieng) {
878 std::string par_name;
885 if (eng_names.size() > 0) {
886 par_name +=
" " + eng_names[ieng];
888 if (det_names.size() > 0) {
889 par_name +=
" " + det_names[idet];
891 if (pt_names.size() > 0) {
892 par_name +=
" " + pt_names[ipt];
904 #if defined(G_DEBUG_SETUP_MODEL)
905 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
906 std::cout <<
"copy old parameter \"";
907 std::cout <<
m_parameters[i].name() <<
"\"" << std::endl;
923 parameters.push_back(par);
924 #if defined(G_DEBUG_SETUP_MODEL)
925 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
926 std::cout <<
"allocate new parameter \"";
927 std::cout << par_name <<
"\"" << std::endl;
942 #if defined(G_DEBUG_SETUP_MODEL)
943 std::cout <<
"GSPIModelDataSpace::setup_pars: ";
944 std::cout <<
"allocated parameter map:" << std::endl;
945 std::cout <<
"- n_pt = " << n_pt << std::endl;
946 std::cout <<
"- n_det = " << n_det << std::endl;
947 std::cout <<
"- n_eng = " << n_eng << std::endl;
948 std::cout <<
"- m_map_size = " <<
m_map_size << std::endl;
952 for (
int i_pt = 0; i_pt < n_pt; ++i_pt) {
953 for (
int i_det = 0; i_det < n_det; ++i_det) {
954 for (
int i_eng = 0; i_eng < n_eng; ++i_eng) {
960 int index = pt_indices[i_pt] * ndet * neng +
961 det_indices[i_det] * neng +
965 int i_map = (i_pt * n_det + i_det) * n_eng + i_eng;
968 m_map[i_map] = index;
971 #if defined(G_DEBUG_SETUP_MODEL_MAP)
972 std::cout <<
"- [" << i_pt <<
"," << i_det <<
"," << i_eng <<
"]";
973 std::cout <<
" = " << index << std::endl;
1004 std::vector<int>* indices,
1005 std::vector<std::string>* names)
1052 std::vector<int>* indices,
1053 std::vector<std::string>* names)
1084 std::vector<int>* indices,
1085 std::vector<std::string>* names)
1110 std::vector<int>* indices,
1111 std::vector<std::string>* names)
1114 int npt = indices->size();
1117 for (
int ipt = 0; ipt < npt; ++ipt) {
1118 (*indices)[ipt] = ipt;
1137 std::vector<int>* indices,
1138 std::vector<std::string>* names)
1141 std::vector<std::string> orbits;
1144 int npt = indices->size();
1147 for (
int ipt = 0; ipt < npt; ++ipt) {
1153 std::string orbit = cube->
ptid(ipt).substr(0,4);
1157 for (
int i = 0; i < orbits.size(); ++i) {
1158 if (orbit == orbits[i]) {
1166 index = orbits.size();
1167 orbits.push_back(orbit);
1168 names->push_back(
"O" + orbit);
1172 (*indices)[ipt] = index;
1192 std::vector<int>* indices,
1193 std::vector<std::string>* names,
1198 double tstart = cube->
gti().tstart().secs();
1199 double tbin = cube->
gti().telapse();
1200 int nbins = int(tbin / time + 0.5);
1204 tbin /= double(nbins);
1207 int npt = indices->size();
1210 for (
int ipt = 0; ipt < npt; ++ipt) {
1213 double t = 0.5 * (cube->
gti().tstart(ipt).secs() +
1214 cube->
gti().tstop(ipt).secs());
1217 int index = int((t - tstart) / tbin);
1221 else if (index >= nbins) {
1226 (*indices)[ipt] = index;
1231 for (
int index = 0, used_index = 0; index < nbins; ++index) {
1235 for (
int ipt = 0; ipt < npt; ++ipt) {
1236 if ((*indices)[ipt] == index) {
1237 (*indices)[ipt] = used_index;
1272 std::vector<int>* indices,
1273 std::vector<std::string>* names)
1279 for (
int i = 0; i < times.
size(); ++i) {
1299 std::vector<int>* indices,
1300 std::vector<std::string>* names)
1306 for (
int i = 0; i < times.
size(); ++i) {
1325 std::vector<int>* indices,
1326 std::vector<std::string>* names)
1329 int ndet = indices->size();
1332 for (
int idet = 0; idet < ndet; ++idet) {
1333 (*indices)[idet] = idet;
1352 std::vector<int>* indices,
1353 std::vector<std::string>* names)
1356 int ndet = indices->size();
1359 for (
int idet = 0; idet < ndet; ++idet) {
1362 int detid = cube->
dir(0, idet).
detid();
1365 if (detid >=0 && detid < 19) {
1366 (*indices)[idet] = 0;
1368 else if (detid >= 19 && detid < 61) {
1369 (*indices)[idet] = 1;
1371 else if (detid >= 61 && detid < 85) {
1372 (*indices)[idet] = 2;
1374 else if (detid >= 85 && detid < 104) {
1375 (*indices)[idet] = 3;
1377 else if (detid >= 104 && detid < 123) {
1378 (*indices)[idet] = 4;
1380 else if (detid >= 123 && detid < 142) {
1381 (*indices)[idet] = 5;
1387 for (
int index = 0, used_index = 0; index < 6; ++index) {
1391 for (
int idet = 0; idet < ndet; ++idet) {
1392 if ((*indices)[idet] == index) {
1393 (*indices)[idet] = used_index;
1427 std::vector<int>* indices,
1428 std::vector<std::string>* names)
1431 int neng = indices->size();
1434 for (
int ieng = 0; ieng < neng; ++ieng) {
1435 (*indices)[ieng] = ieng;
1456 size_t start = method.find(
"date") + 4;
1460 size_t stop = std::string::npos;
1461 if ((stop = method.find(
"min", start)) != std::string::npos) {
1464 else if ((stop = method.find(
"hour", start)) != std::string::npos) {
1467 else if ((stop = method.find(
"day", start)) != std::string::npos) {
1470 else if ((stop = method.find(
"week", start)) != std::string::npos) {
1473 else if ((stop = method.find(
"month", start)) != std::string::npos) {
1476 else if ((stop = method.find(
"year", start)) != std::string::npos) {
1481 if (stop == std::string::npos) {
1482 std::string msg =
"Method string \""+method+
"\" does not contain "
1483 "time units for \"DATE\" method. Please specify one "
1484 "of the following units: min, hour, day, week, "
1509 std::vector<int>* indices,
1510 std::vector<std::string>* names,
1512 const std::string& reason)
const
1515 int npt = indices->size();
1522 for (
int ipt = 0; ipt < npt; ++ipt) {
1523 if ((*indices)[ipt] > igrp_last) {
1532 int igrp_current = (*indices)[ipt_start];
1533 GTime tstart = cube->
gti().tstart(ipt_start);
1534 GTime tstop = cube->
gti().tstop(ipt_start);
1537 for (
int ipt = 0; ipt < npt; ++ipt) {
1541 if ((*indices)[ipt] == igrp_current) {
1542 tstop = cube->
gti().tstop(ipt);
1551 if (((*indices)[ipt] != igrp_current) || (ipt == npt-1)) {
1555 if ((time > tstart) && (time < tstop)) {
1561 for (
int kpt = ipt_start; kpt <= ipt_stop; ++kpt) {
1562 if (cube->
gti().tstart(kpt) > time) {
1563 (*indices)[kpt] = igrp_last;
1573 names->push_back((*names)[igrp_current]+
"-"+reason);
1581 igrp_current = (*indices)[ipt];
1582 tstart = cube->
gti().tstart(ipt);
1583 tstop = cube->
gti().tstop(ipt);
void setup_orbit(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices and names for "orbit" method.
GTimes spi_gedfail_times(void)
Return times of detector failures.
bool contains(const std::string &str, const std::string &substring)
Checks if a substring is in a string.
int * m_map
Parameter map.
void free_members(void)
Delete class members.
int m_map_size
Size of parameter map.
const double & factor_gradient(void) const
Return parameter factor gradient.
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.
int size(void) const
Return number of times.
const std::string & name(void) const
Return parameter name.
void setup_pointing_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices.
std::string print_attributes(void) const
Print model attributes.
Interface definition for the model registry class.
double gradient(void) const
Return parameter gradient.
const GSPIInstDir & dir(const int &ipt, const int &idet) const
Return instrument direction.
virtual GSPIModelDataSpace * clone(void) const
Clone instance.
void setup_point(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup pointing indices and names for "point" method.
std::vector< GModelPar > m_parameters
Model parameters.
Abstract interface for the event classes.
void gti(const GGti >i)
Set Good Time Intervals.
void free_members(void)
Delete class members.
void setup_energy_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup energy indices.
Random number generator class.
virtual int elements(void) const
Return number of GXMLElement children of node.
Time container class definition.
std::string m_method
Fitting method.
void detid(const int &detid)
Set detector identifier.
const int & index(void) const
Return event bin index.
void add_gedanneal(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Modify pointing indices and names for "gedanneal" method.
GTimes spi_annealing_start_times(void)
Return start time of annealing operations.
virtual GSPIEventCube * mc(const GObservation &obs, GRan &ran) const
Return simulated list of events.
bool is_free(void) const
Signal if parameter is free.
bool has_par(const std::string &name) const
Checks if parameter name exists.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual double size(void) const
Return size of event bin.
virtual void read(const GXmlElement &xml)
Read model from XML element.
bool is_notanumber(const double &x)
Signal if argument is not a number.
std::vector< GModelPar * > m_pars
Pointers to all model parameters.
Model parameter class interface definition.
int models(void) const
Return number of models.
int size(void) const
Return number of parameters in model.
const double & scale(void) const
Return parameter scale.
bool is_infinite(const double &x)
Signal if argument is infinite.
GSPIObservation * m_obs
SPI observation.
void init_members(void)
Initialise class members.
INTEGRAL/SPI event bin container class.
void add_gedfail(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Modify pointing indices and names for "gedfail" method.
std::vector< std::string > m_instruments
Instruments to which model applies.
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
INTEGRAL/SPI data space model interface definition.
const GXmlAttribute * attribute(const int &index) const
Return attribute.
INTEGRAL/SPI data space model.
const std::string & name(void) const
Return parameter name.
void free(void)
Free a parameter.
virtual GSPIModelDataSpace & operator=(const GSPIModelDataSpace &model)
Assignment operator.
virtual std::string type(void) const
Return model type.
void clear(void)
Clear parameter.
GSPIModelDataSpace(void)
Void constructor.
Abstract data model class.
virtual GModelData & operator=(const GModelData &model)
Assignment operator.
std::string instruments(void) const
Returns instruments to which model applies.
double get_date_time(const std::string &method) const
Get time scale from method string.
virtual double npred(const GEnergy &obsEng, const GTime &obsTime, const GObservation &obs) const
Return spatially integrated data model.
void copy_members(const GSPIModelDataSpace &model)
Copy class members.
Abstract observation base class.
void free_members(void)
Delete class members.
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
void write_attributes(GXmlElement &xml) const
Write model attributes.
void init_members(void)
Initialise class members.
INTEGRAL/SPI observation class.
void setup_model(const GObservation &obs) const
Setup model.
int m_index
Index of model in event bins.
const std::string & ptid(const int &ipt) const
Return pointing identifier.
const GSPIModelDataSpace g_spi_data_space_seed
virtual ~GSPIModelDataSpace(void)
Destructor.
void setup_ebin(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup energy indices and names for "ebin" method.
virtual GXmlElement * element(const int &index)
Return pointer to GXMLElement child.
std::string m_name
Model name.
INTEGRAL/SPI observation class definition.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
double value(void) const
Return parameter value.
virtual double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const
Evaluate function.
virtual GEvents * events(void)
Return events.
INTEGRAL/SPI event bin class.
Exception handler interface definition.
std::string tolower(const std::string &s)
Convert string to lower case.
std::vector< int > m_eval_inx
void read_attributes(const GXmlElement &xml)
Read model attributes.
virtual int naxis(const int &axis) const
Return number of bins in axis.
int toint(const std::string &arg)
Convert string into integer value.
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.
Model registry class definition.
void setup_evtclass(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices and names for "evtclass" method.
virtual GXmlNode * append(const GXmlNode &node)
Append XML child node.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
const double & model(const int &index) const
Return model value.
INTEGRAL/SPI event bin class definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print model information.
void setup_pars(GSPIEventCube *cube)
Setup parameters.
void setup_dete(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices and names for "dete" method.
void set_pointers(void)
Set pointers.
void init_members(void)
Initialise class members.
virtual void clear(void)
Clear instance.
double todouble(const std::string &arg)
Convert string into double precision value.
Class that handles energies in a unit independent way.
void setup_detector_indices(GSPIEventCube *cube, std::vector< int > *indices, std::vector< std::string > *names)
Setup detector indices.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.