GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RootSpectrumPlayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004-2006 by Olivier Stezowski & Christian Finck *
3  * stezow(AT)ipnl.in2p3.fr, cfinck(AT)ires.in2p3.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 #include <Riostream.h>
24 
25 #include <vector>
26 
27 #ifndef Gw_Peak1D
28 #include "Peak1D.h"
29 #endif
30 
31 #ifndef Gw_RootSpectrumPlayer
32 #include "RootSpectrumPlayer.h"
33 #endif
34 
35 #ifndef ROOT_TCanvas
36 #include "TCanvas.h"
37 #endif
38 
39 #ifndef ROOT_TH1
40 #include "TH1.h"
41 #endif
42 
43 #ifndef ROOT_TH2
44 #include "TH2.h"
45 #endif
46 
47 #ifndef ROOT_TList
48 #include "TList.h"
49 #endif
50 
51 #ifndef ROOT_TObjArray
52 #include "TObjArray.h"
53 #endif
54 
55 #ifndef ROOT_TPad
56 #include "TPad.h"
57 #endif
58 
59 #ifndef ROOT_TString
60 #include "TString.h"
61 #endif
62 
63 #ifndef ROOT_TSpectrum
64 #include "TSpectrum.h"
65 #endif
66 
67 #ifndef ROOT_TVector2
68 #include "TVector2.h"
69 #endif
70 
71 using namespace Gw;
72 
73 //__________________________________________________________
76  fMaxPeak(TParameter<Int_t>("MaxPosition", 100)),
77  fSigma(TParameter<Double_t>("Sigma", 2.0)),
78  fThreshold(TParameter<Double_t>("Threshold", 0.05)),
79  fNbIter(TParameter<Int_t>("NbIter", 20)),
80  fEngine1D(0x0)
81 {
82  //Default Constructor
83  fEngine1D = new TSpectrum(fMaxPeak.GetVal(),fSigma.GetVal());
84  fLog.GetProcessName() = "RootSpectrumPlayerfLog";
85 
86 }
87 
88 //__________________________________________________________
90 {
91  // default destructor
92  if ( fEngine1D )
93  { delete fEngine1D; fEngine1D = 0x0; }
94 }
95 
96 //__________________________________________________________
97 void RootSpectrumPlayer::Print(Option_t* opt) const
98 {
99  // print out
101 }
102 
103 //__________________________________________________________
104 Bool_t RootSpectrumPlayer::SetParameter(const char* name, Double_t value)
105 {
106  Bool_t ok = BaseSpectrumPlayer::SetParameter(name,value);
107  if ( !ok ) {
108  // set search parameter
109  if (strncmp(name, "threshold", strlen(name)) == 0)
110  {
111  ok = true;
112  fThreshold = TParameter<Double_t>(name, value);
113  }
114  if (strncmp(name, "sigma", strlen(name)) == 0)
115  {
116  ok = true;
117  fSigma = TParameter<Double_t>(name, value);
118  if(fEngine1D) delete fEngine1D;
119  fEngine1D = new TSpectrum(fMaxPeak.GetVal(),fSigma.GetVal());
120  }
121  }
122  if ( !ok ) {
123  fLog.SetProcessMethod("SetParameter(const char*, Double_t )");
124  fLog << error << "Unkown parameter: " << name << dolog;
125  }
126  return ok;
127 }
128 
129 //__________________________________________________________
130 Bool_t RootSpectrumPlayer::SetParameter(const char* name, Int_t value)
131 {
132  Bool_t ok = BaseSpectrumPlayer::SetParameter(name,value);
133  if ( !ok ) {
134  if (strncmp(name, "nbiter", strlen(name)) == 0)
135  {
136  ok = true;
137  fNbIter = TParameter<Int_t>(name, value);
138  }
139  if (strncmp(name, "maxpeak", strlen(name)) == 0)
140  {
141  ok = true;
142  fMaxPeak = TParameter<Int_t>(name, value);
143  if(fEngine1D) delete fEngine1D;
144  fEngine1D = new TSpectrum(fMaxPeak.GetVal(),fSigma.GetVal());
145  }
146  }
147  if ( !ok ) {
148  fLog.SetProcessMethod("SetParameter(const char*, Int_t )");
149  fLog << error << "Unkown parameter: " << name << dolog;
150  }
151  return ok;
152 }
153 
154 //__________________________________________________________
155 TParameter<Int_t> RootSpectrumPlayer::GetIntParameter(TString Name)
156 {
157  if(Name == "nbiter") return fNbIter;
158  else if(Name == "maxpeak") return fMaxPeak;
159  else
160  {
161  fLog.SetProcessMethod("GetIntParameter(TString)");
162  fLog << error << "Unkown parameter: " << Name << dolog;
163  return TParameter<Int_t>();
164  }
165 }
166 
167 //__________________________________________________________
168 TParameter<Double_t> RootSpectrumPlayer::GetDoubleParameter(TString Name)
169 {
170  if(Name == "threshold") return fThreshold;
171  else if(Name == "sigma") return fSigma;
172  else
173  {
174  fLog.SetProcessMethod("GetDoubleParameter(TString)");
175  fLog << error << "Unkown parameter: " << Name << dolog;
176  return TParameter<Double_t>();
177  }
178 }
179 
180 //__________________________________________________________
181 Int_t RootSpectrumPlayer::FindPeaks(TH1 *histo, Option_t* o)
182 {
183  // find peaks with TSpectrum->Search ... should be better in a futur implementation to use SearchHighRes
184  Int_t result = 0;
185 
186  // Int_t bin = 0;
187  Double_t posx = 0.;
188  // Double_t posy = 0.;
189 
190  // if Contains +, add to the list
191  TString opt = o;
192  if ( !opt.Contains("+") )
193  GetPeakList()->Clear();
194 
195  Option_t* optPeak;
196  if ( opt.Contains("bg") )
197  optPeak = "bg";
198  else
199  optPeak = "";
200 
201  // activated search for 1D spectrum
202  if ( histo->GetDimension() == 1 ) {
203 
204  TString opt;
205  opt = o;
206  opt += " goff";
207  result = fEngine1D->Search(histo, fSigma.GetVal(), opt.Data(), fThreshold.GetVal());
208  /*TH1* bg = */fEngine1D->Background(histo, fNbIter.GetVal());
209 
210  for(Int_t i = 0; i < fEngine1D->GetNPeaks(); ++i) {
211  posx = fEngine1D->GetPositionX()[i];
212  // bin = bg->FindBin(posx);
213  // posy = bg->GetBinContent(bin);
214  fCreator->CreatePeak(posx, optPeak);
215  }
216  TVirtualPad::Pad()->Update();
217  }
218 
219  if ( histo->GetDimension() == 2) {
220  fLog.SetProcessMethod("FindPeaks(TH1*, Option_t* )");
221  fLog << error << "Dimension not handled " << histo->GetDimension() << dolog;
222  return 0;
223  }
224 
225  return result;
226 }
227 
228 //__________________________________________________________
229 TH1 *RootSpectrumPlayer::Background(const TH1 *histo, Option_t* opt)
230 {
231  TH1 *result = 0x0;
232 
233  // activated search for 1D spectrum
234  if ( histo->GetDimension() == 1 ) {
235  result =
236  fEngine1D->Background(histo,fNbIter.GetVal(),opt);
237 
238  TString bgname = histo->GetName();
239  bgname += "_BG";
240  // bgname += fNbIter.GetVal();
241 
242  result->SetName(bgname.Data());
243  }
244  if ( histo->GetDimension() == 2) {
245  fLog.SetProcessMethod("Background(TH1*, Option_t* )");
246  fLog << error << "Dimension not handled " << histo->GetDimension() << dolog;
247  }
248 
249  return result;
250 }
251 
253 
RootSpectrumPlayer class to handle root spectra using the interface of BasePeak.
LogMessage & error(LogMessage &)
virtual Bool_t SetParameter(const char *name, Double_t value)
To change the parameters for that algorithm.
BaseSpectrumPlayer to work on spectra.
LogMessage fLog
log message
UInt_t value[MaxValue]
Definition: ReadDaqAlone.C:29
TH1F * histo[MaxValue]
Definition: ReadDaqAlone.C:31
virtual TParameter< Double_t > GetDoubleParameter(TString Name)
virtual Int_t FindPeaks(TH1 *histo, Option_t *opt="")
Find peaks.
PeakCreator * fCreator
default PeakCreator
LogMessage & dolog(LogMessage &)
virtual void Print(Option_t *opt="") const
print current peak list
virtual std::string & GetProcessName()
To get the Process name.
Definition: GwLogMessage.h:224
ClassImp(BaseNucleus)
header file for a general 1D peak
virtual void Print(Option_t *opt="") const
Print out informations concerning the parameters of that player.
virtual TSeqCollection * GetPeakList() const
Return the current collection of peaks.
virtual TParameter< Int_t > GetIntParameter(TString Name)
To get the parameters.
virtual TH1 * Background(const TH1 *histo, Option_t *opt="")
Compute the background for that histogram.
virtual void SetProcessMethod(const char *)
To set the current method.
virtual Bool_t SetParameter(const char *, Double_t)
To change the parameters for that algorithm.
virtual BasePeak * CreatePeak(const TH1 *h, Double_t x, Option_t *opt="")
It creates a peak at position x for the 1D spectra.