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