GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSPlayer.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 #include <vector>
25 
26 #ifndef Gw_Peak1D
27 #include "Peak1D.h"
28 #endif
29 
30 #ifndef Gw_GSPlayer
31 #include "GSPlayer.h"
32 #endif
33 
34 #ifndef ROOT_TCanvas
35 #include "TCanvas.h"
36 #endif
37 
38 #ifndef ROOT_TFormula
39 #include "TFormula.h"
40 #endif
41 
42 #ifndef ROOT_TH1
43 #include "TH1.h"
44 #endif
45 
46 #ifndef ROOT_TH2
47 #include "TH2.h"
48 #endif
49 
50 #ifndef ROOT_KeySymbols
51 #include "KeySymbols.h"
52 #endif
53 
54 #ifndef ROOT_TList
55 #include "TList.h"
56 #endif
57 
58 #ifndef ROOT_TLine
59 #include "TLine.h"
60 #endif
61 
62 #ifndef ROOT_TPolyLine
63 #include "TPolyLine.h"
64 #endif
65 
66 #ifndef ROOT_TSpline
67 #include "TSpline.h"
68 #endif
69 
70 #ifndef ROOT_TString
71 #include "TString.h"
72 #endif
73 
74 #ifndef ROOT_TSystem
75 #include "TSystem.h"
76 #endif
77 
78 #ifndef ROOT_TVector2
79 #include "TVector2.h"
80 #endif
81 
82 #ifndef ROOT_TVirtualPad
83 #include "TVirtualPad.h"
84 #endif
85 
86 
87 using namespace Gw;
88 
90 
91 //__________________________________________________________
94 {
95  fLog.GetProcessName() = "GSPlayer";
96 
97  //Default Constructor
98 }
99 
100 //__________________________________________________________
102 {
103  // default destructor
104 }
105 
106 //__________________________________________________________
107 Bool_t GSPlayer::SetParameter(const char* name, Double_t value)
108 {
109  // set parameter
110  Bool_t ok = BaseSpectrumPlayer::SetParameter(name,value);
111  if ( !ok ) {
112  // set search parameter
113 // if (strncmp(name, "FWHM", strlen(name)) == 0)
114 // fDefaultFwhm = TParameter<Double_t>(name, value);
115  }
116  if ( !ok ) {
117  fLog.SetProcessMethod("SetParameter(const char*, Double_t )");
118  fLog << error << "Unkown parameter: " << name << dolog;
119  }
120  return ok;
121 }
122 
123 //__________________________________________________________
124 Int_t GSPlayer::FindPeaks(TH1 *histo, Option_t* o)
125 {
126  // placed peak with a moving on line
127 
128  Int_t xBinMin = histo->GetXaxis()->GetFirst();
129  Int_t xBinMax = histo->GetXaxis()->GetLast();
130  Double_t xMin = histo->GetXaxis()->GetBinCenter(xBinMin);
131  Double_t xMax = histo->GetXaxis()->GetBinCenter(xBinMax);
132  Double_t xMin0 = xMin;
133  Double_t xMax0 = xMax;
134  Double_t yMax = histo->GetMaximum()*1.05;
135  Double_t yMin = histo->GetMinimum()*0.95;
136 
137  Double_t range = xMax - xMin;
138  Double_t width = GetDefaultPeakWidth()->Eval(xMin)*15;
139  Int_t nSpec = int(range/width + 0.5);
140  Int_t xBin = 0;
141 
142  Int_t result = 0;
143  Int_t eventType = 0;;
144 
145  // if Contains +, add to the list
146  TString opt = o;
147  if ( !opt.Contains("+") )
148  GetPeakList()->Delete();
149 
150  Option_t* optPeak;
151  if ( opt.Contains("bg") )
152  optPeak = "bg";
153  else
154  optPeak = "";
155 
156 
157 
158  // activated search for 1D spectrum
159  if ( histo->GetDimension() == 1 ) {
160  // starts search once button1 is pressed
161  do {
162  gSystem->ProcessEvents();
163  eventType = TVirtualPad::Pad()->GetEvent();
164  } while(eventType != kButton1Down);
165 
166  // loop over ranges
167  for (Int_t k = 0; k < nSpec; ++k) {
168  xMax = xMin+width;
169  histo->SetAxisRange(xMin, xMax);
170  xMin = xMax;
171  TLine line;
172  xBinMin = histo->GetXaxis()->GetFirst();
173  xBinMax = histo->GetXaxis()->GetLast();
174  yMax = histo->GetMaximum();
175  yMin = histo->GetMinimum();
176 
177  // loop over bins of the given range
178  for (Int_t i = xBinMin; i < xBinMax; ++i) {
179 
180  xBin = int(histo->GetXaxis()->GetBinCenter(i) + 0.5);
181 
182  // set line
183  line.SetX1(xBin); line.SetY1(yMin);
184  line.SetX2(xBin); line.SetY2(yMax);
185  line.Draw();
186  TVirtualPad::Pad()->Update();
187 
188  // keep position of line.
189  gSystem->ProcessEvents();
190  // press q to proceed next range
191  if (TVirtualPad::Pad()->GetEvent() == kKeyPress) {
192  Int_t keysym = TVirtualPad::Pad()->GetEventY();
193  if ( (EKeySym)keysym == kKey_s) {
194  fCreator->CreatePeak(histo->FindBin(xBin), optPeak);
195  TVirtualPad::Pad()->Update();
196  }
197  }
198 
199  Int_t kk;
200  do {
201  gSystem->ProcessEvents();
202  eventType = TVirtualPad::Pad()->GetEvent();
203  kk = TVirtualPad::Pad()->GetEventY();
204  } while((EKeySym)kk == kKey_s);
205 
206  gSystem->Sleep(100);
207  }
208  }
209  histo->SetAxisRange(xMin0, xMax0);
210  TVirtualPad::Pad()->Update();
211  }
212 
213  if ( histo->GetDimension() == 2) {
214  fLog.SetProcessMethod("FindPeaks(TH1*, Option_t* )");
215  fLog << error << "2D search peak not implemented yet" << dolog;
216  return 0;
217  }
218 
219  CollectPeaks();
220  result = GetPeakList()->GetEntries();
221 
222  return result;
223 }
224 
225 
226 //__________________________________________________________
227 TH1* GSPlayer::Background(const TH1* histo, Option_t* /*opt*/)
228 {
229  // generates bkg for polymarker (for the moment)
230  TH1* bkg = 0x0;
231  TPolyLine* line = 0x0;
232 
233  TList* list = TVirtualPad::Pad()->GetListOfPrimitives();
234  for (Int_t i = 0; i < list->GetEntries(); ++i) {
235  TObject* o = list->At(i);
236  const char* name = o->ClassName();
237  TString tmp(name);
238 
239  // check polyline in pad
240  if ( tmp.CompareTo("TPolyLine") == 0 )
241  line = static_cast<TPolyLine*>(o);
242 
243  // check bkg spectrum in pad
244  if ( o->InheritsFrom("TH1")) {
245  bkg = static_cast<TH1*>(o);
246  tmp = bkg->GetName();
247  if (tmp.Contains("Bkg"))
248  return bkg;
249  else
250  bkg = 0x0;
251  }
252  }
253 
254  if (!line && !bkg) {
255  fLog.SetProcessMethod("Background(const TH1*)");
256  fLog << error << "No background line/spectrum defined" << dolog;
257  return bkg;
258  }
259 
260  Double_t* x = line->GetX();
261  Double_t* y = line->GetY();
262  Int_t n = line->Size();
263 
264  bkg = (TH1*)histo->Clone();
265  Int_t xBinMin = bkg->FindBin(x[0]);
266  Int_t xBinMax = bkg->FindBin(x[n-1]);
267  bkg->Reset("ICE");
268 
269  TSpline3 spline("Bkg", x, y, n);
270 
271  // building backgroud spectrum
272  for (Int_t i = xBinMin; i < xBinMax; ++i) {
273  Double_t xBin = histo->GetXaxis()->GetBinCenter(i);
274  bkg->SetBinContent(i, spline.Eval(xBin));
275  }
276 
277  return bkg;
278 }
279 
280 
LogMessage & error(LogMessage &)
virtual Int_t FindPeaks(TH1 *histo, Option_t *opt="")
Find peak in current histo graphically.
Definition: GSPlayer.cpp:124
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 ~GSPlayer()
Definition: GSPlayer.cpp:101
virtual Bool_t SetParameter(const char *name, Double_t value)
To change the parameters for that algorithm.
Definition: GSPlayer.cpp:107
PeakCreator * fCreator
default PeakCreator
LogMessage & dolog(LogMessage &)
Most of the methods relies on graphical approach.
Definition: GSPlayer.h:46
virtual TH1 * Background(const TH1 *histo, Option_t *)
Compute the background for that histogram.
Definition: GSPlayer.cpp:227
virtual const TF1 * GetDefaultPeakWidth() const
Get formula that gives the with as a function of energy.
header file for a general 1D peak
virtual TSeqCollection * GetPeakList() const
Return the current collection of peaks.
virtual Int_t CollectPeaks(Option_t *opt="")
Collect the peaks from the current pad.
ClassImp(GSPlayer) GSPlayer
Definition: GSPlayer.cpp:89
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.