CMS 3D CMS Logo

DiMuonMassBiasClient.cc
Go to the documentation of this file.
1 // user includes
6 
7 // RooFit includes
8 #include "TCanvas.h"
9 #include "RooAddPdf.h"
10 #include "RooDataHist.h"
11 #include "RooExponential.h"
12 #include "RooGaussian.h"
13 #include "RooPlot.h"
14 #include "RooRealVar.h"
15 #include "RooVoigtian.h"
16 
17 //-----------------------------------------------------------------------------------
19  : TopFolder_(iConfig.getParameter<std::string>("FolderName")),
20  fitBackground_(iConfig.getParameter<bool>("fitBackground")),
21  debugMode_(iConfig.getParameter<bool>("debugMode")),
22  MEtoHarvest_(iConfig.getParameter<std::vector<std::string>>("MEtoHarvest"))
23 //-----------------------------------------------------------------------------------
24 {
25  edm::LogInfo("DiMuonMassBiasClient") << "DiMuonMassBiasClient::Constructing DiMuonMassBiasClient ";
26 
27  // fill the parameters for the fit
32 
33  if (debugMode_) {
34  edm::LogPrint("DiMuonMassBiasClient")
35  << "mean: " << meanConfig_[0] << " (" << meanConfig_[1] << "," << meanConfig_[2] << ") " << std::endl;
36  edm::LogPrint("DiMuonMassBiasClient")
37  << "width: " << widthConfig_[0] << " (" << widthConfig_[1] << "," << widthConfig_[2] << ")" << std::endl;
38  edm::LogPrint("DiMuonMassBiasClient")
39  << "sigma: " << sigmaConfig_[0] << " (" << sigmaConfig_[1] << "," << sigmaConfig_[2] << ")" << std::endl;
40  }
41 }
42 
43 //-----------------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------------
46 {
47  edm::LogInfo("DiMuonMassBiasClient") << "DiMuonMassBiasClient::Deleting DiMuonMassBiasClient ";
48 }
49 
50 //-----------------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------------
53 {
54  edm::LogInfo("DiMuonMassBiasClient") << "DiMuonMassBiasClient::beginJob done";
55 }
56 
57 //-----------------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------------
60 {
61  edm::LogInfo("DiMuonMassBiasClient") << "DiMuonMassBiasClient:: Begining of Run";
62 }
63 
64 //-----------------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------------
67 {
68  iBooker.setCurrentFolder(TopFolder_ + "/DiMuonMassBiasMonitor/MassBias/Profiles");
69  for (const auto& [key, ME] : harvestTargets_) {
70  if (ME == nullptr) {
71  edm::LogError("DiMuonMassBiasClient") << "could not find MonitorElement for key: " << key << std::endl;
72  continue;
73  }
74 
75  const auto& title = ME->getTitle();
76  const auto& xtitle = ME->getAxisTitle(1);
77  const auto& ytitle = ME->getAxisTitle(2);
78  const auto& nbins = ME->getNbinsX();
79  const auto& xmin = ME->getAxisMin(1);
80  const auto& xmax = ME->getAxisMax(1);
81  MonitorElement* meanToBook =
82  iBooker.book1D(("Mean" + key), (title + ";" + xtitle + ";" + ytitle), nbins, xmin, xmax);
83  meanProfiles_.insert({key, meanToBook});
84 
85  MonitorElement* sigmaToBook =
86  iBooker.book1D(("Sigma" + key), (title + ";" + xtitle + ";" + "#sigma of " + ytitle), nbins, xmin, xmax);
87  widthProfiles_.insert({key, sigmaToBook});
88  }
89 }
90 
91 //-----------------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------------
94 {
95  std::string inFolder = TopFolder_ + "/DiMuonMassBiasMonitor/MassBias/";
96 
97  //loop on the list of histograms to harvest
98  for (const auto& hname : MEtoHarvest_) {
99  MonitorElement* toHarvest = iGetter.get(inFolder + hname);
100 
101  if (toHarvest == nullptr) {
102  edm::LogError("DiMuonMassBiasClient") << "could not find input MonitorElement: " << inFolder + hname << std::endl;
103  continue;
104  }
105 
106  harvestTargets_.insert({hname, toHarvest});
107  }
108 }
109 
110 //-----------------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------------
113 {
114  edm::LogInfo("DiMuonMassBiasClient") << "DiMuonMassBiasClient::endLuminosityBlock";
115 
116  getMEsToHarvest(igetter);
117  bookMEs(ibooker);
118 
119  for (const auto& [key, ME] : harvestTargets_) {
120  if (debugMode_)
121  edm::LogPrint("DiMuonMassBiasClient") << "dealing with key: " << key << std::endl;
122  TH2F* bareHisto = ME->getTH2F();
123  for (int bin = 1; bin <= ME->getNbinsX(); bin++) {
124  if (debugMode_)
125  edm::LogPrint("DiMuonMassBiasClient") << "dealing with bin: " << bin << std::endl;
126  TH1D* Proj = bareHisto->ProjectionY(Form("%s_proj_%i", key.c_str(), bin), bin, bin);
128 
129  // fill the mean profiles
130  const Measurement1D& bias = results.getBias();
131  meanProfiles_[key]->setBinContent(bin, bias.value());
132  meanProfiles_[key]->setBinError(bin, bias.error());
133 
134  // fill the width profiles
135  const Measurement1D& width = results.getWidth();
136  widthProfiles_[key]->setBinContent(bin, width.value());
137  widthProfiles_[key]->setBinError(bin, width.error());
138  }
139  }
140 }
141 
142 //-----------------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------------
145 {
146  if (hist->GetEntries() < diMuonMassBias::minimumHits) {
147  edm::LogWarning("DiMuonMassBiasClient") << " Input histogram:" << hist->GetName() << " has not enough entries ("
148  << hist->GetEntries() << ") for a meaningful Voigtian fit!\n"
149  << "Skipping!";
150 
151  return diMuonMassBias::fitOutputs(Measurement1D(0., 0.), Measurement1D(0., 0.));
152  }
153 
154  TCanvas* c1 = new TCanvas();
155  if (debugMode_) {
156  c1->Clear();
157  c1->SetLeftMargin(0.15);
158  c1->SetRightMargin(0.10);
159  }
160 
161  // silence messages
162  RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
163 
164  Double_t xmin = hist->GetXaxis()->GetXmin();
165  Double_t xmax = hist->GetXaxis()->GetXmax();
166 
167  if (debugMode_) {
168  edm::LogPrint("DiMuonMassBiasClient") << "fitting range: (" << xmin << "-" << xmax << ")" << std::endl;
169  }
170 
171  RooRealVar InvMass("InvMass", "di-muon mass M(#mu^{+}#mu^{-}) [GeV]", xmin, xmax);
172  RooPlot* frame = InvMass.frame();
173  RooDataHist datahist("datahist", "datahist", InvMass, RooFit::Import(*hist));
174  datahist.plotOn(frame);
175 
176  RooRealVar mean("#mu", "mean", meanConfig_[0], meanConfig_[1], meanConfig_[2]); //90.0, 60.0, 120.0 (for Z)
177  RooRealVar width("width", "width", widthConfig_[0], widthConfig_[1], widthConfig_[2]); // 5.0, 0.0, 120.0 (for Z)
178  RooRealVar sigma("#sigma", "sigma", sigmaConfig_[0], sigmaConfig_[1], sigmaConfig_[2]); // 5.0, 0.0, 120.0 (for Z)
179  RooVoigtian voigt("voigt", "voigt", InvMass, mean, width, sigma);
180 
181  RooRealVar lambda("#lambda", "slope", -0.01, -100., 1.);
182  RooExponential expo("expo", "expo", InvMass, lambda);
183 
184  RooRealVar b("N_{b}", "Number of background events", 0, hist->GetEntries() / 10.);
185  RooRealVar s("N_{s}", "Number of signal events", 0, hist->GetEntries());
186 
187  RooAddPdf fullModel("fullModel", "Signal + Background Model", RooArgList(voigt, expo), RooArgList(s, b));
188  if (fitBackground_) {
189  fullModel.fitTo(datahist, RooFit::PrintLevel(-1), RooFit::Save(), RooFit::Range(xmin, xmax));
190  fullModel.plotOn(frame, RooFit::LineColor(kRed));
191  fullModel.plotOn(frame, RooFit::Components(expo), RooFit::LineStyle(kDashed)); //Other option
192  fullModel.paramOn(frame, RooFit::Layout(0.65, 0.90, 0.90));
193  } else {
194  voigt.fitTo(datahist, RooFit::PrintLevel(-1), RooFit::Save(), RooFit::Range(xmin, xmax));
195  voigt.plotOn(frame, RooFit::LineColor(kRed)); //this will show fit overlay on canvas
196  voigt.paramOn(frame, RooFit::Layout(0.65, 0.90, 0.90)); //this will display the fit parameters on canvas
197  }
198 
199  // Redraw data on top and print / store everything
200  datahist.plotOn(frame);
201  frame->GetYaxis()->SetTitle("n. of events");
202  TString histName = hist->GetName();
203  frame->SetName("frame" + histName);
204  frame->SetTitle(hist->GetTitle());
205  frame->Draw();
206 
207  if (debugMode_) {
208  c1->Print("fit_debug" + histName + ".pdf");
209  }
210  delete c1;
211 
212  float mass_mean = mean.getVal();
213  float mass_sigma = sigma.getVal();
214 
215  float mass_mean_err = mean.getError();
216  float mass_sigma_err = sigma.getError();
217 
218  Measurement1D resultM(mass_mean, mass_mean_err);
219  Measurement1D resultW(mass_sigma, mass_sigma_err);
220 
221  return diMuonMassBias::fitOutputs(resultM, resultW);
222 }
223 
224 //-----------------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------------
227 {
229  desc.add<std::string>("FolderName", "DiMuonMassBiasMonitor");
230  desc.add<bool>("fitBackground", false);
231  desc.add<bool>("debugMode", false);
232 
234  fit_par.add<std::vector<double>>("mean_par",
237  std::numeric_limits<float>::max()}); // par = mean
238 
239  fit_par.add<std::vector<double>>("width_par",
242  std::numeric_limits<float>::max()}); // par = width
243 
244  fit_par.add<std::vector<double>>("sigma_par",
247  std::numeric_limits<float>::max()}); // par = sigma
248 
249  desc.add<edm::ParameterSetDescription>("fit_par", fit_par);
250 
251  desc.add<std::vector<std::string>>("MEtoHarvest",
252  {"DiMuMassVsMuMuPhi",
253  "DiMuMassVsMuMuEta",
254  "DiMuMassVsMuPlusPhi",
255  "DiMuMassVsMuPlusEta",
256  "DiMuMassVsMuMinusPhi",
257  "DiMuMassVsMuMinusEta",
258  "DiMuMassVsMuMuDeltaEta",
259  "DiMuMassVsCosThetaCS"});
260  descriptions.addWithDefaultLabel(desc);
261 }
262 
void getMEsToHarvest(DQMStore::IGetter &igetter)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
static PFTauRenderPlugin instance
PixelRecoRange< float > Range
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~DiMuonMassBiasClient() override
Destructor.
const std::string TopFolder_
void beginRun(edm::Run const &run, edm::EventSetup const &eSetup) override
BeginRun.
Log< level::Error, false > LogError
std::map< std::string, MonitorElement * > harvestTargets_
std::map< std::string, MonitorElement * > meanProfiles_
std::map< std::string, MonitorElement * > widthProfiles_
Definition: ME.h:11
std::vector< std::string > MEtoHarvest_
Log< level::Warning, true > LogPrint
void dqmEndJob(DQMStore::IBooker &ibooker_, DQMStore::IGetter &igetter_) override
EndJob.
__shared__ Hist hist
void beginJob(void) override
BeginJob.
Log< level::Info, false > LogInfo
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double b
Definition: hdecay.h:118
static constexpr int minimumHits
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:680
DiMuonMassBiasClient(const edm::ParameterSet &ps)
Constructor.
double value() const
Definition: Measurement1D.h:25
void bookMEs(DQMStore::IBooker &ibooker)
book MEs
double error() const
Definition: Measurement1D.h:27
Log< level::Warning, false > LogWarning
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
void fillArrayF(float *x, const edm::ParameterSet &cfg, const char *name)
diMuonMassBias::fitOutputs fitVoigt(TH1 *hist, const bool &fitBackground=false) const
Definition: Run.h:45