CMS 3D CMS Logo

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