CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ResidualFitter.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include "TCanvas.h"
5 #include "TH2.h"
6 #include "TF1.h"
7 #include "TDirectory.h"
8 
9 #include <string>
10 
11 using namespace std;
12 
13 // TCanvas* ResidualFitter::canvasFit_ = 0;
14 
15 //ClassImp(ResidualFitter)
16 
17 int ResidualFitter::xCanvas_ = 600;
18 int ResidualFitter::yCanvas_ = 600;
19 
20 
22  const char* title,
23  int nbinsx, double xlow, double xup,
24  int nbinsy, double ylow, double yup,
25  int nbinsz, double zlow, double zup)
26  : TH3D( name, title,
27  nbinsx, xlow, xup,
28  nbinsy, ylow, yup,
29  nbinsz, zlow, zup ),
30  fitFunction_( new TF1("gaus", "gaus") ),
31  canvasFit_(0),
32  curBin_(0),
33  autoRangeN_(0),
34  minN_(5) {
35 
36  cout<<"creating residual fitter with name "<<name<<endl;
37 
38  string meanname = name; meanname += "_mean";
39  mean_ = new TH2D(meanname.c_str(), meanname.c_str(),
40  nbinsx, xlow, xup,
41  nbinsy, ylow, yup);
42  mean_->SetStats(0);
43 
44  string sigmaname = name; sigmaname += "_sigma";
45  sigma_ = new TH2D(sigmaname.c_str(), sigmaname.c_str(),
46  nbinsx, xlow, xup,
47  nbinsy, ylow, yup);
48 
49  sigma_->SetStats(0);
50 
51  string chi2name = name; chi2name += "_chi2";
52  chi2_ = new TH2D(chi2name.c_str(), chi2name.c_str(),
53  nbinsx, xlow, xup,
54  nbinsy, ylow, yup);
55  chi2_->SetStats(0);
56 
57  // string nseenname = name; nseenname += "_nseen";
58  // nseen_ = new TH2D(nseenname.c_str(), nseenname.c_str(),
59  // nbinsx, xlow, xup,
60  // nbinsy, ylow, yup);
61  // nseen_->SetStats(0);
62 
63  gDirectory->ls();
64 
65  CreateCanvas();
66 }
67 
69  delete fitFunction_;
70  if(canvas_) delete canvas_;
71  if(curBin_) delete curBin_;
72 
73  delete mean_;
74  delete sigma_;
75  delete chi2_;
76  // delete nseen_;
77 }
78 
80  string cname = "ResidualFitterCanvas_"; cname += GetName();
81  canvas_ = new TCanvas(cname.c_str(), cname.c_str(),xCanvas_, yCanvas_);
82 
83  canvas_ ->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
84  "ResidualFitter",
85  this, "ExecuteEvent(Int_t,Int_t,Int_t,TObject*)");
86 
87 
88 }
89 
90 
92 
93  Reset();
94 
95  for(unsigned it = 0; it<true_.size(); it++) {
96  for(unsigned im = 0; im<meas_.size(); im++) {
97  TH3D::Fill(true_[it].x_,
98  true_[it].y_,
99  meas_[it].z_ - true_[it].z_ );
100  }
101  }
102 }
103 
105 
106  mean_->Reset();
107  sigma_->Reset();
108  chi2_->Reset();
109  // nseen_->Reset();
110 
111  cout<<"ResidualFitter::FitSlicesZ"<<endl;
112  if(f1) SetFitFunction(f1);
113 
114  for(int ix=1; ix<=GetNbinsX(); ix++) {
115  for(int iy=1; iy<=GetNbinsY(); iy++) {
116  Fit(ix, iy, "Q0");
117  }
118  }
119 
120  // TH3D::FitSlicesZ(f1);
121 }
122 
123 
124 void ResidualFitter::ExecuteEvent(Int_t event, Int_t px, Int_t py, TObject *sel) {
125 
126  if( event != kButton1Down ) return;
127 
128  TH2* histo2d = dynamic_cast<TH2*>(sel);
129  if(!histo2d) return;
130 
131  float x = canvas_->AbsPixeltoX(px);
132  float y = canvas_->AbsPixeltoY(py);
133  x = canvas_->PadtoX(x);
134  y = canvas_->PadtoY(y);
135 
136  ShowFit(histo2d, x, y);
137 }
138 
139 
140 void ResidualFitter::ShowFit(TH2* histo2d, double x, double y) {
141 
142  if(!canvasFit_) {
143  string cname = "ResidualFitterCanvasFit_"; cname += GetName();
144  canvasFit_ = new TCanvas(cname.c_str(), cname.c_str(),300,300);
145  }
146  canvasFit_ ->cd();
147 
148  int binx = histo2d->GetXaxis()->FindBin(x);
149  int biny = histo2d->GetYaxis()->FindBin(y);
150 
151  if(binx == oldBinx_ && biny == oldBiny_ ) return;
152  oldBinx_ = binx;
153  oldBiny_ = biny;
154 
155  Fit(binx, biny);
156 
157  canvasFit_->Modified();
158  canvasFit_->Update();
159 
160  canvas_->Modified();
161  canvas_->Update();
162  canvas_->cd();
163 }
164 
165 
166 void ResidualFitter::Fit(int binx, int biny, const char* opt) {
167  TH1::AddDirectory(0);
168 
169  if(curBin_) delete curBin_;
170  curBin_ = TH3::ProjectionZ("", binx, binx, biny, biny);
171 
172  if(curBin_->GetEntries() < minN_ ) {
173  TH1::AddDirectory(1);
174  return;
175  }
176 
177  string sopt = fitOptions_; sopt += opt;
178 
179  if( autoRangeN_ ) {
180  double maxpos = curBin_->GetBinCenter( curBin_->GetMaximumBin() );
181 
182  double minrange = maxpos-curBin_->GetRMS()* autoRangeN_;
183  double maxrange = maxpos+curBin_->GetRMS()* autoRangeN_;
184 
185  fitFunction_->SetRange( minrange, maxrange );
186  cout<<"range : "<<minrange<<" "<<maxrange<<endl;
187  }
188 
189  curBin_->Fit(fitFunction_, sopt.c_str() );
190 
191 
192  double chi2overndf=0;
193  if(fitFunction_->GetNDF() ) {
194  chi2overndf = fitFunction_->GetChisquare()/ fitFunction_->GetNDF();
195  mean_->SetBinContent(binx,biny, fitFunction_->GetParameter(1) );
196  mean_->SetBinError(binx,biny, fitFunction_->GetParError(1) );
197  sigma_->SetBinContent(binx,biny, fitFunction_->GetParameter(2) );
198  sigma_->SetBinError(binx,biny, fitFunction_->GetParError(2) );
199 
200  chi2_->SetBinContent(binx,biny,chi2overndf);
201  }
202  // nseen_->SetBinContent(binx, biny,
203  // fitFunction_->Integral( fitFunction_->GetXmin(),
204  // fitFunction_->GetXmax())
205  // /curBin_->GetBinWidth(1) );
206 
207  TH1::AddDirectory(1);
208 }
209 
210 
void Fit(int x, int y, const char *opt="")
ResidualFitter(const char *name, const char *title, int nbinsx, double xlow, double xup, int nbinsy, double ylow, double yup, int nbinsz, double zlow, double zup)
void ExecuteEvent(Int_t event, Int_t px, Int_t py, TObject *sel)
void FitSlicesZ(TF1 *f1=0)
TCanvas * canvas_
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
static int xCanvas_
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::string fitOptions_
std::vector< ResidualFitter::Point > true_
std::vector< ResidualFitter::Point > meas_
static int yCanvas_
TCanvas * canvasFit_
void SetFitFunction(TF1 *func)
tuple cout
Definition: gather_cfg.py:121
void Reset(std::vector< TH2F > &depth)
Definition: DDAxes.h:10
void ShowFit(TH2 *h2d, double x, double y)