GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseCalib.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004-2006 by Olivier Stezowski & Christophe Theisen *
3  * stezow(AT)ipnl.in2p3.fr, christophe.theisen(AT)cea.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef GW_BASECALIB_H
24 #include "BaseCalib.h"
25 #endif
26 
27 #ifndef ROOT_TMath
28 #include "TMath.h"
29 #endif
30 
31 #ifndef ROOT_TAxis
32 #include "TAxis.h"
33 #endif
34 
35 #ifndef ROOT_TLine
36 #include "TLine.h"
37 #endif
38 
39 #include <iostream>
40 #include <fstream>
41 
42 using namespace std;
43 using namespace Gw;
44 
45 // ROOT dictionnary
47 
48 //__________________________________________________________________________________________________
49 BaseCalib::BaseCalib()
50 : fTabulated(0),
51  fRaw(0),
52  fCalibrator()
53 {
54  //Defaut Constructor
55 }
56 
57 //__________________________________________________________________________________________________
59 { //Destructor
60 
61 }
62 
63 //__________________________________________________________________________________________________
64 void BaseCalib::AddPoint(const Double_t channel, const Double_t energy)
65 {
66  fRaw.Set(fRaw.GetSize()+1);
67  fTabulated.Set(fTabulated.GetSize()+1);
68  fRaw.AddAt(channel,fRaw.GetSize()-1);
69  fTabulated.AddAt(energy,fTabulated.GetSize()-1);
70 }
71 
72 //__________________________________________________________________________________________________
73 void BaseCalib::Calibrate(TF1 *function) const
74 {
75  if (function == 0) {
76  printf("Info in <BaseCalib::Calibrate> function NOT defined !!\n");
77  return;
78  }
79  if (function->GetNpar() > fRaw.GetSize()) {
80  printf("Info in <BaseCalib::Calibrate> Nb of points < Nb of fit parameters, abort calibration !!\n");
81  return;
82  }
83  TGraphErrors *graph = new TGraphErrors(fRaw.GetSize(),fRaw.GetArray(),fTabulated.GetArray());
84  graph->Fit(function,"","",graph->GetXaxis()->GetXmin(),graph->GetXaxis()->GetXmax());
85  graph->Draw("AP");
86  DrawResults(function);
87 }
88 
89 //__________________________________________________________________________________________________
90 void BaseCalib::Calibrate(const char* filename, TF1 *function)
91 {
92  if (function == 0) {
93  printf("Info in <BaseCalib::Calibrate> function NOT defined !!\n");
94  return;
95  }
96  TGraphErrors *graph = new TGraphErrors(filename);
97  fTabulated.Set(graph->GetN(),graph->GetY());
98  fRaw.Set(graph->GetN(),graph->GetX());
99  if (function->GetNpar() > fRaw.GetSize()) {
100  printf("Info in <BaseCalib::Calibrate> Nb of points < Nb of fit parameters, abort calibration !!\n");
101  return;
102  }
103  graph->Fit(function,"","",graph->GetXaxis()->GetXmin(),graph->GetXaxis()->GetXmax());
104  graph->Draw("AP");
105  DrawResults(function);
106 }
107 
108 //__________________________________________________________________________________________________
109 void BaseCalib::Calibrate(TF1 *function, const Int_t nbpoints, const Double_t *raw, const Double_t *tabulated)
110 {
111  if (function == 0) {
112  printf("Info in <BaseCalib::Calibrate> function NOT defined !!\n");
113  return;
114  }
115  TGraphErrors *graph = new TGraphErrors(nbpoints,raw,tabulated);
116  fTabulated.Set(graph->GetN(),graph->GetY());
117  fRaw.Set(graph->GetN(),graph->GetX());
118  if (function->GetNpar() > fRaw.GetSize()) {
119  printf("Info in <BaseCalib::Calibrate> Nb of points < Nb of fit parameters, abort calibration !!\n");
120  return;
121  }
122  graph->Fit(function,"","",graph->GetXaxis()->GetXmin(),graph->GetXaxis()->GetXmax());
123  graph->Draw("AP");
124  DrawResults(function);
125 }
126 
127 //__________________________________________________________________________________________________
128 void BaseCalib::Calibrate(Int_t dimension)
129 {
130  TMath::Abs(dimension);
131  TString str = "[0]";
132  for (Int_t i=1 ; i<dimension ; i++) {
133  str += "["; str += i; str += "*"; str += "TMath::Power(x,"; str += i; str +=")";
134  }
135  TF1 *ftemp = new TF1("ftemp",str.Data());
136  if (ftemp->GetNpar() > fRaw.GetSize()) {
137  printf("Info in <BaseCalib::Calibrate> Nb of points < Nb of fit parameters, abort calibration !!\n");
138  return;
139  }
140  TGraphErrors *graph = new TGraphErrors(fRaw.GetSize(),fRaw.GetArray(),fTabulated.GetArray());
141  graph->Fit(ftemp,"","",graph->GetXaxis()->GetXmin(),graph->GetXaxis()->GetXmax());
142  graph->Draw("AP");
143  DrawResults(ftemp);
144 
145  if (ftemp) delete ftemp;
146 }
147 
148 //__________________________________________________________________________________________________
149 void BaseCalib::AlignMax(TH1 *histo,TF1 *function, const Double_t AlignValue,
150  const Bool_t invert, const Double_t xmin, const Double_t xmax)
151 {
152  //Perform the alignment of the maximum of a TH1 on AlignValue
153  //The search for maximum if performed in the range [xmin,xmax] of the histogram
154  //If xmin=0, xmax=0 (by default), the search for maximum if performed in the total range of the histogram
155 
156  //if invert=true, all the histogram is inverted (can be usefull for TAC spectrum)
157 
158  //A polynomial function must be used
159  // par 0 -> shift
160  // par 1 -> proportional coef
161 
162  if (function == 0) {
163  printf("Info in <BaseCalib::AlignMax> function NOT defined !!\n");
164  return;
165  }
166  if (histo == 0) {
167  printf("Info in <BaseCalib::AlignMax> histo NOT defined !!\n");
168  return;
169  }
170 
171  if (histo->GetDimension() > 1) {
172  printf("Info in <BaseCalib::AlignMax> Method not yet defined for Histogram Dimension >= 2 !!\n");
173  return;
174  }
175 
176  Double_t maxvalue=0.0, shift=0.0;
177  histo->GetXaxis()->SetRangeUser(xmin,xmax);
178  maxvalue = histo->GetBinCenter(histo->GetMaximumBin());
179  if (invert == false) {
180  shift = AlignValue - maxvalue;
181  function->SetParameter(0,shift);
182  function->SetParameter(1,1.0);
183  } else {
184  shift = AlignValue + maxvalue;
185  function->SetParameter(0,shift);
186  function->SetParameter(1,-1.0);
187  }
188  DrawResults(function);
189 }
190 
191 //__________________________________________________________________________________________________
192 void BaseCalib::AlignMax(TH1 *histo,TF1 *function, const Double_t AlignValue,
193  const Double_t ProportionalCoef, const Bool_t invert, const Double_t xmin, const Double_t xmax)
194 {
195  //Perform the alignment of the maximum of a TH1 on AlignValue with a proportional coefficient
196  //The search for maximum if performed in the range [xmin,xmax] of the histogram
197  //If xmin=0, xmax=0 (by default), the search for maximum if performed in the total range of the histogram
198 
199  //if invert=true, all the histogram is inverted (can be usefull for TAC spectrum)
200 
201  //A polynomial function must be used
202  // par 0 -> shift
203  // par 1 -> proportional coef
204 
205  if (function == 0) {
206  printf("Info in <BaseCalib::AlignMax> function NOT defined !!\n");
207  return;
208  }
209  if (histo == 0) {
210  printf("Info in <BaseCalib::AlignMax> histo NOT defined !!\n");
211  return;
212  }
213 
214  if (histo->GetDimension() > 1) {
215  printf("Info in <BaseCalib::AlignMax> Method not yet defined for Histogram Dimension >= 2 !!\n");
216  return;
217  }
218 
219  Double_t maxvalue=0.0, shift=0.0;
220  histo->GetXaxis()->SetRangeUser(xmin,xmax);
221  maxvalue = histo->GetBinCenter(histo->GetMaximumBin());
222  if (invert == false) {
223  shift = AlignValue - ProportionalCoef*maxvalue;
224  function->SetParameter(0,shift);
225  function->SetParameter(1,ProportionalCoef);
226  } else {
227  shift = AlignValue + ProportionalCoef*maxvalue;
228  function->SetParameter(0,shift);
229  function->SetParameter(1,-ProportionalCoef);
230  }
231  DrawResults(function);
232 }
233 
234 //__________________________________________________________________________________________________
235 void BaseCalib::CheckCalibration(TH1 *histo, const Double_t value, Double_t xmin, Double_t xmax, const Int_t color)
236 {
237  // Display a histogram between the user range defined by xmin and xmax
238  // A vertical TLine is drawn at value
239 
240  if (histo == 0) {
241  printf("Info in <BaseCalib::CheckCalibration> histo NOT defined !!\n");
242  return;
243  }
244  if (histo->GetDimension() > 1) {
245  printf("Info in <BaseCalib::CheckCalibration> Method not yet defined for Histogram Dimension >= 2 !!\n");
246  return;
247  }
248 
249  if (xmin == -1111) xmin = histo->GetXaxis()->GetXmin();
250  if (xmax == -1111) xmax = histo->GetXaxis()->GetXmax();
251  if (xmin > xmax) histo->GetXaxis()->SetRangeUser(xmax,xmin); else histo->GetXaxis()->SetRangeUser(xmin,xmax);
252 
253  //Create a new TLine
254  TLine *line = new TLine();
255  line->SetLineColor(color); line->SetLineStyle(2);
256  histo->Draw();
257  line->DrawLine(value,0.0,value,histo->GetMaximum());
258 }
259 
260 //__________________________________________________________________________________________________
261 void BaseCalib::CheckCalibration(TH1 *histo, const Int_t nbvalue, const Double_t *values,
262  Double_t xmin, Double_t xmax, const Int_t color)
263 {
264  //Display a histogram between the user range defined by xmin and xmax
265  // nbvalue vertical TLines are drawn at values
266 
267  if (histo == 0) {
268  printf("Info in <BaseCalib::CheckCalibration> histo NOT defined !!\n");
269  return;
270  }
271  if (histo->GetDimension() > 1) {
272  printf("Info in <BaseCalib::CheckCalibration> Method not yet defined for Histogram Dimension >= 2 !!\n");
273  return;
274  }
275 
276  if (xmin == -1111) xmin = histo->GetXaxis()->GetXmin();
277  if (xmax == -1111) xmax = histo->GetXaxis()->GetXmax();
278  if (xmin > xmax) histo->GetXaxis()->SetRangeUser(xmax,xmin); else histo->GetXaxis()->SetRangeUser(xmin,xmax);
279 
280  TArrayD array(nbvalue,values);
281  histo->Draw();
282 
283  for (Int_t i=0 ; i<array.GetSize() ; i++) {
284  TLine *line = new TLine();
285  line->SetLineColor(color); line->SetLineStyle(2);
286  line->DrawLine(array[i],0.0,array[i],histo->GetMaximum());
287  }
288 }
289 
290 //__________________________________________________________________________________________________
291 void BaseCalib::CheckCalibration(TH1 *histo, const char *SourceName, Double_t xmin, Double_t xmax, const Int_t color)
292 {
293  // Display a histogram between the user range defined by xmin and xmax
294  // vertical TLines are drawn corresponding to SourceName (most intense peaks)
295  // For the moment the sources are : Co60, Eu152, Ba133, Na22, K40
296 
297  TString opt = SourceName;
298  opt.ToLower();
299 
300  Double_t *energies = 0;
301 
302  //Create new TLines according to the SourceName
303  if ( (!opt.CompareTo("co60")) or (!opt.CompareTo("60co")) ) {
304  energies = new Double_t[2];
305  energies[0] = 1173.228;
306  energies[1] = 1332.492;
307  CheckCalibration(histo,2,energies,xmin,xmax,color);
308 
309  } else if ( (!opt.CompareTo("eu152")) or (!opt.CompareTo("152eu")) ) {
310  energies = new Double_t[7];
311  energies[0] = 121.7830;
312  energies[1] = 244.6920;
313  energies[2] = 344.2760;
314  energies[3] = 778.9030;
315  energies[4] = 964.1310;
316  energies[5] = 1112.1160;
317  energies[6] = 1408.0110;
318  CheckCalibration(histo,7,energies,xmin,xmax,color);
319  } else if ( (!opt.CompareTo("ba133")) or (!opt.CompareTo("133ba")) ) {
320  energies = new Double_t[4];
321  energies[0] = 80.9971;
322  energies[1] = 276.3997;
323  energies[2] = 302.8510;
324  energies[3] = 356.0134;
325  CheckCalibration(histo,4,energies,xmin,xmax,color);
326  } else if ( (!opt.CompareTo("na22")) or (!opt.CompareTo("22na")) ) {
327  energies = new Double_t[2];
328  energies[0] = 511.0;
329  energies[1] = 1274.537;
330  CheckCalibration(histo,2,energies,xmin,xmax,color);
331  } else if ( (!opt.CompareTo("k40")) or (!opt.CompareTo("40k")) ) {
332  energies = new Double_t[1];
333  energies[0] = 1311.07;
334  CheckCalibration(histo,1,energies,xmin,xmax,color);
335  } else if ( (!opt.CompareTo("y88")) or (!opt.CompareTo("88y")) ) {
336  energies = new Double_t[2];
337  energies[0] = 898.042;
338  energies[1] = 1836.063;
339  CheckCalibration(histo,2,energies,xmin,xmax,color);
340  } else {
341  printf("Info in <BaseCalib::CheckCalibration> This source is not yet defined - use the manual method !!\n");
342  }
343 
344  if (energies != 0) delete [] energies;
345 
346 }
347 
348 //__________________________________________________________________________________________________
349 void BaseCalib::DrawResults(const TF1 *function) const
350 {
351  if (function == 0) {
352  printf("Info in <BaseCalib::DrawResults> function NOT defined !!\n");
353  return;
354  }
355 
356  printf("***************************************\n");
357  printf("*** RESULTS FROM CALIBRATION ***\n");
358  printf("***************************************\n");
359  for (Int_t i=0 ; i<function->GetNpar() ; i++) {
360  printf("* par #%3i = %10.5f +- %10.5f *\n",i,function->GetParameter(i),function->GetParError(i));
361  }
362  printf("***************************************\n");
363 }
364 
365 
printf("******************************************************************** \n")
TArrayD fTabulated
Definition: BaseCalib.h:92
void AddPoint(const Double_t channel, const Double_t energy)
Definition: BaseCalib.cpp:64
UInt_t value[MaxValue]
Definition: ReadDaqAlone.C:29
TH1F * histo[MaxValue]
Definition: ReadDaqAlone.C:31
static void CheckCalibration(TH1 *histo, const Double_t value, Double_t xmin=-1111, Double_t xmax=-1111, const Int_t color=2)
Definition: BaseCalib.cpp:235
header file for the calibration facility
BaseCalib is a tool class.
Definition: BaseCalib.h:56
void Calibrate(TF1 *function) const
Definition: BaseCalib.cpp:73
void DrawResults(const TF1 *function) const
Definition: BaseCalib.cpp:349
TArrayD fRaw
Definition: BaseCalib.h:93
virtual ~BaseCalib()
Definition: BaseCalib.cpp:58
ClassImp(BaseCalib)
void AlignMax(TH1 *histo, TF1 *function, const Double_t AlignValue, const Bool_t invert=false, const Double_t xmin=0, const Double_t xmax=0)
Perform alignment of the maximum of a TH1 on a value.
Definition: BaseCalib.cpp:149