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  if (((TObject*)histo)->InheritsFrom("TH2")) {
75  m_h2_map[etaReg] =
76  fs.make<TH2F>((name + "_" + m_etaRegionNames[etaReg]).c_str(),
77  (title + m_etaRegionNames[etaReg] + ";" + xTitle + ";" + yTitle + ";" + zTitle).c_str(),
78  histo->GetNbinsX(),
79  histo->GetXaxis()->GetXmin(),
80  histo->GetXaxis()->GetXmax(),
81  histo->GetNbinsY(),
82  histo->GetYaxis()->GetXmin(),
83  histo->GetYaxis()->GetXmax());
84  } else {
85  m_h1_map[etaReg] = fs.make<TH1F>((name + "_" + m_etaRegionNames[etaReg]).c_str(),
86  (title + m_etaRegionNames[etaReg] + ";" + xTitle + ";" + yTitle).c_str(),
87  histo->GetNbinsX(),
88  histo->GetXaxis()->GetXmin(),
89  histo->GetXaxis()->GetXmax());
90  }
91  }
92 
93  // flip the is booked bit
94  m_isBooked = true;
95  }
96 
97  //________________________________________________________________________________//
98  // Determine the eta region based on eta values
99  etaRegion getEtaRegion(const double eta1, const double eta2) {
100  bool isEta1Barrel = std::abs(eta1) <= m_etaBoundary;
101  bool isEta2Barrel = std::abs(eta2) <= m_etaBoundary;
102 
103  if (isEta1Barrel && isEta2Barrel) {
104  return etaRegion::BARBAR;
105  } else if ((isEta1Barrel && eta2 > m_etaBoundary) || (isEta2Barrel && eta1 > m_etaBoundary)) {
106  return etaRegion::BARFWD;
107  } else if ((isEta1Barrel && eta2 < -m_etaBoundary) || (isEta2Barrel && eta1 < -m_etaBoundary)) {
108  return etaRegion::BARBWD;
109  } else if (eta1 > m_etaBoundary && eta2 > m_etaBoundary) {
110  return etaRegion::FWDFWD;
111  } else if (eta1 < -m_etaBoundary && eta2 < -m_etaBoundary) {
112  return etaRegion::BWDBWD;
113  } else if ((eta1 > m_etaBoundary && eta2 < -m_etaBoundary) || (eta2 > m_etaBoundary && eta1 < -m_etaBoundary)) {
114  return etaRegion::FWDBWD;
115  }
116 
117  // Default case if none of the conditions match
118  return etaRegion::END; // Adjust the default based on your logic
119  }
120 
121  //________________________________________________________________________________//
122  inline void fillTH1Plots(const float val, const std::pair<TLorentzVector, TLorentzVector>& momenta) {
123  if (!m_isBooked) {
124  edm::LogError("PlotsVsDiLeptonRegion")
125  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
126  return;
127  }
128 
129  etaRegion region = getEtaRegion(momenta.first.Eta(), momenta.second.Eta());
130  if (region == etaRegion::END) {
131  edm::LogError("PlotsVsDiLeptonRegion") << "undefined di-muon kinematics" << std::endl;
132  }
133  m_h1_map[region]->Fill(val);
134  }
135 
136  //________________________________________________________________________________//
137  inline void fillTH2Plots(const float valX,
138  const float valY,
139  const std::pair<TLorentzVector, TLorentzVector>& momenta) {
140  if (!m_isBooked) {
141  edm::LogError("PlotsVsDiLeptonRegion")
142  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
143  return;
144  }
145 
146  etaRegion region = getEtaRegion(momenta.first.Eta(), momenta.second.Eta());
147  if (region == etaRegion::END) {
148  edm::LogError("PlotsVsDiLeptonRegion") << "undefined di-muon kinematics" << std::endl;
149  }
150  m_h2_map[region]->Fill(valX, valY);
151  }
152 
153  private:
154  const std::vector<etaRegion> m_etaRegions = {etaRegion::BARBAR,
160 
161  const std::vector<std::string> m_etaRegionNames = {"barrel-barrel",
162  "barrel-forward",
163  "barrel-backward",
164  "forward-forward",
165  "backward-backward",
166  "forward-backward"};
167  const float m_etaBoundary;
169  std::map<etaRegion, TH1F*> m_h1_map;
170  std::map<etaRegion, TH2F*> m_h2_map;
171  };
172 
173  //
174  // Ancillary class for plotting
175  //
177  public:
179 
180  //________________________________________________________________________________//
181  // overloaded constructor
183  : m_name(name), m_title(tt), m_ytitle(ytt), m_isBooked(false), m_flav(FLAV) {
184  if (m_flav < 0) {
185  edm::LogError("PlotsVsKinematics") << "The initialization flavour is not correct!" << std::endl;
186  }
187  }
188 
189  ~PlotsVsKinematics() = default;
190 
191  //________________________________________________________________________________//
192  inline void bookFromPSet(const TFileDirectory& fs, const edm::ParameterSet& hpar) {
193  std::string namePostfix;
194  std::string titlePostfix;
195  float xmin, xmax;
196 
197  std::string sed = (m_flav ? "e" : "#mu");
198 
199  for (const auto& xAx : axisChoices) {
200  switch (xAx) {
201  case xAxis::Z_PHI:
202  xmin = -M_PI;
203  xmax = M_PI;
204  namePostfix = m_flav ? "EEPhi" : "MMPhi";
205  titlePostfix = fmt::sprintf("%s%s pair #phi;%s^{+}%s^{-} #phi", sed, sed, sed, sed);
206  break;
207  case xAxis::Z_ETA:
208  xmin = -3.5;
209  xmax = 3.5;
210  namePostfix = m_flav ? "EEEta" : "MuMuEta";
211  titlePostfix = fmt::sprintf("%s%s pair #eta;%s^{+}%s^{-} #eta", sed, sed, sed, sed);
212  break;
213  case xAxis::LP_PHI:
214  xmin = -M_PI;
215  xmax = M_PI;
216  namePostfix = m_flav ? "EPlusPhi" : "MuPlusPhi";
217  titlePostfix = fmt::sprintf("%s^{+} #phi;%s^{+} #phi [rad]", sed, sed);
218  break;
219  case xAxis::LP_ETA:
220  xmin = -2.4;
221  xmax = 2.4;
222  namePostfix = m_flav ? "EPlusEta" : "MuPlusEta";
223  titlePostfix = fmt::sprintf("%s^{+} #eta;%s^{+} #eta", sed, sed);
224  break;
225  case xAxis::LM_PHI:
226  xmin = -M_PI;
227  xmax = M_PI;
228  namePostfix = m_flav ? "EMinusPhi" : "MuMinusPhi";
229  titlePostfix = fmt::sprintf("%s^{-} #phi;%s^{-} #phi [rad]", sed, sed);
230  break;
231  case xAxis::LM_ETA:
232  xmin = -2.4;
233  xmax = 2.4;
234  namePostfix = m_flav ? "EMinusEta" : "MuMinusEta";
235  titlePostfix = fmt::sprintf("%s^{-} #eta;%s^{+} #eta", sed, sed);
236  break;
237  default:
238  throw cms::Exception("LogicalError") << " there is not such Axis choice as " << xAx;
239  }
240 
241  const auto& h2name = fmt::sprintf("%sVs%s", hpar.getParameter<std::string>("name"), namePostfix);
242  const auto& h2title = fmt::sprintf("%s vs %s;%s% s",
243  hpar.getParameter<std::string>("title"),
244  titlePostfix,
245  hpar.getParameter<std::string>("title"),
246  hpar.getParameter<std::string>("yUnits"));
247 
248  m_h2_map[xAx] = fs.make<TH2F>(h2name.c_str(),
249  h2title.c_str(),
250  hpar.getParameter<int32_t>("NxBins"),
251  xmin,
252  xmax,
253  hpar.getParameter<int32_t>("NyBins"),
254  hpar.getParameter<double>("ymin"),
255  hpar.getParameter<double>("ymax"));
256  }
257 
258  // flip the is booked bit
259  m_isBooked = true;
260  }
261 
262  //________________________________________________________________________________//
263  inline void bookPlots(
264  TFileDirectory& fs, const float valmin, const float valmax, const int nxbins, const int nybins) {
265  if (m_name.empty() && m_title.empty() && m_ytitle.empty()) {
266  edm::LogError("PlotsVsKinematics")
267  << "In" << __FUNCTION__ << "," << __LINE__
268  << "trying to book plots without the right constructor being called!" << std::endl;
269  return;
270  }
271 
272  std::string dilep = (m_flav ? "e^{+}e^{-}" : "#mu^{+}#mu^{-}");
273  std::string lep = (m_flav ? "e^{+}" : "#mu^{+}");
274  std::string lem = (m_flav ? "e^{-}" : "#mu^{-}");
275 
276  static constexpr float maxMuEta = 2.4;
277  static constexpr float maxMuMuEta = 3.5;
278  TH1F::SetDefaultSumw2(kTRUE);
279 
280  // clang-format off
281  m_h2_map[xAxis::Z_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuEta", m_name).c_str(),
282  fmt::sprintf("%s vs %s pair #eta;%s #eta;%s", m_title, dilep, dilep, m_ytitle).c_str(),
283  nxbins, -M_PI, M_PI,
284  nybins, valmin, valmax);
285 
286  m_h2_map[xAxis::Z_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuPhi", m_name).c_str(),
287  fmt::sprintf("%s vs %s pair #phi;%s #phi [rad];%s", m_title, dilep, dilep, m_ytitle).c_str(),
288  nxbins, -maxMuMuEta, maxMuMuEta,
289  nybins, valmin, valmax);
290 
291  m_h2_map[xAxis::LP_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusEta", m_name).c_str(),
292  fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lep, lep, m_ytitle).c_str(),
293  nxbins, -maxMuEta, maxMuEta,
294  nybins, valmin, valmax);
295 
296  m_h2_map[xAxis::LP_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusPhi", m_name).c_str(),
297  fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lep, lep, m_ytitle).c_str(),
298  nxbins, -M_PI, M_PI,
299  nybins, valmin, valmax);
300 
301  m_h2_map[xAxis::LM_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusEta", m_name).c_str(),
302  fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lem, lem, m_ytitle).c_str(),
303  nxbins, -maxMuEta, maxMuEta,
304  nybins, valmin, valmax);
305 
306  m_h2_map[xAxis::LM_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusPhi", m_name).c_str(),
307  fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lem, lem, m_ytitle).c_str(),
308  nxbins, -M_PI, M_PI,
309  nybins, valmin, valmax);
310  // clang-format on
311 
312  // flip the is booked bit
313  m_isBooked = true;
314  }
315 
316  //________________________________________________________________________________//
317  inline void fillPlots(const float val, const std::pair<TLorentzVector, TLorentzVector>& momenta) {
318  if (!m_isBooked) {
319  edm::LogError("PlotsVsKinematics")
320  << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
321  return;
322  }
323 
324  m_h2_map[xAxis::Z_ETA]->Fill((momenta.first + momenta.second).Eta(), val);
325  m_h2_map[xAxis::Z_PHI]->Fill((momenta.first + momenta.second).Phi(), val);
326  m_h2_map[xAxis::LP_ETA]->Fill((momenta.first).Eta(), val);
327  m_h2_map[xAxis::LP_PHI]->Fill((momenta.first).Phi(), val);
328  m_h2_map[xAxis::LM_ETA]->Fill((momenta.second).Eta(), val);
329  m_h2_map[xAxis::LM_PHI]->Fill((momenta.second).Phi(), val);
330  }
331 
332  private:
334  const std::vector<xAxis> axisChoices = {
335  xAxis::Z_PHI, xAxis::Z_ETA, xAxis::LP_PHI, xAxis::LP_ETA, xAxis::LM_PHI, xAxis::LM_ETA};
336 
340 
343 
344  std::map<xAxis, TH2F*> m_h2_map;
345  };
346 } // namespace DiLeptonHelp
347 #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)