GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaselineWatcher.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4 #include <sstream>
5 #include <vector>
6 #include <string>
7 #include <map>
8 
9 #include "TCanvas.h"
10 #include "TROOT.h"
11 #include "TStyle.h"
12 #include "TH1.h"
13 #include "TF1.h"
14 #include "TSpectrum.h"
15 #include "TObjArray.h"
16 #include "BaselineWatcher.h"
17 #include "TBrowser.h"
18 #include "TMath.h"
19 #include "TVirtualFitter.h"
20 #include "TPavesText.h"
21 #include "TObject.h"
22 #include "FitSpectra.h"
23 
24 #include "LoupeOnPad.h"
25 
26 using namespace std;
27 
28 //ClassImp(BaselineWatcher);
29 
30 BaselineWatcher::BaselineWatcher(const char* name, const char *title) : SpectraViewerWatcher(name,title)
31 {
32  fDefinePersoBackground = false;
33 }
34 
35 void BaselineWatcher::Exec(Option_t *option)
36 {
37  TString Opt = option;
38  Opt.ToLower();
39 
40  TString FitType = "CBDS";
41 
42  if(Opt.Contains("baseline"))
43  {
44  TString CrystalName = fCrystalName;
45  CrystalName.ToLower();
46 
47  if(Opt.Contains("all") || Opt.Contains(CrystalName))
48  {
50 
51  if(Opt.Contains("calib"))
52  {
53  if(Opt.Contains("gaus")) FitType = "Gaus";
54 
55  FitAll(FitType);
56  }
57  }
58  }
59 }
60 
61 void BaselineWatcher::FitAll(Option_t *FitType)
62 {
63  cout<<"Baseline parameters for crystal "<<fCrystalName<<endl;
64  cout<<"Seg"<<"\t\t\t"<<"GausMean"<<"\t\t"<<"FWHM"<<endl;
65 
66  TCanvas *CurrentCanvas = gPad->GetCanvas();
67 
68  int Pad=0;
69  while(CurrentCanvas->cd(Pad) != CurrentCanvas->cd(Pad+1))
70  {
71  Pad++;
72  CurrentCanvas->cd(Pad);
73  FitSelected(FitType,true);
74  }
75 
76  CurrentCanvas->cd();
77  CurrentCanvas->Update();
78  CurrentCanvas->Modified();
79 
80  ofstream FileOut(((TString)"FitResults/" + fCrystalName + "_Baselines.fit"));
81  FileOut<<"#Seg"<<"\t"<<"GausMean"<<"\t"<<"FWHM"<<endl;
82  for(int i=0 ; i<38 ; i++)FileOut<<i<<"\t"<<TabMean[i]<<"\t\t"<<TabFWHM[i]<<endl;
83  FileOut.close();
84 }
85 
86 void BaselineWatcher::FitSelected(Option_t *FitType, bool all)
87 {
88  FitSpectra *FS = new FitSpectra(FitType,"baseline");
89  FS->GetCurrentHistogram();
91  {
93  }
94  FS->Fit();
95 
97 
98  int HistNbr = -1;
99 
100  if(FS->GetHistogram()!=0x0)
101  {
102  if(!all)
103  {
104  cout<<"Baseline parameters for crystal "<<fCrystalName<<endl;
105  cout<<"Seg"<<"\t\t\t"<<"GausMean"<<"\t\t"<<"FWHM"<<endl;
106  }
107 
108  TString Name = FS->GetHistName();
109  TObjArray *array = Name.Tokenize("_");
110  TObject *LastToken = array->At(array->GetLast());
111 
112  HistNbr = ((TString)LastToken->GetName()).Atoi();
113 
114  PadPos HistPosition = fHistPositionList[HistNbr];
115 
116  TString SegName = HistPosition.SegName;
117 
118  double Mean = FS->GetMean().at(0);
119  double FWHM = FS->GetFWHM().at(0);
120 
121  TabMean[HistNbr] = Mean;
122  TabFWHM[HistNbr] = FWHM;
123 
124  if(SegName.Contains("Core"))cout<<"Seg "<<HistNbr<<"("<<SegName<<") :\t"<<Mean<<"\t\t"<<FWHM<<endl;
125  else cout<<"Seg "<<HistNbr<<"("<<SegName<<") : \t"<<Mean<<"\t\t"<<FWHM<<endl;
126 
127  }
128  else if(gPad->GetListOfPrimitives()->GetSize()>1)
129  {
130  TString Name = FS->GetHistName();
131  TObjArray *array = Name.Tokenize("_");
132  TObject *LastToken = array->At(array->GetLast());
133 
134  HistNbr = ((TString)LastToken->GetName()).Atoi();
135 
136  PadPos HistPosition = fHistPositionList[HistNbr];
137 
138  TString SegName = HistPosition.SegName;
139 
140  if(SegName.Contains("Core"))cout<<"Seg "<<HistNbr<<"("<<SegName<<") :\t"<<0<<"\t\t"<<0<<"\t Broken Segment"<<endl;
141  else cout<<"Seg "<<HistNbr<<"("<<SegName<<") : \t"<<0<<"\t\t"<<0<<"\t Broken Segment"<<endl;
142 
143  TabMean[HistNbr] = 0.;
144  TabFWHM[HistNbr] = 0.;
145  }
146 
147  //delete FS;
148 }
149 
150 void BaselineWatcher::ShowBaselinesMap(int LibraryNumber, Option_t *option)
151 {
152  TString opt = (TString)option;
153  TCanvas *c = 0x0;
154 
155  if(opt.Contains("same",TString::kIgnoreCase)) c = (TCanvas*)gPad->GetCanvas();
156  else
157  {
158  TString Name = "Baselines_" + fCrystalName;
159  TString Title = "Mapping of the baselines for crystal " + fCrystalName;
160 
161  c = CanvasVisu::BuildSegmentedCanvas(Name,Title);
162  }
163  if(c == 0x0) return;
164 
165  c->cd();
166 
167  TList *ListOfHists = (TList*)fListOfLibrairies->At(LibraryNumber);
168 
169  for(int i=0 ; i<fNSg ; i++)
170  {
171  PadPos HistPosition = fHistPositionList[i];
172 
173  TPad *pad = (TPad*)c->cd(HistPosition.PadNmbr);
174 
175  TH1 *h = 0x0;
176 
177  TPaveText *p = (TPaveText*)pad->GetListOfPrimitives()->FindObject("InfoPad");
178 
179  h = ((TH1*)ListOfHists->At(i));
180  h->GetXaxis()->SetLabelSize(0.07);
181  h->GetYaxis()->SetLabelSize(0.07);
182 
183  int col = 0;
184  for(int k=0 ; k<pad->GetListOfPrimitives()->GetSize() ; k++)
185  {
186  if(pad->GetListOfPrimitives()->At(k)->InheritsFrom("TH1"))col++;
187  }
188 
189  h->SetLineColor(GetColor(col));
190  h->SetFillColor(0);
191  h->SetStats(0);
192  h->Draw(opt);
193 
194  if(p)p->Draw();
195 
196  pad->Modified();
197  pad->Update();
198  }
199  for(int i=0 ; i<fNC ; i++)
200  {
201  PadPos HistPosition = fHistPositionList[fNSg+i];
202 
203  TPad *pad = (TPad*)c->cd(HistPosition.PadNmbr);
204 
205  TH1 *h = 0x0;
206 
207  TPaveText *p = (TPaveText*)pad->GetListOfPrimitives()->FindObject("InfoPad");
208 
209  h = ((TH1*)ListOfHists->At(fNSg+i));
210  h->GetXaxis()->SetLabelSize(0.07);
211  h->GetYaxis()->SetLabelSize(0.07);
212 
213  int col = 0;
214  for(int k=0 ; k<pad->GetListOfPrimitives()->GetSize() ; k++)
215  {
216  if(pad->GetListOfPrimitives()->At(k)->InheritsFrom("TH1"))col++;
217  }
218  h->SetLineColor(GetColor(col));
219  h->SetFillColor(0);
220  h->SetStats(0);
221  h->Draw(opt);
222 
223  if(p)p->Draw();
224 
225  pad->Modified();
226  pad->Update();
227  }
228 
229  if(!opt.Contains("same"))
230  {
231  new LoupeOnPad(c);
232  SetLoupe(false);
233  }
234 }
235 
236 void BaselineWatcher::SetPersoBackground(int NumberIterations, int Direction,int FilterOrder,bool Smoothing,int smoothingWindow,bool Compton)
237 {
238  fDefinePersoBackground = true;
239  fNumberIterations = NumberIterations;
240  fDirection = Direction;
241  fFilterOrder = FilterOrder;
242  fSmoothing = Smoothing;
243  fsmoothingWindow = smoothingWindow;
244  fCompton = Compton;
245 }
246 
vector< PadPos > fHistPositionList
Definition: CanvasVisu.h:44
void Fit()
Definition: FitSpectra.cpp:160
void FitSelected(Option_t *FitType="CBDS", bool all=false)
std::vector< double > GetMean()
Definition: FitSpectra.h:100
TString fCrystalName
Definition: CanvasVisu.h:67
TList * fListOfLibrairies
Definition: CanvasVisu.h:64
void FitAll(Option_t *FitType="CBDS")
LoupOnPad, a class to Loupe on a Pad in a Canvas with many pads in it.
Definition: LoupeOnPad.h:76
virtual void SetLoupe(bool loupeon=true)
void SetPersoBackground(int NumberIterations=100, int Direction=TSpectrum::kBackDecreasingWindow, int FilterOrder=TSpectrum::kBackOrder8, bool Smoothing=true, int smoothingWindow=TSpectrum::kBackSmoothing3, bool Compton=false)
Definition: FitSpectra.cpp:755
TCanvas * BuildSegmentedCanvas(TString CanvasName, TString CanvasTitle)
Definition: CanvasVisu.C:125
void SetPersoBackground(int NumberIterations=100, int Direction=TSpectrum::kBackDecreasingWindow, int FilterOrder=TSpectrum::kBackOrder8, bool Smoothing=true, int smoothingWindow=TSpectrum::kBackSmoothing3, bool Compton=false)
double TabMean[38]
Color_t GetColor(int i)
Definition: CanvasVisu.h:49
BaselineWatcher(const char *name, const char *title)
void ShowBaselinesMap(int LibraryNumber=0, Option_t *option="")
void GetCurrentHistogram()
Definition: FitSpectra.cpp:132
std::vector< double > GetFWHM()
Definition: FitSpectra.h:104
TString GetHistName()
Definition: FitSpectra.h:42
TH1 * GetHistogram()
Definition: FitSpectra.h:39
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual void Exec(Option_t *option)
double TabFWHM[38]