GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  fxLinesFinder(0),
30  fxCalibrator(0),
31  fxCalibCurve(0),
32  fxCalibSpectrum(0)
33 {
34  fxDatabase = "calilines.txt";
35  for(Int_t ord=0;ord<__POLORDER__;++ord) fdA[ord]=0;
36  for(Int_t ix=0;ix<__LINESNUMBER__;++ix) {
37  fiLinesChannel[ix] = 0;
38  ffLinesEnergy[ix] = 0;
39  fxLinesNames[ix] = TString::Format("Defaultline-%d",ix);
40  }
41 }
42 //***********************************************************
43 TXXXCalibPar::TXXXCalibPar(const char* name, TH1* spectrum, TGraph* curve) :
44  TGo4Parameter(name),
45  fbRecalibrate(kFALSE),
46  fbReadDatabase(kFALSE),
47  fxLinesFinder(0),
48  fxCalibrator(0),
49  fxCalibCurve(curve),
50  fxCalibSpectrum(spectrum)
51 {
52  // Set up fitters:
53  fxLinesFinder = new TGo4Fitter("Linefinder", TGo4Fitter::ff_least_squares, kTRUE);
54  fxCalibrator = new TGo4Fitter("Calibrator", TGo4Fitter::ff_least_squares, kTRUE);
55  if(fxCalibSpectrum)
56  {
59  }
60  else
61  {
62  fxSpectrumName="Please specify calibration spectrum";
63  }
64  if(fxCalibCurve)
65  {
67  fxGraphName=fxCalibCurve->GetName();
68  }
69  else
70  {
71  fxSpectrumName="Please specify fit graph name";
72  }
74  // note that __POLORDER__ is number of polynom parameters here
75  // i.e. true order of polynom +1
76  for(Int_t i=0; i<__POLORDER__;++i) {
77  fdA[i]=1/(i+1);
78  TString modname = TString::Format("A_%d",i);
79  TGo4FitModel* mod = fxCalibrator->FindModel(modname.Data());
80  if(mod) {
81  // for the beginning, disable models beyond order 1:
82  if(i>1) mod->ClearAssignmentTo(__GRAPHNAME__);
83  } else
84  TGo4Log::Error("could not find model %s", modname.Data());
85  }
86 
87  for(Int_t ix=0;ix<__LINESNUMBER__;++ix) {
88  fiLinesChannel[ix]=0;
89  ffLinesEnergy[ix]=0;
90  }
91  fxDatabase="calilines.txt";
92  ReadDatabase();
93 }
94 //***********************************************************
96 {
97  delete fxLinesFinder;
98  delete fxCalibrator;
99 }
100 //***********************************************************
101 
103  {
104  fxCalibSpectrum = h1;
106  }
107 
108 
109 //-----------------------------------------------------------
110 Int_t TXXXCalibPar::PrintParameter(Text_t * n, Int_t)
111 {
112  return 0;
113 }
114 //-----------------------------------------------------------
116 {
118  TXXXCalibPar * from = dynamic_cast<TXXXCalibPar*> (source);
119  if (from==0) {
120  TGo4Log::Error("Wrong parameter class: %s", source->ClassName());
121  return kFALSE;
122  }
123 
124  for(Int_t ord=0;ord<__POLORDER__;++ord)
125  fdA[ord] = from->fdA[ord];
128  if(fxLinesFinder) delete fxLinesFinder;
130  from->fxLinesFinder = 0; // adopt lines finder
131  if(fxCalibrator) delete fxCalibrator;
132  fxCalibrator = from->fxCalibrator;
133  from->fxCalibrator = 0; // adopt calibration fitter
134 
135  // note: graph with calibration curve is not copied!
136 
137  for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
138  {
139  fiLinesChannel[ix]=from->fiLinesChannel[ix];
140  ffLinesEnergy[ix]=from->ffLinesEnergy[ix];
141  fxLinesNames[ix]=from->fxLinesNames[ix];
142  //std::cout <<"updated line:"<<fxLinesNames[ix].Data() << std::endl;
143  }
144  std::cout <<"Updated Parameter:" << std::endl;
145  //PrintParameter(0,0);
146  // get references to graph and histogram from analysis:
147  // note that updatefrom is only used on analysis side here!
148  fxCalibCurve = dynamic_cast<TGraph*>(TGo4Analysis::Instance()->GetObject(fxGraphName.Data())) ;
149 
150  if(fxCalibCurve==0)
151  {
152  std::cout <<"Graph "<<fxGraphName.Data() << " not existing in analysis"<< std::endl;
153  }
154  else
155  {
156  std::cout <<"Updated graph pointer ref to "<<fxCalibCurve << std::endl;
157  }
158 
159  // now reread database if desired:
160  if(fbReadDatabase)
161  {
162  std::cout <<"Reread database" << std::endl;
163  ReadDatabase();
164  }
165 
166  if(fbRecalibrate)
167  {
168  std::cout <<"Recalibrating..." << std::endl;
169  // first we get the channels from the linesfinder fitter:
170 
171  for(Int_t i=0; i<__LINESNUMBER__;++i)
172  {
173  const char* linename=fxLinesNames[i];
174  TGo4FitModel* mod=fxLinesFinder->FindModel(linename);
175  if(mod)
176  {
177  // check here if component is active or not
178  if(mod->IsAssignTo(__DATANAME__))
179  fiLinesChannel[i]=(Int_t) mod->GetParValue("Pos");
180  else
181  fiLinesChannel[i]=0; // mark not active lines
182  }
183  else
184  {
185  //std::cout <<"could not find model "<<linename << std::endl;
186  }
187  }
188 
189 
190  // setup calibration graph with the new channel coords:
191  if(fxCalibCurve)
192  {
193  fxCalibCurve->Set(0);
194  Int_t point=0;
195  for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
196  {
197  if(fiLinesChannel[ix]!=0)
198  {
199  fxCalibCurve->SetPoint(point,
200  fiLinesChannel[ix],
201  ffLinesEnergy[ix]);
202  // we only fit active lines
203  ++point;
204  }
205  } // for
206  // now perform fit of calibration graph:
210  printf("fxCalibrator = %p\n", fxCalibrator);
211  // finally, copy results of calibration to the parameter fields:
212  for(Int_t i=0; i<__POLORDER__;++i) {
213  TGo4FitModel* mod = fxCalibrator->FindModel(Form("A_%d",i));
214  if(mod) {
215  // check here if component is active or not
216  if(mod->IsAssignTo(__GRAPHNAME__))
217  fdA[i] = mod->GetParValue("Ampl");
218  else
219  fdA[i]=0;
220  }
221  }
222 
223  }
224  else
225  {
226  TGo4Log::Error("Calibration parameter %s has no TGraph!",
227  GetName());
228  }
229  }
230 
231  return kTRUE;
232 }
233 
235 {
236  // read energies from file:
237  char nextline[__TEXTMAX__];
238  char buf[__TEXTMAX__];
239  std::ifstream database(fxDatabase.Data());
240  if(!database)
241  {
242  TGo4Log::Error("Open error of calibration energy file %s",
243  fxDatabase.Data());
244  }
245  else
246  {
247  Int_t ix=0;
248  while(1){
249  do{
250  database.getline(nextline,__TEXTMAX__,'\n' ); // read whole line
251  if(database.eof() || !database.good())
252  {
253  break;
254  }
255  //std::cout <<"read line:"<<nextline << std::endl;
256  }while(strstr(nextline,"#") || strstr(nextline,"!") ); // skip any comments
257  if(database.eof() || !database.good()) break;
258  sscanf(nextline,"%s %f %d",buf,
259  &ffLinesEnergy[ix],
260  &fiLinesChannel[ix]);
261  fxLinesNames[ix]=buf;
262  // std::cout <<"\tname:"<<fxLinesNames[ix].Data() << std::endl;
263  // std::cout <<"\te:"<<ffLinesEnergy[ix] << std::endl;
264  // std::cout <<"\tch:"<<fiLinesChannel[ix] << std::endl;
265 
267  fxLinesNames[ix].Data(),
268  fiLinesChannel[ix],
269  TMath::Sqrt((Long_t) fiLinesChannel[ix]));
270  ix++;
271  } // while(1)
272  //std::cout <<"scanned lines:"<<ix << std::endl;
273 
274  } // if (database==0)
275 }
276 
277 
278 
279 Double_t TXXXCalibPar::Energy(Int_t channel)
280 {
281  Double_t result=0;
282  for(Int_t ord=0;ord<__POLORDER__; ++ord)
283  {
284  result+=fdA[ord]*TMath::Power(channel,ord);
285  }
286  return result;
287 }
TString fxGraphName
Reference to graph containing the calibration points.
Definition: TXXXCalibPar.h:71
TGo4FitDataGraph * AddGraph(const char *DataName, TGraph *gr, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
Definition: TGo4Fitter.cxx:140
void PrintLines() const
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
Bool_t UpdateFrom(TGo4Parameter *)
Double_t fdA[__POLORDER__]
Definition: TXXXCalibPar.h:48
TGo4FitModel * FindModel(const char *ModelName)
Definition: TGo4Fitter.cxx:196
#define __LINESNUMBER__
Definition: TXXXCalibPar.h:19
TString fxLinesNames[__LINESNUMBER__]
Definition: TXXXCalibPar.h:60
TH1 * fxCalibSpectrum
Definition: TXXXCalibPar.h:74
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:223
void ClearAssignmentTo(const char *DataName)
Bool_t fbReadDatabase
Definition: TXXXCalibPar.h:52
#define __GRAPHNAME__
Definition: TXXXCalibPar.h:23
Double_t Energy(Int_t channel)
#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:345
Int_t fiLinesChannel[__LINESNUMBER__]
Definition: TXXXCalibPar.h:56
void ReadDatabase()
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=0)
void SetCalibSpectrum(TH1 *h1)
Bool_t fbRecalibrate
Definition: TXXXCalibPar.h:50
TGo4Fitter * fxLinesFinder
Definition: TXXXCalibPar.h:63
#define __DATANAME__
Definition: TXXXCalibPar.h:22
TGraph * fxCalibCurve
Definition: TXXXCalibPar.h:69
Int_t PrintParameter(Text_t *n, Int_t)
TGo4Fitter * fxCalibrator
Definition: TXXXCalibPar.h:66
virtual ~TXXXCalibPar()
TString fxDatabase
Definition: TXXXCalibPar.h:54
TNamed * GetObject(const char *name, const char *folder=0)
Bool_t IsAssignTo(const char *DataName) const
Definition: TGo4FitModel.h:141
TGo4FitDataHistogram * SetH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE)
Definition: TGo4Fitter.cxx:132
static TGo4Analysis * Instance()
static void Error(const char *text,...)
Definition: TGo4Log.cxx:313
TString fxSpectrumName
Reference to histogram containing the calibration spectrum.
Definition: TXXXCalibPar.h:77
Double_t GetParValue(const char *ParName)
Float_t ffLinesEnergy[__LINESNUMBER__]
Definition: TXXXCalibPar.h:58
TGo4FitDataHistogram * AddH1(const char *DataName, TH1 *histo, Bool_t Owned=kFALSE, Double_t lrange=0., Double_t rrange=0.)
Definition: TGo4Fitter.cxx:124