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