CMS 3D CMS Logo

PVValidationHelpers.cc
Go to the documentation of this file.
3 #include <cassert>
4 #include <numeric>
5 #include <algorithm>
6 #include <iostream>
7 #include "TMath.h"
8 #include "TH1.h"
9 #include "TF1.h"
10 
11 //*************************************************************
12 void PVValHelper::add(std::map<std::string, TH1*>& h, TH1* hist)
13 //*************************************************************
14 {
15  h[hist->GetName()]=hist;
16  hist->StatOverflows(kTRUE);
17 }
18 
19 //*************************************************************
20 void PVValHelper::fill(std::map<std::string, TH1*>& h,const std::string& s, double x)
21 //*************************************************************
22 {
23  if(h.count(s)==0){
24  edm::LogWarning("PVValidationHelpers") << "Trying to fill non-existing Histogram named " << s << std::endl;
25  return;
26  }
27  h[s]->Fill(x);
28 }
29 
30 //*************************************************************
31 void PVValHelper::fill(std::map<std::string, TH1*>& h,const std::string& s, double x, double y)
32 //*************************************************************
33 {
34  if(h.count(s)==0){
35  edm::LogWarning("PVValidationHelpers") << "Trying to fill non-existing Histogram named " << s << std::endl;
36  return;
37  }
38  h[s]->Fill(x,y);
39 }
40 
41 //*************************************************************
42 void PVValHelper::fillByIndex(std::vector<TH1F*>& h, unsigned int index, double x,std::string tag)
43 //*************************************************************
44 {
45  assert(!h.empty());
46  if(index <= h.size()){
47  h[index]->Fill(x);
48  } else {
49  edm::LogWarning("PVValidationHelpers") << "Trying to fill non-existing Histogram with index " << index << " for array with size: "<<h.size()<<" tag: "<< tag<< std::endl;
50  return;
51  }
52 }
53 
54 //*************************************************************
55 void PVValHelper::shrinkHistVectorToFit(std::vector<TH1F*>&h, unsigned int desired_size)
56 //*************************************************************
57 {
58  h.erase(h.begin()+desired_size,h.end());
59 }
60 
61 //*************************************************************
63 //*************************************************************
64 {
66  switch(type)
67  {
68  // absoulte
69 
70  case PVValHelper::dxy :
71  returnType = std::make_tuple("dxy","d_{xy}","[#mum]");
72  break;
73  case PVValHelper::dx :
74  returnType = std::make_tuple("dx","d_{x}","[#mum]");
75  break;
76  case PVValHelper::dy :
77  returnType = std::make_tuple("dy","d_{y}","[#mum]");
78  break;
79  case PVValHelper::dz :
80  returnType = std::make_tuple("dz","d_{z}","[#mum]");
81  break;
82  case PVValHelper::IP2D :
83  returnType = std::make_tuple("IP2D","IP_{2D}","[#mum]");
84  break;
85  case PVValHelper::resz :
86  returnType = std::make_tuple("resz","z_{trk}-z_{vtx}","[#mum]");
87  break;
88  case PVValHelper::IP3D :
89  returnType = std::make_tuple("IP3D","IP_{3D}","[#mum]");
90  break;
91  case PVValHelper::d3D :
92  returnType = std::make_tuple("d3D","d_{3D}","[#mum]");
93  break;
94 
95  // normalized
96 
98  returnType = std::make_tuple("norm_dxy","d_{xy}/#sigma_{d_{xy}}","");
99  break;
100  case PVValHelper::norm_dx :
101  returnType = std::make_tuple("norm_dx","d_{x}/#sigma_{d_{x}}","");
102  break;
103  case PVValHelper::norm_dy :
104  returnType = std::make_tuple("norm_dy","d_{y}/#sigma_{d_{y}}","");
105  break;
106  case PVValHelper::norm_dz :
107  returnType = std::make_tuple("norm_dz","d_{z}/#sigma_{d_{z}}","");
108  break;
110  returnType = std::make_tuple("norm_IP2D","IP_{2D}/#sigma_{IP_{2D}}","");
111  break;
113  returnType = std::make_tuple("norm_resz","z_{trk}-z_{vtx}/#sigma_{res_{z}}","");
114  break;
116  returnType = std::make_tuple("norm_IP3D","IP_{3D}/#sigma_{IP_{3D}}","");
117  break;
118  case PVValHelper::norm_d3D :
119  returnType = std::make_tuple("norm_d3D","d_{3D}/#sigma_{d_{3D}}","");
120  break;
121 
122  default:
123  edm::LogWarning("PVValidationHelpers") <<" getTypeString() unknown residual type: "<<type<<std::endl;
124  }
125 
126  return returnType;
127 
128 }
129 
130 //*************************************************************
132 //*************************************************************
133 {
134  PVValHelper::plotLabels returnVar;
135  switch(var)
136  {
137  case PVValHelper::phi :
138  returnVar = std::make_tuple("phi","#phi","[rad]");
139  break;
140  case PVValHelper::eta :
141  returnVar = std::make_tuple("eta","#eta","");
142  break;
143  case PVValHelper::pT :
144  returnVar = std::make_tuple("pT","p_{T}","[GeV]");
145  break;
147  returnVar = std::make_tuple("pTCentral","p_{T} |#eta|<1.","[GeV]");
148  break;
149  case PVValHelper::ladder :
150  returnVar = std::make_tuple("ladder","ladder number","");
151  break;
152  case PVValHelper::modZ :
153  returnVar = std::make_tuple("modZ","module number","");
154  break;
155  default:
156  edm::LogWarning("PVValidationHelpers") <<" getVarString() unknown plot variable: "<<var<<std::endl;
157  }
158 
159  return returnVar;
160 
161 }
162 
163 //*************************************************************
164 std::vector<float> PVValHelper::generateBins(int n, float start,float range)
165 //*************************************************************
166 {
167 
168  std::vector<float> v(n);
169  float interval = range/(n-1);
170  std::iota(v.begin(),v.end(),1.);
171 
172  //std::cout<<" interval:"<<interval<<std::endl;
173  //for(float &a : v) { std::cout<< a << " "; }
174  //std::cout<< "\n";
175 
176  std::for_each(begin(v), end(v), [&](float& a) { a = start+((a-1)*interval); });
177 
178  return v;
179 
180 }
181 
182 //*************************************************************
184 //*************************************************************
185 {
186  double median=0.;
187  double q = 0.5; // 0.5 quantile for "median"
188  // protect against empty histograms
189  if(histo->Integral()!=0){
190  histo->GetQuantiles(1, &median, &q);
191  }
192 
193  Measurement1D result(median,median/TMath::Sqrt(histo->GetEntries()));
194 
195  return result;
196 
197 }
198 
199 //*************************************************************
201 //*************************************************************
202 {
203 
204  int nbins = histo->GetNbinsX();
205  double median = getMedian(histo).value();
206  double x_lastBin = histo->GetBinLowEdge(nbins+1);
207  const char *HistoName =histo->GetName();
208  std::string Finalname = "resMed_";
209  Finalname.append(HistoName);
210  TH1F *newHisto = new TH1F(Finalname.c_str(),Finalname.c_str(),nbins,0.,x_lastBin);
211  double *residuals = new double[nbins];
212  const float *weights = histo->GetArray();
213 
214  for (int j = 0; j < nbins; j++) {
215  residuals[j] = std::abs(median - histo->GetBinCenter(j+1));
216  newHisto->Fill(residuals[j],weights[j]);
217  }
218 
219  double theMAD = (PVValHelper::getMedian(newHisto).value())*1.4826;
220 
221  delete[] residuals; residuals=nullptr;
222  newHisto->Delete("");
223 
224  Measurement1D result(theMAD,theMAD/histo->GetEntries());
225  return result;
226 }
227 
228 
229 //*************************************************************
230 std::pair<Measurement1D, Measurement1D> PVValHelper::fitResiduals(TH1 *hist)
231 //*************************************************************
232 {
233  //float fitResult(9999);
234  //if (hist->GetEntries() < 20) return ;
235 
236  float mean = hist->GetMean();
237  float sigma = hist->GetRMS();
238 
239  TF1 func("tmp", "gaus", mean - 1.5*sigma, mean + 1.5*sigma);
240  if (0 == hist->Fit(&func,"QNR")) { // N: do not blow up file by storing fit!
241  mean = func.GetParameter(1);
242  sigma = func.GetParameter(2);
243  // second fit: three sigma of first fit around mean of first fit
244  func.SetRange(mean - 2*sigma, mean + 2*sigma);
245  // I: integral gives more correct results if binning is too wide
246  // L: Likelihood can treat empty bins correctly (if hist not weighted...)
247  if (0 == hist->Fit(&func, "Q0LR")) {
248  if (hist->GetFunction(func.GetName())) { // Take care that it is later on drawn:
249  hist->GetFunction(func.GetName())->ResetBit(TF1::kNotDraw);
250  }
251  }
252  }
253 
254  float res_mean = func.GetParameter(1);
255  float res_width = func.GetParameter(2);
256 
257  float res_mean_err = func.GetParError(1);
258  float res_width_err = func.GetParError(2);
259 
260  Measurement1D resultM(res_mean,res_mean_err);
261  Measurement1D resultW(res_width,res_width_err);
262 
263  std::pair<Measurement1D, Measurement1D> result;
264 
265  result = std::make_pair(resultM,resultW);
266  return result;
267 }
Definition: start.py:1
type
Definition: HCALResponse.h:21
Measurement1D getMedian(TH1F *histo)
void fillByIndex(std::vector< TH1F * > &h, unsigned int index, double x, std::string tag="")
std::tuple< std::string, std::string, std::string > plotLabels
std::pair< Measurement1D, Measurement1D > fitResiduals(TH1 *hist)
std::vector< float > generateBins(int n, float start, float range)
Measurement1D getMAD(TH1F *histo)
plotLabels getVarString(plotVariable var)
T x() const
Cartesian x coordinate.
edm::TypeWithDict returnType(const edm::FunctionWithDict &func)
Definition: returnType.cc:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define end
Definition: vmac.h:37
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
plotLabels getTypeString(residualType type)
void fill(std::map< std::string, TH1 * > &h, const std::string &s, double x)
void shrinkHistVectorToFit(std::vector< TH1F * > &h, unsigned int desired_size)
double value() const
Definition: Measurement1D.h:28
std::string HistoName
#define begin
Definition: vmac.h:30
double a
Definition: hdecay.h:121