CMS 3D CMS Logo

DiLeptonVertexHelpers.h
Go to the documentation of this file.
1 #ifndef Alignment_OfflineValidation_DiLeptonVertexHelpers_h
2 #define Alignment_OfflineValidation_DiLeptonVertexHelpers_h
3 
4 #include <vector>
5 #include <string>
6 #include <fmt/printf.h>
7 #include "TH2F.h"
8 #include "TLorentzVector.h"
12 
13 namespace DiLeptonHelp {
14 
15  //
16  // Ancillary struct for counting
17  //
18  struct Counts {
19  unsigned int eventsTotal;
20  unsigned int eventsAfterMult;
21  unsigned int eventsAfterPt;
22  unsigned int eventsAfterEta;
23  unsigned int eventsAfterVtx;
24  unsigned int eventsAfterDist;
25 
26  public:
27  void printCounts() {
28  edm::LogInfo("DiLeptonHelpCounts") << " Total Events: " << eventsTotal << "\n"
29  << " After multiplicity: " << eventsAfterMult << "\n"
30  << " After pT cut: " << eventsAfterPt << "\n"
31  << " After eta cut: " << eventsAfterEta << "\n"
32  << " After Vtx: " << eventsAfterVtx << "\n"
33  << " After VtxDist: " << eventsAfterDist << std::endl;
34  }
35 
36  void zeroAll() {
37  eventsTotal = 0;
38  eventsAfterMult = 0;
39  eventsAfterPt = 0;
40  eventsAfterEta = 0;
41  eventsAfterVtx = 0;
42  eventsAfterDist = 0;
43  }
44  };
45 
46  enum flavour { MM = 0, EE = 1, UNDEF = -1 };
47 
49 
50  //
51  // Ancillary class for plotting in different kinematics regions
52  // of the two muon tracks
53  //
55  public:
56  PlotsVsDiLeptonRegion(const float etaBoundary) : m_etaBoundary(etaBoundary) {}
57  ~PlotsVsDiLeptonRegion() = default;
58 
59  //________________________________________________________________________________//
60  inline void bookSet(const TFileDirectory& fs, const TH1* histo) {
61  const std::string name = histo->GetName();
62  const std::string title = histo->GetTitle();
63  const std::string xTitle = histo->GetXaxis()->GetTitle();
64  const std::string yTitle = histo->GetYaxis()->GetTitle();
65  std::string zTitle = "";
66  if (((TObject*)histo)->InheritsFrom("TH2")) {
67  zTitle = histo->GetZaxis()->GetTitle();
68  }
69 
70  for (const auto& etaReg : m_etaRegions) {
71  if (etaReg == etaRegion::END)
72  continue;
73 
74  const auto& toSub = m_etaRegionNames[etaReg];
75 
76  if (((TObject*)histo)->InheritsFrom("TH2")) {
77  m_h2_map[etaReg] = fs.make<TH2F>(
78  (name + "_" + toSub).c_str(),
79  (title + " (" + toSub + ");" + xTitle + " (" + toSub + ") ;" + yTitle + " (" + toSub + ");" + zTitle)
80  .c_str(),
81  histo->GetNbinsX(),
82  histo->GetXaxis()->GetXmin(),
83  histo->GetXaxis()->GetXmax(),
84  histo->GetNbinsY(),
85  histo->GetYaxis()->GetXmin(),
86  histo->GetYaxis()->GetXmax());
87  } else {
88  m_h1_map[etaReg] = fs.make<TH1F>(
89  (name + "_" + toSub).c_str(),
90  (title + " (" + toSub + ");" + xTitle + " (" + toSub + ") ;" + yTitle + " (" + toSub + ")").c_str(),
91  histo->GetNbinsX(),
92  histo->GetXaxis()->GetXmin(),
93  histo->GetXaxis()->GetXmax());
94  }
95  }
96 
97  // flip the is booked bit
98  m_isBooked = true;
99  }
100 
101  //________________________________________________________________________________//
102  // Determine the eta region based on eta values
103  etaRegion getEtaRegion(const double eta1, const double eta2) {
104  bool isEta1Barrel = std::abs(eta1) <= m_etaBoundary;
105  bool isEta2Barrel = std::abs(eta2) <= m_etaBoundary;
106 
107  if (isEta1Barrel && isEta2Barrel) {
108  return etaRegion::BARBAR;
109  } else if ((isEta1Barrel && eta2 > m_etaBoundary) || (isEta2Barrel && eta1 > m_etaBoundary)) {
110  return etaRegion::BARFWD;
111  } else if ((isEta1Barrel && eta2 < -m_etaBoundary) || (isEta2Barrel && eta1 < -m_etaBoundary)) {
112  return etaRegion::BARBWD;
113  } else if (eta1 > m_etaBoundary && eta2 > m_etaBoundary) {
114  return etaRegion::FWDFWD;
115  } else if (eta1 < -m_etaBoundary && eta2 < -m_etaBoundary) {
116  return etaRegion::BWDBWD;
117  } else if ((eta1 > m_etaBoundary && eta2 < -m_etaBoundary) || (eta2 > m_etaBoundary && eta1 < -m_etaBoundary)) {
118  return etaRegion::FWDBWD;
119  }
120 
121  // Default case if none of the conditions match
122  return etaRegion::END; // Adjust the default based on your logic
123  }
124 
125  //________________________________________________________________________________//
126  inline void fillTH1Plots(const float val, const std::pair<TLorentzVector, TLorentzVector>& momenta) {
127  if (!m_isBooked) {
128  edm::LogError("PlotsVsDiLeptonRegion")
129  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
130  return;
131  }
132 
133  etaRegion region = getEtaRegion(momenta.first.Eta(), momenta.second.Eta());
134  if (region == etaRegion::END) {
135  edm::LogError("PlotsVsDiLeptonRegion") << "undefined di-muon kinematics" << std::endl;
136  }
137  m_h1_map[region]->Fill(val);
138  }
139 
140  //________________________________________________________________________________//
141  inline void fillTH2Plots(const float valX,
142  const float valY,
143  const std::pair<TLorentzVector, TLorentzVector>& momenta) {
144  if (!m_isBooked) {
145  edm::LogError("PlotsVsDiLeptonRegion")
146  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
147  return;
148  }
149 
150  etaRegion region = getEtaRegion(momenta.first.Eta(), momenta.second.Eta());
151  if (region == etaRegion::END) {
152  edm::LogError("PlotsVsDiLeptonRegion") << "undefined di-muon kinematics" << std::endl;
153  }
154  m_h2_map[region]->Fill(valX, valY);
155  }
156 
157  private:
158  const std::vector<etaRegion> m_etaRegions = {etaRegion::BARBAR,
164 
165  const std::vector<std::string> m_etaRegionNames = {"barrel-barrel",
166  "barrel-forward",
167  "barrel-backward",
168  "forward-forward",
169  "backward-backward",
170  "forward-backward"};
171  const float m_etaBoundary;
173  std::map<etaRegion, TH1F*> m_h1_map;
174  std::map<etaRegion, TH2F*> m_h2_map;
175  };
176 
177  //
178  // Ancillary class for plotting
179  //
181  public:
183 
184  //________________________________________________________________________________//
185  // overloaded constructor
187  : m_name(name), m_title(tt), m_ytitle(ytt), m_isBooked(false), m_flav(FLAV) {
188  if (m_flav < 0) {
189  edm::LogError("PlotsVsKinematics") << "The initialization flavour is not correct!" << std::endl;
190  }
191  }
192 
193  ~PlotsVsKinematics() = default;
194 
195  //________________________________________________________________________________//
196  inline void bookFromPSet(const TFileDirectory& fs, const edm::ParameterSet& hpar) {
197  std::string namePostfix;
198  std::string titlePostfix;
199  float xmin, xmax;
200 
201  std::string sed = (m_flav ? "e" : "#mu");
202 
203  for (const auto& xAx : axisChoices) {
204  switch (xAx) {
205  case xAxis::Z_PHI:
206  xmin = -M_PI;
207  xmax = M_PI;
208  namePostfix = m_flav ? "EEPhi" : "MMPhi";
209  titlePostfix = fmt::sprintf("%s%s pair #phi;%s^{+}%s^{-} #phi", sed, sed, sed, sed);
210  break;
211  case xAxis::Z_ETA:
212  xmin = -3.5;
213  xmax = 3.5;
214  namePostfix = m_flav ? "EEEta" : "MuMuEta";
215  titlePostfix = fmt::sprintf("%s%s pair #eta;%s^{+}%s^{-} #eta", sed, sed, sed, sed);
216  break;
217  case xAxis::LP_PHI:
218  xmin = -M_PI;
219  xmax = M_PI;
220  namePostfix = m_flav ? "EPlusPhi" : "MuPlusPhi";
221  titlePostfix = fmt::sprintf("%s^{+} #phi;%s^{+} #phi [rad]", sed, sed);
222  break;
223  case xAxis::LP_ETA:
224  xmin = -2.4;
225  xmax = 2.4;
226  namePostfix = m_flav ? "EPlusEta" : "MuPlusEta";
227  titlePostfix = fmt::sprintf("%s^{+} #eta;%s^{+} #eta", sed, sed);
228  break;
229  case xAxis::LM_PHI:
230  xmin = -M_PI;
231  xmax = M_PI;
232  namePostfix = m_flav ? "EMinusPhi" : "MuMinusPhi";
233  titlePostfix = fmt::sprintf("%s^{-} #phi;%s^{-} #phi [rad]", sed, sed);
234  break;
235  case xAxis::LM_ETA:
236  xmin = -2.4;
237  xmax = 2.4;
238  namePostfix = m_flav ? "EMinusEta" : "MuMinusEta";
239  titlePostfix = fmt::sprintf("%s^{-} #eta;%s^{+} #eta", sed, sed);
240  break;
241  default:
242  throw cms::Exception("LogicalError") << " there is not such Axis choice as " << xAx;
243  }
244 
245  const auto& h2name = fmt::sprintf("%sVs%s", hpar.getParameter<std::string>("name"), namePostfix);
246  const auto& h2title = fmt::sprintf("%s vs %s;%s% s",
247  hpar.getParameter<std::string>("title"),
248  titlePostfix,
249  hpar.getParameter<std::string>("title"),
250  hpar.getParameter<std::string>("yUnits"));
251 
252  m_h2_map[xAx] = fs.make<TH2F>(h2name.c_str(),
253  h2title.c_str(),
254  hpar.getParameter<int32_t>("NxBins"),
255  xmin,
256  xmax,
257  hpar.getParameter<int32_t>("NyBins"),
258  hpar.getParameter<double>("ymin"),
259  hpar.getParameter<double>("ymax"));
260  }
261 
262  // flip the is booked bit
263  m_isBooked = true;
264  }
265 
266  //________________________________________________________________________________//
267  inline void bookPlots(
268  TFileDirectory& fs, const float valmin, const float valmax, const int nxbins, const int nybins) {
269  if (m_name.empty() && m_title.empty() && m_ytitle.empty()) {
270  edm::LogError("PlotsVsKinematics")
271  << "In" << __FUNCTION__ << "," << __LINE__
272  << "trying to book plots without the right constructor being called!" << std::endl;
273  return;
274  }
275 
276  std::string dilep = (m_flav ? "e^{+}e^{-}" : "#mu^{+}#mu^{-}");
277  std::string lep = (m_flav ? "e^{+}" : "#mu^{+}");
278  std::string lem = (m_flav ? "e^{-}" : "#mu^{-}");
279 
280  static constexpr float maxMuEta = 2.4;
281  static constexpr float maxMuMuEta = 3.5;
282  TH1F::SetDefaultSumw2(kTRUE);
283 
284  // clang-format off
285  m_h2_map[xAxis::Z_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuEta", m_name).c_str(),
286  fmt::sprintf("%s vs %s pair #eta;%s #eta;%s", m_title, dilep, dilep, m_ytitle).c_str(),
287  nxbins, -M_PI, M_PI,
288  nybins, valmin, valmax);
289 
290  m_h2_map[xAxis::Z_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuPhi", m_name).c_str(),
291  fmt::sprintf("%s vs %s pair #phi;%s #phi [rad];%s", m_title, dilep, dilep, m_ytitle).c_str(),
292  nxbins, -maxMuMuEta, maxMuMuEta,
293  nybins, valmin, valmax);
294 
295  m_h2_map[xAxis::LP_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusEta", m_name).c_str(),
296  fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lep, lep, m_ytitle).c_str(),
297  nxbins, -maxMuEta, maxMuEta,
298  nybins, valmin, valmax);
299 
300  m_h2_map[xAxis::LP_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusPhi", m_name).c_str(),
301  fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lep, lep, m_ytitle).c_str(),
302  nxbins, -M_PI, M_PI,
303  nybins, valmin, valmax);
304 
305  m_h2_map[xAxis::LM_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusEta", m_name).c_str(),
306  fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lem, lem, m_ytitle).c_str(),
307  nxbins, -maxMuEta, maxMuEta,
308  nybins, valmin, valmax);
309 
310  m_h2_map[xAxis::LM_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusPhi", m_name).c_str(),
311  fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lem, lem, m_ytitle).c_str(),
312  nxbins, -M_PI, M_PI,
313  nybins, valmin, valmax);
314  // clang-format on
315 
316  // flip the is booked bit
317  m_isBooked = true;
318  }
319 
320  //________________________________________________________________________________//
321  inline void fillPlots(const float val, const std::pair<TLorentzVector, TLorentzVector>& momenta) {
322  if (!m_isBooked) {
323  edm::LogError("PlotsVsKinematics")
324  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
325  return;
326  }
327 
328  m_h2_map[xAxis::Z_ETA]->Fill((momenta.first + momenta.second).Eta(), val);
329  m_h2_map[xAxis::Z_PHI]->Fill((momenta.first + momenta.second).Phi(), val);
330  m_h2_map[xAxis::LP_ETA]->Fill((momenta.first).Eta(), val);
331  m_h2_map[xAxis::LP_PHI]->Fill((momenta.first).Phi(), val);
332  m_h2_map[xAxis::LM_ETA]->Fill((momenta.second).Eta(), val);
333  m_h2_map[xAxis::LM_PHI]->Fill((momenta.second).Phi(), val);
334  }
335 
336  private:
338  const std::vector<xAxis> axisChoices = {
339  xAxis::Z_PHI, xAxis::Z_ETA, xAxis::LP_PHI, xAxis::LP_ETA, xAxis::LM_PHI, xAxis::LM_ETA};
340 
344 
347 
348  std::map<xAxis, TH2F*> m_h2_map;
349  };
350 } // namespace DiLeptonHelp
351 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void fillTH1Plots(const float val, const std::pair< TLorentzVector, TLorentzVector > &momenta)
Log< level::Error, false > LogError
std::map< etaRegion, TH2F * > m_h2_map
std::map< xAxis, TH2F * > m_h2_map
const std::vector< xAxis > axisChoices
const std::vector< std::string > m_etaRegionNames
Definition: TTTypes.h:54
const std::vector< etaRegion > m_etaRegions
void bookSet(const TFileDirectory &fs, const TH1 *histo)
PlotsVsKinematics(flavour FLAV, const std::string &name, const std::string &tt, const std::string &ytt)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define M_PI
Log< level::Info, false > LogInfo
void bookPlots(TFileDirectory &fs, const float valmin, const float valmax, const int nxbins, const int nybins)
std::map< etaRegion, TH1F * > m_h1_map
PlotsVsDiLeptonRegion(const float etaBoundary)
etaRegion getEtaRegion(const double eta1, const double eta2)
void fillPlots(const float val, const std::pair< TLorentzVector, TLorentzVector > &momenta)
void bookFromPSet(const TFileDirectory &fs, const edm::ParameterSet &hpar)
void fillTH2Plots(const float valX, const float valY, const std::pair< TLorentzVector, TLorentzVector > &momenta)