GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
TXXXCalibPar.cxx
Go to the documentation of this file.
1// $Id: TXXXCalibPar.cxx 478 2009-10-29 12:26:09Z linev $
2//-----------------------------------------------------------------------
3// The GSI Online Offline Object Oriented (Go4) Project
4// Experiment Data Processing at EE department, GSI
5//-----------------------------------------------------------------------
6// Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7// Planckstr. 1, 64291 Darmstadt, Germany
8// Contact: http://go4.gsi.de
9//-----------------------------------------------------------------------
10// This software can be used under the license agreements as stated
11// in Go4License.txt file which is part of the distribution.
12//-----------------------------------------------------------------------
13
14#include "TXXXCalibPar.h"
15
16#include "TMath.h"
17#include "TH1.h"
18
19#include "TGo4Log.h"
20#include "TGo4Fitter.h"
21#include "TGo4FitModel.h"
22#include "TGo4Analysis.h"
23
24//***********************************************************
27 fbRecalibrate(kFALSE),
28 fbReadDatabase(kFALSE)
29{
30 fxDatabase = "calilines.txt";
31 for (Int_t ord = 0; ord < __POLORDER__; ++ord)
32 fdA[ord] = 0;
33 for (Int_t ix = 0; ix < __LINESNUMBER__; ++ix) {
34 fiLinesChannel[ix] = 0;
35 ffLinesEnergy[ix] = 0;
36 fxLinesNames[ix] = TString::Format("Defaultline-%d",ix);
37 }
38}
39//***********************************************************
40TXXXCalibPar::TXXXCalibPar(const char *name, TH1 *spectrum, TGraph *curve) :
41 TGo4Parameter(name),
42 fbRecalibrate(kFALSE),
43 fbReadDatabase(kFALSE),
44 fxCalibCurve(curve),
45 fxCalibSpectrum(spectrum)
46{
47 // Set up fitters:
48 fxLinesFinder = new TGo4Fitter("Linefinder", TGo4Fitter::ff_least_squares, kTRUE);
49 fxCalibrator = new TGo4Fitter("Calibrator", TGo4Fitter::ff_least_squares, kTRUE);
50 if (fxCalibSpectrum) {
53 } else {
54 fxSpectrumName = "Please specify calibration spectrum";
55 }
56 if (fxCalibCurve) {
57 fxCalibrator->AddGraph(__GRAPHNAME__, fxCalibCurve, kFALSE);
58 fxGraphName = fxCalibCurve->GetName();
59 } else {
60 fxSpectrumName = "Please specify fit graph name";
61 }
62 fxCalibrator->AddPolynomX(__GRAPHNAME__, "A", __POLORDER__ - 1);
63 // note that __POLORDER__ is number of polynom parameters here
64 // i.e. true order of polynom +1
65 for (Int_t i = 0; i < __POLORDER__; ++i) {
66 fdA[i] = 1 / (i + 1);
67 TString modname = TString::Format("A_%d",i);
68 TGo4FitModel *mod = fxCalibrator->FindModel(modname.Data());
69 if(mod) {
70 // for the beginning, disable models beyond order 1:
71 if(i>1) mod->ClearAssignmentTo(__GRAPHNAME__);
72 } else
73 TGo4Log::Error("could not find model %s", modname.Data());
74 }
75
76 for (Int_t ix = 0; ix < __LINESNUMBER__; ++ix) {
77 fiLinesChannel[ix] = 0;
78 ffLinesEnergy[ix] = 0;
79 }
80 fxDatabase = "calilines.txt";
82}
83
84//***********************************************************
90
91//***********************************************************
93 {
94 fxCalibSpectrum = h1;
97 }
98
99//***********************************************************
101{
103 auto from = dynamic_cast<TXXXCalibPar *> (source);
104 if (!from) {
105 TGo4Log::Error("Wrong parameter class: %s", source->ClassName());
106 return kFALSE;
107 }
108
109 for (Int_t ord = 0; ord < __POLORDER__; ++ord)
110 fdA[ord] = from->fdA[ord];
111 fbRecalibrate = from->fbRecalibrate;
112 fbReadDatabase = from->fbReadDatabase;
113 if(fxLinesFinder) delete fxLinesFinder;
114 fxLinesFinder = from->fxLinesFinder;
115 from->fxLinesFinder = nullptr; // adopt lines finder
116 if(fxCalibrator) delete fxCalibrator;
117 fxCalibrator = from->fxCalibrator;
118 from->fxCalibrator = nullptr; // adopt calibration fitter
119
120 // note: graph with calibration curve is not copied!
121
122 for (Int_t ix = 0; ix < __LINESNUMBER__; ++ix) {
123 fiLinesChannel[ix] = from->fiLinesChannel[ix];
124 ffLinesEnergy[ix] = from->ffLinesEnergy[ix];
125 fxLinesNames[ix] = from->fxLinesNames[ix];
126 }
127 std::cout << "Updated Parameter:" << std::endl;
128 //Print();
129 // get references to graph and histogram from analysis:
130 // note that updatefrom is only used on analysis side here!
131 fxCalibCurve = dynamic_cast<TGraph *>(TGo4Analysis::Instance()->GetObject(fxGraphName.Data()));
132
133 if(!fxCalibCurve)
134 {
135 std::cout <<"Graph "<<fxGraphName.Data() << " not existing in analysis"<< std::endl;
136 }
137 else
138 {
139 std::cout <<"Updated graph pointer ref to "<<fxCalibCurve << std::endl;
140 }
141
142 // now reread database if desired:
144 {
145 std::cout <<"Reread database" << std::endl;
146 ReadDatabase();
147 }
148
149 if(fbRecalibrate)
150 {
151 std::cout <<"Recalibrating..." << std::endl;
152 // first we get the channels from the linesfinder fitter:
153
154 for (Int_t i = 0; i < __LINESNUMBER__; ++i) {
155 const char *linename = fxLinesNames[i];
156 TGo4FitModel *mod = fxLinesFinder->FindModel(linename);
157 if(mod) {
158 // check here if component is active or not
159 if(mod->IsAssignTo(__DATANAME__))
160 fiLinesChannel[i]=(Int_t) mod->GetParValue("Pos");
161 else
162 fiLinesChannel[i] = 0; // mark not active lines
163 }
164 }
165
166 // setup calibration graph with the new channel coords:
167 if(fxCalibCurve)
168 {
169 fxCalibCurve->Set(0);
170 Int_t point = 0;
171 for (Int_t ix = 0; ix < __LINESNUMBER__; ++ix) {
172 if (fiLinesChannel[ix] != 0) {
173 fxCalibCurve->SetPoint(point, fiLinesChannel[ix], ffLinesEnergy[ix]);
174 // we only fit active lines
175 ++point;
176 }
177 } // for
178 // now perform fit of calibration graph:
179 fxCalibrator->SetObject(__GRAPHNAME__, fxCalibCurve, kFALSE);
180 fxCalibrator->DoActions();
181 fxCalibrator->PrintLines();
182 printf("fxCalibrator = %p\n", fxCalibrator);
183 // finally, copy results of calibration to the parameter fields:
184 for (Int_t i = 0; i < __POLORDER__; ++i) {
185 TString modname = TString::Format("A_%d", i);
186 TGo4FitModel *mod = fxCalibrator->FindModel(modname.Data());
187 if (mod) {
188 // check here if component is active or not
189 if (mod->IsAssignTo(__GRAPHNAME__))
190 fdA[i] = mod->GetParValue("Ampl");
191 else
192 fdA[i] = 0;
193 }
194 }
195 }
196 else
197 {
198 TGo4Log::Error("Calibration parameter %s has no TGraph!",
199 GetName());
200 }
201 }
202
203 return kTRUE;
204}
205
206//***********************************************************
208{
209 // read energies from file:
210 char nextline[__TEXTMAX__];
211 char buf[__TEXTMAX__];
212 std::ifstream database(fxDatabase.Data());
213 if(!database)
214 {
215 TGo4Log::Error("Open error of calibration energy file %s",
216 fxDatabase.Data());
217 }
218 else
219 {
220 Int_t ix = 0;
221 while(1){
222 do{
223 database.getline(nextline,__TEXTMAX__,'\n' ); // read whole line
224 if(database.eof() || !database.good())
225 {
226 break;
227 }
228 }while(strstr(nextline,"#") || strstr(nextline,"!") ); // skip any comments
229 if(database.eof() || !database.good()) break;
230 sscanf(nextline,"%s %f %d",buf,
231 &ffLinesEnergy[ix],
232 &fiLinesChannel[ix]);
233 fxLinesNames[ix]=buf;
234 // std::cout <<"\tname:"<<fxLinesNames[ix].Data() << std::endl;
235 // std::cout <<"\te:"<<ffLinesEnergy[ix] << std::endl;
236 // std::cout <<"\tch:"<<fiLinesChannel[ix] << std::endl;
237
238 fxLinesFinder->AddGauss1(__DATANAME__,
239 fxLinesNames[ix].Data(),
240 fiLinesChannel[ix],
241 TMath::Sqrt((Long_t) fiLinesChannel[ix]));
242 ix++;
243 } // while(1)
244 }
245}
246
247//***********************************************************
248Double_t TXXXCalibPar::Energy(Int_t channel)
249{
250 Double_t result = 0.;
251 for (Int_t ord = 0; ord < __POLORDER__; ++ord)
252 result += fdA[ord] * TMath::Power(channel, ord);
253
254 return result;
255}
#define __TEXTMAX__
#define __GRAPHNAME__
#define __LINESNUMBER__
#define __DATANAME__
#define __POLORDER__
static TGo4Analysis * Instance()
return analysis instance
TNamed * GetObject(const char *name, const char *folder=nullptr)
Searches for object by name in all directories.
Basic abstract class for representing model components of fitted data.
Bool_t IsAssignTo(const char *DataName) const
Checks, if model assigned to given data.
void ClearAssignmentTo(const char *DataName)
Remove assignment to given data (if exists).
Double_t GetParValue(const char *ParName)
Return value of parameter with given name.
Central class of Go4Fit package.
Definition TGo4Fitter.h:38
@ ff_least_squares
Definition TGo4Fitter.h:40
static void Error(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 3.
Definition TGo4Log.cxx:320
TH1 * fxCalibSpectrum
Reference to histogram containing the calibration spectrum.
Double_t fdA[__POLORDER__]
Calibration polynom coeff.
void ReadDatabase()
Read calibration lines from database (ascii file)
Double_t Energy(Int_t channel)
calculate energy from channel number using the current polynom
Int_t fiLinesChannel[__LINESNUMBER__]
Centroid channel numbers for fitted lines.
void SetCalibSpectrum(TH1 *h1)
Reset reference to calibration spectrum in fitter.
TString fxDatabase
Filename for ascii file with linesname - energy.
TGraph * fxCalibCurve
Reference to graph containing the calibration points.
TString fxLinesNames[__LINESNUMBER__]
Database names of calibration lines.
TGo4Fitter * fxLinesFinder
Fitter to search lines.
Bool_t fbReadDatabase
Set to kTRUE to re-read energies from external ascii file.
TGo4Fitter * fxCalibrator
Fitter for calibration of channel/energies with polynom.
Bool_t fbRecalibrate
Set to kTRUE to make calibration fit in update from.
Float_t ffLinesEnergy[__LINESNUMBER__]
Database energies of calibration lines.
virtual ~TXXXCalibPar()
Bool_t UpdateFrom(TGo4Parameter *) override
Update contents of parameter class with external object.
TString fxGraphName
Reference to graph containing the calibration points.
TString fxSpectrumName
Reference to histogram containing the calibration spectrum.