00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TXXXCalibPar.h"
00017
00018 #include "TMath.h"
00019 #include "TH1.h"
00020 #include "TGraph.h"
00021 #include "Riostream.h"
00022 #include "snprintf.h"
00023
00024 #include "TGo4Log.h"
00025 #include "TGo4Fitter.h"
00026 #include "TGo4FitModel.h"
00027 #include "TGo4Analysis.h"
00028
00029
00030 TXXXCalibPar::TXXXCalibPar() :
00031 TGo4Parameter(),
00032 fbRecalibrate(kFALSE),
00033 fbReadDatabase(kFALSE),
00034 fxLinesFinder(0),
00035 fxCalibrator(0),
00036 fxCalibCurve(0),
00037 fxCalibSpectrum(0)
00038 {
00039 fxDatabase="calilines.txt";
00040 Text_t buf[__TEXTMAX__];
00041 for(Int_t ord=0;ord<__POLORDER__;++ord) fdA[ord]=0;
00042 for(Int_t ix=0;ix<__LINESNUMBER__;++ix) {
00043 fiLinesChannel[ix]=0;
00044 ffLinesEnergy[ix]=0;
00045 snprintf(buf,__TEXTMAX__,"Defaultline-%d",ix);
00046 fxLinesNames[ix]=buf;
00047 }
00048 }
00049
00050 TXXXCalibPar::TXXXCalibPar(const char* name, TH1* spectrum, TGraph* curve) :
00051 TGo4Parameter(name),
00052 fbRecalibrate(kFALSE),
00053 fbReadDatabase(kFALSE),
00054 fxLinesFinder(0),
00055 fxCalibrator(0),
00056 fxCalibCurve(curve),
00057 fxCalibSpectrum(spectrum)
00058 {
00059
00060 fxLinesFinder=new TGo4Fitter("Linefinder", TGo4Fitter::ff_least_squares, kTRUE);
00061 fxCalibrator=new TGo4Fitter("Calibrator", TGo4Fitter::ff_least_squares, kTRUE);
00062 if(fxCalibSpectrum)
00063 {
00064 fxLinesFinder->AddH1(__DATANAME__, fxCalibSpectrum, kFALSE);
00065 fxSpectrumName=fxCalibSpectrum->GetName();
00066 }
00067 else
00068 {
00069 fxSpectrumName="Please specify calibration spectrum";
00070 }
00071 if(fxCalibCurve)
00072 {
00073 fxCalibrator->AddGraph(__GRAPHNAME__, fxCalibCurve, kFALSE);
00074 fxGraphName=fxCalibCurve->GetName();
00075 }
00076 else
00077 {
00078 fxSpectrumName="Please specify fit graph name";
00079 }
00080 fxCalibrator->AddPolynomX(__GRAPHNAME__,"A",__POLORDER__-1);
00081
00082
00083 Text_t modname[__TEXTMAX__];
00084 for(Int_t i=0; i<__POLORDER__;++i)
00085 {
00086 fdA[i]=1/(i+1);
00087 snprintf(modname,__TEXTMAX__,"A_%d",i);
00088 TGo4FitModel* mod=fxCalibrator->FindModel(modname);
00089 if(mod)
00090 {
00091
00092 if(i>1) mod->ClearAssignmentTo(__GRAPHNAME__);
00093 }
00094 else
00095 cout <<"could not find model "<<modname << endl;
00096 }
00097
00098 for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00099 {
00100 fiLinesChannel[ix]=0;
00101 ffLinesEnergy[ix]=0;
00102 }
00103 fxDatabase="calilines.txt";
00104 ReadDatabase();
00105 }
00106
00107 TXXXCalibPar::~TXXXCalibPar()
00108 {
00109 delete fxLinesFinder;
00110 delete fxCalibrator;
00111 }
00112
00113
00114
00115 Int_t TXXXCalibPar::PrintParameter(Text_t * n, Int_t){
00116
00117
00118 return 0;
00119 }
00120
00121 Bool_t TXXXCalibPar::UpdateFrom(TGo4Parameter *pp){
00123
00124
00125 if(pp->InheritsFrom("TXXXCalibPar"))
00126 {
00127 TXXXCalibPar * from;
00128 from = (TXXXCalibPar *) pp;
00129 for(Int_t ord=0;ord<__POLORDER__;++ord)
00130 {
00131 fdA[ord]=from->fdA[ord];
00132 }
00133 fbRecalibrate=from->fbRecalibrate;
00134 fbReadDatabase=from->fbReadDatabase;
00135 if(fxLinesFinder) delete fxLinesFinder;
00136 fxLinesFinder=from->fxLinesFinder;
00137 from->fxLinesFinder=0;
00138 if(fxCalibrator) delete fxCalibrator;
00139 fxCalibrator=from->fxCalibrator;
00140 from->fxCalibrator=0;
00141
00142
00143
00144
00145 for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00146 {
00147 fiLinesChannel[ix]=from->fiLinesChannel[ix];
00148 ffLinesEnergy[ix]=from->ffLinesEnergy[ix];
00149 fxLinesNames[ix]=from->fxLinesNames[ix];
00150
00151 }
00152 cout <<"Updated Parameter:" << endl;
00153
00154
00155
00156 fxCalibCurve=dynamic_cast<TGraph*>(TGo4Analysis::Instance()->GetObject(fxGraphName.Data())) ;
00157
00158 if(fxCalibCurve==0)
00159 {
00160 cout <<"Graph "<<fxGraphName.Data() << " not existing in analysis"<< endl;
00161 }
00162 else
00163 {
00164 cout <<"Updated graph pointer ref to "<<fxCalibCurve << endl;
00165 }
00166
00167
00168
00169
00170 if(fbReadDatabase)
00171 {
00172 cout <<"Reread database" << endl;
00173 ReadDatabase();
00174 }
00175
00176 if(fbRecalibrate)
00177 {
00178 cout <<"Recalibrating..." << endl;
00179
00180
00181 for(Int_t i=0; i<__LINESNUMBER__;++i)
00182 {
00183 const Text_t* linename=fxLinesNames[i];
00184 TGo4FitModel* mod=fxLinesFinder->FindModel(linename);
00185 if(mod)
00186 {
00187
00188 if(mod->IsAssignTo(__DATANAME__))
00189 fiLinesChannel[i]=(Int_t) mod->GetParValue("Pos");
00190 else
00191 fiLinesChannel[i]=0;
00192 }
00193 else
00194 {
00195
00196 }
00197 }
00198
00199
00200
00201 if(fxCalibCurve)
00202 {
00203 fxCalibCurve->Set(0);
00204 Int_t point=0;
00205 for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00206 {
00207 if(fiLinesChannel[ix]!=0)
00208 {
00209 fxCalibCurve->SetPoint(point,
00210 fiLinesChannel[ix],
00211 ffLinesEnergy[ix]);
00212
00213 ++point;
00214 }
00215 }
00216
00217 fxCalibrator->SetObject(__GRAPHNAME__, fxCalibCurve, kFALSE);
00218 fxCalibrator->DoActions();
00219 fxCalibrator->PrintLines();
00220
00221 Text_t modname[__TEXTMAX__];
00222 for(Int_t i=0; i<__POLORDER__;++i)
00223 {
00224 snprintf(modname,__TEXTMAX__,"A_%d",i);
00225 TGo4FitModel* mod=fxCalibrator->FindModel(modname);
00226 if(mod)
00227 {
00228
00229 if(mod->IsAssignTo(__GRAPHNAME__))
00230 fdA[i]=mod->GetParValue("Ampl");
00231 else
00232 fdA[i]=0;
00233 }
00234 else
00235 ;
00236
00237 }
00238
00239 }
00240 else
00241 {
00242 TGo4Log::Error("Calibration parameter %s has no TGraph!",
00243 GetName());
00244 }
00245
00246 }
00247
00248 }
00249 else
00250 {
00251 cout << "Wrong parameter object: " << pp->ClassName() << endl;
00252
00253 }
00254 return kTRUE;
00255 }
00256
00257 void TXXXCalibPar::ReadDatabase()
00258 {
00259
00260 Text_t nextline[__TEXTMAX__];
00261 Text_t buf[__TEXTMAX__];
00262 std::ifstream database(fxDatabase.Data());
00263 if(database==0)
00264 {
00265 TGo4Log::Error("Open error of calibration energy file %s",
00266 fxDatabase.Data());
00267 }
00268 else
00269 {
00270 Int_t ix=0;
00271 while(1){
00272 do{
00273 database.getline(nextline,__TEXTMAX__,'\n' );
00274 if(database.eof() || !database.good())
00275 {
00276 break;
00277 }
00278
00279 }while(strstr(nextline,"#") || strstr(nextline,"!") );
00280 if(database.eof() || !database.good()) break;
00281 sscanf(nextline,"%s %f %d",buf,
00282 &ffLinesEnergy[ix],
00283 &fiLinesChannel[ix]);
00284 fxLinesNames[ix]=buf;
00285
00286
00287
00288
00289 fxLinesFinder->AddGauss1(__DATANAME__,
00290 fxLinesNames[ix].Data(),
00291 fiLinesChannel[ix],
00292 TMath::Sqrt((Long_t) fiLinesChannel[ix]));
00293 ix++;
00294 }
00295
00296
00297 }
00298
00299
00300 }
00301
00302
00303
00304 Double_t TXXXCalibPar::Energy(Int_t channel)
00305 {
00306 Double_t result=0;
00307 for(Int_t ord=0;ord<__POLORDER__; ++ord)
00308 {
00309 result+=fdA[ord]*TMath::Power(channel,ord);
00310 }
00311 return result;
00312 }
00313
00314