00001 #include "GainVoltPmtParameters.h"
00002 #include <math.h>
00003
00004 int GainVoltPmtParameters::ioMode=2;
00005
00008 double GainVoltPmtParameters::_defaultA = 2.56e-29;
00009 double GainVoltPmtParameters::_defaultB = 10.71;
00010
00011 GainVoltPmtParameters::GainVoltPmtParameters()
00012 {}
00013
00014 GainVoltPmtParameters::GainVoltPmtParameters(double refVoltage, double gains[5])
00015 {
00016 set(refVoltage,gains);
00017 }
00018
00019 GainVoltPmtParameters::GainVoltPmtParameters(int n, double volts[], double gains[])
00020 {
00021 set(n,volts,gains);
00022 }
00023
00024 GainVoltPmtParameters::GainVoltPmtParameters(const GainVoltPmtParameters& p)
00025 {}
00026
00027 GainVoltPmtParameters::~GainVoltPmtParameters()
00028 {}
00029
00030 GainVoltPmtParameters & GainVoltPmtParameters::operator=(const GainVoltPmtParameters&p)
00031 {
00032 _id = p._id;
00033 _a = p._a;
00034 _b = p._b;
00035 _data = p._data;
00036 return *this;
00037 }
00038
00042 void GainVoltPmtParameters::set(double refVolt, double gains[5])
00043 {
00044 if (refVolt<=100)
00045 throw runtime_error("GainVoltPmtParameters::set(double,int,double[]) - F - invalid reference voltage");
00046 _data[refVolt-100] = gains[0];
00047 _data[refVolt- 50] = gains[1];
00048 _data[refVolt ] = gains[2];
00049 _data[refVolt- 50] = gains[3];
00050 _data[refVolt+100] = gains[4];
00051 }
00052
00057 void GainVoltPmtParameters::set(int n, double volts[], double gains[])
00058 {
00059 if (n<=0)
00060 throw runtime_error("GainVoltPmtParameters::set() - F - invalid number of points");
00061 for (int i=0;i<n;i++)
00062 {
00063
00064 _data[volts[i]] = gains[i];
00065 }
00066 }
00067
00069 bool GainVoltPmtParameters::isValid() const
00070 {
00071
00072 map<double,double>::const_iterator i;
00073 for (i=_data.begin();i!=_data.end();i++)
00074 {
00075 if (i->first<200 || i->second<1)
00076 return false;
00077 }
00078 return true;
00079 }
00080
00081 void GainVoltPmtParameters::fit()
00082 {
00083
00084 if (isValid())
00085 {
00086 _fit.fit(&_data);
00087 _a = _fit.getCoefficient();
00088 _b = _fit.getExponent();
00089 }
00090 else
00091 {
00092 cout <<"GainVoltPmtParameters::fit() - Default Parameters used for " << *this<<endl;
00093 _a = _defaultA;
00094 _b = _defaultB;
00095 }
00096 }
00097
00098 ostream& operator<<(ostream& os, GainVoltPmtParameters& object)
00099 {
00100 os << object._id << "\t";
00101 if (object.ioMode&1) os << object._a <<"\t" << object._b<< "\t";
00102 if (object.ioMode&2)
00103 {
00104 map<double,double>::const_iterator i;
00105 if (object.ioMode&4)
00106 {
00107 os << object.getNPoints()<<"\t";
00108 for (i=object._data.begin();i!=object._data.end();i++) os << i->first << "\t" << i->second<<"\t";
00109 }
00110 else
00111 {
00112 i=object._data.begin(); i++;i++;
00113 os << i->first <<"\t";
00114 for (i=object._data.begin();i!=object._data.end();i++) os << i->second << "\t";
00115 }
00116 }
00117 os << endl;
00118 return os;
00119 }
00120
00121 istream& operator>>(istream& is, GainVoltPmtParameters& object)
00122 {
00123 double x,y;
00124 is >> object._id;
00125 if (object.ioMode&1)
00126 {
00127 is >> x >> y;
00128 object._a = x;
00129 object._b = y;
00130 }
00131 if (object.ioMode&2)
00132 {
00133 int n;
00134 if (object.ioMode&4)
00135 {
00136 is >> n;
00137 for (int i=0;i<n;i++)
00138 {
00139 is >> x >> y;
00140 object._data[x] = y;
00141 }
00142 }
00143 else
00144 {
00145 double refVolt,gain;
00146 is >> refVolt;
00147 is >> gain; object._data[refVolt-100] = gain;
00148 is >> gain; object._data[refVolt- 50] = gain;
00149 is >> gain; object._data[refVolt ] = gain;
00150 is >> gain; object._data[refVolt+ 50] = gain;
00151 is >> gain; object._data[refVolt+100] = gain;
00152 }
00153 }
00154 else
00155 cout << "no data input "<<endl;
00156 return is;
00157 }
00158
00160 double GainVoltPmtParameters::getMultConstant() const
00161 {
00162 return _a;
00163 }
00164
00166 double GainVoltPmtParameters::getExponent() const
00167 {
00168 return _b;
00169 }
00170
00172 double GainVoltPmtParameters::getGain(double voltage) const
00173 {
00174 if (voltage<=0)
00175 throw runtime_error("GainVoltPmtParameters::getGain(double) - F - given voltage<0");
00176 return exp(_a+_b*::log(voltage));
00177 }
00178
00180 double GainVoltPmtParameters::getVoltage(double gain) const
00181 {
00182 if (gain<=0) return 0;
00183 return exp(::log(gain)/_b-_a);
00184 }
00185
00186
00187
00188
00189
00190
00191
00193 int GainVoltPmtParameters::getNPoints() const
00194 {
00195 return _data.size();
00196 }
00197
00199 void GainVoltPmtParameters::print()
00200 {
00201 cout << "N Points:" << getNPoints() << endl
00202 << "Voltage(V) Gain(Relative)"<<endl
00203 << "============================="<<endl;
00204 map<double,double>::const_iterator i;
00205 for (i=_data.begin();i!=_data.end();i++)
00206 {
00207 cout << i->first << "\t" << i->second<<endl;
00208 }
00209 cout <<"============================="<<endl
00210 << " a : " << _a<< endl
00211 << " b : " << _b<< endl;
00212
00213 }
00214
00215 PmtIdentifier & GainVoltPmtParameters::getPmtIdentifier()
00216 {
00217 return _id;
00218 }
00219
00220 void GainVoltPmtParameters::setDefaults(double multCoefficient, double exponent)
00221 {
00222 _defaultA = multCoefficient;
00223 _defaultB = exponent;
00224 }