CMS 3D CMS Logo

LA_Results.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include <regex>
6 #include <boost/algorithm/string/erase.hpp>
7 #include <TF1.h>
8 #include <TGraphErrors.h>
9 #include <TProfile.h>
10 
12  Result p;
13  const std::string base = boost::erase_all_copy(name, method(m));
14 
15  const TH1* const h = book[name];
16  const TH1* const reco = book[base + "_reconstruction"];
17  const TH1* const field = book[base + "_field"];
18 
19  if (reco)
20  p.reco = std::make_pair<float, float>(reco->GetMean(), reco->GetMeanError());
21  if (field)
22  p.field = field->GetMean();
23  if (h) {
24  switch (m) {
25  case WIDTH: {
26  const TF1* const f = h->GetFunction("LA_profile_fit");
27  if (!f)
28  break;
29  p.measured = std::make_pair<float, float>(f->GetParameter(0), f->GetParError(0));
30  p.chi2 = f->GetChisquare();
31  p.ndof = f->GetNDF();
32  p.entries = (unsigned)(h->GetEntries());
33  break;
34  }
35  case PROB1:
36  case AVGV2:
37  case AVGV3:
38  case RMSV2:
39  case RMSV3: /*case MULTI:*/ {
40  const TF1* const f = h->GetFunction("SymmetryFit");
41  if (!f)
42  break;
43  p.measured = std::make_pair<float, float>(p.reco.first + f->GetParameter(0), f->GetParameter(1));
44  p.chi2 = f->GetParameter(2);
45  p.ndof = (unsigned)(f->GetParameter(3));
46  p.entries = (m & PROB1) ? (unsigned)book[base + "_w1"]->GetEntries()
47  : (m & (AVGV2 | RMSV2)) ? (unsigned)book[base + method(AVGV2, false)]->GetEntries()
48  : (m & (AVGV3 | RMSV3)) ? (unsigned)book[base + method(AVGV3, false)]->GetEntries()
49  : 0;
50  break;
51  }
52  default:
53  break;
54  }
55  }
56  return p;
57 }
58 
59 std::map<uint32_t, LA_Filler_Fitter::Result> LA_Filler_Fitter::module_results(const Book& book, const Method m) {
60  std::map<uint32_t, Result> results;
61  for (Book::const_iterator it = book.begin(".*_module\\d*" + method(m)); it != book.end(); ++it) {
62  const uint32_t detid =
63  std::stoul(std::regex_replace(it->first, std::regex(".*_module(\\d*)_.*"), std::string("\\1")));
64  results[detid] = result(m, it->first, book);
65  }
66  return results;
67 }
68 
69 std::map<std::string, LA_Filler_Fitter::Result> LA_Filler_Fitter::layer_results(const Book& book, const Method m) {
70  std::map<std::string, Result> results;
71  for (Book::const_iterator it = book.begin(".*layer\\d.*" + method(m)); it != book.end(); ++it) {
72  const std::string name = boost::erase_all_copy(it->first, method(m));
73  results[name] = result(m, it->first, book);
74  }
75  return results;
76 }
77 
78 std::map<std::string, std::vector<LA_Filler_Fitter::Result> > LA_Filler_Fitter::ensemble_results(const Book& book,
79  const Method m) {
80  std::map<std::string, std::vector<Result> > results;
81  for (Book::const_iterator it = book.begin(".*_sample.*" + method(m)); it != book.end(); ++it) {
82  const std::string name = std::regex_replace(it->first, std::regex("sample\\d*_"), "");
83  results[name].push_back(result(m, it->first, book));
84  }
85  return results;
86 }
87 
89  typedef std::map<std::string, std::vector<Result> > results_t;
90  results_t results;
91  for (int m = FIRST_METHOD; m <= LAST_METHOD; m <<= 1)
92  if (methods_ & m) {
93  results_t g = ensemble_results(book, (Method)(m));
94  results.insert(g.begin(), g.end());
95  }
96 
97  for (auto const& group : results) {
98  const std::string name = group.first;
99  for (auto const& p : group.second) {
100  const float pad = (ensembleUp_ - ensembleLow_) / 10;
101  book.fill(p.reco.first, name + "_ensembleReco", 12 * ensembleBins_, ensembleLow_ - pad, ensembleUp_ + pad);
102  book.fill(p.measured.first, name + "_measure", 12 * ensembleBins_, ensembleLow_ - pad, ensembleUp_ + pad);
103  book.fill(p.measured.second, name + "_merr", 500, 0, 0.01);
104  book.fill((p.measured.first - p.reco.first) / p.measured.second, name + "_pull", 500, -10, 10);
105  }
106  //Need our own copy for thread safety
107  TF1 gaus("mygaus", "gaus");
108  book[name + "_measure"]->Fit(&gaus, "LLQ");
109  book[name + "_merr"]->Fit(&gaus, "LLQ");
110  book[name + "_pull"]->Fit(&gaus, "LLQ");
111  }
112 }
113 
114 std::map<std::string, std::vector<LA_Filler_Fitter::EnsembleSummary> > LA_Filler_Fitter::ensemble_summary(
115  const Book& book) {
116  std::map<std::string, std::vector<EnsembleSummary> > summary;
117  for (Book::const_iterator it = book.begin(".*_ensembleReco"); it != book.end(); ++it) {
118  const std::string base = boost::erase_all_copy(it->first, "_ensembleReco");
119 
120  const TH1* const reco = it->second;
121  const TH1* const measure = book[base + "_measure"];
122  const TH1* const merr = book[base + "_merr"];
123  const TH1* const pull = book[base + "_pull"];
124 
126  s.samples = reco->GetEntries();
127  s.truth = reco->GetMean();
128  s.meanMeasured = std::make_pair<float, float>(measure->GetFunction("gaus")->GetParameter(1),
129  measure->GetFunction("gaus")->GetParError(1));
130  s.sigmaMeasured = std::make_pair<float, float>(measure->GetFunction("gaus")->GetParameter(2),
131  measure->GetFunction("gaus")->GetParError(2));
132  s.meanUncertainty = std::make_pair<float, float>(merr->GetFunction("gaus")->GetParameter(1),
133  merr->GetFunction("gaus")->GetParError(1));
134  s.pull = std::make_pair<float, float>(pull->GetFunction("gaus")->GetParameter(2),
135  pull->GetFunction("gaus")->GetParError(2));
136 
137  const std::string name = std::regex_replace(base, std::regex("ensembleBin\\d*_"), "");
138  summary[name].push_back(s);
139  }
140  return summary;
141 }
142 
143 std::pair<std::pair<float, float>, std::pair<float, float> > LA_Filler_Fitter::offset_slope(
144  const std::vector<LA_Filler_Fitter::EnsembleSummary>& ensembles) {
145  try {
146  std::vector<float> x, y, xerr, yerr;
147  for (auto const& ensemble : ensembles) {
148  x.push_back(ensemble.truth);
149  xerr.push_back(0);
150  y.push_back(ensemble.meanMeasured.first);
151  yerr.push_back(ensemble.meanMeasured.second);
152  }
153  TGraphErrors graph(x.size(), &(x[0]), &(y[0]), &(xerr[0]), &(yerr[0]));
154  //Need our own copy for thread safety
155  TF1 pol1("mypol1", "pol1");
156  graph.Fit(&pol1);
157  const TF1* const fit = graph.GetFunction("pol1");
158 
159  return std::make_pair(std::make_pair(fit->GetParameter(0), fit->GetParError(0)),
160  std::make_pair(fit->GetParameter(1), fit->GetParError(1)));
161  } catch (edm::Exception const& e) {
162  std::cerr << "Fitting Line Failed " << std::endl << e << std::endl;
163  return std::make_pair(std::make_pair(0, 0), std::make_pair(0, 0));
164  }
165 }
166 
167 float LA_Filler_Fitter::pull(const std::vector<LA_Filler_Fitter::EnsembleSummary>& ensembles) {
168  float p(0), w(0);
169  for (auto const& ensemble : ensembles) {
170  const float unc2 = pow(ensemble.pull.second, 2);
171  p += ensemble.pull.first / unc2;
172  w += 1 / unc2;
173  }
174  return p / w;
175 }
176 
177 std::ostream& operator<<(std::ostream& strm, const LA_Filler_Fitter::Result& r) {
178  return strm << r.reco.first << "\t" << r.reco.second << "\t" << r.measured.first << "\t" << r.measured.second << "\t"
179  << r.calMeasured.first << "\t" << r.calMeasured.second << "\t" << r.field << "\t" << r.chi2 << "\t"
180  << r.ndof << "\t" << r.entries;
181 }
182 
183 std::ostream& operator<<(std::ostream& strm, const LA_Filler_Fitter::EnsembleSummary& e) {
184  return strm << e.truth << "\t" << e.meanMeasured.first << "\t" << e.meanMeasured.second << "\t"
185  << e.sigmaMeasured.first << "\t" << e.sigmaMeasured.second << "\t" << e.meanUncertainty.first << "\t"
186  << e.meanUncertainty.second << "\t" << e.pull.first << "\t" << e.pull.second << "\t" << e.samples;
187 }
static std::map< std::string, Result > layer_results(const Book &, const Method)
Definition: LA_Results.cc:69
T w() const
void summarize_ensembles(Book &) const
Definition: LA_Results.cc:88
base
Main Program
Definition: newFWLiteAna.py:92
static std::pair< std::pair< float, float >, std::pair< float, float > > offset_slope(const std::vector< EnsembleSummary > &)
Definition: LA_Results.cc:143
static std::map< std::string, std::vector< EnsembleSummary > > ensemble_summary(const Book &)
Definition: LA_Results.cc:114
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
static Result result(Method, const std::string name, const Book &)
Definition: LA_Results.cc:11
double f[11][100]
iterator begin(string_t re=".*")
Definition: Book.h:57
static std::string method(Method m, bool fit=true)
static std::map< std::string, std::vector< Result > > ensemble_results(const Book &, const Method)
Definition: LA_Results.cc:78
void fill(double_t X, const char *name, uint_t NbinsX, double_t Xlow, double_t Xup, double_t W=1)
Definition: Book.h:96
static std::map< uint32_t, Result > module_results(const Book &, const Method)
Definition: LA_Results.cc:59
const double ensembleUp_
fixed size matrix
boost::filter_iterator< match_name, book_t::const_iterator > const_iterator
Definition: Book.h:56
static float pull(const std::vector< EnsembleSummary > &)
Definition: LA_Results.cc:167
Definition: Book.h:16
results
Definition: mysort.py:8
std::ostream & operator<<(std::ostream &strm, const LA_Filler_Fitter::Result &r)
Definition: LA_Results.cc:177
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
const double ensembleLow_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
iterator end(string_t re=".*")
Definition: Book.h:65