GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
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 //***********************************************************
26  TGo4Parameter(),
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 //***********************************************************
40 TXXXCalibPar::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) {
52  fxSpectrumName = fxCalibSpectrum->GetName();
53  } else {
54  fxSpectrumName = "Please specify calibration spectrum";
55  }
56  if (fxCalibCurve) {
58  fxGraphName = fxCalibCurve->GetName();
59  } else {
60  fxSpectrumName = "Please specify fit graph name";
61  }
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";
81  ReadDatabase();
82 }
83 
84 //***********************************************************
86 {
87  delete fxLinesFinder;
88  delete fxCalibrator;
89 }
90 
91 //***********************************************************
93  {
94  fxCalibSpectrum = h1;
95  if(fxLinesFinder)
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:
143  if(fbReadDatabase)
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:
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 
239  fxLinesNames[ix].Data(),
240  fiLinesChannel[ix],
241  TMath::Sqrt((Long_t) fiLinesChannel[ix]));
242  ix++;
243  } // while(1)
244  }
245 }
246 
247 //***********************************************************
248 Double_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 }
TString fxGraphName
Reference to graph containing the calibration points.
Definition: TXXXCalibPar.h:70
TGo4FitDataGraph * AddGraph(const char *DataName, TGraph *gr, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
Definition: TGo4Fitter.cxx:144
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
Double_t fdA[__POLORDER__]
Definition: TXXXCalibPar.h:47
TGo4FitModel * FindModel(const char *ModelName)
Definition: TGo4Fitter.cxx:205
#define __LINESNUMBER__
Definition: TXXXCalibPar.h:19
TString fxLinesNames[__LINESNUMBER__]
Definition: TXXXCalibPar.h:59
TH1 * fxCalibSpectrum
Definition: TXXXCalibPar.h:73
void AddPolynomX(const char *DataName, const char *NamePrefix, Int_t MaxOrder=1, Int_t GroupIndex=0, Double_t lrange=0., Double_t rrange=0.)
Definition: TGo4Fitter.cxx:235
void ClearAssignmentTo(const char *DataName)
Bool_t fbReadDatabase
Definition: TXXXCalibPar.h:51
#define __GRAPHNAME__
Definition: TXXXCalibPar.h:23
Double_t Energy(Int_t channel)
Bool_t IsAssignTo(const char *DataName) const
Definition: TGo4FitModel.h:141
void PrintLines() const
#define __POLORDER__
Definition: TXXXCalibPar.h:21
#define __TEXTMAX__
Definition: TXXXCalibPar.h:20
TGo4FitModelGauss1 * AddGauss1(const char *DataName, const char *ModelName, Double_t iPosition, Double_t iWidth, Double_t iAmpl=1., Int_t Axis=0)
Definition: TGo4Fitter.cxx:382
Int_t fiLinesChannel[__LINESNUMBER__]
Definition: TXXXCalibPar.h:55
void ReadDatabase()
void SetCalibSpectrum(TH1 *h1)
Bool_t fbRecalibrate
Definition: TXXXCalibPar.h:49
TGo4Fitter * fxLinesFinder
Definition: TXXXCalibPar.h:62
#define __DATANAME__
Definition: TXXXCalibPar.h:22
TGraph * fxCalibCurve
Definition: TXXXCalibPar.h:68
TNamed * GetObject(const char *name, const char *folder=nullptr)
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
TGo4Fitter * fxCalibrator
Definition: TXXXCalibPar.h:65
virtual ~TXXXCalibPar()
Bool_t UpdateFrom(TGo4Parameter *) override
TString fxDatabase
Definition: TXXXCalibPar.h:53
TGo4FitDataHistogram * SetH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE)
Definition: TGo4Fitter.cxx:136
static TGo4Analysis * Instance()
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=nullptr)
TString fxSpectrumName
Reference to histogram containing the calibration spectrum.
Definition: TXXXCalibPar.h:76
Double_t GetParValue(const char *ParName)
Float_t ffLinesEnergy[__LINESNUMBER__]
Definition: TXXXCalibPar.h:57
TGo4FitDataHistogram * AddH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
Definition: TGo4Fitter.cxx:127