Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-27 22:36:53

0001 #ifndef Alignment_OfflineValidation_DiLeptonVertexHelpers_h
0002 #define Alignment_OfflineValidation_DiLeptonVertexHelpers_h
0003 
0004 #include <vector>
0005 #include <string>
0006 #include <fmt/printf.h>
0007 #include "TH2F.h"
0008 #include "TLorentzVector.h"
0009 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 
0013 namespace DiLeptonHelp {
0014 
0015   //
0016   // Ancillary struct for counting
0017   //
0018   struct Counts {
0019     unsigned int eventsTotal;
0020     unsigned int eventsAfterMult;
0021     unsigned int eventsAfterPt;
0022     unsigned int eventsAfterEta;
0023     unsigned int eventsAfterVtx;
0024     unsigned int eventsAfterDist;
0025 
0026   public:
0027     void printCounts() {
0028       edm::LogInfo("DiLeptonHelpCounts") << " Total Events: " << eventsTotal << "\n"
0029                                          << " After multiplicity: " << eventsAfterMult << "\n"
0030                                          << " After pT cut: " << eventsAfterPt << "\n"
0031                                          << " After eta cut: " << eventsAfterEta << "\n"
0032                                          << " After Vtx: " << eventsAfterVtx << "\n"
0033                                          << " After VtxDist: " << eventsAfterDist << std::endl;
0034     }
0035 
0036     void zeroAll() {
0037       eventsTotal = 0;
0038       eventsAfterMult = 0;
0039       eventsAfterPt = 0;
0040       eventsAfterEta = 0;
0041       eventsAfterVtx = 0;
0042       eventsAfterDist = 0;
0043     }
0044   };
0045 
0046   enum flavour { MM = 0, EE = 1, UNDEF = -1 };
0047 
0048   //
0049   // Ancillary class for plotting
0050   //
0051   class PlotsVsKinematics {
0052   public:
0053     PlotsVsKinematics(flavour FLAV) : m_name(""), m_title(""), m_ytitle(""), m_isBooked(false), m_flav(FLAV) {}
0054 
0055     //________________________________________________________________________________//
0056     // overloaded constructor
0057     PlotsVsKinematics(flavour FLAV, const std::string& name, const std::string& tt, const std::string& ytt)
0058         : m_name(name), m_title(tt), m_ytitle(ytt), m_isBooked(false), m_flav(FLAV) {
0059       if (m_flav < 0) {
0060         edm::LogError("PlotsVsKinematics") << "The initialization flavour is not correct!" << std::endl;
0061       }
0062     }
0063 
0064     ~PlotsVsKinematics() = default;
0065 
0066     //________________________________________________________________________________//
0067     inline void bookFromPSet(const TFileDirectory& fs, const edm::ParameterSet& hpar) {
0068       std::string namePostfix;
0069       std::string titlePostfix;
0070       float xmin, xmax;
0071 
0072       std::string sed = (m_flav ? "e" : "#mu");
0073 
0074       for (const auto& xAx : axisChoices) {
0075         switch (xAx) {
0076           case xAxis::Z_PHI:
0077             xmin = -M_PI;
0078             xmax = M_PI;
0079             namePostfix = m_flav ? "EEPhi" : "MMPhi";
0080             titlePostfix = fmt::sprintf("%s%s pair #phi;%s^{+}%s^{-} #phi", sed, sed, sed, sed);
0081             break;
0082           case xAxis::Z_ETA:
0083             xmin = -3.5;
0084             xmax = 3.5;
0085             namePostfix = m_flav ? "EEEta" : "MuMuEta";
0086             titlePostfix = fmt::sprintf("%s%s pair #eta;%s^{+}%s^{-} #eta", sed, sed, sed, sed);
0087             break;
0088           case xAxis::LP_PHI:
0089             xmin = -M_PI;
0090             xmax = M_PI;
0091             namePostfix = m_flav ? "EPlusPhi" : "MuPlusPhi";
0092             titlePostfix = fmt::sprintf("%s^{+} #phi;%s^{+} #phi [rad]", sed, sed);
0093             break;
0094           case xAxis::LP_ETA:
0095             xmin = -2.4;
0096             xmax = 2.4;
0097             namePostfix = m_flav ? "EPlusEta" : "MuPlusEta";
0098             titlePostfix = fmt::sprintf("%s^{+} #eta;%s^{+} #eta", sed, sed);
0099             break;
0100           case xAxis::LM_PHI:
0101             xmin = -M_PI;
0102             xmax = M_PI;
0103             namePostfix = m_flav ? "EMinusPhi" : "MuMinusPhi";
0104             titlePostfix = fmt::sprintf("%s^{-} #phi;%s^{-} #phi [rad]", sed, sed);
0105             break;
0106           case xAxis::LM_ETA:
0107             xmin = -2.4;
0108             xmax = 2.4;
0109             namePostfix = m_flav ? "EMinusEta" : "MuMinusEta";
0110             titlePostfix = fmt::sprintf("%s^{-} #eta;%s^{+} #eta", sed, sed);
0111             break;
0112           default:
0113             throw cms::Exception("LogicalError") << " there is not such Axis choice as " << xAx;
0114         }
0115 
0116         const auto& h2name = fmt::sprintf("%sVs%s", hpar.getParameter<std::string>("name"), namePostfix);
0117         const auto& h2title = fmt::sprintf("%s vs %s;%s% s",
0118                                            hpar.getParameter<std::string>("title"),
0119                                            titlePostfix,
0120                                            hpar.getParameter<std::string>("title"),
0121                                            hpar.getParameter<std::string>("yUnits"));
0122 
0123         m_h2_map[xAx] = fs.make<TH2F>(h2name.c_str(),
0124                                       h2title.c_str(),
0125                                       hpar.getParameter<int32_t>("NxBins"),
0126                                       xmin,
0127                                       xmax,
0128                                       hpar.getParameter<int32_t>("NyBins"),
0129                                       hpar.getParameter<double>("ymin"),
0130                                       hpar.getParameter<double>("ymax"));
0131       }
0132 
0133       // flip the is booked bit
0134       m_isBooked = true;
0135     }
0136 
0137     //________________________________________________________________________________//
0138     inline void bookPlots(
0139         TFileDirectory& fs, const float valmin, const float valmax, const int nxbins, const int nybins) {
0140       if (m_name.empty() && m_title.empty() && m_ytitle.empty()) {
0141         edm::LogError("PlotsVsKinematics")
0142             << "In" << __FUNCTION__ << "," << __LINE__
0143             << "trying to book plots without the right constructor being called!" << std::endl;
0144         return;
0145       }
0146 
0147       std::string dilep = (m_flav ? "e^{+}e^{-}" : "#mu^{+}#mu^{-}");
0148       std::string lep = (m_flav ? "e^{+}" : "#mu^{+}");
0149       std::string lem = (m_flav ? "e^{-}" : "#mu^{-}");
0150 
0151       static constexpr float maxMuEta = 2.4;
0152       static constexpr float maxMuMuEta = 3.5;
0153       TH1F::SetDefaultSumw2(kTRUE);
0154 
0155       // clang-format off
0156       m_h2_map[xAxis::Z_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuEta", m_name).c_str(),
0157                          fmt::sprintf("%s vs %s pair #eta;%s #eta;%s", m_title, dilep, dilep, m_ytitle).c_str(),
0158                          nxbins, -M_PI, M_PI,
0159                          nybins, valmin, valmax);
0160       
0161       m_h2_map[xAxis::Z_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMuPhi", m_name).c_str(),
0162                          fmt::sprintf("%s vs %s pair #phi;%s #phi [rad];%s", m_title, dilep, dilep, m_ytitle).c_str(),
0163                          nxbins, -maxMuMuEta, maxMuMuEta,
0164                          nybins, valmin, valmax);
0165       
0166       m_h2_map[xAxis::LP_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusEta", m_name).c_str(),
0167                           fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lep, lep, m_ytitle).c_str(),
0168                           nxbins, -maxMuEta, maxMuEta,
0169                           nybins, valmin, valmax);
0170       
0171       m_h2_map[xAxis::LP_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuPlusPhi", m_name).c_str(),
0172                           fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lep, lep, m_ytitle).c_str(),
0173                           nxbins, -M_PI, M_PI,
0174                           nybins, valmin, valmax);
0175       
0176       m_h2_map[xAxis::LM_ETA] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusEta", m_name).c_str(),
0177                           fmt::sprintf("%s vs %s #eta;%s #eta;%s", m_title, lem, lem, m_ytitle).c_str(),
0178                           nxbins, -maxMuEta, maxMuEta,
0179                           nybins, valmin, valmax);
0180       
0181       m_h2_map[xAxis::LM_PHI] = fs.make<TH2F>(fmt::sprintf("%sVsMuMinusPhi", m_name).c_str(),
0182                           fmt::sprintf("%s vs %s #phi;%s #phi [rad];%s", m_title, lem, lem,  m_ytitle).c_str(),
0183                           nxbins, -M_PI, M_PI,
0184                           nybins, valmin, valmax);
0185       // clang-format on
0186 
0187       // flip the is booked bit
0188       m_isBooked = true;
0189     }
0190 
0191     //________________________________________________________________________________//
0192     inline void fillPlots(const float val, const std::pair<TLorentzVector, TLorentzVector>& momenta) {
0193       if (!m_isBooked) {
0194         edm::LogError("PlotsVsKinematics")
0195             << "In" << __FUNCTION__ << "," << __LINE__ << "trying to fill a plot not booked!" << std::endl;
0196         return;
0197       }
0198 
0199       m_h2_map[xAxis::Z_ETA]->Fill((momenta.first + momenta.second).Eta(), val);
0200       m_h2_map[xAxis::Z_PHI]->Fill((momenta.first + momenta.second).Phi(), val);
0201       m_h2_map[xAxis::LP_ETA]->Fill((momenta.first).Eta(), val);
0202       m_h2_map[xAxis::LP_PHI]->Fill((momenta.first).Phi(), val);
0203       m_h2_map[xAxis::LM_ETA]->Fill((momenta.second).Eta(), val);
0204       m_h2_map[xAxis::LM_PHI]->Fill((momenta.second).Phi(), val);
0205     }
0206 
0207   private:
0208     enum xAxis { Z_PHI, Z_ETA, LP_PHI, LP_ETA, LM_PHI, LM_ETA };
0209     const std::vector<xAxis> axisChoices = {
0210         xAxis::Z_PHI, xAxis::Z_ETA, xAxis::LP_PHI, xAxis::LP_ETA, xAxis::LM_PHI, xAxis::LM_ETA};
0211 
0212     const std::string m_name;
0213     const std::string m_title;
0214     const std::string m_ytitle;
0215 
0216     bool m_isBooked;
0217     flavour m_flav;
0218 
0219     std::map<xAxis, TH2F*> m_h2_map;
0220   };
0221 }  // namespace DiLeptonHelp
0222 #endif