CMS 3D CMS Logo

ErrorsAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ErrorsAnalyzer
4 // Class: ErrorsAnalyzer
5 //
13 //
14 // Original Author: Marco De Mattia
15 // Created: Thu Sep 11 12:16:00 CEST 2008
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
29 
30 #include <TH1D.h>
31 #include <TProfile.h>
32 #include <TString.h>
33 #include <TCanvas.h>
34 #include <TGraphAsymmErrors.h>
35 #include <TROOT.h>
36 
39 #include "MuScleFitUtils.h"
40 
41 //
42 // class declaration
43 //
44 
46 public:
47  explicit ErrorsAnalyzer(const edm::ParameterSet&);
48  ~ErrorsAnalyzer() override;
49 
50 private:
51  void analyze(const edm::Event&, const edm::EventSetup&) override;
52  void fillHistograms();
53  void drawHistograms(const TProfile* histo,
54  const TProfile* histoPlusErr,
55  const TProfile* histoMinusErr,
56  const TString& type);
57  void fillValueError();
58  void endJob() override{};
59 
60  TString treeFileName_;
62  uint32_t maxEvents_;
63  TString outputFileName_;
64  int ptBins_;
65  double ptMin_;
66  double ptMax_;
67  int etaBins_;
68  double etaMin_;
69  double etaMax_;
70  bool debug_;
71 
72  std::vector<double> parameters_;
73  std::vector<double> errors_;
74  std::vector<int> errorFactors_;
75 
76  std::vector<double> valuePlusError_;
77  std::vector<double> valueMinusError_;
78 
79  TProfile* sigmaPtVsEta_;
82 
83  TProfile* sigmaPtVsPt_;
86 
87  // Mass resolution
88  TProfile* sigmaMassVsEta_;
91 
92  TProfile* sigmaMassVsPt_;
95 };
96 
98  : treeFileName_(iConfig.getParameter<std::string>("InputFileName")),
99  resolFitType_(iConfig.getParameter<int>("ResolFitType")),
100  maxEvents_(iConfig.getParameter<int>("MaxEvents")),
101  outputFileName_(iConfig.getParameter<std::string>("OutputFileName")),
102  ptBins_(iConfig.getParameter<int>("PtBins")),
103  ptMin_(iConfig.getParameter<double>("PtMin")),
104  ptMax_(iConfig.getParameter<double>("PtMax")),
105  etaBins_(iConfig.getParameter<int>("EtaBins")),
106  etaMin_(iConfig.getParameter<double>("EtaMin")),
107  etaMax_(iConfig.getParameter<double>("EtaMax")),
108  debug_(iConfig.getParameter<bool>("Debug")) {
109  parameters_ = iConfig.getParameter<std::vector<double> >("Parameters");
110  errors_ = iConfig.getParameter<std::vector<double> >("Errors");
111  errorFactors_ = iConfig.getParameter<std::vector<int> >("ErrorFactors");
112 
113  if ((parameters_.size() != errors_.size()) || (parameters_.size() != errorFactors_.size())) {
114  std::cout << "Error: parameters and errors have different number of values" << std::endl;
115  exit(1);
116  }
117 
118  fillValueError();
119 
120  sigmaPtVsPt_ = new TProfile("sigmaPtVsPtProfile", "sigmaPtVsPt", ptBins_, ptMin_, ptMax_);
121  sigmaPtVsPtPlusErr_ = new TProfile("sigmaPtVsPtPlusErrProfile", "sigmaPtVsPtPlusErr", ptBins_, ptMin_, ptMax_);
122  sigmaPtVsPtMinusErr_ = new TProfile("sigmaPtVsPtMinusErrProfile", "sigmaPtVsPtMinusErr", ptBins_, ptMin_, ptMax_);
123 
124  sigmaPtVsEta_ = new TProfile("sigmaPtVsEtaProfile", "sigmaPtVsEta", etaBins_, etaMin_, etaMax_);
125  sigmaPtVsEtaPlusErr_ = new TProfile("sigmaPtVsEtaPlusErrProfile", "sigmaPtVsEtaPlusErr", etaBins_, etaMin_, etaMax_);
127  new TProfile("sigmaPtVsEtaMinusErrProfile", "sigmaPtVsEtaMinusErr", etaBins_, etaMin_, etaMax_);
128 
129  sigmaMassVsPt_ = new TProfile("sigmaMassVsPtProfile", "sigmaMassVsPt", ptBins_, ptMin_, ptMax_);
130  sigmaMassVsPtPlusErr_ = new TProfile("sigmaMassVsPtPlusErrProfile", "sigmaMassVsPtPlusErr", ptBins_, ptMin_, ptMax_);
132  new TProfile("sigmaMassVsPtMinusErrProfile", "sigmaMassVsPtMinusErr", ptBins_, ptMin_, ptMax_);
133 
134  sigmaMassVsEta_ = new TProfile("sigmaMassVsEtaProfile", "sigmaMassVsEta", etaBins_, etaMin_, etaMax_);
136  new TProfile("sigmaMassVsEtaPlusErrProfile", "sigmaMassVsEtaPlusErr", etaBins_, etaMin_, etaMax_);
138  new TProfile("sigmaMassVsEtaMinusErrProfile", "sigmaMassVsEtaMinusErr", etaBins_, etaMin_, etaMax_);
139 }
140 
142  valuePlusError_.resize(parameters_.size());
143  valueMinusError_.resize(parameters_.size());
144 
145  std::vector<double>::const_iterator parIt = parameters_.begin();
146  std::vector<double>::const_iterator errIt = errors_.begin();
147  std::vector<int>::const_iterator errFactorIt = errorFactors_.begin();
148  int i = 0;
149  for (; parIt != parameters_.end(); ++parIt, ++errIt, ++errFactorIt, ++i) {
150  valuePlusError_[i] = *parIt + (*errIt) * (*errFactorIt);
151  valueMinusError_[i] = *parIt - (*errIt) * (*errFactorIt);
152  }
153 }
154 
156  gROOT->SetStyle("Plain");
157 
158  fillHistograms();
159 
160  TFile* outputFile = new TFile(outputFileName_, "RECREATE");
161 
164 
167 
168  outputFile->Write();
169  outputFile->Close();
170 }
171 
173  const TProfile* histoPlusErr,
174  const TProfile* histoMinusErr,
175  const TString& type) {
176  TH1D* sigmaPtVsEtaTH1D =
177  new TH1D(type, type, histo->GetNbinsX(), histo->GetXaxis()->GetXmin(), histo->GetXaxis()->GetXmax());
178  TH1D* sigmaPtVsEtaPlusErrTH1D = new TH1D(type + "PlusErr",
179  type + "PlusErr",
180  histo->GetNbinsX(),
181  histo->GetXaxis()->GetXmin(),
182  histo->GetXaxis()->GetXmax());
183  TH1D* sigmaPtVsEtaMinusErrTH1D = new TH1D(type + "MinusErr",
184  type + "MinusErr",
185  histo->GetNbinsX(),
186  histo->GetXaxis()->GetXmin(),
187  histo->GetXaxis()->GetXmax());
188 
189  TH1D* sigmaMassVsEtaTH1D =
190  new TH1D(type, type, histo->GetNbinsX(), histo->GetXaxis()->GetXmin(), histo->GetXaxis()->GetXmax());
191  TH1D* sigmaMassVsEtaPlusErrTH1D = new TH1D(type + "PlusErr",
192  type + "PlusErr",
193  histo->GetNbinsX(),
194  histo->GetXaxis()->GetXmin(),
195  histo->GetXaxis()->GetXmax());
196  TH1D* sigmaMassVsEtaMinusErrTH1D = new TH1D(type + "MinusErr",
197  type + "MinusErr",
198  histo->GetNbinsX(),
199  histo->GetXaxis()->GetXmin(),
200  histo->GetXaxis()->GetXmax());
201 
202  TCanvas* canvas = new TCanvas("canvas" + type, "canvas" + type, 1000, 800);
203  for (int iBin = 1; iBin <= histo->GetNbinsX(); ++iBin) {
204  sigmaPtVsEtaTH1D->SetBinContent(iBin, histo->GetBinContent(iBin));
205  sigmaPtVsEtaPlusErrTH1D->SetBinContent(iBin, histoPlusErr->GetBinContent(iBin));
206  sigmaPtVsEtaMinusErrTH1D->SetBinContent(iBin, histoMinusErr->GetBinContent(iBin));
207  }
208  int numBins = sigmaPtVsEtaTH1D->GetNbinsX();
209  // Draw TGraph with asymmetric errors
210  Double_t* values = sigmaPtVsEtaTH1D->GetArray();
211  Double_t* valuesPlus = sigmaPtVsEtaPlusErrTH1D->GetArray();
212  Double_t* valuesMinus = sigmaPtVsEtaMinusErrTH1D->GetArray();
213  double* posErrors = new double[numBins];
214  double* negErrors = new double[numBins];
215 
216  TGraphAsymmErrors* graphAsymmErrors = new TGraphAsymmErrors(sigmaPtVsEtaTH1D);
217  TGraph* graph = new TGraph(sigmaPtVsEtaTH1D);
218 
219  for (int i = 1; i <= numBins; ++i) {
220  // std::cout << "filling " << i << std::endl;
221  posErrors[i - 1] = valuesPlus[i] - values[i];
222  if (valuesMinus[i] < 0)
223  negErrors[i - 1] = values[i];
224  else
225  negErrors[i - 1] = values[i] - valuesMinus[i];
226 
227  graphAsymmErrors->SetPointEYlow(i - 1, negErrors[i - 1]);
228  graphAsymmErrors->SetPointEYhigh(i - 1, posErrors[i - 1]);
229  }
230 
231  canvas->Draw();
232 
233  delete[] posErrors;
234  delete[] negErrors;
235 
236  graphAsymmErrors->SetTitle("");
237  graphAsymmErrors->SetFillColor(kGray);
238  graphAsymmErrors->Draw("A2");
239  TString title(type);
240  if (type == "Eta")
241  title = "#eta";
242  graphAsymmErrors->GetXaxis()->SetTitle(title);
243  graphAsymmErrors->GetYaxis()->SetTitle("#sigmaPt/Pt");
244  graph->Draw();
245 
246  // graph->SetLineColor(kGray);
247  // graph->SetMarkerColor(kBlack);
248  // graph->Draw("AP");
249 
250  // if( debug_ ) {
251  // sigmaPtVsEtaPlusErrTH1D->SetLineColor(kRed);
252  // sigmaPtVsEtaMinusErrTH1D->SetLineColor(kBlue);
253  // }
254  // else {
255  // sigmaPtVsEtaPlusErrTH1D->SetFillColor(kGray);
256  // sigmaPtVsEtaPlusErrTH1D->SetLineColor(kWhite);
257  // sigmaPtVsEtaMinusErrTH1D->SetFillColor(kWhite);
258  // sigmaPtVsEtaMinusErrTH1D->SetLineColor(kWhite);
259  // }
260  // sigmaPtVsEtaPlusErrTH1D->Draw();
261  // sigmaPtVsEtaTH1D->Draw("SAME");
262  // sigmaPtVsEtaMinusErrTH1D->Draw("SAME");
263 
264  sigmaPtVsEtaPlusErrTH1D->Write();
265  sigmaPtVsEtaTH1D->Write();
266  sigmaPtVsEtaMinusErrTH1D->Write();
267 
268  sigmaPtVsEtaPlusErr_->Write();
269  sigmaPtVsEta_->Write();
270  sigmaPtVsEtaMinusErr_->Write();
271 
272  // Mass
273  sigmaMassVsEtaPlusErrTH1D->Write();
274  sigmaMassVsEtaTH1D->Write();
275  sigmaMassVsEtaMinusErrTH1D->Write();
276 
277  sigmaMassVsEtaPlusErr_->Write();
278  sigmaMassVsEta_->Write();
279  sigmaMassVsEtaMinusErr_->Write();
280 
281  canvas->Write();
282 }
283 
284 // ------------ method called to for each event ------------
286 
288  std::cout << "Reading muon pairs from Root Tree in " << treeFileName_ << std::endl;
289  RootTreeHandler rootTreeHandler;
290 
291  typedef std::vector<std::pair<lorentzVector, lorentzVector> > MuonPairVector;
292  MuonPairVector savedPair;
293  std::vector<std::pair<unsigned int, unsigned long long> > evtRun;
294  rootTreeHandler.readTree(maxEvents_, treeFileName_, &savedPair, 0, &evtRun);
295  // rootTreeHandler.readTree(maxEvents, inputRootTreeFileName_, &savedPair, &(MuScleFitUtils::genPair));
296 
301  MuScleFitUtils::resfind = std::vector<int>(6, 0);
302 
303  // Loop on all the pairs
304  unsigned int i = 0;
305  MuonPairVector::iterator it = savedPair.begin();
306  std::cout << "Starting loop on " << savedPair.size() << " muons" << std::endl;
307  for (; it != savedPair.end(); ++it, ++i) {
308  double pt1 = it->first.pt();
309  double eta1 = it->first.eta();
310  double pt2 = it->second.pt();
311  double eta2 = it->second.eta();
312 
313  if (debug_) {
314  std::cout << "pt1 = " << pt1 << ", eta1 = " << eta1 << ", pt2 = " << pt2 << ", eta2 = " << eta2 << std::endl;
315  }
316 
317  if (pt1 == 0 && pt2 == 0 && eta1 == 0 && eta2 == 0)
318  continue;
319 
320  double sigmaPt1 = resolutionFunctionForVec->sigmaPt(pt1, eta1, parameters_);
321  double sigmaPt2 = resolutionFunctionForVec->sigmaPt(pt2, eta2, parameters_);
322  double sigmaPtPlusErr1 = resolutionFunctionForVec->sigmaPt(pt1, eta1, valuePlusError_);
323  double sigmaPtPlusErr2 = resolutionFunctionForVec->sigmaPt(pt2, eta2, valuePlusError_);
324  double sigmaPtMinusErr1 = resolutionFunctionForVec->sigmaPt(pt1, eta1, valueMinusError_);
325  double sigmaPtMinusErr2 = resolutionFunctionForVec->sigmaPt(pt2, eta2, valueMinusError_);
326 
327  double sigmaMass = MuScleFitUtils::massResolution(it->first, it->second, parameters_);
328  double sigmaMassPlusErr = MuScleFitUtils::massResolution(it->first, it->second, valuePlusError_);
329  double sigmaMassMinusErr = MuScleFitUtils::massResolution(it->first, it->second, valueMinusError_);
330 
331  if (debug_) {
332  std::cout << "sigmaPt1 = " << sigmaPt1 << " + " << sigmaPtPlusErr1 << " - " << sigmaPtMinusErr1 << std::endl;
333  std::cout << "sigmaPt2 = " << sigmaPt2 << " + " << sigmaPtPlusErr2 << " - " << sigmaPtMinusErr2 << std::endl;
334  std::cout << "sigmaMass = " << sigmaMass << " + " << sigmaMassPlusErr << " - " << sigmaMassMinusErr << std::endl;
335  }
336 
337  // Protections from nans
338  if (pt1 != pt1)
339  continue;
340  if (pt2 != pt2)
341  continue;
342  if (eta1 != eta1)
343  continue;
344  if (eta2 != eta2)
345  continue;
346  if (sigmaPt1 != sigmaPt1)
347  continue;
348  if (sigmaPt2 != sigmaPt2)
349  continue;
350  if (sigmaPtPlusErr1 != sigmaPtPlusErr1)
351  continue;
352  if (sigmaPtPlusErr2 != sigmaPtPlusErr2)
353  continue;
354  if (sigmaPtMinusErr1 != sigmaPtMinusErr1)
355  continue;
356  if (sigmaPtMinusErr2 != sigmaPtMinusErr2)
357  continue;
358  if (sigmaMass != sigmaMass)
359  continue;
360  if (sigmaMassPlusErr != sigmaMassPlusErr)
361  continue;
362  if (sigmaMassMinusErr != sigmaMassMinusErr)
363  continue;
364 
365  std::cout << "Muon pair number " << i << std::endl;
366 
367  // std::cout << "sigmaPt1 = " << sigmaPt1 << ", sigmaPt2 = " << sigmaPt2 << std::endl;
368  // std::cout << "sigmaPtPlusErr1 = " << sigmaPtPlusErr1 << ", sigmaPtMinusErr2 = " << sigmaPtMinusErr2 << std::endl;
369  // std::cout << "sigmaPtMinusErr1 = " << sigmaPtPlusErr1 << ", sigmaPtMinusErr2 = " << sigmaPtMinusErr2 << std::endl;
370 
371  sigmaPtVsPt_->Fill(pt1, sigmaPt1);
372  sigmaPtVsPt_->Fill(pt2, sigmaPt2);
373  sigmaPtVsPtPlusErr_->Fill(pt1, sigmaPtPlusErr1);
374  sigmaPtVsPtPlusErr_->Fill(pt2, sigmaPtPlusErr2);
375  sigmaPtVsPtMinusErr_->Fill(pt1, sigmaPtMinusErr1);
376  sigmaPtVsPtMinusErr_->Fill(pt2, sigmaPtMinusErr2);
377 
378  sigmaPtVsEta_->Fill(eta1, sigmaPt1);
379  sigmaPtVsEta_->Fill(eta2, sigmaPt2);
380  sigmaPtVsEtaPlusErr_->Fill(eta1, sigmaPtPlusErr1);
381  sigmaPtVsEtaPlusErr_->Fill(eta2, sigmaPtPlusErr2);
382  sigmaPtVsEtaMinusErr_->Fill(eta1, sigmaPtMinusErr1);
383  sigmaPtVsEtaMinusErr_->Fill(eta2, sigmaPtMinusErr2);
384 
385  sigmaMassVsPt_->Fill(pt1, sigmaMass);
386  sigmaMassVsPt_->Fill(pt2, sigmaMass);
387  sigmaMassVsPtPlusErr_->Fill(pt1, sigmaMassPlusErr);
388  sigmaMassVsPtPlusErr_->Fill(pt2, sigmaMassPlusErr);
389  sigmaMassVsPtMinusErr_->Fill(pt1, sigmaMassMinusErr);
390  sigmaMassVsPtMinusErr_->Fill(pt2, sigmaMassMinusErr);
391 
392  sigmaMassVsEta_->Fill(eta1, sigmaMass);
393  sigmaMassVsEta_->Fill(eta2, sigmaMass);
394  sigmaMassVsEtaPlusErr_->Fill(eta1, sigmaMassPlusErr);
395  sigmaMassVsEtaPlusErr_->Fill(eta2, sigmaMassPlusErr);
396  sigmaMassVsEtaMinusErr_->Fill(eta1, sigmaMassMinusErr);
397  sigmaMassVsEtaMinusErr_->Fill(eta2, sigmaMassMinusErr);
398  }
399 }
400 
401 //define this as a plug-in
TProfile * sigmaMassVsPtPlusErr_
TProfile * sigmaMassVsEta_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
~ErrorsAnalyzer() override
std::vector< double > valueMinusError_
std::vector< double > errors_
void drawHistograms(const TProfile *histo, const TProfile *histoPlusErr, const TProfile *histoMinusErr, const TString &type)
TProfile * sigmaPtVsPtMinusErr_
TProfile * sigmaMassVsPtMinusErr_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static bool debugMassResol_
TProfile * sigmaMassVsEtaPlusErr_
void analyze(const edm::Event &, const edm::EventSetup &) override
static int debug
TString outputFileName_
void readTree(const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< unsigned int, unsigned long long > > *evtRun, MuonPairVector *genPair=nullptr)
TProfile * sigmaPtVsEtaMinusErr_
resolutionFunctionBase< double * > * resolutionFunctionService(const int identifier)
Service to build the resolution functor corresponding to the passed identifier.
Definition: Functions.cc:70
virtual double sigmaPt(const double &pt, const double &eta, const T &parval)=0
int iEvent
Definition: GenABIO.cc:224
resolutionFunctionBase< std::vector< double > > * resolutionFunctionVecService(const int identifier)
Service to build the resolution functor corresponding to the passed identifier when receiving a std::...
Definition: Functions.cc:92
static double massResolution(const lorentzVector &mu1, const lorentzVector &mu2)
std::vector< int > errorFactors_
uint32_t maxEvents_
std::vector< double > valuePlusError_
TProfile * sigmaPtVsPtPlusErr_
TProfile * sigmaPtVsPt_
std::vector< std::pair< lorentzVector, lorentzVector > > MuonPairVector
TProfile * sigmaMassVsEtaMinusErr_
TProfile * sigmaMassVsPt_
def canvas(sub, attr)
Definition: svgfig.py:482
std::vector< double > parameters_
ErrorsAnalyzer(const edm::ParameterSet &)
static std::vector< int > resfind
TString treeFileName_
TProfile * sigmaPtVsEta_
TProfile * sigmaPtVsEtaPlusErr_
static resolutionFunctionBase< double * > * resolutionFunction
def exit(msg="")
void endJob() override