CMS 3D CMS Logo

TH2Analyzer.cc
Go to the documentation of this file.
2 
3 #include "TF1.h"
4 #include "TH1D.h"
5 #include "TH2D.h"
6 #include "TPad.h"
7 
8 #include <algorithm>
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 
13 using namespace std;
14 
15 // remove?
16 void TH2Analyzer::Eval(int rebinFactor) {
17  // std::cout << "Eval!" << std::endl;
18 
19  Reset();
20 
21  const string bname = hist2D_->GetName();
22  const string rebinName = bname + "_rebin";
23  rebinnedHist2D_ = (TH2D *)hist2D_->Clone(rebinName.c_str());
24  rebinnedHist2D_->RebinX(rebinFactor);
25 
26  const string averageName = bname + "_average";
27  average_ = new TH1D(averageName.c_str(),
28  "arithmetic average",
29  rebinnedHist2D_->GetNbinsX(),
30  rebinnedHist2D_->GetXaxis()->GetXmin(),
31  rebinnedHist2D_->GetXaxis()->GetXmax());
32 
33  const string rmsName = bname + "_RMS";
34  RMS_ = new TH1D(rmsName.c_str(),
35  "RMS",
36  rebinnedHist2D_->GetNbinsX(),
37  rebinnedHist2D_->GetXaxis()->GetXmin(),
38  rebinnedHist2D_->GetXaxis()->GetXmax());
39 
40  const string sigmaGaussName = bname + "_sigmaGauss";
41  sigmaGauss_ = new TH1D(sigmaGaussName.c_str(),
42  "sigmaGauss",
43  rebinnedHist2D_->GetNbinsX(),
44  rebinnedHist2D_->GetXaxis()->GetXmin(),
45  rebinnedHist2D_->GetXaxis()->GetXmax());
46 
47  const string meanXName = bname + "_meanX";
48  meanXslice_ = new TH1D(meanXName.c_str(),
49  "meanX",
50  rebinnedHist2D_->GetNbinsX(),
51  rebinnedHist2D_->GetXaxis()->GetXmin(),
52  rebinnedHist2D_->GetXaxis()->GetXmax());
53 
54  ProcessSlices(rebinnedHist2D_);
55 }
56 
58  if (rebinnedHist2D_)
59  delete rebinnedHist2D_;
60  if (average_)
61  delete average_;
62  if (RMS_)
63  delete RMS_;
64  if (sigmaGauss_)
65  delete sigmaGauss_;
66  if (meanXslice_)
67  delete meanXslice_;
68 
69  // for( unsigned i=0; i<parameters_.size(); ++i) {
70  // delete parameters_[i];
71  //}
72 
73  // parameters_.clear();
74 }
75 
76 void TH2Analyzer::Eval(const int rebinFactor, const int binxmin, const int binxmax, const bool cst_binning) {
77  Reset();
78  const string bname = hist2D_->GetName();
79  const string rebinName = bname + "_rebin";
80 
81  if (binxmax > hist2D_->GetNbinsX()) {
82  std::cout << "Error: TH2Analyzer.cc: binxmax>hist2D_->GetNbinsX()" << std::endl;
83  return;
84  }
85 
86  if (cst_binning) {
87  // std::cout << "hist2D_->GetXaxis()->GetBinLowEdge(" << binxmin << ") = "
88  // << hist2D_->GetXaxis()->GetBinLowEdge(binxmin) << std::endl; std::cout <<
89  // "hist2D_->GetXaxis()->GetBinUpEdge(" << binxmax << ") = " <<
90  // hist2D_->GetXaxis()->GetBinUpEdge(binxmax) << std::endl; std::cout <<
91  // "hist2D_->GetNbinsY() = " << hist2D_->GetNbinsY() << std::endl; std::cout
92  // << "hist2D_->GetYaxis()->GetXmin() = " << hist2D_->GetYaxis()->GetXmin()
93  // << std::endl; std::cout << "hist2D_->GetYaxis()->GetXmax() = " <<
94  // hist2D_->GetYaxis()->GetXmax() << std::endl;
95  rebinnedHist2D_ = new TH2D(rebinName.c_str(),
96  "rebinned histo",
97  binxmax - binxmin + 1,
98  hist2D_->GetXaxis()->GetBinLowEdge(binxmin),
99  hist2D_->GetXaxis()->GetBinUpEdge(binxmax),
100  hist2D_->GetNbinsY(),
101  hist2D_->GetYaxis()->GetXmin(),
102  hist2D_->GetYaxis()->GetXmax());
103  for (int binyc = 1; binyc < hist2D_->GetNbinsY() + 1; ++binyc) {
104  for (int binxc = binxmin; binxc < binxmax + 1; ++binxc) {
105  // std::cout << "hist2D_->GetBinContent(" << binxc << "," << binyc << ")
106  // = " << hist2D_->GetBinContent(binxc,binyc) << std::endl; std::cout <<
107  // "hist2D_->GetBinError(" << binxc << "," << binyc << ") = " <<
108  // hist2D_->GetBinError(binxc,binyc) << std::endl; std::cout <<
109  // "binxc-binxmin+1 = " << binxc-binxmin+1 << std::endl;
110  rebinnedHist2D_->SetBinContent(binxc - binxmin + 1, binyc, hist2D_->GetBinContent(binxc, binyc));
111  rebinnedHist2D_->SetBinError(binxc - binxmin + 1, binyc, hist2D_->GetBinError(binxc, binyc));
112  }
113  }
114  rebinnedHist2D_->RebinX(rebinFactor);
115 
116  const string averageName = bname + "_average";
117  average_ = new TH1D(averageName.c_str(),
118  "arithmetic average",
119  rebinnedHist2D_->GetNbinsX(),
120  rebinnedHist2D_->GetXaxis()->GetXmin(),
121  rebinnedHist2D_->GetXaxis()->GetXmax());
122 
123  const string rmsName = bname + "_RMS";
124  RMS_ = new TH1D(rmsName.c_str(),
125  "RMS",
126  rebinnedHist2D_->GetNbinsX(),
127  rebinnedHist2D_->GetXaxis()->GetXmin(),
128  rebinnedHist2D_->GetXaxis()->GetXmax());
129 
130  const string sigmaGaussName = bname + "_sigmaGauss";
131  sigmaGauss_ = new TH1D(sigmaGaussName.c_str(),
132  "sigmaGauss",
133  rebinnedHist2D_->GetNbinsX(),
134  rebinnedHist2D_->GetXaxis()->GetXmin(),
135  rebinnedHist2D_->GetXaxis()->GetXmax());
136 
137  const string meanXName = bname + "_meanX";
138  meanXslice_ = new TH1D(meanXName.c_str(),
139  "meanX",
140  rebinnedHist2D_->GetNbinsX(),
141  rebinnedHist2D_->GetXaxis()->GetXmin(),
142  rebinnedHist2D_->GetXaxis()->GetXmax());
143  } else {
144  // binning is not constant, but made to obtain almost the same number of
145  // events in each bin
146 
147  // std::cout << "binxmax-binxmin+1 = " << binxmax-binxmin+1 << std::endl;
148  // std::cout << "rebinFactor = " << rebinFactor << std::endl;
149  // std::cout << "(binxmax-binxmin+1)/rebinFactor = " <<
150  // (binxmax-binxmin+1)/rebinFactor << std::endl; std::cout <<
151  // "((binxmax-binxmin+1)%rebinFactor) = " <<
152  // ((binxmax-binxmin+1)%rebinFactor) << std::endl; std::cout <<
153  // "abs((binxmax-binxmin+1)/rebinFactor) = " <<
154  // std::abs((binxmax-binxmin+1)/rebinFactor) << std::endl;
155 
156  unsigned int nbin = 0;
157  if (((binxmax - binxmin + 1) % rebinFactor) != 0.0) {
158  nbin = std::abs((binxmax - binxmin + 1) / rebinFactor) + 1;
159  } else
160  nbin = (binxmax - binxmin + 1) / rebinFactor;
161 
162  double *xlow = new double[nbin + 1];
163  int *binlow = new int[nbin + 1];
164 
165  TH1D *h0_slice1 = hist2D_->ProjectionY("h0_slice1", binxmin, binxmax, "");
166  const unsigned int totalNumberOfEvents = static_cast<unsigned int>(h0_slice1->GetEntries());
167  // std::cout << "totalNumberOfEvents = " << totalNumberOfEvents <<
168  // std::endl;
169  delete h0_slice1;
170 
171  unsigned int neventsc = 0;
172  unsigned int binXmaxc = binxmin + 1;
173  xlow[0] = hist2D_->GetXaxis()->GetBinLowEdge(binxmin);
174  binlow[0] = binxmin;
175  for (unsigned int binc = 1; binc < nbin; ++binc) {
176  while (neventsc < binc * totalNumberOfEvents / nbin) {
177  TH1D *h0_slice1c = hist2D_->ProjectionY("h0_slice1", binxmin, binXmaxc, "");
178  neventsc = static_cast<unsigned int>(h0_slice1c->GetEntries());
179  // //std::cout << "FL : neventsc = " << neventsc << std::endl;
180  // //std::cout << "FL : binXmaxc = " << binXmaxc << std::endl;
181  ++binXmaxc;
182  delete h0_slice1c;
183  }
184  // std::cout << "binXmaxc-1 = " << binXmaxc-1 << std::endl;
185  binlow[binc] = binXmaxc - 1;
186  xlow[binc] = hist2D_->GetXaxis()->GetBinLowEdge(binXmaxc - 1);
187  }
188  xlow[nbin] = hist2D_->GetXaxis()->GetBinUpEdge(binxmax);
189  binlow[nbin] = binxmax;
190 
191  // for (unsigned int binc=0;binc<nbin+1;++binc)
192  //{
193  // std::cout << "xlow[" << binc << "] = " << xlow[binc] << std::endl;
194  //}
195 
196  rebinnedHist2D_ = new TH2D(rebinName.c_str(),
197  "rebinned histo",
198  nbin,
199  xlow,
200  hist2D_->GetNbinsY(),
201  hist2D_->GetYaxis()->GetXmin(),
202  hist2D_->GetYaxis()->GetXmax());
203  for (int binyc = 1; binyc < hist2D_->GetNbinsY() + 1; ++binyc) {
204  for (unsigned int binxc = 1; binxc < nbin + 1; ++binxc) {
205  double sum = 0.0;
206  for (int binxh2c = binlow[binxc - 1]; binxh2c < binlow[binxc]; ++binxh2c) {
207  sum += hist2D_->GetBinContent(binxh2c, binyc);
208  }
209  rebinnedHist2D_->SetBinContent(binxc, binyc, sum);
210  }
211  }
212 
213  const string averageName = bname + "_average";
214  average_ = new TH1D(averageName.c_str(), "arithmetic average", nbin, xlow);
215 
216  const string rmsName = bname + "_RMS";
217  RMS_ = new TH1D(rmsName.c_str(), "RMS", nbin, xlow);
218 
219  const string sigmaGaussName = bname + "_sigmaGauss";
220  sigmaGauss_ = new TH1D(sigmaGaussName.c_str(), "sigmaGauss", nbin, xlow);
221 
222  const string meanXName = bname + "_meanX";
223  meanXslice_ = new TH1D(meanXName.c_str(), "meanX", nbin, xlow);
224  delete[] xlow;
225  delete[] binlow;
226  }
227  ProcessSlices(rebinnedHist2D_);
228 }
229 
231  // std::cout << "ProcessSlices!" << std::endl;
232 
233  TH1::AddDirectory(false);
234 
235  for (int i = 1; i <= histo->GetNbinsX(); ++i) {
236  TH1D *proj = histo->ProjectionY("toto", i, i);
237  const double mean = proj->GetMean();
238  const double rms = proj->GetRMS();
239  // std::cout << "mean = " << mean << std::endl;
240  // std::cout << "rms = " << rms << std::endl;
241  average_->SetBinContent(i, mean);
242  average_->SetBinError(i, proj->GetMeanError());
243  RMS_->SetBinContent(i, rms);
244  RMS_->SetBinError(i, proj->GetRMSError());
245 
246  const double error = (histo->GetXaxis()->GetBinUpEdge(i) - histo->GetXaxis()->GetBinLowEdge(i)) / 2.0;
247  // std::cout << "error = " << error << std::endl;
248  meanXslice_->SetBinContent(i, histo->GetXaxis()->GetBinLowEdge(i) + error);
249  meanXslice_->SetBinError(i, error);
250  // std::cout << "histo->GetXaxis()->GetBinLowEdge(" << i << ") = "
251  // << histo->GetXaxis()->GetBinLowEdge(i) << std::endl;
252  // std::cout << "meanXslice_->GetBinError(" << i << ") = "
253  // << meanXslice_->GetBinError(i) << std::endl;
254  ProcessSlice(i, proj);
255  delete proj;
256  }
257 
258  TH1::AddDirectory(true);
259 }
260 
261 void TH2Analyzer::ProcessSlice(const int i, TH1D *proj) const {
262  // const double mean = proj->GetMean();
263  const double rms = proj->GetRMS();
264  // std::cout << "FL: mean = " << mean << std::endl;
265  // std::cout << "FL: rms = " << rms << std::endl;
266 
267  if (rms != 0.0) {
268  const double fitmin = proj->GetMean() - proj->GetRMS();
269  const double fitmax = proj->GetMean() + proj->GetRMS();
270 
271  // std::cout << "i = " << i << std::endl;
272  // std::cout << "fitmin = " << fitmin << std::endl;
273  // std::cout << "fitmax = " << fitmax << std::endl;
274 
275  // proj->Draw();
276  TF1 *f1 = new TF1("f1", "gaus", fitmin, fitmax);
277  f1->SetParameters(proj->GetRMS(), proj->GetMean(), proj->GetBinContent(proj->GetMaximumBin()));
278  proj->Fit(f1, "R", "", proj->GetXaxis()->GetXmin(), proj->GetXaxis()->GetXmax());
279 
280  // std::ostringstream oss;
281  // oss << i;
282  // const std::string plotfitname="Plots/fitbin_"+oss.str()+".eps";
283  // gPad->SaveAs( plotfitname.c_str() );
284  // std::cout << "param1 = " << f1->GetParameter(1) << std::endl;
285  // std::cout << "param2 = " << f1->GetParameter(2) << std::endl;
286  // std::cout << "paramError2 = " << f1->GetParError(2) << std::endl;
287 
288  sigmaGauss_->SetBinContent(i, f1->GetParameter(2));
289  sigmaGauss_->SetBinError(i, f1->GetParError(2));
290  delete f1;
291  } else {
292  sigmaGauss_->SetBinContent(i, 0.0);
293  sigmaGauss_->SetBinError(i, 0.0);
294  }
295 }
void ProcessSlice(const int i, TH1D *histo) const
Definition: TH2Analyzer.cc:261
void Eval(const int rebinFactor)
Definition: TH2Analyzer.cc:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void ProcessSlices(const TH2D *histo)
Definition: TH2Analyzer.cc:230
void Reset()
Definition: TH2Analyzer.cc:57
void Reset(std::vector< TH2F > &depth)