00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #include "Calibrator/CalVaLinearity.h"
00014 #include "MessageService/MsgService.h"
00015 #include "DatabaseInterface/DbiOutRowStream.h"
00016 #include "DatabaseInterface/DbiResultSet.h"
00017 #include "DatabaseInterface/DbiValidityRec.h"
00018 #include <cmath>
00019
00020 ClassImp(CalVaLinearity)
00021
00022
00023
00024
00025 CVSID("$Id: CalVaLinearity.cxx,v 1.9 2007/01/15 19:52:01 rhatcher Exp $\n \
00026 CVSID_DBIRESULTPTR ");
00027
00028
00029
00030
00031 #include "DatabaseInterface/DbiResultPtr.tpl"
00032 template class DbiResultPtr<CalVaLinearity>;
00033
00034 #include "DatabaseInterface/DbiWriter.tpl"
00035 template class DbiWriter<CalVaLinearity>;
00036
00037 Double_t CalVaLinearity::Linearize(const Double_t adc) const
00038 {
00039
00040 Double_t arg[1];
00041 Double_t par[8];
00042
00043 arg[0] = adc;
00044
00045 for (Int_t i=0; i<6; i++) par[i] = fParam[i];
00046 par[6] = 0.;
00047 par[7] = 1.;
00048
00049
00050
00051 return CalVaLinearity::FitFunction(arg,par);
00052 }
00053
00054 Double_t CalVaLinearity::UnLinearize(const Double_t linearAdc) const
00055 {
00056
00057 Double_t arg[1];
00058 Double_t par[8];
00059
00060 arg[0] = linearAdc;
00061
00062 for (Int_t i=0; i<6; i++) par[i] = fParam[i];
00063 par[6] = 0.;
00064 par[7] = 1.;
00065
00066 par[0] = par[0] + 1;
00067
00068 return CalVaLinearity::FitFunction(arg,par);
00069 }
00070
00071 void CalVaLinearity::Fill(DbiResultSet& rs,
00072 const DbiValidityRec* ) {
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 if ( rs.TableName() == "CALVALINEARITY" ) {
00090
00091 rs >> fVAChannel >> fParam[0]
00092 >> fParam[1] >> fParam[2] >> fParam[3] >> fParam[4] >> fParam[5];
00093 fParam[6] = 0.;
00094 fParam[7] = 1.;
00095 } else {
00096
00097 Int_t numCol = rs.NumCols();
00098 fVAChannel = 0;
00099
00100
00101 for (Int_t curCol = rs.HasRowCounter() ? 3 : 2; curCol <= numCol; ++curCol) {
00102 string colName = rs.CurColName();
00103 if ( colName == "VACHANNEL" ) rs >> fVAChannel;
00104 else if( colName == "FSEL" ) rs >> fParam[0];
00105 else if( colName == "PARAM1" ) rs >> fParam[1];
00106 else if( colName == "PARAM2" ) rs >> fParam[2];
00107 else if( colName == "PARAM3" ) rs >> fParam[3];
00108 else if( colName == "PARAM4" ) rs >> fParam[4];
00109 else if( colName == "PARAM5" ) rs >> fParam[5];
00110 else {
00111 MSG("Dbi",Msg::kDebug) << "Ignoring column " << curCol
00112 << "(" << colName << ")"
00113 << "; not part of CalVaLinearity"
00114 << endl;
00115 rs.IncrementCurCol();
00116 }
00117 }
00118 }
00119 }
00120
00121
00122 void CalVaLinearity::Store(DbiOutRowStream& ors,
00123 const DbiValidityRec* ) const {
00124
00125
00126
00127
00128
00129
00130
00131 ors << fVAChannel << fParam[0]
00132 << fParam[1] << fParam[2] << fParam[3] << fParam[4] << fParam[5];
00133 }
00134
00135 UInt_t CalVaLinearity::Rcid2Index(const RawChannelId& rcid)
00136 {
00137
00138
00139 return
00140 ( rcid.GetDetector() <<28) |
00141 ( rcid.GetCrate() <<24) | ( rcid.GetVarcId() <<20) |
00142 ( rcid.GetVmm() <<16) | ( rcid.GetVaAdcSel() <<12) |
00143 ( rcid.GetVaChip() << 8) | ( rcid.GetVaChannel()<< 0);
00144 }
00145
00146 RawChannelId CalVaLinearity::Index2Rcid(Int_t index)
00147 {
00148
00149
00150 UInt_t crate = (index>>24)&0xf;
00151 UInt_t varc = (index>>20)&0xf;
00152 UInt_t vmm = (index>>16)&0xf;
00153 UInt_t vfb = (index>>12)&0xf;
00154 UInt_t va = (index>> 8)&0xf;
00155 UInt_t ch = (index>> 0)&0xff;
00156
00157 return RawChannelId(Detector::kFar
00158 ,ElecType::kVA
00159 ,crate,varc,vmm,vfb,va,ch);
00160 }
00161
00162 Double_t CalVaLinearity::FitFunction(Double_t *arg, Double_t *par)
00163 {
00164
00165
00166
00167
00168 double x = *arg;
00169 double dtype = par[0];
00170 int type = static_cast<int>(dtype+0.1);
00171 double k1 = par[1];
00172 double k2 = par[2];
00173 double k3 = par[3];
00174 double k4 = par[4];
00175
00176 double ped = par[6];
00177 double gain = par[7];
00178
00179
00180
00181
00182
00183 if (x < 0.1) return x;
00184
00185 if (type == 24) {
00186 double x1 = x;
00187 x1 = ped + x1 * gain ;
00188 x1 = Pole(x1,k1,k2,-1.);
00189 if ((x1-k3) > 0) {
00190 x1 = (x1 - k3)/(1. - (x1-k3)/k4) + k3;
00191 }
00192 return x1;
00193 }
00194 else if (type == 25) {
00195 double x1 = x;
00196 if ((x1-k3) > 0) {
00197 x1 = (x1 - k3)/(1. + (x1-k3)/k4) + k3;
00198 }
00199 x1 = Pole(x1,k1,k2,+1.);
00200 x1 = (x1-ped)/gain ;
00201 return x1;
00202 }
00203
00204
00205
00206
00207
00208 else if (type == 4) {
00209 double x1 = x;
00210 x1 = ped + x1 * gain ;
00211 x1 = Pole(x1,k1,k2,-1.);
00212 return x1;
00213 }
00214 else if (type == 5) {
00215 double x1 = x;
00216 x1 = Pole(x1,k1,k2,+1.);
00217 x1 = (x1-ped)/gain ;
00218 return x1;
00219
00220 }
00221
00222
00223
00224
00225
00226 else if (type == 0) {
00227 return ped + x * gain ;
00228 }
00229 else if (type == 1) {
00230 return (x-ped)/gain;
00231 }
00232
00233
00234
00235
00236
00237 else if (type == 10) {
00238 return x;
00239 }
00240 else if (type == 12) {
00241 return 0.;
00242 }
00243 else if (type == 14) {
00244 return k1;
00245 }
00246 else if (type == 16) {
00247 return 1.e7;
00248 }
00249
00250 return x;
00251 }
00252
00253 Double_t CalVaLinearity::Pole(Double_t x, Double_t k1,
00254 Double_t k2, Double_t sign)
00255 {
00256
00257
00258
00259
00260
00261
00262 Double_t x1 = x;
00263 if (k1 <= 500.) {
00264 x1 = 0.;
00265 } else if (sign <= 0. && k1 <= x1) {
00266 x1 = 0.;
00267 } else if (x1 < 0.1) {
00268 } else if (k2 <= 0.1) {
00269 } else {
00270 Double_t a = k2*log(x1/k1);
00271 if (a > 228.) {
00272 if (sign < 0.) {
00273
00274 x1 = k1;
00275 }
00276 } else {
00277 Double_t b = pow(1. + sign * exp(a), 1./k2);
00278 if (b > 1.0e-20) {
00279 x1 = x1 / b;
00280 } else {
00281 x1 = x1*1.0e20;
00282 }
00283 }
00284 }
00285 return x1;
00286 }